2019-03-01 14:37:58 +01:00
|
|
|
package org.gcube.phd;
|
|
|
|
|
2019-03-04 16:52:05 +01:00
|
|
|
import static org.gcube.common.authorization.client.Constants.authorizationService;
|
|
|
|
|
2019-03-01 16:48:43 +01:00
|
|
|
import java.io.UnsupportedEncodingException;
|
2019-03-01 14:37:58 +01:00
|
|
|
import java.util.ArrayList;
|
2019-03-04 16:52:05 +01:00
|
|
|
import java.util.Calendar;
|
2019-03-01 14:37:58 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
2019-03-04 16:52:05 +01:00
|
|
|
import java.util.SortedMap;
|
|
|
|
import java.util.TreeSet;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2019-03-01 14:37:58 +01:00
|
|
|
|
|
|
|
import org.apache.commons.math3.stat.descriptive.SummaryStatistics;
|
2019-03-04 16:52:05 +01:00
|
|
|
import org.gcube.accounting.aggregator.utility.Utility;
|
|
|
|
import org.gcube.accounting.analytics.Filter;
|
|
|
|
import org.gcube.accounting.analytics.Info;
|
|
|
|
import org.gcube.accounting.analytics.TemporalConstraint;
|
|
|
|
import org.gcube.accounting.analytics.TemporalConstraint.AggregationMode;
|
|
|
|
import org.gcube.accounting.analytics.persistence.AccountingPersistenceQuery;
|
|
|
|
import org.gcube.accounting.analytics.persistence.AccountingPersistenceQueryFactory;
|
|
|
|
import org.gcube.accounting.datamodel.aggregation.AggregatedServiceUsageRecord;
|
|
|
|
import org.gcube.common.authorization.client.Constants;
|
|
|
|
import org.gcube.common.authorization.library.AuthorizationEntry;
|
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
|
|
|
import org.gcube.common.authorization.library.provider.UserInfo;
|
2019-03-01 14:37:58 +01:00
|
|
|
import org.gcube.common.resources.gcore.GCoreEndpoint;
|
|
|
|
import org.gcube.common.resources.gcore.GenericResource;
|
|
|
|
import org.gcube.common.resources.gcore.HostingNode;
|
|
|
|
import org.gcube.common.resources.gcore.Resource;
|
|
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
|
|
|
import org.gcube.common.resources.gcore.Software;
|
2019-03-04 16:52:05 +01:00
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
2019-03-01 14:37:58 +01:00
|
|
|
import org.gcube.common.scope.impl.ScopeBean;
|
2019-03-04 16:52:05 +01:00
|
|
|
import org.gcube.documentstore.records.AggregatedRecord;
|
2019-03-01 14:37:58 +01:00
|
|
|
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
|
|
|
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
|
|
|
import org.gcube.resources.discovery.icclient.ICFactory;
|
2019-03-04 16:52:05 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2019-03-01 14:37:58 +01:00
|
|
|
|
|
|
|
public class ContextStatistic {
|
|
|
|
|
2019-03-04 16:52:05 +01:00
|
|
|
protected Logger logger = LoggerFactory.getLogger(ContextStatistic.class);
|
|
|
|
|
|
|
|
public static Calendar START_TIME;
|
|
|
|
public static Calendar END_TIME;
|
|
|
|
private static AccountingPersistenceQuery apq;
|
|
|
|
|
|
|
|
static {
|
|
|
|
START_TIME = Utility.getAggregationStartCalendar(2017, Calendar.JANUARY, 1);
|
|
|
|
END_TIME = Utility.getAggregationStartCalendar(2017, Calendar.DECEMBER, 1);
|
|
|
|
apq = AccountingPersistenceQueryFactory.getInstance();
|
|
|
|
}
|
|
|
|
|
2019-03-01 14:37:58 +01:00
|
|
|
private ScopeBean scopeBean;
|
|
|
|
|
|
|
|
public ScopeBean getScopeBean() {
|
|
|
|
return scopeBean;
|
|
|
|
}
|
2019-03-01 16:48:43 +01:00
|
|
|
|
|
|
|
private Map<Class<? extends Resource>,Set<ResourceInfo<? extends Resource>>> resourceMap;
|
|
|
|
private Map<Class<? extends Resource>,SummaryStatistics> statisticsMap;
|
2019-03-04 16:52:05 +01:00
|
|
|
private SortedMap<Calendar, Info> accountingData;
|
2019-03-01 14:37:58 +01:00
|
|
|
|
|
|
|
public ContextStatistic(ScopeBean scopeBean) {
|
|
|
|
this.scopeBean = scopeBean;
|
|
|
|
}
|
|
|
|
|
|
|
|
public <R extends Resource> void analizeInstances(Class<R> clz) throws Exception {
|
|
|
|
Set<ResourceInfo<? extends Resource>> resources = resourceMap.get(clz);
|
2019-03-04 16:52:05 +01:00
|
|
|
if(resources==null) {
|
|
|
|
resources = new TreeSet<>();
|
|
|
|
resourceMap.put(clz, resources);
|
|
|
|
}
|
2019-03-01 14:37:58 +01:00
|
|
|
DiscoveryClient<R> client = ICFactory.clientFor(clz);
|
|
|
|
SimpleQuery query = ICFactory.queryFor(clz);
|
2019-03-04 16:52:05 +01:00
|
|
|
// logger.debug(query.expression());
|
2019-03-01 14:37:58 +01:00
|
|
|
List<R> instances = client.submit(query);
|
|
|
|
for(R r : instances) {
|
|
|
|
ResourceInfo<R> resourceInfo = new ResourceInfo<R>(r);
|
|
|
|
resources.add(resourceInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 16:52:05 +01:00
|
|
|
@SuppressWarnings("unused")
|
|
|
|
private String generateUserToken(String context) throws Exception {
|
|
|
|
AuthorizationEntry authorizationEntry = Constants.authorizationService()
|
|
|
|
.get(SecurityTokenProvider.instance.get());
|
|
|
|
UserInfo userInfo = (UserInfo) authorizationEntry.getClientInfo();
|
|
|
|
String userToken = authorizationService().generateUserToken(userInfo, context);
|
|
|
|
logger.trace("Token for Context {} for {} is {}", context, userInfo.getId(), userToken);
|
|
|
|
return userToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-01 16:48:43 +01:00
|
|
|
public Map<Class<? extends Resource>,Set<ResourceInfo<? extends Resource>>> getAllResources() throws Exception {
|
|
|
|
if(resourceMap == null) {
|
2019-03-01 14:37:58 +01:00
|
|
|
resourceMap = new HashMap<>();
|
|
|
|
List<Class<? extends Resource>> classes = new ArrayList<>();
|
|
|
|
classes.add(GCoreEndpoint.class);
|
|
|
|
classes.add(HostingNode.class);
|
|
|
|
classes.add(ServiceEndpoint.class);
|
|
|
|
classes.add(GenericResource.class);
|
|
|
|
classes.add(Software.class);
|
|
|
|
|
2019-03-04 16:52:05 +01:00
|
|
|
String contextFullName = scopeBean.toString();
|
|
|
|
/*
|
|
|
|
String token = generateUserToken(contextFullName);
|
|
|
|
ContextTest.setContext(token);
|
|
|
|
*/
|
|
|
|
ScopeProvider.instance.set(contextFullName);
|
|
|
|
|
2019-03-01 14:37:58 +01:00
|
|
|
for(Class<? extends Resource> clz : classes) {
|
2019-03-04 16:52:05 +01:00
|
|
|
Thread.sleep(TimeUnit.SECONDS.toMillis(5));
|
2019-03-01 14:37:58 +01:00
|
|
|
analizeInstances(clz);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resourceMap;
|
|
|
|
}
|
|
|
|
|
2019-03-01 16:48:43 +01:00
|
|
|
public static Map<Class<? extends Resource>,SummaryStatistics> generateStatistics(
|
|
|
|
Map<Class<? extends Resource>,Set<ResourceInfo<? extends Resource>>> resourceMap)
|
|
|
|
throws UnsupportedEncodingException {
|
|
|
|
Map<Class<? extends Resource>,SummaryStatistics> statisticsMap = new HashMap<>();
|
|
|
|
for(Class<? extends Resource> clz : resourceMap.keySet()) {
|
|
|
|
SummaryStatistics summaryStatistics = new SummaryStatistics();
|
|
|
|
Set<ResourceInfo<? extends Resource>> resources = resourceMap.get(clz);
|
|
|
|
for(ResourceInfo<? extends Resource> resourceInfo : resources) {
|
|
|
|
summaryStatistics.addValue(resourceInfo.getSize());
|
2019-03-01 14:37:58 +01:00
|
|
|
}
|
2019-03-04 16:52:05 +01:00
|
|
|
statisticsMap.put(clz, summaryStatistics);
|
2019-03-01 14:37:58 +01:00
|
|
|
}
|
|
|
|
return statisticsMap;
|
|
|
|
}
|
|
|
|
|
2019-03-04 16:52:05 +01:00
|
|
|
public SortedMap<Calendar, Info> getAccountingData() throws Exception {
|
|
|
|
if(accountingData==null) {
|
|
|
|
|
|
|
|
TemporalConstraint temporalConstraint = new TemporalConstraint(
|
|
|
|
START_TIME.getTimeInMillis(), END_TIME.getTimeInMillis(),
|
|
|
|
AggregationMode.MONTHLY);
|
|
|
|
List<Filter> filters = new ArrayList<>();
|
|
|
|
Filter filter = new Filter("serviceClass", "InformationSystem");
|
|
|
|
filters.add(filter);
|
|
|
|
|
|
|
|
Class<? extends AggregatedRecord<?, ?>> clz = AggregatedServiceUsageRecord.class;
|
|
|
|
|
|
|
|
// AccountingPersistenceQueryFactory.getForcedQueryScope().set(scopeBean.toString());
|
|
|
|
|
|
|
|
accountingData = apq.getTimeSeries(clz, temporalConstraint,
|
|
|
|
filters, true);
|
|
|
|
}
|
|
|
|
return accountingData;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map<Class<? extends Resource>,SummaryStatistics> getStatistics() throws Exception {
|
2019-03-01 16:48:43 +01:00
|
|
|
if(statisticsMap == null) {
|
|
|
|
getAllResources();
|
|
|
|
statisticsMap = generateStatistics(resourceMap);
|
|
|
|
}
|
|
|
|
return statisticsMap;
|
|
|
|
}
|
|
|
|
|
2019-03-01 14:37:58 +01:00
|
|
|
}
|