Commit 8217417d authored by Noel Alonso's avatar Noel Alonso
Browse files

Añade beans para el manejo de series temporales

Refactoriza y crea una base común para series temporales
Transforma mappers de orika a mapstruct
Añade controladores
parent 9fead2b1
Loading
Loading
Loading
Loading
+88 −0
Original line number Diff line number Diff line
package es.redmic.timeseriesview.common.controller;

import javax.validation.Valid;

import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import es.redmic.exception.custom.ResourceNotFoundException;
import es.redmic.exception.databinding.DTONotValidException;
import es.redmic.models.es.common.dto.ElasticSearchDTO;
import es.redmic.models.es.common.dto.JSONCollectionDTO;
import es.redmic.models.es.common.dto.SuperDTO;
import es.redmic.models.es.common.query.dto.DataQueryDTO;
import es.redmic.timeserieslib.dto.series.SeriesBaseDTO;
import es.redmic.timeseriesview.common.service.RSeriesESService;
import es.redmic.timeseriesview.model.common.SeriesCommon;
import es.redmic.viewlib.common.controller.RController;

/*-
 * #%L
 * Time series view
 * %%
 * Copyright (C) 2019 REDMIC Project / Server
 * %%
 * 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.
 * #L%
 */

public abstract class RSeriesController<TModel extends SeriesCommon, TDTO extends SeriesBaseDTO, TQueryDTO extends DataQueryDTO>
	extends RController<TModel, TDTO, TQueryDTO> {

	private RSeriesESService<TModel, TDTO, TQueryDTO> serviceES;

	protected RSeriesController(RSeriesESService<TModel, TDTO, TQueryDTO> service) {
		super(service);
		this.serviceES = service;
	}

	@GetMapping(value = "")
	@ResponseBody
	public SuperDTO search(@RequestParam(required = false, value = "from") Integer from,
			@RequestParam(required = false, value = "size") Integer size) {

		DataQueryDTO queryDTO = serviceES.createSimpleQueryDTOFromQueryParams(from, size);
		return new ElasticSearchDTO(serviceES.find(queryDTO));
	}

	@PostMapping(value = "/_search")
	@ResponseBody
	public SuperDTO advancedSearch(@Valid @RequestBody TQueryDTO queryDTO, BindingResult bindingResult) {

		if (bindingResult != null && bindingResult.hasErrors())
			throw new DTONotValidException(bindingResult);

		JSONCollectionDTO result = serviceES.find(queryDTO);
		return new ElasticSearchDTO(result, result.getTotal());
	}

	@GetMapping(value = "/{id}")
	@ResponseBody
	public SuperDTO get(@PathVariable("id") String id) {

		TDTO result = serviceES.searchById(id);
		return new ElasticSearchDTO(result, result == null ? 0 : 1);
	}

	@GetMapping(value = "/_suggest")
	@ResponseBody
	public SuperDTO suggest() {

		throw new ResourceNotFoundException();
	}
}
+33 −0
Original line number Diff line number Diff line
package es.redmic.timeseriesview.common.mapper;

/*-
 * #%L
 * Time series view
 * %%
 * Copyright (C) 2019 - 2021 REDMIC Project / Server
 * %%
 * 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.
 * #L%
 */

import es.redmic.models.es.common.dto.AggregationsDTO;
import es.redmic.models.es.geojson.common.model.Aggregations;
import es.redmic.timeserieslib.dto.series.SeriesCommonDTO;
import es.redmic.timeseriesview.model.common.SeriesCommon;
import es.redmic.viewlib.common.mapper.es2dto.BaseESMapper;

public abstract class SeriesESMapper<TDTO extends SeriesCommonDTO, TModel extends SeriesCommon>
	extends BaseESMapper<TDTO, TModel> {

	public abstract AggregationsDTO map(Aggregations aggregations);
}
+109 −0
Original line number Diff line number Diff line
package es.redmic.timeseriesview.common.service;

