Commit 7ac94830 authored by Maxym Borodenko's avatar Maxym Borodenko
Browse files

KPI ExecutionRun totalValue

parent 149f3644
/** /*
* Copyright 2014 Global Crop Diversity Trust * Copyright 2019 Global Crop Diversity Trust
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ **/
package org.genesys2.server.model.kpi; package org.genesys2.server.model.kpi;
...@@ -24,6 +24,8 @@ import javax.persistence.Column; ...@@ -24,6 +24,8 @@ import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.Temporal; import javax.persistence.Temporal;
import javax.persistence.TemporalType; import javax.persistence.TemporalType;
...@@ -42,6 +44,9 @@ public class ExecutionRun extends BasicModel { ...@@ -42,6 +44,9 @@ public class ExecutionRun extends BasicModel {
*/ */
private static final long serialVersionUID = -2475286586650646568L; private static final long serialVersionUID = -2475286586650646568L;
/** The sum of all observations.value */
private Double totalValue;
@NotNull @NotNull
@Column(nullable = false) @Column(nullable = false)
@Temporal(TemporalType.TIMESTAMP) @Temporal(TemporalType.TIMESTAMP)
...@@ -55,6 +60,14 @@ public class ExecutionRun extends BasicModel { ...@@ -55,6 +60,14 @@ public class ExecutionRun extends BasicModel {
@OneToMany(mappedBy="executionRun", cascade = { CascadeType.PERSIST }, orphanRemoval = true) @OneToMany(mappedBy="executionRun", cascade = { CascadeType.PERSIST }, orphanRemoval = true)
private List<Observation> observations; 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() { public Date getTimestamp() {
return timestamp; return timestamp;
} }
...@@ -78,4 +91,12 @@ public class ExecutionRun extends BasicModel { ...@@ -78,4 +91,12 @@ public class ExecutionRun extends BasicModel {
public void setObservations(List<Observation> observations) { public void setObservations(List<Observation> observations) {
this.observations = observations; this.observations = observations;
} }
public Double getTotalValue() {
return totalValue;
}
public void setTotalValue(Double totalValue) {
this.totalValue = totalValue;
}
} }
...@@ -5406,3 +5406,18 @@ databaseChangeLog: ...@@ -5406,3 +5406,18 @@ databaseChangeLog:
- column: - column:
name: origins name: origins
type: varchar(200) 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 { ...@@ -453,6 +453,34 @@ public class KPIServiceTest extends AbstractServicesTest {
assertThat(obsRepository.count(), is(0l)); 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 @Test
public void testExecutionLinkedCollection() { public void testExecutionLinkedCollection() {
KPIParameter param1 = kpiService.save(createParameter("institute", FaoInstitute.class, "Institute")); 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