From b127e430159495b91bf62ef5d26df70c4f8020f7 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Thu, 14 Oct 2021 10:15:10 +0400 Subject: [PATCH] add logging --- src/main/java/ru/ulstu/HttpUtils.java | 19 +++++++++++++++++ .../controller/TimeSeriesController.java | 21 +++++++++++++++---- .../ru/ulstu/datamodel/ForecastParams.java | 8 +++++++ 3 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 src/main/java/ru/ulstu/HttpUtils.java diff --git a/src/main/java/ru/ulstu/HttpUtils.java b/src/main/java/ru/ulstu/HttpUtils.java new file mode 100644 index 0000000..2bc73f3 --- /dev/null +++ b/src/main/java/ru/ulstu/HttpUtils.java @@ -0,0 +1,19 @@ +/* + * Copyright (C) 2021 Anton Romanov - All Rights Reserved + * You may use, distribute and modify this code, please write to: romanov73@gmail.com. + * + */ + +package ru.ulstu; + +import javax.servlet.http.HttpServletRequest; + +public class HttpUtils { + public static String getUserIp(HttpServletRequest request) { + String xfHeader = request.getHeader("X-Forwarded-For"); + if (xfHeader == null) { + return request.getRemoteAddr(); + } + return xfHeader.split(",")[0]; + } +} diff --git a/src/main/java/ru/ulstu/controller/TimeSeriesController.java b/src/main/java/ru/ulstu/controller/TimeSeriesController.java index 6667b64..ac7fba5 100644 --- a/src/main/java/ru/ulstu/controller/TimeSeriesController.java +++ b/src/main/java/ru/ulstu/controller/TimeSeriesController.java @@ -7,12 +7,15 @@ package ru.ulstu.controller; import io.swagger.annotations.ApiOperation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; 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.RestController; +import ru.ulstu.HttpUtils; import ru.ulstu.configuration.ApiConfiguration; import ru.ulstu.datamodel.ForecastParams; import ru.ulstu.datamodel.ModelingResult; @@ -21,12 +24,14 @@ import ru.ulstu.datamodel.ts.TimeSeries; import ru.ulstu.service.MethodParamBruteForce; import ru.ulstu.service.TimeSeriesService; +import javax.servlet.http.HttpServletRequest; import java.lang.reflect.InvocationTargetException; import java.util.concurrent.ExecutionException; @RestController @RequestMapping(ApiConfiguration.API_1_0) public class TimeSeriesController { + private final static Logger LOGGER = LoggerFactory.getLogger(TimeSeriesController.class); private final TimeSeriesService timeSeriesService; private final MethodParamBruteForce methodParamBruteForce; @@ -39,14 +44,22 @@ public class TimeSeriesController { @PostMapping("getForecast") @ApiOperation("Получить прогноз временного ряда") - public ResponseEntity getForecastTimeSeries(@RequestBody ForecastParams forecastParams) throws ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, ModelingException { - return new ResponseEntity<>(timeSeriesService.getForecast(forecastParams.getOriginalTimeSeries(), + public ResponseEntity getForecastTimeSeries(@RequestBody ForecastParams forecastParams, HttpServletRequest request) throws ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, ModelingException { + LOGGER.info("User ip: " + HttpUtils.getUserIp(request)); + LOGGER.info("Forecast: " + forecastParams); + ResponseEntity result = new ResponseEntity<>(timeSeriesService.getForecast(forecastParams.getOriginalTimeSeries(), forecastParams.getCountForecast()), HttpStatus.OK); + LOGGER.info("Forecast result complete"); + return result; } @PostMapping("getSmoothed") @ApiOperation("Получить сглаженный временной ряд") - public ResponseEntity getSmoothedTimeSeries(@RequestBody TimeSeries timeSeries) throws ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { - return new ResponseEntity<>(methodParamBruteForce.getSmoothedTimeSeries(timeSeries), HttpStatus.OK); + public ResponseEntity getSmoothedTimeSeries(@RequestBody TimeSeries timeSeries, HttpServletRequest request) throws ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { + LOGGER.info("User ip: " + HttpUtils.getUserIp(request)); + LOGGER.info("Time series for smoothing: " + timeSeries); + ResponseEntity result = new ResponseEntity<>(methodParamBruteForce.getSmoothedTimeSeries(timeSeries), HttpStatus.OK); + LOGGER.info("Smoothing complete"); + return result; } } diff --git a/src/main/java/ru/ulstu/datamodel/ForecastParams.java b/src/main/java/ru/ulstu/datamodel/ForecastParams.java index d042cca..e9b080c 100644 --- a/src/main/java/ru/ulstu/datamodel/ForecastParams.java +++ b/src/main/java/ru/ulstu/datamodel/ForecastParams.java @@ -27,4 +27,12 @@ public class ForecastParams { public void setCountForecast(int countForecast) { this.countForecast = countForecast; } + + @Override + public String toString() { + return "ForecastParams{" + + "originalTimeSeries=" + originalTimeSeries + + ", countForecast=" + countForecast + + '}'; + } }