Added ChartJS compatible all-time average observation score endpoint.

This commit is contained in:
neviyn 2018-09-16 17:03:02 +01:00
parent 583e07877d
commit 5fc9495b43
3 changed files with 65 additions and 1 deletions

View File

@ -0,0 +1,57 @@
package uk.co.neviyn.Observations.api;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.NonNull;
/**
* AverageStats in a format easily readable by Chart.js
*/
public class AverageStatsChartJs {
@NonNull
@JsonProperty
private List<String> labels = new ArrayList<String>();
@NonNull
@JsonProperty
List<Dataset> datasets = new ArrayList<Dataset>();
public AverageStatsChartJs(List<AverageStats> inputData){
List<Float> monitoring = new ArrayList<Float>(),
control = new ArrayList<Float>(),
conservatism = new ArrayList<Float>(),
teamwork = new ArrayList<Float>(),
knowledge = new ArrayList<Float>();
for(AverageStats averageStats: inputData){
labels.add(averageStats.getDate().toString());
monitoring.add(averageStats.getMonitoring());
control.add(averageStats.getControl());
conservatism.add(averageStats.getConservatism());
teamwork.add(averageStats.getTeamwork());
knowledge.add(averageStats.getKnowledge());
}
datasets.add(new Dataset("Monitoring", "", monitoring));
datasets.add(new Dataset("Control", "", control));
datasets.add(new Dataset("Conservatism", "", conservatism));
datasets.add(new Dataset("Teamwork", "", teamwork));
datasets.add(new Dataset("Knowledge", "", knowledge));
}
@AllArgsConstructor
class Dataset {
@NonNull
@JsonProperty
private final String label;
@NonNull
@JsonProperty
private final String borderColor;
@NonNull
@JsonProperty
private final List<Float> data;
}
}

View File

@ -47,7 +47,7 @@ public interface ObservationDao {
@Bind("endDate")DateTime endDate);
@SqlQuery("SELECT AVG(monitoring) as monitoring, AVG(control) as control, AVG(conservatism) as conservatism, " +
"AVG(teamwork) as teamwork, AVG(knowledge) as knowledge, date FROM observations GROUP BY date")
"AVG(teamwork) as teamwork, AVG(knowledge) as knowledge, date FROM observations GROUP BY date ORDER BY date")
@RegisterRowMapper(AverageStats.Mapper.class)
List<AverageStats> averageObservationScoresByDay();
}

View File

@ -15,6 +15,7 @@ import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import uk.co.neviyn.Observations.api.AverageStats;
import uk.co.neviyn.Observations.api.AverageStatsChartJs;
import uk.co.neviyn.Observations.api.NewObservation;
import uk.co.neviyn.Observations.core.Observation;
import uk.co.neviyn.Observations.dao.ObservationDao;
@ -49,4 +50,10 @@ public class ObservationResource {
public List<AverageStats> averageObservationScores(){
return dao.averageObservationScoresByDay();
}
@Path("/average/all/chartjs")
@GET
public AverageStatsChartJs averageStatsChartJs(){
return new AverageStatsChartJs(dao.averageObservationScoresByDay());
}
}