Commit d072460b authored by Matija Obreza's avatar Matija Obreza

Introduced ExecutionRun to group Observations

parent f11aded1
...@@ -144,4 +144,5 @@ public class Execution extends VersionedAuditedModel { ...@@ -144,4 +144,5 @@ public class Execution extends VersionedAuditedModel {
public void setTitle(String title) { public void setTitle(String title) {
this.title = title; this.title = title;
} }
} }
/**
* Copyright 2014 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.genesys2.server.model.BusinessModel;
import com.fasterxml.jackson.annotation.JsonIgnore;
@Entity
@Table(name = "kpiexecutionrun")
public class ExecutionRun extends BusinessModel {
@Column(nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date timestamp = new Date();
@JsonIgnore
@ManyToOne(cascade = {}, optional = false)
private Execution execution;
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
public Execution getExecution() {
return execution;
}
public void setExecution(Execution execution) {
this.execution = execution;
}
}
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
package org.genesys2.server.model.kpi; package org.genesys2.server.model.kpi;
import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
...@@ -29,13 +28,9 @@ import javax.persistence.ManyToMany; ...@@ -29,13 +28,9 @@ import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.PrePersist; import javax.persistence.PrePersist;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.genesys2.server.model.BusinessModel; import org.genesys2.server.model.BusinessModel;
import com.fasterxml.jackson.annotation.JsonIgnore;
/** /**
* Holds results of {@link Execution} run. * Holds results of {@link Execution} run.
*/ */
...@@ -43,17 +38,12 @@ import com.fasterxml.jackson.annotation.JsonIgnore; ...@@ -43,17 +38,12 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
@Table(name = "kpiobservation") @Table(name = "kpiobservation")
public class Observation extends BusinessModel { public class Observation extends BusinessModel {
@Column(nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date timestamp = new Date();
@Column(name = "`value`") @Column(name = "`value`")
private double value; private double value;
@JsonIgnore
@ManyToOne(cascade = {}, fetch = FetchType.LAZY, optional = false) @ManyToOne(cascade = {}, fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "executionId") @JoinColumn(name = "executionRunId")
private Execution execution; private ExecutionRun executionRun;
@ManyToMany(fetch = FetchType.LAZY) @ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "kpiobservationdimension", joinColumns = @JoinColumn(name = "observationId"), inverseJoinColumns = @JoinColumn(name = "dimensionKeyId")) @JoinTable(name = "kpiobservationdimension", joinColumns = @JoinColumn(name = "observationId"), inverseJoinColumns = @JoinColumn(name = "dimensionKeyId"))
...@@ -91,24 +81,16 @@ public class Observation extends BusinessModel { ...@@ -91,24 +81,16 @@ public class Observation extends BusinessModel {
this.dimensionCount = dimensionCount; this.dimensionCount = dimensionCount;
} }
public Date getTimestamp() { public ExecutionRun getExecutionRun() {
return timestamp; return executionRun;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
public Execution getExecution() {
return execution;
} }
public void setExecution(Execution execution) { public void setExecutionRun(ExecutionRun executionRun) {
this.execution = execution; this.executionRun = executionRun;
} }
@Override @Override
public String toString() { public String toString() {
return execution.getName() + "=" + value + " D=" + dimensions + " T=" + timestamp.getTime(); return "value =" + value + " D=" + dimensions;
} }
} }
/**
* Copyright 2014 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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.persistence.domain.kpi;
import java.util.List;
import org.genesys2.server.model.kpi.Execution;
import org.genesys2.server.model.kpi.ExecutionRun;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ExecutionRunRepository extends JpaRepository<ExecutionRun, Long> {
List<ExecutionRun> findByExecution(Execution execution, Pageable pageable);
}
...@@ -28,13 +28,14 @@ import org.springframework.data.jpa.repository.Query; ...@@ -28,13 +28,14 @@ import org.springframework.data.jpa.repository.Query;
public interface ObservationRepository extends JpaRepository<Observation, Long>, ObservationCustomRepository { public interface ObservationRepository extends JpaRepository<Observation, Long>, ObservationCustomRepository {
@Query("select o from Observation o where o.executionRun.execution=?1")
List<Observation> findByExecution(Execution execution, Pageable page); List<Observation> findByExecution(Execution execution, Pageable page);
@Query("select o from Observation o where o.execution=?1 and ?2 member of o.dimensions order by o.timestamp desc") @Query("select o from Observation o where o.executionRun.execution=?1 and ?2 member of o.dimensions order by o.executionRun.timestamp desc")
List<Observation> listObservationsByDimensionKey(Execution execution, DimensionKey next, Pageable page); List<Observation> listObservationsByDimensionKey(Execution execution, DimensionKey next, Pageable page);
@Modifying @Modifying
@Query("delete from Observation o where o.execution=?1") @Query("delete from Observation o where o.executionRun.execution=?1")
void deleteByExecution(Execution execution); void deleteByExecution(Execution execution);
} }
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
package org.genesys2.server.service.impl; package org.genesys2.server.service.impl;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -32,12 +33,14 @@ import org.apache.commons.logging.LogFactory; ...@@ -32,12 +33,14 @@ import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.kpi.Dimension; import org.genesys2.server.model.kpi.Dimension;
import org.genesys2.server.model.kpi.DimensionKey; import org.genesys2.server.model.kpi.DimensionKey;
import org.genesys2.server.model.kpi.Execution; import org.genesys2.server.model.kpi.Execution;
import org.genesys2.server.model.kpi.ExecutionRun;
import org.genesys2.server.model.kpi.JpaDimension; import org.genesys2.server.model.kpi.JpaDimension;
import org.genesys2.server.model.kpi.KPIParameter; import org.genesys2.server.model.kpi.KPIParameter;
import org.genesys2.server.model.kpi.Observation; import org.genesys2.server.model.kpi.Observation;
import org.genesys2.server.persistence.domain.kpi.DimensionKeyRepository; import org.genesys2.server.persistence.domain.kpi.DimensionKeyRepository;
import org.genesys2.server.persistence.domain.kpi.DimensionRepository; import org.genesys2.server.persistence.domain.kpi.DimensionRepository;
import org.genesys2.server.persistence.domain.kpi.ExecutionRepository; import org.genesys2.server.persistence.domain.kpi.ExecutionRepository;
import org.genesys2.server.persistence.domain.kpi.ExecutionRunRepository;
import org.genesys2.server.persistence.domain.kpi.KPIParameterRepository; import org.genesys2.server.persistence.domain.kpi.KPIParameterRepository;
import org.genesys2.server.persistence.domain.kpi.ObservationRepository; import org.genesys2.server.persistence.domain.kpi.ObservationRepository;
import org.genesys2.server.service.KPIService; import org.genesys2.server.service.KPIService;
...@@ -72,6 +75,9 @@ public class KPIServiceImpl implements KPIService { ...@@ -72,6 +75,9 @@ public class KPIServiceImpl implements KPIService {
@Autowired @Autowired
private DimensionKeyRepository dimensionKeyRepository; private DimensionKeyRepository dimensionKeyRepository;
@Autowired
private ExecutionRunRepository executionRunRepository;
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#parameter, 'ADMINISTRATION')") @PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#parameter, 'ADMINISTRATION')")
@Override @Override
@Transactional @Transactional
...@@ -221,8 +227,13 @@ public class KPIServiceImpl implements KPIService { ...@@ -221,8 +227,13 @@ public class KPIServiceImpl implements KPIService {
@Override @Override
@Transactional @Transactional
public List<Observation> save(Execution execution, List<Observation> observations) { public List<Observation> save(Execution execution, List<Observation> observations) {
ExecutionRun executionRun=new ExecutionRun();
executionRun.setExecution(execution);
executionRun.setTimestamp(new Date());
executionRunRepository.save(executionRun);
for (Observation obs : observations) { for (Observation obs : observations) {
obs.setExecution(execution); obs.setExecutionRun(executionRun);
Set<DimensionKey> dims = new HashSet<DimensionKey>(); Set<DimensionKey> dims = new HashSet<DimensionKey>();
for (DimensionKey dk : obs.getDimensions()) { for (DimensionKey dk : obs.getDimensions()) {
DimensionKey existing = dimensionKeyRepository.findByNameAndValue(dk.getName(), dk.getValue()); DimensionKey existing = dimensionKeyRepository.findByNameAndValue(dk.getName(), dk.getValue());
...@@ -271,7 +282,6 @@ public class KPIServiceImpl implements KPIService { ...@@ -271,7 +282,6 @@ public class KPIServiceImpl implements KPIService {
Observation observation = new Observation(); Observation observation = new Observation();
observation.setValue(res); observation.setValue(res);
observation.setExecution(paramExec);
for (int i = 0; i < array.length; i++) { for (int i = 0; i < array.length; i++) {
String name = paramExec.getDimension(i).getName(); String name = paramExec.getDimension(i).getName();
String value = array[i].toString(); String value = array[i].toString();
......
...@@ -230,7 +230,6 @@ public class KPIServiceTest { ...@@ -230,7 +230,6 @@ public class KPIServiceTest {
} }
Observation observation = new Observation(); Observation observation = new Observation();
observation.setExecution(paramExec);
observation.setValue((Long) res); observation.setValue((Long) res);
for (int i = 0; i < array.length; i++) { for (int i = 0; i < array.length; i++) {
DimensionKey dk = new DimensionKey(); DimensionKey dk = new DimensionKey();
......
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