From 5fc9495b430fbca985031d8f1cf636d4cfbcbb1b Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Sun, 16 Sep 2018 17:03:02 +0100 Subject: [PATCH] Added ChartJS compatible all-time average observation score endpoint. --- .../Observations/api/AverageStatsChartJs.java | 57 +++++++++++++++++++ .../Observations/dao/ObservationDao.java | 2 +- .../resources/ObservationResource.java | 7 +++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 backend/src/main/java/uk/co/neviyn/Observations/api/AverageStatsChartJs.java diff --git a/backend/src/main/java/uk/co/neviyn/Observations/api/AverageStatsChartJs.java b/backend/src/main/java/uk/co/neviyn/Observations/api/AverageStatsChartJs.java new file mode 100644 index 0000000..5f49cbc --- /dev/null +++ b/backend/src/main/java/uk/co/neviyn/Observations/api/AverageStatsChartJs.java @@ -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 labels = new ArrayList(); + + @NonNull + @JsonProperty + List datasets = new ArrayList(); + + public AverageStatsChartJs(List inputData){ + List monitoring = new ArrayList(), + control = new ArrayList(), + conservatism = new ArrayList(), + teamwork = new ArrayList(), + knowledge = new ArrayList(); + 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 data; + } +} diff --git a/backend/src/main/java/uk/co/neviyn/Observations/dao/ObservationDao.java b/backend/src/main/java/uk/co/neviyn/Observations/dao/ObservationDao.java index 7fa90df..3d4b40b 100644 --- a/backend/src/main/java/uk/co/neviyn/Observations/dao/ObservationDao.java +++ b/backend/src/main/java/uk/co/neviyn/Observations/dao/ObservationDao.java @@ -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 averageObservationScoresByDay(); } diff --git a/backend/src/main/java/uk/co/neviyn/Observations/resources/ObservationResource.java b/backend/src/main/java/uk/co/neviyn/Observations/resources/ObservationResource.java index 7e4bc63..74566ff 100644 --- a/backend/src/main/java/uk/co/neviyn/Observations/resources/ObservationResource.java +++ b/backend/src/main/java/uk/co/neviyn/Observations/resources/ObservationResource.java @@ -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 averageObservationScores(){ return dao.averageObservationScoresByDay(); } + + @Path("/average/all/chartjs") + @GET + public AverageStatsChartJs averageStatsChartJs(){ + return new AverageStatsChartJs(dao.averageObservationScoresByDay()); + } }