diff --git a/src/test/java/org/gcube/phd/ContextStatistic.java b/src/test/java/org/gcube/phd/ContextStatistic.java new file mode 100644 index 0000000..8634ba9 --- /dev/null +++ b/src/test/java/org/gcube/phd/ContextStatistic.java @@ -0,0 +1,80 @@ +package org.gcube.phd; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.commons.math3.stat.descriptive.SummaryStatistics; +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; +import org.gcube.common.scope.impl.ScopeBean; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.SimpleQuery; +import org.gcube.resources.discovery.icclient.ICFactory; + +public class ContextStatistic { + + private ScopeBean scopeBean; + + public ScopeBean getScopeBean() { + return scopeBean; + } + + private Map, Set>> resourceMap; + private Map, SummaryStatistics> statisticsMap; + + public ContextStatistic(ScopeBean scopeBean) { + this.scopeBean = scopeBean; + } + + public void analizeInstances(Class clz) throws Exception { + Set> resources = resourceMap.get(clz); + + DiscoveryClient client = ICFactory.clientFor(clz); + SimpleQuery query = ICFactory.queryFor(clz); + List instances = client.submit(query); + for(R r : instances) { + ResourceInfo resourceInfo = new ResourceInfo(r); + resources.add(resourceInfo); + } + } + + public Map, Set>> getAllResources() throws Exception { + if(resourceMap ==null) { + resourceMap = new HashMap<>(); + List> classes = new ArrayList<>(); + classes.add(GCoreEndpoint.class); + classes.add(HostingNode.class); + classes.add(ServiceEndpoint.class); + classes.add(GenericResource.class); + classes.add(Software.class); + + for(Class clz : classes) { + analizeInstances(clz); + } + } + return resourceMap; + } + + public Map, SummaryStatistics> getStatistcs() throws Exception { + if(statisticsMap==null) { + statisticsMap = new HashMap<>(); + getAllResources(); + for(Class clz : resourceMap.keySet()) { + SummaryStatistics summaryStatistics = new SummaryStatistics(); + Set> resources = resourceMap.get(clz); + for(ResourceInfo resourceInfo : resources) { + summaryStatistics.addValue(resourceInfo.getSize()); + } + } + } + return statisticsMap; + } + +} diff --git a/src/test/java/org/gcube/phd/ResourceInfo.java b/src/test/java/org/gcube/phd/ResourceInfo.java new file mode 100644 index 0000000..fb599a0 --- /dev/null +++ b/src/test/java/org/gcube/phd/ResourceInfo.java @@ -0,0 +1,37 @@ +package org.gcube.phd; + +import java.io.StringWriter; +import java.io.UnsupportedEncodingException; + +import org.gcube.common.resources.gcore.Resource; +import org.gcube.common.resources.gcore.Resources; + +public class ResourceInfo { + + private R r; + private String unmarshalledResource; + private Integer size; + + public ResourceInfo(R r) { + this.r = r; + } + + protected String getUnmarshalledResource() { + if(unmarshalledResource==null) { + StringWriter stringWriter = new StringWriter(); + Resources.marshal(r, stringWriter); + unmarshalledResource = stringWriter.toString(); + } + return unmarshalledResource; + } + + public int getSize() throws UnsupportedEncodingException { + if(size==null) { + String unmarshalledR = getUnmarshalledResource(); + final byte[] grUTF8Bytes = unmarshalledR.getBytes("UTF-8"); + size = grUTF8Bytes.length; + } + return size; + } + +} diff --git a/src/test/java/org/gcube/phd/OLDGlobalStatistics.java b/src/test/java/org/gcube/phd/oldclasses/OLDGlobalStatistics.java similarity index 99% rename from src/test/java/org/gcube/phd/OLDGlobalStatistics.java rename to src/test/java/org/gcube/phd/oldclasses/OLDGlobalStatistics.java index 278f0f8..75c4570 100644 --- a/src/test/java/org/gcube/phd/OLDGlobalStatistics.java +++ b/src/test/java/org/gcube/phd/oldclasses/OLDGlobalStatistics.java @@ -1,4 +1,4 @@ -package org.gcube.phd; +package org.gcube.phd.oldclasses; import static org.gcube.common.authorization.client.Constants.authorizationService; diff --git a/src/test/java/org/gcube/phd/Statistics.java b/src/test/java/org/gcube/phd/oldclasses/Statistics.java similarity index 99% rename from src/test/java/org/gcube/phd/Statistics.java rename to src/test/java/org/gcube/phd/oldclasses/Statistics.java index f0b3ccc..aaed014 100644 --- a/src/test/java/org/gcube/phd/Statistics.java +++ b/src/test/java/org/gcube/phd/oldclasses/Statistics.java @@ -1,4 +1,4 @@ -package org.gcube.phd; +package org.gcube.phd.oldclasses; import java.io.BufferedWriter; import java.io.File; diff --git a/src/test/java/org/gcube/phd/StatisticsAggregator.java b/src/test/java/org/gcube/phd/oldclasses/StatisticsAggregator.java similarity index 94% rename from src/test/java/org/gcube/phd/StatisticsAggregator.java rename to src/test/java/org/gcube/phd/oldclasses/StatisticsAggregator.java index 396a516..cc7c2f6 100644 --- a/src/test/java/org/gcube/phd/StatisticsAggregator.java +++ b/src/test/java/org/gcube/phd/oldclasses/StatisticsAggregator.java @@ -1,4 +1,4 @@ -package org.gcube.phd; +package org.gcube.phd.oldclasses; import static org.gcube.common.authorization.client.Constants.authorizationService; @@ -44,7 +44,7 @@ public class StatisticsAggregator extends ContextElaborator { private Map contextCount; // > - private Map> globalMap; + private Map> infrastructuresResourcesMap; // > private Map>> voContextMap; @@ -52,7 +52,7 @@ public class StatisticsAggregator extends ContextElaborator { public StatisticsAggregator() { contextCount = new HashMap<>(); - globalMap = new HashMap<>(); + infrastructuresResourcesMap = new HashMap<>(); voContextMap = new HashMap<>(); directory = new File("/home/lucafrosini/Desktop/GlobalStatistics"); if(!directory.exists()) { @@ -86,12 +86,12 @@ public class StatisticsAggregator extends ContextElaborator { } public void analizeInstances(Class clz) throws Exception { - Map resourcesGlobalMap = globalMap.get(clz.getSimpleName()); - Map resourcesPerContextMap = null; + Map resourcesGlobalMap = infrastructuresResourcesMap.get(clz.getSimpleName()); + Map resourcesPerVOMap = null; Type type = currentScopeBean.type(); if(type != Type.INFRASTRUCTURE) { - resourcesPerContextMap = getVOContextMap().get(clz.getSimpleName()); + resourcesPerVOMap = getVOContextMap().get(clz.getSimpleName()); } DiscoveryClient client = ICFactory.clientFor(clz); @@ -101,8 +101,8 @@ public class StatisticsAggregator extends ContextElaborator { String id = r.id(); int resourceSize = getResourceSize(r); if(type != Type.INFRASTRUCTURE) { - if(!resourcesPerContextMap.containsKey(id)) { - resourcesPerContextMap.put(id, resourceSize); + if(!resourcesPerVOMap.containsKey(id)) { + resourcesPerVOMap.put(id, resourceSize); } } @@ -122,7 +122,7 @@ public class StatisticsAggregator extends ContextElaborator { for(Class clz : classes) { Map resourcesGlobalMap = new HashMap<>(); - globalMap.put(clz.getSimpleName(), resourcesGlobalMap); + infrastructuresResourcesMap.put(clz.getSimpleName(), resourcesGlobalMap); if(currentScopeBean.type() != Type.INFRASTRUCTURE) { Map> contextMap = getVOContextMap(); @@ -252,7 +252,7 @@ public class StatisticsAggregator extends ContextElaborator { stringBuffer.append("Whole Infrastructure Statistics"); printLineToFile(stringBuffer.toString(), statisticsFile); addHeader(); - getStatistcsFromMap(globalMap); + getStatistcsFromMap(infrastructuresResourcesMap); printLineToFile("", statisticsFile); printLineToFile("", statisticsFile); printLineToFile("", statisticsFile);