From bca338e2ee2751ae3e4d86534035945d17e8c07f Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Thu, 6 Oct 2022 16:04:47 +0400 Subject: [PATCH] #59 -- send time series to remove service --- build.gradle | 3 ++ .../config/WebClientConfiguration.java | 13 +++++++ .../ru/ulstu/extractor/http/HttpService.java | 36 +++++++++++++++++++ .../ulstu/extractor/http/JsonTimeSeries.java | 35 ++++++++++++++++++ .../extractor/http/JsonTimeSeriesValue.java | 35 ++++++++++++++++++ .../extractor/model/TimeSeriesValue.java | 10 +++--- .../extractor/service/TimeSeriesService.java | 24 +++++++++++-- .../java/ru/ulstu/extractor/ts/CommitsTS.java | 2 +- .../extractor/ts/TimeSeriesDateMapper.java | 4 +-- .../db/changelog-20220621_120000-schema.xml | 9 +++++ 10 files changed, 161 insertions(+), 10 deletions(-) create mode 100644 src/main/java/ru/ulstu/extractor/config/WebClientConfiguration.java create mode 100644 src/main/java/ru/ulstu/extractor/http/HttpService.java create mode 100644 src/main/java/ru/ulstu/extractor/http/JsonTimeSeries.java create mode 100644 src/main/java/ru/ulstu/extractor/http/JsonTimeSeriesValue.java diff --git a/build.gradle b/build.gradle index 29c6ed9..a94560f 100644 --- a/build.gradle +++ b/build.gradle @@ -48,6 +48,9 @@ dependencies { implementation group: 'org.springframework.boot', name: 'spring-boot-starter-jetty' implementation group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf' implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa' + implementation group: 'org.springframework.boot', name: 'spring-boot-starter-webflux' + implementation group: 'org.json', name: 'json', version: '20220320' + implementation group: 'nz.net.ultraq.thymeleaf', name: 'thymeleaf-layout-dialect' implementation group: 'com.fasterxml.jackson.module', name: 'jackson-module-afterburner' implementation group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate5' diff --git a/src/main/java/ru/ulstu/extractor/config/WebClientConfiguration.java b/src/main/java/ru/ulstu/extractor/config/WebClientConfiguration.java new file mode 100644 index 0000000..473a544 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/config/WebClientConfiguration.java @@ -0,0 +1,13 @@ +package ru.ulstu.extractor.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.reactive.function.client.WebClient; + +@Configuration +public class WebClientConfiguration { + @Bean + public WebClient webClient(WebClient.Builder webClientBuilder) { + return webClientBuilder.build(); + } +} diff --git a/src/main/java/ru/ulstu/extractor/http/HttpService.java b/src/main/java/ru/ulstu/extractor/http/HttpService.java new file mode 100644 index 0000000..9a5d660 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/http/HttpService.java @@ -0,0 +1,36 @@ +package ru.ulstu.extractor.http; + +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Service; +import org.springframework.web.reactive.function.BodyInserters; +import org.springframework.web.reactive.function.client.WebClient; + +import java.util.Optional; + +@Service +public class HttpService { + private final Logger log = LoggerFactory.getLogger(HttpService.class); + private final WebClient client; + + public HttpService(WebClient client) { + this.client = client; + } + + public JSONObject post(String url, JSONObject postData) { + log.debug("Service call: {}", url); + JSONObject response = new JSONObject(Optional.ofNullable(client + .post() + .uri(url) + .contentType(MediaType.APPLICATION_JSON) + .body(BodyInserters.fromValue(postData.toString())) + .accept(MediaType.APPLICATION_JSON) + .retrieve() + .bodyToMono(String.class) + .block()).orElse("{response:\"empty\"}")); + log.debug("Service response: {}", response); + return response; + } +} diff --git a/src/main/java/ru/ulstu/extractor/http/JsonTimeSeries.java b/src/main/java/ru/ulstu/extractor/http/JsonTimeSeries.java new file mode 100644 index 0000000..71506d1 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/http/JsonTimeSeries.java @@ -0,0 +1,35 @@ +package ru.ulstu.extractor.http; + +import ru.ulstu.extractor.model.TimeSeries; + +import java.util.List; +import java.util.stream.Collectors; + +public class JsonTimeSeries { + private String name; + private List values; + + public JsonTimeSeries(TimeSeries timeSeries) { + this.name = timeSeries.getName(); + this.values = timeSeries.getValues() + .stream() + .map(JsonTimeSeriesValue::new) + .collect(Collectors.toList()); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getValues() { + return values; + } + + public void setValues(List values) { + this.values = values; + } +} diff --git a/src/main/java/ru/ulstu/extractor/http/JsonTimeSeriesValue.java b/src/main/java/ru/ulstu/extractor/http/JsonTimeSeriesValue.java new file mode 100644 index 0000000..f615f7d --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/http/JsonTimeSeriesValue.java @@ -0,0 +1,35 @@ +package ru.ulstu.extractor.http; + +import ru.ulstu.extractor.model.TimeSeriesValue; + +import java.time.LocalDateTime; +import java.time.ZoneId; + +public class JsonTimeSeriesValue { + private LocalDateTime date; + private Double value; + + public JsonTimeSeriesValue(TimeSeriesValue timeSeriesValue) { + this.value = timeSeriesValue.getValue(); + this.date = timeSeriesValue.getDate() + .toInstant() + .atZone(ZoneId.systemDefault()) + .toLocalDateTime(); + } + + public LocalDateTime getDate() { + return date; + } + + public void setDate(LocalDateTime date) { + this.date = date; + } + + public Double getValue() { + return value; + } + + public void setValue(Double value) { + this.value = value; + } +} diff --git a/src/main/java/ru/ulstu/extractor/model/TimeSeriesValue.java b/src/main/java/ru/ulstu/extractor/model/TimeSeriesValue.java index 673f156..06fc168 100644 --- a/src/main/java/ru/ulstu/extractor/model/TimeSeriesValue.java +++ b/src/main/java/ru/ulstu/extractor/model/TimeSeriesValue.java @@ -6,17 +6,17 @@ import java.util.Date; @Entity public class TimeSeriesValue extends BaseEntity { private Date date; - private Integer value; + private Double value; public TimeSeriesValue() { } - public TimeSeriesValue(Date date, Integer value) { + public TimeSeriesValue(Date date, Double value) { this.date = date; this.value = value; } - public TimeSeriesValue(TimeSeries timeSeries, Date date, Integer value) { + public TimeSeriesValue(TimeSeries timeSeries, Date date, Double value) { this.date = date; this.value = value; } @@ -25,7 +25,7 @@ public class TimeSeriesValue extends BaseEntity { return date; } - public Integer getValue() { + public Double getValue() { return value; } @@ -33,7 +33,7 @@ public class TimeSeriesValue extends BaseEntity { this.date = date; } - public void setValue(Integer value) { + public void setValue(Double value) { this.value = value; } } diff --git a/src/main/java/ru/ulstu/extractor/service/TimeSeriesService.java b/src/main/java/ru/ulstu/extractor/service/TimeSeriesService.java index 9707172..706b52f 100644 --- a/src/main/java/ru/ulstu/extractor/service/TimeSeriesService.java +++ b/src/main/java/ru/ulstu/extractor/service/TimeSeriesService.java @@ -5,9 +5,12 @@ package ru.ulstu.extractor.service; +import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; +import ru.ulstu.extractor.http.HttpService; +import ru.ulstu.extractor.http.JsonTimeSeries; import ru.ulstu.extractor.model.TimeSeries; import ru.ulstu.extractor.model.TimeSeriesValue; import ru.ulstu.extractor.repository.TimeSeriesRepository; @@ -28,10 +31,15 @@ public class TimeSeriesService { private final TimeSeriesRepository timeSeriesRepository; private final TimeSeriesValueRepository timeSeriesValueRepository; private final TimeSeriesDateMapper.TimeSeriesInterval timeSeriesInterval = TimeSeriesDateMapper.TimeSeriesInterval.HOUR; + private final HttpService httpService; + private final static String TIME_SERIES_SERVICE_URL = "http://time-series.athene.tech/api/1.0/add-time-series?setKey=git-extractor"; - public TimeSeriesService(TimeSeriesRepository timeSeriesRepository, TimeSeriesValueRepository timeSeriesValueRepository) { + public TimeSeriesService(TimeSeriesRepository timeSeriesRepository, + TimeSeriesValueRepository timeSeriesValueRepository, + HttpService httpService) { this.timeSeriesRepository = timeSeriesRepository; this.timeSeriesValueRepository = timeSeriesValueRepository; + this.httpService = httpService; } /** @@ -59,6 +67,7 @@ public class TimeSeriesService { TimeSeries savedTimeSeries = timeSeriesRepository.save(timeSeries); LOG.debug("Clear {} time series values ", timeSeriesValuesToRemove.size()); timeSeriesValueRepository.deleteAll(timeSeriesValuesToRemove); + sendToTimeSeriesService(savedTimeSeries); return savedTimeSeries; } @@ -80,7 +89,7 @@ public class TimeSeriesService { }).collect(Collectors.toList()); } - public void addTimeSeriesValue(String timeSeriesName, Date date, Integer value) { + public void addTimeSeriesValue(String timeSeriesName, Date date, Double value) { LOG.debug("Start add time series values to {} time series values ", timeSeriesName); TimeSeries timeSeries = findOrCreate(timeSeriesName); timeSeriesValueRepository.save(new TimeSeriesValue(timeSeries, date, value)); @@ -93,4 +102,15 @@ public class TimeSeriesService { public TimeSeriesDateMapper.TimeSeriesInterval getTimeSeriesInterval() { return timeSeriesInterval; } + + private void sendToTimeSeriesService(TimeSeries timeSeries) { + new Thread(() -> { + try { + httpService.post(TIME_SERIES_SERVICE_URL, new JSONObject(new JsonTimeSeries(timeSeries))); + LOG.debug("Успешно отправлен на сервис"); + } catch (Exception ex) { + LOG.debug(ex.getMessage()); + } + }).start(); + } } diff --git a/src/main/java/ru/ulstu/extractor/ts/CommitsTS.java b/src/main/java/ru/ulstu/extractor/ts/CommitsTS.java index 47f447a..3067117 100644 --- a/src/main/java/ru/ulstu/extractor/ts/CommitsTS.java +++ b/src/main/java/ru/ulstu/extractor/ts/CommitsTS.java @@ -33,7 +33,7 @@ public class CommitsTS extends AbstractTimeSeriesCreator { result.put(String.format("%s %s %s", getTimeSeriesName(), repositoryId, branchName), commitService.findByRepositoryIdAndName(repositoryId, branchName) .stream() - .map(c -> new TimeSeriesValue(c.getDate(), 1)) + .map(c -> new TimeSeriesValue(c.getDate(), 1.0)) .collect(Collectors.toList())); return result; } diff --git a/src/main/java/ru/ulstu/extractor/ts/TimeSeriesDateMapper.java b/src/main/java/ru/ulstu/extractor/ts/TimeSeriesDateMapper.java index 19298a0..dec6918 100644 --- a/src/main/java/ru/ulstu/extractor/ts/TimeSeriesDateMapper.java +++ b/src/main/java/ru/ulstu/extractor/ts/TimeSeriesDateMapper.java @@ -23,10 +23,10 @@ public class TimeSeriesDateMapper { .map(timeSeriesValue -> new TimeSeriesValue(trimTo(timeSeriesInterval, timeSeriesValue.getDate()), timeSeriesValue.getValue())) .collect(Collectors.toList()); - Map groupedTimeSeriesValues = trimmedTimeSeriesValues + Map groupedTimeSeriesValues = trimmedTimeSeriesValues .stream() .collect(Collectors.groupingBy(TimeSeriesValue::getDate, - Collectors.summingInt(TimeSeriesValue::getValue))); + Collectors.summingDouble(TimeSeriesValue::getValue))); return groupedTimeSeriesValues.entrySet() .stream() diff --git a/src/main/resources/db/changelog-20220621_120000-schema.xml b/src/main/resources/db/changelog-20220621_120000-schema.xml index aaccfc8..e501fe2 100644 --- a/src/main/resources/db/changelog-20220621_120000-schema.xml +++ b/src/main/resources/db/changelog-20220621_120000-schema.xml @@ -11,4 +11,13 @@ + + + + + + + + + \ No newline at end of file