From 5e3e88741b41a0cdf7bfdbb84a4aa6cb68a56e55 Mon Sep 17 00:00:00 2001 From: Alessandro Pieve Date: Tue, 8 Nov 2016 09:00:47 +0000 Subject: [PATCH] add getContextTimeSeries git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-analytics-persistence-couchbase@133949 82a268e6-3cf1-43bd-a215-b396298e98cf --- pom.xml | 2 +- .../AccountingPersistenceQueryCouchBase.java | 58 ++++++++++++++---- ...countingPersistenceQueryCouchBaseTest.java | 59 ++++++++++++++++--- 3 files changed, 100 insertions(+), 19 deletions(-) diff --git a/pom.xml b/pom.xml index cc80ac0..fae78c3 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ org.gcube.accounting accounting-analytics-persistence-couchbase - 1.1.0-SNAPSHOT + 1.2.0-SNAPSHOT Accounting Analytics Persistence CouchBase Accounting Analytics Persistence CouchBase Implementation diff --git a/src/main/java/org/gcube/accounting/analytics/persistence/couchbase/AccountingPersistenceQueryCouchBase.java b/src/main/java/org/gcube/accounting/analytics/persistence/couchbase/AccountingPersistenceQueryCouchBase.java index fa5bcb0..ad1a8b9 100644 --- a/src/main/java/org/gcube/accounting/analytics/persistence/couchbase/AccountingPersistenceQueryCouchBase.java +++ b/src/main/java/org/gcube/accounting/analytics/persistence/couchbase/AccountingPersistenceQueryCouchBase.java @@ -387,11 +387,13 @@ AccountingPersistenceBackendQuery { protected SortedMap mapReduceQuery( Class> clz, - TemporalConstraint temporalConstraint, List filters) + TemporalConstraint temporalConstraint, List filters,String context) throws Exception { - - String currentScope = ScopeProvider.instance.get(); - + String currentScope=null; + if (context==null) + currentScope = ScopeProvider.instance.get(); + else + currentScope = context; JsonArray startKey = JsonArray.create(); startKey.add(currentScope); JsonArray endKey = JsonArray.create(); @@ -463,8 +465,9 @@ AccountingPersistenceBackendQuery { } String designDocId = getDesignDocId(clz); + //ADD A SPECIFY DESIGN DOC FAMILY String designDocIdNew = getDesignDocIdSpecific(clz,keys); - + logger.trace("designDocIdNew :{}",designDocIdNew); for (Object temporal: temporalStartKey.toList()){ if (!temporal.toString().isEmpty()) startKey.add(temporal); @@ -553,16 +556,21 @@ AccountingPersistenceBackendQuery { return infos; } + + + + @Override public SortedMap getTimeSeries( Class> clz, TemporalConstraint temporalConstraint, List filters) throws Exception { - SortedMap map = mapReduceQuery(clz, temporalConstraint, filters); + SortedMap map = mapReduceQuery(clz, temporalConstraint, filters,null); return map; } + @Override public SortedMap> getTopValues( Class> clz, @@ -590,7 +598,7 @@ AccountingPersistenceBackendQuery { for(NumberedFilter nf : top){ filters.add(nf); SortedMap map = - mapReduceQuery(clz, temporalConstraint, filters); + mapReduceQuery(clz, temporalConstraint, filters,null); ret.put(nf, map); filters.remove(nf); @@ -599,6 +607,30 @@ AccountingPersistenceBackendQuery { } + @Override + public SortedMap> getContextTimeSeries( + Class> clz, + TemporalConstraint temporalConstraint, List filters,List contexts) + throws Exception { + logger.trace("getContextTimeSeries"); + SortedSet listContexts = new TreeSet(); + for (String context:contexts){ + Filter contextLabel= new Filter("context",context); + listContexts.add(contextLabel); + } + SortedMap> ret = new TreeMap<>(); + + for(Filter nf : listContexts){ + //filters.add(nf); + logger.debug("detail time series :{}",nf.toString()); + SortedMap map = + mapReduceQuery(clz, temporalConstraint, filters,nf.getValue()); + ret.put(nf, map); + filters.remove(nf); + } + return ret; + + } protected String getQualifiedProperty(String property){ //DEVELOPING @@ -769,9 +801,9 @@ AccountingPersistenceBackendQuery { int scopeDateGroupLevel = 2; int groupLevel = scopeDateGroupLevel; - + //NO ADD A SPECIFIY DESIGN DOC ID FAMILY String designDocId = getDesignDocId(clz)+"Value"; - + String viewName = key; logger.trace("designDocId:{} view:{} ",designDocId,key); logger.trace("startKey:{}",startKey); @@ -875,7 +907,7 @@ AccountingPersistenceBackendQuery { int groupLevel = scopeDateGroupLevel; String designDocId = getDesignDocId(clz); - + //ADD A SPECIFIY DESIGN DOC ID FAMILY String viewName = applicant.getKey(); ViewQuery query = ViewQuery.from(designDocId, viewName); @@ -946,7 +978,7 @@ AccountingPersistenceBackendQuery { aggregationMode, false, false); String designDocId = getDesignDocId(clz); - + //ADD A SPECIFIY DESIGN DOC ID FAMILY for (Filters singleFilter:filterPackageQuota){ String viewNameTmp=null; @@ -1084,6 +1116,8 @@ AccountingPersistenceBackendQuery { aggregationMode, false, false); String designDocId = getDesignDocId(clz); + //ADD A SPECIFIC DESIGN DOC ID FAMILY + for (TotalFilters totalfilters:listUsage){ Double totalQuota=0.00; @@ -1214,4 +1248,6 @@ AccountingPersistenceBackendQuery { + + } diff --git a/src/test/java/org/gcube/accounting/analytics/persistence/couchbase/AccountingPersistenceQueryCouchBaseTest.java b/src/test/java/org/gcube/accounting/analytics/persistence/couchbase/AccountingPersistenceQueryCouchBaseTest.java index 940529e..836ca8d 100644 --- a/src/test/java/org/gcube/accounting/analytics/persistence/couchbase/AccountingPersistenceQueryCouchBaseTest.java +++ b/src/test/java/org/gcube/accounting/analytics/persistence/couchbase/AccountingPersistenceQueryCouchBaseTest.java @@ -272,8 +272,7 @@ public class AccountingPersistenceQueryCouchBaseTest { filters2.add(new Filter("serviceName", "CkanConnector")); simpleFilter2.setFilters(filters2); - filterPackageQuota1.add(simpleFilter2); - + filterPackageQuota1.add(simpleFilter2); Filters simpleFilter3 = new Filters(); List filters3=new ArrayList(); @@ -285,9 +284,6 @@ public class AccountingPersistenceQueryCouchBaseTest { TotalFilters totalfilter1=new TotalFilters(); totalfilter1.setTotalFilters(filterPackageQuota1); - - - List listTotalFilter=new ArrayList(); listTotalFilter.add(totalfilter); listTotalFilter.add(totalfilter1); @@ -317,7 +313,7 @@ public class AccountingPersistenceQueryCouchBaseTest { @Test public void testTopService() throws Exception { Calendar startTime = Calendar.getInstance(); - startTime.set(2016, Calendar.AUGUST, 29); + startTime.set(2016, Calendar.SEPTEMBER, 27); Calendar endTime = Calendar.getInstance(); endTime.set(2016, Calendar.SEPTEMBER, 28,23,59); @@ -334,11 +330,12 @@ public class AccountingPersistenceQueryCouchBaseTest { SortedMap> set = accountingPersistenceQueryCouchBase.getTopValues( clz, temporalConstraint, filters, - AggregatedServiceUsageRecord.CALLERQUALIFIER, null); + AggregatedServiceUsageRecord.CALLED_METHOD, null); logger.debug("Result final{}", set); } + @Test public void getQuerableKeyService() throws Exception{ SortedSet keys; @@ -442,4 +439,52 @@ public class AccountingPersistenceQueryCouchBaseTest { e.printStackTrace(); } } + + + @Test + public void testContextService() throws Exception { + Calendar startTime = Calendar.getInstance(); + startTime.set(2016, Calendar.SEPTEMBER, 1); + Calendar endTime = Calendar.getInstance(); + endTime.set(2016, Calendar.OCTOBER, 20,23,59); + + List filters = new ArrayList(); + //filters.add(new Filter(AggregatedServiceUsageRecord.CONSUMER_ID, "alessandro.pieve")); + + + TemporalConstraint temporalConstraint = + new TemporalConstraint(startTime.getTimeInMillis(), + endTime.getTimeInMillis(), AggregationMode.DAILY); + + Class clz = + AggregatedServiceUsageRecord.class; + + List context=new ArrayList(); + + context.add("/gcube"); + context.add("/gcube/devNext"); + context.add("/gcube/devNext/nextNext"); + + context.add("/gcube/devsec"); + context.add("/gcube/devsec/devVRE"); + + + context.add("/gcube/devsec/preVRE"); + + context.add("/gcube/preprod/preVRE"); + context.add("/gcube/preprod"); + + + SortedMap> setContext = + accountingPersistenceQueryCouchBase.getContextTimeSeries( + clz, temporalConstraint, filters, + context); + + + logger.debug("Result final{}", setContext); + + } + + + }