You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stratos.apache.org by la...@apache.org on 2013/07/15 14:08:07 UTC
git commit: Add UsageUtil class to the component
Updated Branches:
refs/heads/master 555e2bdec -> fae75b11a
Add UsageUtil class to the component
Signed-off-by: Lakmal Warusawithana <la...@wso2.com>
Project: http://git-wip-us.apache.org/repos/asf/incubator-stratos/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-stratos/commit/fae75b11
Tree: http://git-wip-us.apache.org/repos/asf/incubator-stratos/tree/fae75b11
Diff: http://git-wip-us.apache.org/repos/asf/incubator-stratos/diff/fae75b11
Branch: refs/heads/master
Commit: fae75b11ab84ae903b97eafe5ded4b5f11428c0e
Parents: 555e2bd
Author: Manula Thantriwatte <ma...@wso2.com>
Authored: Mon Jul 15 17:32:22 2013 +0530
Committer: Lakmal Warusawithana <la...@wso2.com>
Committed: Mon Jul 15 17:37:51 2013 +0530
----------------------------------------------------------------------
.../stratos/usage/ui/utils/UsageUtil.java | 251 +++++++++++++++++++
.../usage/ui/i18n/JSResources.properties | 2 +
.../stratos/usage/ui/i18n/Resources.properties | 57 +++++
3 files changed, 310 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/fae75b11/components/org.apache.stratos.usage.ui/src/main/java/org/apache/stratos/usage/ui/utils/UsageUtil.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.usage.ui/src/main/java/org/apache/stratos/usage/ui/utils/UsageUtil.java b/components/org.apache.stratos.usage.ui/src/main/java/org/apache/stratos/usage/ui/utils/UsageUtil.java
new file mode 100644
index 0000000..e1b6760
--- /dev/null
+++ b/components/org.apache.stratos.usage.ui/src/main/java/org/apache/stratos/usage/ui/utils/UsageUtil.java
@@ -0,0 +1,251 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.stratos.usage.ui.utils;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.stratos.common.util.CommonUtil;
+import org.wso2.carbon.registry.common.ui.UIException;
+import org.apache.stratos.usage.stub.beans.xsd.*;
+import org.apache.stratos.usage.ui.clients.UsageServiceClient;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletRequest;
+import javax.servlet.http.HttpSession;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.List;
+
+public class UsageUtil {
+ private static final Log log = LogFactory.getLog(UsageUtil.class);
+
+ private static long KB_LIMIT = 1024;
+ private static long MB_LIMIT = 1024 * 1024;
+ private static long GB_LIMIT = 1024 * 1024 * 1024;
+ private static long TB_LIMIT = (long) 1024 * 1024 * 1024 * 1024;
+
+ public static TenantUsage retrieveCurrentTenantUsage(ServletRequest request,
+ ServletConfig config, HttpSession session) throws Exception {
+ try {
+ UsageServiceClient serviceClient = new UsageServiceClient(config, session);
+ String yearMonth = request.getParameter("year-month");
+ if (yearMonth == null) {
+ // get the current year month
+ yearMonth = getCurrentYearMonth();
+ }
+ return serviceClient.retrieveCurrentTenantUsage(yearMonth);
+ } catch (Exception e) {
+ String msg = "Failed to get current tenant usage.";
+ log.error(msg, e);
+ throw new UIException(msg, e);
+ }
+ }
+
+ public static TenantUsage[] retrieveTenantUsages(ServletRequest request,
+ ServletConfig config, HttpSession session) throws Exception {
+ try {
+ UsageServiceClient serviceClient = new UsageServiceClient(config, session);
+ String yearMonth = request.getParameter("year-month");
+ if (yearMonth == null) {
+ // get the current year month
+ yearMonth = getCurrentYearMonth();
+ }
+ return serviceClient.retrieveTenantUsages(yearMonth);
+ } catch (Exception e) {
+ String msg = "Failed to get all tenants usages.";
+ log.error(msg, e);
+ throw new UIException(msg, e);
+ }
+ }
+
+ public static PaginatedTenantUsageInfo retrievePaginatedTenantUsages(ServletRequest request,
+ ServletConfig config, HttpSession session) throws Exception {
+ String requestedPage = request.getParameter("requestedPage");
+ int pageNumber = 1;
+ int numberOfPages = 1;
+ int entriesPerPage = 15;
+ if (requestedPage != null && requestedPage.length() > 0) {
+ pageNumber = new Integer(requestedPage);
+ }
+
+ try {
+ UsageServiceClient serviceClient = new UsageServiceClient(config, session);
+ String yearMonth = request.getParameter("year-month");
+ if (yearMonth == null) {
+ // get the current year month
+ yearMonth = getCurrentYearMonth();
+ }
+ return serviceClient.retrievePaginatedTenantUsages(yearMonth, pageNumber, entriesPerPage);
+ } catch (Exception e) {
+ String msg = "Failed to get all tenants usages.";
+ log.error(msg, e);
+ throw new UIException(msg, e);
+ }
+ }
+
+ public static TenantUsage retrieveTenantUsage(ServletRequest request,
+ ServletConfig config, HttpSession session) throws Exception {
+ try {
+ UsageServiceClient serviceClient = new UsageServiceClient(config, session);
+ String yearMonth = request.getParameter("year-month");
+ if (yearMonth == null) {
+ // get the current year month
+ yearMonth = getCurrentYearMonth();
+ }
+ String tenantIdStr = request.getParameter("tenant-id");
+ if (tenantIdStr == null) {
+ tenantIdStr = "0";
+ }
+ return serviceClient.retrieveTenantUsage(yearMonth, Integer.parseInt(tenantIdStr));
+ } catch (Exception e) {
+ String msg = "Failed to get tenant usages.";
+ log.error(msg, e);
+ throw new UIException(msg, e);
+ }
+ }
+
+
+ public static String convertBytesToString(long storage) {
+ if (storage < KB_LIMIT) {
+ return storage + " Byte(s)";
+ } else if (storage < MB_LIMIT) {
+ return storage / KB_LIMIT + " KByte(s)";
+ } else if (storage < GB_LIMIT) {
+ return storage / MB_LIMIT + " MByte(s)";
+ } else if (storage < TB_LIMIT) {
+ return storage / GB_LIMIT + " GByte(s)";
+ } else {
+ return storage / TB_LIMIT + " TByte(s)";
+ }
+ }
+
+ public static String getCurrentYearMonth() {
+ Calendar calendar = Calendar.getInstance();
+ return CommonUtil.getMonthString(calendar);
+ }
+
+ public static String[] getYearMonths() {
+ // we will list 100 months for now
+ List<String> yearMonths = new ArrayList<String>();
+ for (int i = 0; i > -100; i--) {
+ String yearMonth = CommonUtil.getMonthString(i);
+ yearMonths.add(yearMonth);
+ }
+ return yearMonths.toArray(new String[yearMonths.size()]);
+ }
+
+ public static String getCurrentDataStorage(TenantUsage usage) {
+ TenantDataCapacity regData = usage.getRegistryCapacity();
+ long currentData = 0;
+ if (regData != null) {
+ currentData = regData.getRegistryContentCapacity();
+ }
+ return convertBytesToString(currentData);
+ }
+
+ public static String getHistoryDataStorage(TenantUsage usage) {
+ TenantDataCapacity historyData = usage.getRegistryCapacity();
+ long currentData = 0;
+ if (historyData != null) {
+ currentData = historyData.getRegistryContentHistoryCapacity();
+ }
+ return convertBytesToString(currentData);
+ }
+
+ public static String getTotalDataStorage(TenantUsage usage) {
+ TenantDataCapacity regData = usage.getRegistryCapacity();
+ long totalDataStorage = 0;
+ if (regData != null) {
+ totalDataStorage =
+ regData.getRegistryContentCapacity() + regData.getRegistryContentHistoryCapacity();
+ }
+ return convertBytesToString(totalDataStorage);
+ }
+
+ public static String getIncomingBandwidth(BandwidthStatistics bandwidth) {
+ long totalBW = 0;
+ if (bandwidth != null) {
+ totalBW = bandwidth.getIncomingBandwidth();
+ }
+ return convertBytesToString(totalBW);
+ }
+
+ public static String getOutgoingBandwidth(BandwidthStatistics bandwidth) {
+ long totalBW = 0;
+ if (bandwidth != null) {
+ totalBW = bandwidth.getOutgoingBandwidth();
+ }
+ return convertBytesToString(totalBW);
+ }
+
+ public static String getTotalBandwidth(BandwidthStatistics bandwidth) {
+ long totalBW = 0;
+ if (bandwidth != null) {
+ totalBW = bandwidth.getIncomingBandwidth() + bandwidth.getOutgoingBandwidth();
+ }
+ return convertBytesToString(totalBW);
+ }
+
+ public static InstanceUsageStatics[] retrieveInstanceUsage(ServletRequest request,
+ ServletConfig config, HttpSession session)
+ throws Exception {
+
+ try {
+ UsageServiceClient serviceClient = new UsageServiceClient(config, session);
+ InstanceUsageStatics[] returnInstanceUsage = serviceClient.retrieveInstanceUsage();
+ return returnInstanceUsage;
+ } catch (Exception e) {
+ String msg = "Failed to get current instance usage.";
+ log.error(msg, e);
+ throw new UIException(msg, e);
+ }
+ }
+
+ public static PaginatedInstanceUsage retrievePaginatedInstanceUsages(ServletRequest request,
+ ServletConfig config, HttpSession session) throws Exception {
+ String requestedPage = request.getParameter("requestedPage");
+ int pageNumber = 1;
+ int numberOfPages = 1;
+ int entriesPerPage = 15;
+ if (requestedPage != null && requestedPage.length() > 0) {
+ pageNumber = new Integer(requestedPage);
+ }
+ try {
+ UsageServiceClient serviceClient = new UsageServiceClient(config, session);
+ String yearMonth = request.getParameter("year-month");
+ if (yearMonth == null) {
+ // get the current year month
+ yearMonth = getCurrentYearMonth();
+ }
+ return serviceClient.retrievePaginatedInstanceUsage(yearMonth, pageNumber, entriesPerPage);
+ } catch (Exception e) {
+ String msg = "Failed to get paginated instance usages.";
+ log.error(msg, e);
+ throw new UIException(msg, e);
+ }
+ }
+ public static String getAPIUsage(TenantUsage usage) {
+ long count = 0;
+
+ if (usage.getApiManagerUsageStats() != null) {
+ count =usage.getApiManagerUsageStats()[0].getRequestCount();
+ }
+ return Long.toString(count);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/fae75b11/components/org.apache.stratos.usage.ui/src/main/resources/org/apache/stratos/usage/ui/i18n/JSResources.properties
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.usage.ui/src/main/resources/org/apache/stratos/usage/ui/i18n/JSResources.properties b/components/org.apache.stratos.usage.ui/src/main/resources/org/apache/stratos/usage/ui/i18n/JSResources.properties
new file mode 100644
index 0000000..e7490b0
--- /dev/null
+++ b/components/org.apache.stratos.usage.ui/src/main/resources/org/apache/stratos/usage/ui/i18n/JSResources.properties
@@ -0,0 +1,2 @@
+empty=To make sure the js properties file is not empty
+session.timed.out=Session timed out. Please login again
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/fae75b11/components/org.apache.stratos.usage.ui/src/main/resources/org/apache/stratos/usage/ui/i18n/Resources.properties
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.usage.ui/src/main/resources/org/apache/stratos/usage/ui/i18n/Resources.properties b/components/org.apache.stratos.usage.ui/src/main/resources/org/apache/stratos/usage/ui/i18n/Resources.properties
new file mode 100644
index 0000000..d7eda93
--- /dev/null
+++ b/components/org.apache.stratos.usage.ui/src/main/resources/org/apache/stratos/usage/ui/i18n/Resources.properties
@@ -0,0 +1,57 @@
+usage.menu=User Usage
+user.usage.report=User Usage Report
+tenant.menu=Tenant Usage
+tenants.usage.report=Tenant Usage Report
+tenant.usage.report=Usage Report
+data.storage.name=Data Storage
+current.data.storage=Current Data Storage
+history.data.storage=Historical Data Storage
+total.data.storage=Total Data Storage
+registry.content.storage.name=Registry Content
+registry.bandwidth.usage=Registry Bandwidth Usage
+service.bandwidth.usage=Service Bandwidth Usage
+webapp.bandwidth.usage=Webapp Bandwidth Usage
+server.name=Server Name
+all.server.name=All Server Total
+incoming.bandwidth=Incoming Bandwidth
+outgoing.bandwidth=Outgoing Bandwidth
+total.bandwidth=Total Bandwidth
+storage.usage=Storage Usage
+number.of.users=Number of Users
+users=Users
+usage.monitoring=Usage Monitoring
+all.tenant.usage.report=All Tenant Usage
+tenant.domain=Domain
+tenant.id=Id
+full.report=Full Report
+report.duration=Report Duration
+year.month=Year-Month
+back=Back
+page.x.to.y=Page {0}
+prev=Prev
+next=Next
+service.usage.stat=Service Usage Statistics
+service.usage.request=Request Count
+service.usage.response=Response Count
+service.usage.fault=Fault Count
+service.total.request=Service Requests
+registry.total.bandwidth=Total Registry Bandwidth
+service.total.bandwidth=Total Service Bandwidth
+instance.usage.report=Instance Usage Report
+instance.server.url=Instance Server URL
+instance.id=Instance ID
+start.time=Start Time
+stop.time=Stop Time
+used.time.in.hours=Used Time In Hours
+instances.data=Instances Data
+failed.to.get.instance.data=Failed To Get Instance Data
+empty.instance.data=Instance Data Not Found
+usage.data.not.available=Usage Data not available
+empty.usage.data=Usage Data Not Found
+webapp.total.bandwidth=Total WebApp Bandwidth
+number.of.api.calls= Number of API Calls
+api.usage=API Usage
+cartridge.stat=Cartridge Usage
+cartridge.type=Cartridge Type
+image.id=Image ID
+cartridge.hours=Hours
\ No newline at end of file