212 lines
7.0 KiB
Java
212 lines
7.0 KiB
Java
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<Type,Integer> 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<? extends Resource> 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<Class<? extends Resource>,SummaryStatistics> statistics) throws Exception {
|
|
for(Class<? extends Resource> clz : statistics.keySet()) {
|
|
addResourceStatistic(clz, statistics.get(clz));
|
|
}
|
|
}
|
|
|
|
private void generateStatistics(
|
|
SortedMap<ScopeBean,Map<Class<? extends Resource>,Set<ResourceInfo<? extends Resource>>>> 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<Class<? extends Resource>,Set<ResourceInfo<? extends Resource>>> resourceMap = aggregatedResources
|
|
.get(scopeBean);
|
|
Map<Class<? extends Resource>,SummaryStatistics> statistics = ContextStatistic
|
|
.generateStatistics(resourceMap);
|
|
addStatistics(statistics);
|
|
|
|
printLineToFile("", statisticsFile);
|
|
printLineToFile("", statisticsFile);
|
|
}
|
|
}
|
|
|
|
private SortedMap<ScopeBean,Map<Class<? extends Resource>,Set<ResourceInfo<? extends Resource>>>> getAggregatedResources(
|
|
SortedMap<ScopeBean,ContextStatistic> contextStatistics) throws Exception {
|
|
|
|
SortedMap<ScopeBean,Map<Class<? extends Resource>,Set<ResourceInfo<? extends Resource>>>> aggregatedResources = new TreeMap<>(
|
|
new ScopeBeanComparator());
|
|
|
|
Map<Class<? extends Resource>,Set<ResourceInfo<? extends Resource>>> infrastructureResourcesByClass = null;
|
|
Map<Class<? extends Resource>,Set<ResourceInfo<? extends Resource>>> voResourcesByClass = null;
|
|
|
|
for(ScopeBean scopeBean : contextStatistics.keySet()) {
|
|
|
|
ContextStatistic contextStatistic = contextStatistics.get(scopeBean);
|
|
Map<Class<? extends Resource>,Set<ResourceInfo<? extends Resource>>> 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<? extends Resource> clz : resourceMap.keySet()) {
|
|
Set<ResourceInfo<? extends Resource>> resources = resourceMap.get(clz);
|
|
|
|
Set<ResourceInfo<? extends Resource>> infrastructureResources = infrastructureResourcesByClass.get(clz);
|
|
if(infrastructureResources == null) {
|
|
infrastructureResources = new HashSet<>();
|
|
infrastructureResourcesByClass.put(clz, infrastructureResources);
|
|
}
|
|
|
|
infrastructureResources.addAll(resources);
|
|
|
|
Set<ResourceInfo<? extends Resource>> 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<Type,Integer> contextCount = statisticsCollector.getContextCount();
|
|
addContextCount(contextCount);
|
|
|
|
SortedMap<ScopeBean,ContextStatistic> contextStatistics = statisticsCollector.getStatistics();
|
|
SortedMap<ScopeBean,Map<Class<? extends Resource>,Set<ResourceInfo<? extends Resource>>>> aggregatedResources = getAggregatedResources(
|
|
contextStatistics);
|
|
generateStatistics(aggregatedResources);
|
|
}
|
|
|
|
@Test
|
|
public void test() throws Exception {
|
|
ContextTest.setContextByName("/d4science.research-infrastructures.eu");
|
|
generateStatistics();
|
|
}
|
|
|
|
}
|