/*-
 * #%L
 * Time series view
 * %%
 * Copyright (C) 2019 - 2021 REDMIC Project / Server
 * %%
 * 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.
 * #L%
 */

import org.mapstruct.factory.Mappers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import es.redmic.elasticsearchlib.series.repository.RSeriesESRepository;
import es.redmic.exception.common.ExceptionType;
import es.redmic.exception.common.InternalException;
import es.redmic.models.es.common.dto.JSONCollectionDTO;
import es.redmic.models.es.common.query.dto.DataQueryDTO;
import es.redmic.models.es.common.query.dto.MgetDTO;
import es.redmic.models.es.series.common.model.SeriesHitWrapper;
import es.redmic.models.es.series.common.model.SeriesHitsWrapper;
import es.redmic.models.es.series.common.model.SeriesSearchWrapper;
import es.redmic.timeserieslib.dto.series.SeriesBaseDTO;
import es.redmic.timeseriesview.mapper.TimeSeriesESMapper;
import es.redmic.timeseriesview.model.common.SeriesCommon;
import es.redmic.viewlib.common.service.IBaseService;
import es.redmic.viewlib.common.service.RBaseService;

public abstract class RSeriesESService<TModel extends SeriesCommon, TDTO extends SeriesBaseDTO, TQueryDTO extends DataQueryDTO>
	extends RBaseService<TModel, TDTO, TQueryDTO> implements IBaseService<TModel, TDTO, TQueryDTO> {

	private RSeriesESRepository<TModel, TQueryDTO> repository;

	protected final Logger LOGGER = LoggerFactory.getLogger(RSeriesESService.class);

	protected RSeriesESService() {
		super();
	}

	protected RSeriesESService(RSeriesESRepository<TModel, TQueryDTO> repository) {
		super();
		this.repository = repository;
	}

	public TDTO get(String id) {

		return viewResultToDTO(repository.findById(id));
	}

	@SuppressWarnings("unchecked")
	public TModel findById(String id) {

		SeriesHitWrapper<?> hitWrapper = repository.findById(id);
		return (TModel) hitWrapper.get_source();
	}

	@SuppressWarnings("unchecked")
	public TDTO searchById(String id) {

		SeriesSearchWrapper<?> hitsWrapper = repository.searchByIds(new String[] { id });
		if (hitsWrapper.getTotal() == 1)
			return viewResultToDTO((TModel) hitsWrapper.getSource(0));
		else if (hitsWrapper.getTotal() > 1) {
			LOGGER.debug("Existe más de un resultado para el mismo id");
			throw new InternalException(ExceptionType.INTERNAL_EXCEPTION);
		}
		return null;
	}

	public JSONCollectionDTO find(DataQueryDTO query) {

		SeriesSearchWrapper<TModel> result = repository.find(query);

		JSONCollectionDTO collection = viewResultToDTO(result.getHits());
		collection.set_aggs(Mappers.getMapper(TimeSeriesESMapper.class).map(result.getAggregations()));
		return collection;
	}

	public JSONCollectionDTO mget(MgetDTO dto) {

		return viewResultToDTO(repository.mget(dto));
	}

	public DataQueryDTO createSimpleQueryDTOFromQueryParams(Integer from, Integer size) {
		return repository.createSimpleQueryDTOFromQueryParams(from, size);
	}

	protected abstract TDTO viewResultToDTO(SeriesHitWrapper<TModel> viewResult);

	protected abstract TDTO viewResultToDTO(TModel model);

	protected abstract JSONCollectionDTO viewResultToDTO(SeriesSearchWrapper<TModel> viewResult);

	protected abstract JSONCollectionDTO viewResultToDTO(SeriesHitsWrapper<TModel> viewResult);

}
+45 −0
Original line number Diff line number Diff line
package es.redmic.timeseriesview.common.utils;

/*-
 * #%L
 * Time series view
 * %%
 * Copyright (C) 2019 - 2021 REDMIC Project / Server
 * %%
 * 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.
 * #L%
 */

import java.util.ArrayList;
import java.util.List;

import es.redmic.models.es.common.query.dto.DataQueryDTO;

public abstract class DataDefinitionUtils {

	public static void addDataDefinitionFieldToReturn(DataQueryDTO query) {

		List<String> returnFields = query.getReturnFields();

		if (returnFields == null) {
			returnFields = new ArrayList<>();
			returnFields.add("date");
			returnFields.add("value");
			returnFields.add("dataDefinition");
		}
		else
			returnFields.add("dataDefinition");

		query.setReturnFields(returnFields);
	}
}
+43 −10
Original line number Diff line number Diff line
@@ -20,34 +20,67 @@ package es.redmic.timeseriesview.controller;
 * #L%
 */

import javax.annotation.PostConstruct;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import es.redmic.elasticsearchlib.common.query.SeriesQueryUtils;
import es.redmic.exception.databinding.DTONotValidException;
import es.redmic.models.es.common.DataPrefixType;
import es.redmic.models.es.common.dto.SuperDTO;
import es.redmic.models.es.common.query.dto.DataQueryDTO;
import es.redmic.timeseriesview.service.WindRoseESService;
import es.redmic.timeserieslib.dto.series.TimeSeriesDTO;
import es.redmic.models.es.common.query.dto.AggsPropertiesDTO;
import es.redmic.timeseriesview.common.controller.RSeriesController;
import es.redmic.timeseriesview.model.timeseries.TimeSeries;
import es.redmic.timeseriesview.service.TimeSeriesESService;

@RestController
@RequestMapping(value = "${controller.mapping.TIMESERIES}")
public class WindRoseController {
public class TimeSeriesController extends RSeriesController<TimeSeries, TimeSeriesDTO, DataQueryDTO>{

	WindRoseESService service;
	private TimeSeriesESService serviceES;

	@Autowired
	public WindRoseController(WindRoseESService service) {
		this.service = service;
	public TimeSeriesController(TimeSeriesESService service) {
		super(service);
		this.serviceES = service;
	}

	@RequestMapping(value = "${controller.mapping.SERIES_WINDROSE}/_search", method = RequestMethod.POST)
	@PostConstruct
	private void postConstruct() {
		setFieldsExcludedOnQuery(SeriesQueryUtils.getFieldsExcludedOnQuery());
	}

	@PostMapping(value = "${controller.mapping.SERIES_TEMPORALDATA}/_search")
	@ResponseBody
	public SuperDTO findTemporalData(@Valid @RequestBody DataQueryDTO queryDTO, BindingResult bindingResult) {

		if (bindingResult != null && bindingResult.hasErrors())
			throw new DTONotValidException(bindingResult);

		if (queryDTO.getInterval() != null) {
			AggsPropertiesDTO agg = new AggsPropertiesDTO();
			agg.setField("temporaldata");

			queryDTO.addAgg(agg);
			queryDTO.setSize(0);
		}

		queryDTO.setDataType(DataPrefixType.getPrefixTypeFromClass(TimeSeriesDTO.class));

		return serviceES.findTemporalDataStatistics(queryDTO);
	}

	@PostMapping(value = "${controller.mapping.SERIES_WINDROSE}/_search")
	@ResponseBody
	public SuperDTO getRosewindData(@PathVariable(name = "activityId", required = false) String activityId,
			@Valid @RequestBody DataQueryDTO queryDTO, BindingResult bindingResult) {
@@ -55,6 +88,6 @@ public class WindRoseController {
		if (bindingResult != null && bindingResult.hasErrors())
			throw new DTONotValidException(bindingResult);

		return service.getWindRoseData(queryDTO, activityId);
		return serviceES.getWindRoseData(queryDTO, activityId);
	}
}
Loading