Skip to content

Commit

Permalink
MET-6078 Add latest data to historical result
Browse files Browse the repository at this point in the history
  • Loading branch information
andyjmaclean committed Aug 26, 2024
1 parent 3e23ed0 commit 056cb7f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package eu.europeana.statistics.dashboard.rest.controller;

import java.util.List;

import eu.europeana.statistics.dashboard.common.internal.model.Target;
Expand Down Expand Up @@ -48,7 +47,11 @@ public CountryTargetDataController(CountryTargetService countryTargetService) {
@ApiOperation(value = "Returns country target data", response = HistoricalCountryTargetData.class)

public List<HistoricalCountryTargetData> getAllCountryData(){
return countryTargetService.getAllCountryData();
// initalise the result with the latest data
List<HistoricalCountryTargetData> result = countryTargetService.getAllCountryDataLatest();
// add the historic data to the result
result.addAll(countryTargetService.getAllCountryData());
return result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ public List<HistoricalCountryTargetData> getAllCountryData(){
).toList();
}

public List<HistoricalCountryTargetData> getAllCountryDataLatest(){

List<HistoricalCountryTargetData> result = new ArrayList<>();
List<String> countries = mongoSDDao.getAllCountryValuesTargetCollection();

for(String country : countries) {
Historical snapshot = mongoSDDao.generateHistoricalSnapshot(country);
result.add(
new HistoricalCountryTargetData(country, snapshot.getTimestamp(), snapshot.getThreeD(),
snapshot.getHighQuality(), snapshot.getTotalRecords())
);
}
return result;
}

/**
* @return all HistoricalCountryTargetData objects
*/
Expand Down Expand Up @@ -75,5 +90,4 @@ public List<CountryTargetResult> getCountryTargets(){
}
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
import eu.europeana.statistics.dashboard.common.internal.model.StatisticsRecordModel;
import eu.europeana.statistics.dashboard.common.internal.model.Target;
import eu.europeana.statistics.dashboard.common.utils.MongoFieldNames;
import eu.europeana.statistics.dashboard.common.internal.MongoStatisticsField;

import java.util.ArrayList;
import java.util.List;
import java.time.LocalDateTime;
import org.bson.types.ObjectId;

/**
Expand Down Expand Up @@ -168,4 +170,31 @@ public List<Target> getCountryTargets(){
result.forEach(queryResult::add);
return queryResult;
}

/**
* Create a Historical object (with the current time) for the given country
*/
public Historical generateHistoricalSnapshot(String country){
StatisticsQuery query3D = this.createStatisticsQuery();
query3D.withValueFilter(MongoStatisticsField.COUNTRY, List.of(country));
query3D.withValueFilter(MongoStatisticsField.TYPE, List.of("3D"));
query3D.withValueFilter(MongoStatisticsField.CONTENT_TIER, List.of("1", "2", "3", "4")); //Exclude content tier 0
StatisticsData result3dQuery = query3D.queryForStatistics();

StatisticsQuery queryHighQuality = this.createStatisticsQuery();
queryHighQuality.withValueFilter(MongoStatisticsField.COUNTRY, List.of(country));
queryHighQuality.withValueFilter(MongoStatisticsField.CONTENT_TIER, List.of("2", "3", "4"));
queryHighQuality.withValueFilter(MongoStatisticsField.METADATA_TIER, List.of("A", "B", "C"));
StatisticsData resultHighQualityQuery = queryHighQuality.queryForStatistics();

StatisticsQuery queryTotalRecords = this.createStatisticsQuery();
queryTotalRecords.withValueFilter(MongoStatisticsField.COUNTRY, List.of(country));
queryTotalRecords.withValueFilter(MongoStatisticsField.CONTENT_TIER, List.of("1", "2", "3", "4")); //Exclude content tier 0
StatisticsData resultTotalRecords = queryTotalRecords.queryForStatistics();

Historical result = new Historical(country, result3dQuery.getRecordCount(),
resultHighQualityQuery.getRecordCount(), resultTotalRecords.getRecordCount(), LocalDateTime.now());
return result;
}

}
Original file line number Diff line number Diff line change
@@ -1,57 +1,26 @@
package eu.europeana.statistics.dashboard.worker.execution;

import eu.europeana.statistics.dashboard.common.internal.model.Historical;
import eu.europeana.statistics.dashboard.common.internal.MongoStatisticsField;
import eu.europeana.statistics.dashboard.service.persistence.MongoSDDao;
import eu.europeana.statistics.dashboard.service.persistence.StatisticsData;
import eu.europeana.statistics.dashboard.service.persistence.StatisticsQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDateTime;
import java.util.List;


public class HistoricalDataRunner{

private static final Logger LOGGER = LoggerFactory.getLogger(AnalyzerRunner.class);

public static void runHistoricalScript(MongoSDDao mongoSDDao){
List<String> countries = mongoSDDao.getAllCountryValuesTargetCollection();

for(String country : countries) {
writeHistoricalData(country,mongoSDDao);
writeHistoricalData(country, mongoSDDao);
}
}


private static void writeHistoricalData(String country, MongoSDDao mongoSDDao){
LOGGER.info("Starting historical data process for {}", country);
StatisticsQuery query3D = mongoSDDao.createStatisticsQuery();
query3D.withValueFilter(MongoStatisticsField.COUNTRY, List.of(country));
query3D.withValueFilter(MongoStatisticsField.TYPE, List.of("3D"));
query3D.withValueFilter(MongoStatisticsField.CONTENT_TIER, List.of("1", "2", "3", "4")); //Exclude content tier 0
StatisticsData result3dQuery = query3D.queryForStatistics();

StatisticsQuery queryHighQuality = mongoSDDao.createStatisticsQuery();
queryHighQuality.withValueFilter(MongoStatisticsField.COUNTRY, List.of(country));
queryHighQuality.withValueFilter(MongoStatisticsField.CONTENT_TIER, List.of("2", "3", "4"));
queryHighQuality.withValueFilter(MongoStatisticsField.METADATA_TIER, List.of("A", "B", "C"));
StatisticsData resultHighQualityQuery = queryHighQuality.queryForStatistics();

StatisticsQuery queryTotalRecords = mongoSDDao.createStatisticsQuery();
queryTotalRecords.withValueFilter(MongoStatisticsField.COUNTRY, List.of(country));
queryTotalRecords.withValueFilter(MongoStatisticsField.CONTENT_TIER, List.of("1", "2", "3", "4")); //Exclude content tier 0
StatisticsData resultTotalRecords = queryTotalRecords.queryForStatistics();

Historical result = new Historical(country, result3dQuery.getRecordCount(),
resultHighQualityQuery.getRecordCount(), resultTotalRecords.getRecordCount(), LocalDateTime.now());

Historical result = mongoSDDao.generateHistoricalSnapshot(country);
mongoSDDao.saveHistoricalRecord(result);

LOGGER.info("Finished historical data process for {}", country);

}


}

0 comments on commit 056cb7f

Please sign in to comment.