Added route for list of observations.

This commit is contained in:
neviyn 2018-09-27 15:37:11 +01:00
parent ceac6d4079
commit 7a6798a70a
2 changed files with 82 additions and 21 deletions

View File

@ -32,8 +32,34 @@ public class ObservationDao extends AbstractDAO<Observation> {
return currentSession().createQuery("from Observation", Observation.class).list(); return currentSession().createQuery("from Observation", Observation.class).list();
} }
public List<Observation> filteredList(Site site, Tutor tutor, DateTime startDate, DateTime endDate, String whom) {
final CriteriaBuilder builder = currentSession().getCriteriaBuilder();
CriteriaQuery<Observation> criteriaQuery = builder.createQuery(Observation.class);
Root<Observation> root = criteriaQuery.from(Observation.class);
List<Predicate> predicates = new ArrayList<>();
if (site != null) {
predicates.add(builder.equal(root.get("site"), site));
}
if (startDate != null) {
predicates.add(builder.greaterThanOrEqualTo(root.get("date"), startDate));
}
if (endDate != null) {
predicates.add(builder.lessThanOrEqualTo(root.get("date"), endDate));
}
if (whom != null) {
predicates.add(builder.equal(root.get("whom"), whom));
}
if (!predicates.isEmpty()) {
criteriaQuery.having(predicates.toArray(new Predicate[0]));
}
Query<Observation> query = currentSession().createQuery(criteriaQuery);
System.out.println(query.getQueryString());
return query.getResultList();
}
/** /**
* Generate the average observation stats with data constraints. * Generate the average observation stats with data constraints.
*
* @param site Restrict to a site. * @param site Restrict to a site.
* @param tutor Restrict to a tutor. * @param tutor Restrict to a tutor.
* @param startDate Restrict to after a certain date. * @param startDate Restrict to after a certain date.
@ -42,14 +68,13 @@ public class ObservationDao extends AbstractDAO<Observation> {
* @return Average observation scores per day. * @return Average observation scores per day.
*/ */
// TODO: Add tutor filter // TODO: Add tutor filter
public List<AverageStats> averageStats(Site site, Tutor tutor, DateTime startDate, public List<AverageStats> averageStats(Site site, Tutor tutor, DateTime startDate, DateTime endDate, String whom) {
DateTime endDate, String whom) {
CriteriaBuilder builder = currentSession().getCriteriaBuilder(); CriteriaBuilder builder = currentSession().getCriteriaBuilder();
CriteriaQuery<AverageStats> criteriaQuery = builder.createQuery(AverageStats.class); CriteriaQuery<AverageStats> criteriaQuery = builder.createQuery(AverageStats.class);
Root<Observation> root = criteriaQuery.from(Observation.class); Root<Observation> root = criteriaQuery.from(Observation.class);
criteriaQuery.multiselect(builder.avg(root.get("monitoring")), builder.avg(root.get("control")), criteriaQuery.multiselect(builder.avg(root.get("monitoring")), builder.avg(root.get("control")),
builder.avg(root.get("conservatism")), builder.avg(root.get("teamwork")), builder.avg(root.get("conservatism")), builder.avg(root.get("teamwork")), builder.avg(root.get("knowledge")),
builder.avg(root.get("knowledge")), root.get("date")); root.get("date"));
criteriaQuery.groupBy(root.get("date")); criteriaQuery.groupBy(root.get("date"));
List<Predicate> predicates = new ArrayList<>(); List<Predicate> predicates = new ArrayList<>();
if (site != null) { if (site != null) {
@ -64,7 +89,9 @@ public class ObservationDao extends AbstractDAO<Observation> {
if (whom != null) { if (whom != null) {
predicates.add(builder.equal(root.get("whom"), whom)); predicates.add(builder.equal(root.get("whom"), whom));
} }
if (!predicates.isEmpty()) {
criteriaQuery.having(predicates.toArray(new Predicate[0])); criteriaQuery.having(predicates.toArray(new Predicate[0]));
}
Query<AverageStats> query = currentSession().createQuery(criteriaQuery); Query<AverageStats> query = currentSession().createQuery(criteriaQuery);
System.out.println(query.getQueryString()); System.out.println(query.getQueryString());
return query.getResultList(); return query.getResultList();

View File

@ -2,6 +2,7 @@ package uk.co.neviyn.observations.resources;
import io.dropwizard.hibernate.UnitOfWork; import io.dropwizard.hibernate.UnitOfWork;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Set; import java.util.Set;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
@ -38,6 +39,7 @@ public class ObservationResource {
/** /**
* Create a new Observation. * Create a new Observation.
*
* @param newObservation New observation data. * @param newObservation New observation data.
* @return ID of newly created observation. * @return ID of newly created observation.
*/ */
@ -51,12 +53,11 @@ public class ObservationResource {
tutors.add(tutorDao.get(l)); tutors.add(tutorDao.get(l));
} }
final Site site = siteDao.get(newObservation.getSiteId()); final Site site = siteDao.get(newObservation.getSiteId());
Observation observation = Observation.builder().site(site).tutors(tutors) Observation observation = Observation.builder().site(site).tutors(tutors).observed(newObservation.getObserved())
.observed(newObservation.getObserved()).type(TrainingType.valueOf(newObservation.getType())) .type(TrainingType.valueOf(newObservation.getType())).monitoring(newObservation.getMonitoring())
.monitoring(newObservation.getMonitoring()).control(newObservation.getControl()) .control(newObservation.getControl()).conservatism(newObservation.getConservatism())
.conservatism(newObservation.getConservatism()).teamwork(newObservation.getTeamwork()) .teamwork(newObservation.getTeamwork()).knowledge(newObservation.getKnowledge())
.knowledge(newObservation.getKnowledge()).observations(newObservation.getRawData()) .observations(newObservation.getRawData()).date(submissionDate).whom(newObservation.getWhom()).build();
.date(submissionDate).whom(newObservation.getWhom()).build();
for (Tutor t : tutors) { for (Tutor t : tutors) {
t.getObservations().add(observation); t.getObservations().add(observation);
} }
@ -65,8 +66,41 @@ public class ObservationResource {
return observation.getId(); return observation.getId();
} }
@GET
@UnitOfWork
public List<Observation> observations(@QueryParam("site") Integer siteId, @QueryParam("tutor") Integer tutorId,
@QueryParam("startDate") String startDate, @QueryParam("endDate") String endDate,
@QueryParam("whom") String whom) {
Site site = null;
Tutor tutor = null;
DateTime start = null;
DateTime end = null;
try {
site = siteDao.get(siteId);
} catch (Exception e) {
log.warn("Couldn't get site with ID " + siteId);
}
try {
tutor = tutorDao.get(tutorId);
} catch (Exception e) {
log.warn("Couldn't get tutor with ID " + tutorId);
}
try {
start = DateTime.parse(startDate);
} catch (Exception e) {
log.warn("Couldn't get a valid date from " + startDate);
}
try {
end = DateTime.parse(endDate);
} catch (Exception e) {
log.warn("Couldn't get a valid date from " + endDate);
}
return dao.filteredList(site, tutor, start, end, whom);
}
/** /**
* Get a Chart.js compatible llist of average observation scores grouped by day. * Get a Chart.js compatible llist of average observation scores grouped by day.
*
* @param siteId Only observations for site. * @param siteId Only observations for site.
* @param tutorId Only observations for tutor. * @param tutorId Only observations for tutor.
* @param startDate Only observations after startDate. * @param startDate Only observations after startDate.