Skip to content

Commit

Permalink
meteorology-bz-forecast: create base modelStation
Browse files Browse the repository at this point in the history
  • Loading branch information
dulvui committed Nov 14, 2023
1 parent 34ee8d9 commit f174f85
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
Expand All @@ -17,15 +21,28 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.opendatahub.bdp.commons.dc.meteorology.bz.forecast.config.DataConfig;
import com.opendatahub.bdp.commons.dc.meteorology.bz.forecast.config.DataTypes;
import com.opendatahub.bdp.commons.dc.meteorology.bz.forecast.config.ProvenanceConfig;
import com.opendatahub.bdp.commons.dc.meteorology.bz.forecast.config.StationConfig;
import com.opendatahub.bdp.commons.dc.meteorology.bz.forecast.dto.ForecastDto;
import com.opendatahub.bdp.commons.dc.meteorology.bz.forecast.services.S3Service;

import it.bz.idm.bdp.dto.DataMapDto;
import it.bz.idm.bdp.dto.RecordDtoImpl;
import it.bz.idm.bdp.dto.SimpleRecordDto;
import it.bz.idm.bdp.dto.StationDto;
import it.bz.idm.bdp.dto.StationList;

import com.opendatahub.bdp.commons.dc.meteorology.bz.forecast.services.OdhClient;

@Service
public class Scheduler {
private static final Logger LOG = LoggerFactory.getLogger(Scheduler.class);

// hard coded bz coordinates for main Station Dto location 46.49067, 11.33982
private final Double BZ_LAT = 46.49067;
private final Double BZ_LON = 11.33982;

@Lazy
@Autowired
private OdhClient odhClient;
Expand All @@ -46,20 +63,37 @@ public class Scheduler {
@Autowired
private S3Service s3;

// @PostConstruct
// private void postConstruct(){
// try {
// collectForecastData();
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
@PostConstruct
private void postConstruct() {
odhClient.syncDataTypes(stationType,
Arrays.stream(DataTypes.values())
.filter(d -> d.syncToOdh)
.map(DataTypes::toDataTypeDto)
.toList());
}

@Scheduled(cron = "${scheduler.job}")
public void collectForecastData() throws InterruptedException, JsonMappingException, JsonProcessingException {
LOG.info("Cron job started");
s3.downloadFile();
ForecastDto dto = s3.getForecastDto();

// create main station of model
StationDto modelStation = new StationDto(dto.info.model, dto.info.model, BZ_LAT, BZ_LON);
// and its metadata
Map<String, Object> metadata = new HashMap<>();
metadata.put("currentModelRun", dto.info.currentModelRun);
metadata.put("nextModelRun", dto.info.nextModelRun);
metadata.put("fileName", dto.info.fileName);
modelStation.setMetaData(metadata);
// and its data
DataMapDto<RecordDtoImpl> modelMeasurements = new DataMapDto<>();
Long timestamp = 0; // TODO get from dto.info.currentModelRun
SimpleRecordDto record = new SimpleRecordDto(timestamp, dto.info.absTempMax, dataC.period12h);
modelMeasurements.addRecord(dto.info.model, DataTypes.airTemperatureMax.key, record);

odhClient.syncStations(stationC.modelStationType, new StationList(Arrays.asList(modelStation)));
odhClient.pushData(stationC.modelStationType, modelMeasurements);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public class DataConfig {
@Value("${data.period.3h}")
public int period3h;

@Value("${data.period.12h}")
public int period12h;

@Value("${data.period.24h}")
public int period24h;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

@Configuration
public class StationConfig {
@Value("${station.stationType}")
public String stationType;
@Value("${station.modelStationType}")
public String modelStationType;

@Value("${station.dataStationType}")
public String dataStationType;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,24 @@
@JsonDeserialize
public class ForecastDto {

public Info info;
public List<Municipality> municipalities;

@ToString
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Info {
public String model;
public String currentModelRun;
public String nextModelRun;
public String fileName;
public String fileCreationDate;

public int absTempMin;
public int absTempMax;
public int absPrecMin;
public int absPrecMax;
}

@ToString
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Municipality {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void postConstruct() {
mapper = new ObjectMapper();
}

public void downloadFile() throws InterruptedException, JsonMappingException, JsonProcessingException {
public ForecastDto getForecastDto() throws InterruptedException, JsonMappingException, JsonProcessingException {
logger.info("download of file: {} from S3", fileName);
GetObjectRequest objectRequest = GetObjectRequest
.builder()
Expand All @@ -72,8 +72,8 @@ public void downloadFile() throws InterruptedException, JsonMappingException, Js
String string = objectBytes.asUtf8String();
ForecastDto dto = mapper.readValue(string, ForecastDto.class);

logger.info("Successfully obtained bytes from an S3 object {}", string.length());
logger.info("upload of file: {} to S3 done", fileName);
logger.info("download of file: {} to S3 done", fileName);
return dto;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ station.stationType=${ODH_CLIENT_STATIONTYPE}

# Measurement config
data.period.3h=${ODH_CLIENT_PERIOD_3H:10800}
data.period.12h=${ODH_CLIENT_PERIOD_3H:43200}
data.period.24h=${ODH_CLIENT_PERIOD_24H:86400}

# dc-interface configuration (Writer API endpoint)
Expand Down

0 comments on commit f174f85

Please sign in to comment.