Skip to content
Snippets Groups Projects
Commit e045fb43 authored by Tor-Einar Skog's avatar Tor-Einar Skog
Browse files

VIPSNEW-60

parent 407f6390
No related branches found
No related tags found
No related merge requests found
Showing
with 137 additions and 10 deletions
......@@ -60,6 +60,7 @@ import no.bioforsk.vips.logic.scheduling.model.PreprocessorException;
import no.bioforsk.vips.logic.util.Globals;
import no.bioforsk.vips.logic.util.RunModelException;
import no.bioforsk.vips.logic.util.SessionControllerGetter;
import no.bioforsk.vips.logic.util.SystemTime;
import no.bioforsk.web.forms.FormField;
import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonNode;
......@@ -452,7 +453,9 @@ public class ForecastBean {
.withHref(iconPath + "dot_red.png");
// Run through forecast configurations
//Date benchmark = new Date();
List<PointOfInterest> poisWithAggregate = getPointOfInterestForecastsAggregate(organizationId, cropOrganismIds, theDate);
//System.out.println(this.getClass().getName() + " DEBUG: getPointOfInterestForecastsAggregate took " + (new Date().getTime() - benchmark.getTime()) + " ms to complete.");
for(PointOfInterest poiWithAggregate:poisWithAggregate)
{
final Placemark placemark = document.createAndAddPlacemark()
......@@ -472,14 +475,39 @@ public class ForecastBean {
poiWithAggregate.getAltitude() != null ? poiWithAggregate.getAltitude() : 0
));
}
//System.out.println(kml.marshal());
return kml;
}
/**
* The table forecast_result_cache always should contain the forecast
* results from TODAY (The system's time, which is configurable)
*/
public void updateForecastResultCacheTable()
{
String dateFormat = "yyyy-MM-dd";
SimpleDateFormat format = new SimpleDateFormat(dateFormat);
String transactionSQL = new StringBuilder()
.append("BEGIN;")
.append(" LOCK TABLE forecast_result_cache;")
.append(" TRUNCATE forecast_result_cache;")
.append(" INSERT INTO forecast_result_cache ")
.append(" SELECT * FROM forecast_result ")
.append(" WHERE to_char(result_valid_time, '").append(dateFormat).append("') = :dateStr ;")
.append("END;")
.toString();
Query query = em.createNativeQuery(transactionSQL);
query.setParameter("dateStr", format.format(SystemTime.getSystemTime()));
query.executeUpdate();
}
/**
* TODO: This should be cached somehow. Here in app or in web server???
* @param theDate
* Selects the "worst" (highest infection risk) warning status for forecasts
* running at the pois connected to the given organization
* @param organizationId Filter for organization
* @param cropOrganismIds Filter for crops
* @param theDate Filter for date. If theDate=systemDate, data is fetched from the caching table forecast_result_cache
* @return
*/
private List<PointOfInterest> getPointOfInterestForecastsAggregate(Integer organizationId, List<Integer> cropOrganismIds, Date theDate) {
......@@ -495,23 +523,29 @@ public class ForecastBean {
{
pois = em.createNamedQuery("PointOfInterest.findAll").getResultList();
}
SimpleDateFormat format = new SimpleDateFormat(Globals.defaultDateFormat);
String dateFormat = "yyyy-MM-dd";
SimpleDateFormat format = new SimpleDateFormat(dateFormat);
// If theDate=systemDate, data is fetched from the caching table forecast_result_cache
String tableName = (format.format(theDate).equals(format.format(SystemTime.getSystemTime()))) ? "forecast_result_cache" : "forecast_result";
//this.updateForecastResultCacheTable();
for(PointOfInterest poi: pois)
{
String sql = "SELECT max(warning_status) FROM forecast_result \n" +
String sql = "SELECT max(warning_status) FROM " + tableName + " \n" +
"WHERE forecast_configuration_id IN( \n" +
" SELECT forecast_configuration_id \n" +
" FROM forecast_configuration \n" +
" WHERE location_point_of_interest_id=:locationPointOfInterestId \n" +
(cropOrganismIds != null && ! cropOrganismIds.isEmpty() ? " AND crop_organism_id IN (" + StringUtils.join(cropOrganismIds, ",") + ") " : "") +
")\n" +
"AND to_char(result_valid_time, 'yyyy-MM-dd') = :dateStr";
"AND to_char(result_valid_time, '" + dateFormat + "') = :dateStr";
Query q = em.createNativeQuery(sql);
q.setParameter("locationPointOfInterestId", poi.getPointOfInterestId());
q.setParameter("dateStr", format.format(theDate));
Integer result = (Integer) q.getSingleResult();
poi.getProperties().put("forecastsAggregate", result);
}
return pois;
}
......
......@@ -160,12 +160,17 @@ public class SchedulingBean {
// at creation time.
VIPSLogicTaskCollector testCollector = new VIPSLogicTaskCollector(-1);
SchedulingPattern schedulingPattern = new SchedulingPattern("30 * * * *");
testCollector.getTasks().add(schedulingPattern, VipsLogicTaskFactory.createVipsLogicTask(VipsLogicTaskFactory.RUN_ALL_FORECAST_CONFIGURATIONS_TASK));
testCollector.getTasks().add(schedulingPattern, VipsLogicTaskFactory.createVipsLogicTask(VipsLogicTaskFactory.UPDATE_MODEL_INFORMATION_TASK));
SchedulingPattern halfPastPattern = new SchedulingPattern("30 * * * *");
testCollector.getTasks().add(halfPastPattern, VipsLogicTaskFactory.createVipsLogicTask(VipsLogicTaskFactory.RUN_ALL_FORECAST_CONFIGURATIONS_TASK));
testCollector.getTasks().add(halfPastPattern, VipsLogicTaskFactory.createVipsLogicTask(VipsLogicTaskFactory.UPDATE_MODEL_INFORMATION_TASK));
VIPSLogicTaskCollector cacheHandlerCollector = new VIPSLogicTaskCollector(-1);
SchedulingPattern every10MinsPattern = new SchedulingPattern("*/10 * * * *");
cacheHandlerCollector.getTasks().add(every10MinsPattern, VipsLogicTaskFactory.createVipsLogicTask(VipsLogicTaskFactory.UPDATE_FORECAST_RESULT_CACHE_TABLE_TASK));
List<TaskCollector> definedTasks = new ArrayList<>();
definedTasks.add(testCollector);
definedTasks.add(cacheHandlerCollector);
return definedTasks;
}
......
......@@ -24,6 +24,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import no.bioforsk.vips.logic.scheduling.tasks.RunAllForecastConfigurationsTask;
import no.bioforsk.vips.logic.scheduling.tasks.UpdateForecastResultCacheTableTask;
import no.bioforsk.vips.logic.scheduling.tasks.UpdateModelInformationTask;
/**
......@@ -35,8 +36,9 @@ public class VipsLogicTaskFactory {
public static final int RUN_ALL_FORECAST_CONFIGURATIONS_TASK = 1;
public static final int UPDATE_MODEL_INFORMATION_TASK = 2;
public static final int UPDATE_FORECAST_RESULT_CACHE_TABLE_TASK = 3;
private final static int[] ALL_TASK_IDS = {1,2};
private final static int[] ALL_TASK_IDS = {1,2,3};
private static List<VipsLogicTask> allTasksList;
private static Map<String,VipsLogicTask> allTasksMap;
......@@ -57,6 +59,9 @@ public class VipsLogicTaskFactory {
case UPDATE_MODEL_INFORMATION_TASK:
retVal = new UpdateModelInformationTask();
break;
case UPDATE_FORECAST_RESULT_CACHE_TABLE_TASK:
retVal = new UpdateForecastResultCacheTableTask();
break;
default:
return null;
}
......
/*
* Copyright (c) 2014 Bioforsk <http://www.bioforsk.no/>.
*
* This file is part of VIPSLogic.
* VIPSLogic is free software: you can redistribute it and/or modify
* it under the terms of the Bioforsk Open Source License as published by
* Bioforsk, either version 1 of the License, or (at your option) any
* later version.
*
* VIPSLogic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Bioforsk Open Source License for more details.
*
* You should have received a copy of the Bioforsk Open Source License
* along with VIPSLogic. If not, see <http://www.bioforsk.no/licenses/>.
*
*/
package no.bioforsk.vips.logic.scheduling.tasks;
import it.sauronsoftware.cron4j.TaskExecutionContext;
import no.bioforsk.vips.logic.scheduling.VipsLogicTask;
import no.bioforsk.vips.logic.scheduling.VipsLogicTaskFactory;
import no.bioforsk.vips.logic.util.SessionControllerGetter;
import no.bioforsk.web.forms.FormField;
/**
* @copyright 2014 <a href="http://www.bioforsk.no/">Bioforsk</a>
* @author Tor-Einar Skog <tor-einar.skog@bioforsk.no>
*/
public class UpdateForecastResultCacheTableTask extends VipsLogicTask{
@Override
public String getConfigFormDefinition(String language) {
StringBuilder retVal = new StringBuilder()
.append("{")
.append(" \"fields\":[")
.append(" {")
.append(" \"name\":\"factoryId\",")
.append(" \"dataType\":\"").append(FormField.DATA_TYPE_INTEGER).append("\",")
.append(" \"fieldType\":\"").append(FormField.FIELD_TYPE_HIDDEN).append("\",")
.append(" \"webValue\":[\"").append(VipsLogicTaskFactory.UPDATE_FORECAST_RESULT_CACHE_TABLE_TASK).append("\"]")
.append(" }")
.append(" ]")
.append("}");
return retVal.toString();
}
@Override
public void execute(TaskExecutionContext tec) throws RuntimeException {
tec.setCompleteness(0d);
SessionControllerGetter.getForecastBean().updateForecastResultCacheTable();
tec.setCompleteness(1d);
}
@Override
public boolean supportsStatusTracking()
{
return true;
}
@Override
public boolean supportsCompletenessTracking()
{
return true;
}
}
......@@ -32,6 +32,11 @@ import no.bioforsk.web.forms.FormField;
*/
public class UpdateModelInformationTask extends VipsLogicTask{
/**
*
* @param language
* @return
*/
@Override
public String getConfigFormDefinition(String language) {
StringBuilder retVal = new StringBuilder()
......
......@@ -249,3 +249,5 @@ messageDeleted=Message was deleted
taskHistoryStatusFailedCompletely=Failed completely
weatherStationStored=Weather station information was stored
cropOrganismId=Crop
task_UpdateForecastResultCacheTableTask_name=Update forecast result cache table
task_UpdateForecastResultCacheTableTask_description=To speed up certain tasks, for instance aggregating warnings for today, the table forecast_result_cache should contain only today's forecast results. Today = SystemTime.
......@@ -249,3 +249,5 @@ messageDeleted=Message was deleted
taskHistoryStatusFailedCompletely=Failed completely
weatherStationStored=Weather station information was stored
cropOrganismId=Crop
task_UpdateForecastResultCacheTableTask_name=Update forecast result cache table
task_UpdateForecastResultCacheTableTask_description=To speed up certain tasks, for instance aggregating warnings for today, the table forecast_result_cache should contain only today's forecast results. Today = SystemTime.
......@@ -248,3 +248,5 @@ messageDeleted=Message was deleted
taskHistoryStatusFailedCompletely=Failed completely
weatherStationStored=Weather station information was stored
cropOrganismId=Crop
task_UpdateForecastResultCacheTableTask_name=Update forecast result cache table
task_UpdateForecastResultCacheTableTask_description=To speed up certain tasks, for instance aggregating warnings for today, the table forecast_result_cache should contain only today's forecast results. Today = SystemTime.
......@@ -249,3 +249,5 @@ messageDeleted=Meldingen ble slettet
taskHistoryStatusFailedCompletely=Feilet fullstendig
weatherStationStored=Informasjon om m\u00e5lestasjon ble lagret
cropOrganismId=Kultur
task_UpdateForecastResultCacheTableTask_name=Oppdater tabellen forecast_result_cache
task_UpdateForecastResultCacheTableTask_description=For \u00e5 f\u00e5 opp farten p\u00e5 enkelte jobber, for eksempel aggregering av dagens varsler, s\u00e5 skal tabellen forecast_result_cache bare inneholde dagens forecast results. Dagen i dag = SystemTime
......@@ -249,3 +249,5 @@ messageDeleted=Message was deleted
taskHistoryStatusFailedCompletely=Failed completely
weatherStationStored=Weather station information was stored
cropOrganismId=Crop
task_UpdateForecastResultCacheTableTask_name=Update forecast result cache table
task_UpdateForecastResultCacheTableTask_description=To speed up certain tasks, for instance aggregating warnings for today, the table forecast_result_cache should contain only today's forecast results. Today = SystemTime.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment