diff --git a/backend/src/main/java/uk/co/neviyn/Observations/api/AverageStats.java b/backend/src/main/java/uk/co/neviyn/Observations/api/AverageStats.java new file mode 100644 index 0000000..d93042c --- /dev/null +++ b/backend/src/main/java/uk/co/neviyn/Observations/api/AverageStats.java @@ -0,0 +1,33 @@ +package uk.co.neviyn.Observations.api; + +import com.fasterxml.jackson.annotation.JsonProperty; +import java.sql.ResultSet; +import java.sql.SQLException; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; +import org.jdbi.v3.core.mapper.RowMapper; +import org.jdbi.v3.core.statement.StatementContext; +import org.joda.time.LocalDate; + +@Data +@NoArgsConstructor +@RequiredArgsConstructor +public class AverageStats { + + @NonNull + @JsonProperty + private float monitoring, control, conservatism, teamwork, knowledge; + + @NonNull + @JsonProperty + private LocalDate date; + + public static class Mapper implements RowMapper { + public AverageStats map(ResultSet rs, StatementContext ctx) throws SQLException { + return new AverageStats(rs.getFloat("monitoring"), rs.getFloat("control"), rs.getFloat("conservatism"), + rs.getFloat("teamwork"), rs.getFloat("knowledge"), new LocalDate(rs.getDate("date"))); + } + } +} 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 f3f7ce6..7fa90df 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 @@ -8,6 +8,7 @@ import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys; import org.jdbi.v3.sqlobject.statement.SqlQuery; import org.jdbi.v3.sqlobject.statement.SqlUpdate; import org.joda.time.DateTime; +import uk.co.neviyn.Observations.api.AverageStats; import uk.co.neviyn.Observations.api.NewObservation; import uk.co.neviyn.Observations.core.Observation; @@ -44,4 +45,9 @@ public interface ObservationDao { "LEFT JOIN tutor ON observation_tutor.tutorId = tutor.id WHERE tutor.id = :tutorId") List observationsByTutorBetweenDates(@Bind("tutorId")int tutorId, @Bind("startDate")DateTime startDate, @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") + @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 7f88b5d..7e4bc63 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 @@ -14,6 +14,7 @@ import lombok.RequiredArgsConstructor; 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.NewObservation; import uk.co.neviyn.Observations.core.Observation; import uk.co.neviyn.Observations.dao.ObservationDao; @@ -42,4 +43,10 @@ public class ObservationResource { public List observationsBy(@PathParam("tutorId") int tutorId){ return dao.observationsByTutor(tutorId); } + + @Path("/average/all") + @GET + public List averageObservationScores(){ + return dao.averageObservationScoresByDay(); + } }