package org.gcube.phd; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.SortedMap; import java.util.TreeMap; import org.apache.commons.math3.stat.descriptive.SummaryStatistics; import org.gcube.common.resources.gcore.Resource; import org.gcube.common.scope.impl.ScopeBean; import org.gcube.common.scope.impl.ScopeBean.Type; import org.gcube.testutility.ContextTest; import org.junit.Test; public class StatisticsGenerator { private File directory; private File statisticsFile; public StatisticsGenerator() { directory = new File("/home/lucafrosini/Desktop/GlobalStatistics"); if(!directory.exists()) { directory.mkdirs(); } } private void printLineToFile(String line, File file) throws IOException { synchronized(file) { try(FileWriter fw = new FileWriter(file, true); BufferedWriter bw = new BufferedWriter(fw); PrintWriter out = new PrintWriter(bw)) { out.println(line); out.flush(); } catch(IOException e) { throw e; } } } private void addContextCount(Map contextCount) throws Exception { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("Context Type"); stringBuffer.append(","); stringBuffer.append("Count"); printLineToFile(stringBuffer.toString(), statisticsFile); for(Type type : contextCount.keySet()) { stringBuffer = new StringBuffer(); stringBuffer.append(type.name()); stringBuffer.append(","); stringBuffer.append(contextCount.get(type)); printLineToFile(stringBuffer.toString(), statisticsFile); } printLineToFile("", statisticsFile); printLineToFile("", statisticsFile); printLineToFile("", statisticsFile); printLineToFile("", statisticsFile); } private void addHeader() throws IOException { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("Resource Type"); stringBuffer.append(","); stringBuffer.append("Count"); stringBuffer.append(","); stringBuffer.append("Avg Size"); stringBuffer.append(","); stringBuffer.append("Max"); stringBuffer.append(","); stringBuffer.append("Min"); stringBuffer.append(","); stringBuffer.append("Variance"); printLineToFile(stringBuffer.toString(), statisticsFile); } private void addResourceStatistic(Class clz, SummaryStatistics summaryStatistics) throws IOException { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(clz.getSimpleName()); stringBuffer.append(","); stringBuffer.append(summaryStatistics.getN()); stringBuffer.append(","); stringBuffer.append(Double.valueOf(summaryStatistics.getMean()).intValue()); stringBuffer.append(","); stringBuffer.append(Double.valueOf(summaryStatistics.getMax()).intValue()); stringBuffer.append(","); stringBuffer.append(Double.valueOf(summaryStatistics.getMin()).intValue()); stringBuffer.append(","); stringBuffer.append(Double.valueOf(summaryStatistics.getVariance()).intValue()); printLineToFile(stringBuffer.toString(), statisticsFile); } private void addStatistics(Map,SummaryStatistics> statistics) throws Exception { for(Class clz : statistics.keySet()) { addResourceStatistic(clz, statistics.get(clz)); } } private void generateStatistics( SortedMap,Set>>> aggregatedResources) throws Exception { for(ScopeBean scopeBean : aggregatedResources.keySet()) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(scopeBean.toString()); stringBuffer.append(" Statistics"); printLineToFile(stringBuffer.toString(), statisticsFile); addHeader(); Map,Set>> resourceMap = aggregatedResources .get(scopeBean); Map,SummaryStatistics> statistics = ContextStatistic .generateStatistics(resourceMap); addStatistics(statistics); printLineToFile("", statisticsFile); printLineToFile("", statisticsFile); } } private SortedMap,Set>>> getAggregatedResources( SortedMap contextStatistics) throws Exception { SortedMap,Set>>> aggregatedResources = new TreeMap<>( new ScopeBeanComparator()); Map,Set>> infrastructureResourcesByClass = null; Map,Set>> voResourcesByClass = null; for(ScopeBean scopeBean : contextStatistics.keySet()) { ContextStatistic contextStatistic = contextStatistics.get(scopeBean); Map,Set>> resourceMap = contextStatistic .getAllResources(); Type type = scopeBean.type(); switch(type) { case INFRASTRUCTURE: infrastructureResourcesByClass = new HashMap<>(); aggregatedResources.put(scopeBean, infrastructureResourcesByClass); break; case VO: voResourcesByClass = new HashMap<>(); aggregatedResources.put(scopeBean, voResourcesByClass); break; case VRE: break; default: break; } for(Class clz : resourceMap.keySet()) { Set> resources = resourceMap.get(clz); Set> infrastructureResources = infrastructureResourcesByClass.get(clz); if(infrastructureResources == null) { infrastructureResources = new HashSet<>(); infrastructureResourcesByClass.put(clz, infrastructureResources); } infrastructureResources.addAll(resources); Set> voResources = voResourcesByClass.get(clz); if(voResources == null) { voResources = new HashSet<>(); voResourcesByClass.put(clz, voResources); } voResources.addAll(resources); } } return aggregatedResources; } private void generateStatistics() throws Exception { statisticsFile = new File(directory, "all.csv"); if(statisticsFile.exists()) { statisticsFile.delete(); } statisticsFile.createNewFile(); StatisticsCollector statisticsCollector = new StatisticsCollector(); Map contextCount = statisticsCollector.getContextCount(); addContextCount(contextCount); SortedMap contextStatistics = statisticsCollector.getStatistics(); SortedMap,Set>>> aggregatedResources = getAggregatedResources( contextStatistics); generateStatistics(aggregatedResources); } @Test public void test() throws Exception { ContextTest.setContextByName("/d4science.research-infrastructures.eu"); generateStatistics(); } }