diff --git a/pom.xml b/pom.xml index 35c8e62..db76795 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ org.gcube.accounting accounting-analytics - 1.0.0-SNAPSHOT + 1.1.0-SNAPSHOT accounting-analytics @@ -17,7 +17,7 @@ scm:https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/${project.artifactId} https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/${project.artifactId} - + @@ -31,16 +31,22 @@ - + org.slf4j slf4j-api provided + + org.gcube.data.publishing + document-store-lib + [1.0.0-SNAPSHOT,2.0.0-SNAPSHOT) + org.gcube.accounting accounting-lib - provided + [2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT) + org.gcube.resources.discovery @@ -58,17 +64,12 @@ provided - org.reflections - reflections - 0.9.10 - - org.json json 20090211 - - + + junit junit diff --git a/src/main/java/org/gcube/accounting/analytics/ResourceRecordQuery.java b/src/main/java/org/gcube/accounting/analytics/ResourceRecordQuery.java index c44a6cd..4fa9a3c 100644 --- a/src/main/java/org/gcube/accounting/analytics/ResourceRecordQuery.java +++ b/src/main/java/org/gcube/accounting/analytics/ResourceRecordQuery.java @@ -5,6 +5,7 @@ package org.gcube.accounting.analytics; import java.util.ArrayList; import java.util.Calendar; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; @@ -16,13 +17,12 @@ import org.gcube.accounting.analytics.exception.NoAvailableScopeException; import org.gcube.accounting.analytics.exception.NoUsableAccountingPersistenceQueryFound; import org.gcube.accounting.analytics.persistence.AccountingPersistenceBackendQuery; import org.gcube.accounting.analytics.persistence.AccountingPersistenceBackendQueryFactory; -import org.gcube.accounting.datamodel.AggregatedUsageRecord; -import org.gcube.accounting.datamodel.SingleUsageRecord; -import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord; import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.documentstore.records.AggregatedRecord; +import org.gcube.documentstore.records.Record; +import org.gcube.documentstore.records.RecordUtility; import org.json.JSONException; import org.json.JSONObject; -import org.reflections.Reflections; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -33,23 +33,21 @@ public class ResourceRecordQuery { private static Logger logger = LoggerFactory.getLogger(ResourceRecordQuery.class); - protected static Map, Set> resourceRecords = null; + protected static Map, Set> resourceRecords = null; /** * Return a Map containing a set of required fields for each Resource * Records Types * @return the Map */ - public static synchronized Map, Set> getResourceRecordsTypes() { + public static synchronized Map, Set> getResourceRecordsTypes() { if(resourceRecords==null){ - resourceRecords = new HashMap, Set>(); - Package usageRecordPackage = ServiceUsageRecord.class.getPackage(); - Reflections reflections = new Reflections(usageRecordPackage.getName()); - Set> resourceRecordsTypes = reflections.getSubTypesOf(SingleUsageRecord.class); - for(Class resourceRecordsType : resourceRecordsTypes){ + resourceRecords = new HashMap, Set>(); + Collection> resourceRecordsTypes = RecordUtility.getRecordClassesFound().values(); + for(Class resourceRecordsType : resourceRecordsTypes){ try { - SingleUsageRecord singleUsageRecord = resourceRecordsType.newInstance(); - resourceRecords.put(resourceRecordsType, singleUsageRecord.getRequiredFields()); + Record record = resourceRecordsType.newInstance(); + resourceRecords.put(resourceRecordsType, record.getRequiredFields()); } catch (InstantiationException | IllegalAccessException e) { logger.error(String.format("Unable to correctly istantiate %s", resourceRecordsType.getSimpleName()), e); } @@ -138,7 +136,7 @@ public class ResourceRecordQuery { * @return the requested list of Info * @throws Exception if fails */ - public List getInfo(@SuppressWarnings("rawtypes") Class usageRecordType, + public List getInfo(@SuppressWarnings("rawtypes") Class usageRecordType, TemporalConstraint temporalConstraint, List filters, boolean pad) throws Exception { Map unpaddedResults = accountingPersistenceQuery.query(usageRecordType, temporalConstraint, filters); if(!pad){ @@ -155,7 +153,7 @@ public class ResourceRecordQuery { * @return the requested list of Info * @throws Exception if fails */ - public List getInfo(@SuppressWarnings("rawtypes") Class usageRecordType, + public List getInfo(@SuppressWarnings("rawtypes") Class usageRecordType, TemporalConstraint temporalConstraint, List filters) throws Exception{ return getInfo(usageRecordType, temporalConstraint, filters, false); } @@ -166,14 +164,14 @@ public class ResourceRecordQuery { * @return a set containing the list of key * @throws Exception if fails */ - public List getKeys(@SuppressWarnings("rawtypes") Class usageRecordType) throws Exception{ + public List getKeys(@SuppressWarnings("rawtypes") Class usageRecordType) throws Exception{ Set keys = accountingPersistenceQuery.getKeys(usageRecordType); List toSort = new ArrayList(keys); Collections.sort(toSort); return toSort; } - public List getPossibleValuesForKey(@SuppressWarnings("rawtypes") Class usageRecordType, String key) throws Exception { + public List getPossibleValuesForKey(@SuppressWarnings("rawtypes") Class usageRecordType, String key) throws Exception { Set keys = accountingPersistenceQuery.getPossibleValuesForKey(usageRecordType, key); List toSort = new ArrayList(keys); Collections.sort(toSort); diff --git a/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceBackendQuery.java b/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceBackendQuery.java index 5d01de6..fe9364f 100644 --- a/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceBackendQuery.java +++ b/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceBackendQuery.java @@ -11,7 +11,7 @@ import java.util.Set; import org.gcube.accounting.analytics.Filter; import org.gcube.accounting.analytics.Info; import org.gcube.accounting.analytics.TemporalConstraint; -import org.gcube.accounting.datamodel.AggregatedUsageRecord; +import org.gcube.documentstore.records.AggregatedRecord; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -25,14 +25,14 @@ public abstract class AccountingPersistenceBackendQuery { protected abstract void prepareConnection(AccountingPersistenceBackendQueryConfiguration configuration) throws Exception; - protected abstract Map reallyQuery(@SuppressWarnings("rawtypes") Class usageRecordType, + protected abstract Map reallyQuery(@SuppressWarnings("rawtypes") Class usageRecordType, TemporalConstraint temporalConstraint, List filters) throws Exception; /** * Query the persistence obtaining a Map where the date is the key and * the #Info is the value. The result is relative to an Usage Record Type, * respect a TemporalConstraint and can be applied one or more filters. - * @param usageRecordType the Usage Record Type of interest + * @param recordClass the Usage Record Type of interest * @param temporalConstraint the TemporalConstraint (interval and aggregation) * @param filters the filter for the query. If null or empty string get all * data. The filters are evaluated in the order the are presented and are @@ -41,31 +41,31 @@ public abstract class AccountingPersistenceBackendQuery { * requested data * @throws Exception if fails */ - public Map query(@SuppressWarnings("rawtypes") Class usageRecordType, + public Map query(@SuppressWarnings("rawtypes") Class recordClass, TemporalConstraint temporalConstraint, List filters) throws Exception{ - logger.trace("Request query: UsageRecordType={}, {}={}, {}s={}", usageRecordType.newInstance().getUsageRecordType(), + logger.trace("Request query: RecordClass={}, {}={}, {}s={}", recordClass.newInstance().getRecordType(), TemporalConstraint.class.getSimpleName(), temporalConstraint.toString(), Filter.class.getSimpleName(), filters); - return reallyQuery(usageRecordType, temporalConstraint, filters); + return reallyQuery(recordClass, temporalConstraint, filters); } /** * Return the list of key valid for queries a certain usage record type - * @param usageRecordType the usage record type + * @param recordClass the usage record class * @return a set containing the list of key * @throws Exception if fails */ - public abstract Set getKeys(@SuppressWarnings("rawtypes") Class usageRecordType) throws Exception; + public abstract Set getKeys(@SuppressWarnings("rawtypes") Class recordClass) throws Exception; /** * Return the list of possible values for a key for a certain usage record type - * @param usageRecordType the usage record type + * @param recordClass the usage record type * @param key the key * @return a set containing the list of possible values * @throws Exception if fails */ - public abstract Set getPossibleValuesForKey(@SuppressWarnings("rawtypes") Class usageRecordType, String key) throws Exception; + public abstract Set getPossibleValuesForKey(@SuppressWarnings("rawtypes") Class recordClass, String key) throws Exception; /** * Close the connection to persistence diff --git a/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceBackendQueryConfiguration.java b/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceBackendQueryConfiguration.java index e6d5fc1..917d2db 100644 --- a/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceBackendQueryConfiguration.java +++ b/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceBackendQueryConfiguration.java @@ -1,13 +1,9 @@ package org.gcube.accounting.analytics.persistence; -import java.net.URI; - import org.gcube.accounting.persistence.AccountingPersistenceConfiguration; -import org.gcube.common.resources.gcore.ServiceEndpoint; /** * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ - * */ public class AccountingPersistenceBackendQueryConfiguration extends AccountingPersistenceConfiguration { @@ -18,22 +14,15 @@ public class AccountingPersistenceBackendQueryConfiguration extends AccountingPe super(); } - /** - * @param uri the URI of the persistence - * @param username the username to connect to persistence - * @param password the password to connect to persistence - */ - public AccountingPersistenceBackendQueryConfiguration(URI uri, String username, String password){ - super(uri, username, password); - } - /** * @param persistenceClassName The classname of the persistence to instantiate * @throws Exception if fails */ - public AccountingPersistenceBackendQueryConfiguration(String persistenceClassName) throws Exception{ - super.init(); - ServiceEndpoint serviceEndpoint = getServiceEndpoint(SERVICE_ENDPOINT_CATEGORY, SERVICE_ENDPOINT_NAME, persistenceClassName); - setValues(serviceEndpoint, persistenceClassName); + @SuppressWarnings({ "unchecked", "rawtypes" }) + public AccountingPersistenceBackendQueryConfiguration(Class clz) throws Exception{ + super((Class) clz); } + + + } diff --git a/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceBackendQueryFactory.java b/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceBackendQueryFactory.java index a33db39..1d06fd9 100644 --- a/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceBackendQueryFactory.java +++ b/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceBackendQueryFactory.java @@ -45,16 +45,16 @@ public abstract class AccountingPersistenceBackendQueryFactory { try { ServiceLoader serviceLoader = ServiceLoader.load(AccountingPersistenceBackendQuery.class); for (AccountingPersistenceBackendQuery found : serviceLoader) { + Class foundClass = found.getClass(); try { - String foundClassName = found.getClass().getSimpleName(); + String foundClassName = foundClass.getSimpleName(); logger.debug("Testing {}", foundClassName); - - AccountingPersistenceBackendQueryConfiguration configuration = new AccountingPersistenceBackendQueryConfiguration(foundClassName); + AccountingPersistenceBackendQueryConfiguration configuration = new AccountingPersistenceBackendQueryConfiguration(foundClass); found.prepareConnection(configuration); accountingPersistenceQuery = found; break; } catch (Exception e) { - logger.debug(String.format("%s not initialized correctly. It will not be used", found.getClass().getSimpleName())); + logger.debug(String.format("%s not initialized correctly. It will not be used", foundClass.getSimpleName())); } } } catch(Exception e){ diff --git a/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceQuery.java b/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceQuery.java index 507cb6c..cb64479 100644 --- a/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceQuery.java +++ b/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceQuery.java @@ -11,7 +11,7 @@ import java.util.Set; import org.gcube.accounting.analytics.Filter; import org.gcube.accounting.analytics.Info; import org.gcube.accounting.analytics.TemporalConstraint; -import org.gcube.accounting.datamodel.AggregatedUsageRecord; +import org.gcube.documentstore.records.AggregatedRecord; /** * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ @@ -36,7 +36,7 @@ public class AccountingPersistenceQuery { * Query the persistence obtaining a Map where the date is the key and * the #Info is the value. The result is relative to an Usage Record Type, * respect a TemporalConstraint and can be applied one or more filters. - * @param usageRecordType the Usage Record Type of interest + * @param recordClass the Usage Record Type of interest * @param temporalConstraint the TemporalConstraint (interval and aggregation) * @param filters the filter for the query. If null or empty string get all * data. The filters are evaluated in the order the are presented and are @@ -45,19 +45,19 @@ public class AccountingPersistenceQuery { * requested data * @throws Exception if fails */ - public Map query(@SuppressWarnings("rawtypes") Class usageRecordType, + public Map query(@SuppressWarnings("rawtypes") Class recordClass, TemporalConstraint temporalConstraint, List filters) throws Exception{ - return AccountingPersistenceBackendQueryFactory.getInstance().query(usageRecordType, temporalConstraint, filters); + return AccountingPersistenceBackendQueryFactory.getInstance().query(recordClass, temporalConstraint, filters); } /** * Return the list of key valid for queries a certain usage record type - * @param usageRecordType the usage record type + * @param recordClass the usage record type * @return a set containing the list of key * @throws Exception if fails */ - public Set getKeys(@SuppressWarnings("rawtypes") Class usageRecordType) throws Exception { - return AccountingPersistenceBackendQueryFactory.getInstance().getKeys(usageRecordType); + public Set getKeys(@SuppressWarnings("rawtypes") Class recordClass) throws Exception { + return AccountingPersistenceBackendQueryFactory.getInstance().getKeys(recordClass); } @@ -68,7 +68,7 @@ public class AccountingPersistenceQuery { * @return a set containing the list of possible values * @throws Exception if fails */ - public Set getPossibleValuesForKey(@SuppressWarnings("rawtypes") Class usageRecordType, String key) throws Exception { + public Set getPossibleValuesForKey(@SuppressWarnings("rawtypes") Class usageRecordType, String key) throws Exception { return AccountingPersistenceBackendQueryFactory.getInstance().getPossibleValuesForKey(usageRecordType, key); } diff --git a/src/test/java/org/gcube/accounting/analytics/ResourceRecordQueryTest.java b/src/test/java/org/gcube/accounting/analytics/ResourceRecordQueryTest.java index fd19042..9356c10 100644 --- a/src/test/java/org/gcube/accounting/analytics/ResourceRecordQueryTest.java +++ b/src/test/java/org/gcube/accounting/analytics/ResourceRecordQueryTest.java @@ -7,12 +7,13 @@ import java.util.HashSet; import java.util.Set; import org.gcube.accounting.datamodel.BasicUsageRecord; -import org.gcube.accounting.datamodel.SingleUsageRecord; +import org.gcube.accounting.datamodel.RawUsageRecord; import org.gcube.accounting.datamodel.usagerecords.JobUsageRecord; import org.gcube.accounting.datamodel.usagerecords.PortletUsageRecord; import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord; import org.gcube.accounting.datamodel.usagerecords.StorageUsageRecord; import org.gcube.accounting.datamodel.usagerecords.TaskUsageRecord; +import org.gcube.documentstore.records.Record; import org.junit.Assert; import org.junit.Test; @@ -21,7 +22,7 @@ import org.junit.Test; */ public class ResourceRecordQueryTest { - public class TestUsageRecord extends BasicUsageRecord implements SingleUsageRecord { + public class TestUsageRecord extends BasicUsageRecord { /** * Generated Serial Version UID @@ -35,35 +36,32 @@ public class ResourceRecordQueryTest { } - public static Set> getExpectedResourceRecordsTypes(){ - Set> expected = new HashSet>(); + public static Set> getExpectedResourceRecordsTypes(){ + Set> expected = new HashSet>(); expected.add(ServiceUsageRecord.class); expected.add(StorageUsageRecord.class); expected.add(JobUsageRecord.class); expected.add(TaskUsageRecord.class); expected.add(PortletUsageRecord.class); + expected.add(RawUsageRecord.class); return expected; } @Test public void testGetResourceRecordsTypes(){ - Set> expected = getExpectedResourceRecordsTypes(); - Set> found = ResourceRecordQuery.getResourceRecordsTypes().keySet(); + Set> expected = getExpectedResourceRecordsTypes(); + Set> found = ResourceRecordQuery.getResourceRecordsTypes().keySet(); Assert.assertTrue(expected.containsAll(found)); Assert.assertTrue(found.containsAll(expected)); } - - + @Test public void testGetResourceRecordsTypesWithFakeClass(){ - Set> expected = getExpectedResourceRecordsTypes(); + Set> expected = getExpectedResourceRecordsTypes(); expected.add(TestUsageRecord.class); - Set> found = ResourceRecordQuery.getResourceRecordsTypes().keySet(); + Set> found = ResourceRecordQuery.getResourceRecordsTypes().keySet(); Assert.assertTrue(expected.containsAll(found)); Assert.assertFalse(found.containsAll(expected)); } - - - } diff --git a/src/test/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceConfigurationTest.java b/src/test/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceConfigurationTest.java index 398fdb3..bf3664a 100644 --- a/src/test/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceConfigurationTest.java +++ b/src/test/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceConfigurationTest.java @@ -21,16 +21,18 @@ public class AccountingPersistenceConfigurationTest { public static final String[] SCOPES = new String[]{"/gcube", "/gcube/devNext", "/gcube/devsec"}; + public abstract class AccountingPersistenceQueryCouchDB extends AccountingPersistenceBackendQuery {} + @Test public void getUsernamePasswordForScopes() throws Exception{ for(String scope : SCOPES){ ScopeProvider.instance.set(scope); try { - AccountingPersistenceConfiguration persitenceConfiguration = new AccountingPersistenceConfiguration(COUCHDB_CLASS_NAME); - logger.debug("{} {} - {} : {}", scope, - persitenceConfiguration.getUri(), - persitenceConfiguration.getUsername(), - persitenceConfiguration.getPassword()); + AccountingPersistenceBackendQueryConfiguration persitenceConfiguration = new AccountingPersistenceBackendQueryConfiguration(AccountingPersistenceQueryCouchDB.class); + String uri = persitenceConfiguration.getProperty(AccountingPersistenceConfiguration.URL_PROPERTY_KEY); + String username = persitenceConfiguration.getProperty(AccountingPersistenceConfiguration.USERNAME_PROPERTY_KEY); + String password = persitenceConfiguration.getProperty(AccountingPersistenceConfiguration.PASSWORD_PROPERTY_KEY); + logger.debug("{} {} - {} : {}", scope, uri, username, password); }catch(IndexOutOfBoundsException e){ logger.debug("No AccountingPersistenceConfiguration : \n {} {} \n\n", e.getClass().getName(), e.getMessage()); } catch(Exception e){