Commit 29564f43 authored by Matija Obreza's avatar Matija Obreza

Merge branch '408-kpi-executionrun-totalvalue' into 'master'

Resolve "KPI ExecutionRun totalValue"

Closes #408

See merge request genesys-pgr/genesys-server!340
parents 149f3644 7ac94830
/**
* Copyright 2014 Global Crop Diversity Trust
*
/*
* Copyright 2019 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
......@@ -12,7 +12,7 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
**/
package org.genesys2.server.model.kpi;
......@@ -24,6 +24,8 @@ import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
......@@ -42,6 +44,9 @@ public class ExecutionRun extends BasicModel {
*/
private static final long serialVersionUID = -2475286586650646568L;
/** The sum of all observations.value */
private Double totalValue;
@NotNull
@Column(nullable = false)
@Temporal(TemporalType.TIMESTAMP)
......@@ -55,6 +60,14 @@ public class ExecutionRun extends BasicModel {
@OneToMany(mappedBy="executionRun", cascade = { CascadeType.PERSIST }, orphanRemoval = true)
private List<Observation> observations;
@PrePersist
@PreUpdate
private void prePersist() {
if (observations != null) {
this.totalValue = observations.stream().map(Observation::getValue).reduce(0D, (x, y) -> x + y);
}
}
public Date getTimestamp() {
return timestamp;
}
......@@ -78,4 +91,12 @@ public class ExecutionRun extends BasicModel {
public void setObservations(List<Observation> observations) {
this.observations = observations;
}
public Double getTotalValue() {
return totalValue;
}
public void setTotalValue(Double totalValue) {
this.totalValue = totalValue;
}
}
......@@ -5406,3 +5406,18 @@ databaseChangeLog:
- column:
name: origins
type: varchar(200)
- changeSet:
id: 1548674771097-1
author: mborodenko
comment: Extend ExecutionRun with totalValue field
changes:
- addColumn:
tableName: kpiexecutionrun
columns:
- column:
name: totalValue
type: double
- sql:
comment: Count and assign value to totalValue field
sql: update kpiexecutionrun k set k.totalValue = (select sum(obs.value) from kpiobservation obs where obs.executionRunId = k.id)
......@@ -453,6 +453,34 @@ public class KPIServiceTest extends AbstractServicesTest {
assertThat(obsRepository.count(), is(0l));
}
@Test
public void testCountTheSumOfAllObservationsValue() {
KPIParameter param1 = kpiService.save(createParameter("institute", FaoInstitute.class, "Institute"));
Execution exec1 = new Execution();
exec1.setName(EXECUTION_NAME);
exec1.setTitle("Institute count");
exec1.setParameter(param1);
exec1.setType(ExecutionType.AVERAGE);
exec1.setProperty("accessionCount");
JpaDimension dimInst = kpiService.save(createJpaDimension("institute.code", "Institute codes", FaoInstitute.class, "code"));
exec1.addDimension(dimInst, null, "code");
BooleanDimension dimBool = kpiService.save(createBoolDimension("yesno", "Boolean yes and no dimension"));
exec1.addDimension(dimBool, null, "allowMaterialRequests");
Execution loaded = kpiService.save(exec1);
assertThat(loaded.getProperty(), is("accessionCount"));
assertThat(loaded.getType(), is(Execution.ExecutionType.AVERAGE));
ExecutionRun run = kpiService.executeAndSave(loaded);
List<Observation> observations = run.getObservations();
assertThat(observations, hasSize(10));
Double totalValue = observations.stream().map(Observation::getValue).reduce(0D, (x, y) -> x + y);
assertThat(totalValue, is(run.getTotalValue()));
}
@Test
public void testExecutionLinkedCollection() {
KPIParameter param1 = kpiService.save(createParameter("institute", FaoInstitute.class, "Institute"));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment