old some edit

This commit is contained in:
Антон Скалкин 2023-10-10 11:15:10 +04:00
parent d1b57b2f76
commit 3f23d32c87
6 changed files with 87 additions and 25 deletions

View File

@ -10,6 +10,7 @@ import ru.ulstu.datamodel.ts.TimeSeries;
import ru.ulstu.db.DbService;
import ru.ulstu.db.model.TimeSeriesSet;
import ru.ulstu.estimate.CompressionMetricService;
import ru.ulstu.service.PrometheusService;
import ru.ulstu.service.UtilService;
import ru.ulstu.target.AnomalyDifferenceSmoothed;
import ru.ulstu.target.Target;
@ -24,21 +25,39 @@ public class CompressionMetricController {
private final UtilService utilService;
private final DbService dbService;
private final PrometheusService prometheusService;
private final CompressionMetricService compressionMetricService;
private final Target target;
public CompressionMetricController(UtilService utilService, DbService dbService, CompressionMetricService compressionMetricService, AnomalyDifferenceSmoothed anomalyDifferenceSmoothed) {
public CompressionMetricController(UtilService utilService, DbService dbService, PrometheusService prometheusService, CompressionMetricService compressionMetricService, AnomalyDifferenceSmoothed anomalyDifferenceSmoothed) {
this.utilService = utilService;
this.dbService = dbService;
this.prometheusService = prometheusService;
this.compressionMetricService = compressionMetricService;
this.target = anomalyDifferenceSmoothed;
}
@GetMapping("getMetricOfTimeSeries")
@GetMapping("getMetricAtAnomaly")
@Operation(description = "Получить метрику сжатия")
public ResponseEntity<TimeSeries> getRandomTimeSeries(@RequestParam("setKey") String setKey, @RequestParam("timeSeriesKey") String timeSeriesKey) throws ModelingException, ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, IOException {
public ResponseEntity<TimeSeries> getMetricAtAnomaly(@RequestParam("setKey") String setKey, @RequestParam("timeSeriesKey") String timeSeriesKey) throws ModelingException, ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, IOException {
var timeSeries = dbService.getTimeSeries(new TimeSeriesSet(setKey), timeSeriesKey);
var timeSeriesResult = compressionMetricService.getCompressionTimeSeries(timeSeries);
return new ResponseEntity<>(timeSeriesResult, HttpStatus.OK);
}
@GetMapping("getMetricAtAnomalyOfRandom")
@Operation(description = "Получить метрику сжатия")
public ResponseEntity<TimeSeries> getMetricAtAnomalyOfRandom(@RequestParam("lenght") int len) throws ModelingException, ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
var timeSeries = utilService.getRandomTimeSeries(len);
var timeSeriesResult = compressionMetricService.getCompressionTimeSeries(timeSeries);
return new ResponseEntity<>(timeSeriesResult, HttpStatus.OK);
}
@GetMapping("getMetricAtAnomalyOfPrometheus")
@Operation(description = "Получить метрику сжатия")
public ResponseEntity<TimeSeries> getMetricAtAnomalyOfPrometheus(@RequestParam("query") String query) throws ModelingException, ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
var timeSeries = prometheusService.executionQueryRange(query);
var timeSeriesResult = compressionMetricService.getCompressionTimeSeries(timeSeries);
return new ResponseEntity<>(timeSeriesResult, HttpStatus.OK);
}
}

View File

@ -12,6 +12,8 @@ import ru.ulstu.method.ftransform.FTransform;
import ru.ulstu.score.ScoreMethod;
import ru.ulstu.service.MethodParamBruteForce;
import ru.ulstu.service.TimeSeriesService;
import ru.ulstu.service.UtilService;
import ru.ulstu.target.AnomalyDifferenceSmoothed;
import java.lang.reflect.InvocationTargetException;
import java.time.LocalDateTime;
@ -22,31 +24,40 @@ import java.util.stream.Collectors;
@Service
public class CompressionMetricService {
private final int DEFAULT_THREAD_COUNT = 50;
private final int DEFAULT_THREAD_COUNT = 10;
private final TimeSeriesService timeSeriesService;
private final FTransform fTransform;
private final MethodParamBruteForce methodParamBruteForce;
private final UtilService utilService;
private final ScoreMethod scoreMethod;
private final AnomalyDifferenceSmoothed anomalyDifferenceSmoothed;
private final ExecutorService executors = Executors.newFixedThreadPool(DEFAULT_THREAD_COUNT);
public CompressionMetricService(TimeSeriesService timeSeriesService, FTransform fTransform, MethodParamBruteForce methodParamBruteForce, ScoreMethod scoreMethod) {
public CompressionMetricService(TimeSeriesService timeSeriesService, FTransform fTransform, MethodParamBruteForce methodParamBruteForce, UtilService utilService, ScoreMethod scoreMethod, AnomalyDifferenceSmoothed anomalyDifferenceSmoothed) {
this.timeSeriesService = timeSeriesService;
this.fTransform = fTransform;
this.methodParamBruteForce = methodParamBruteForce;
this.utilService = utilService;
this.scoreMethod = scoreMethod;
}
public void getCompressionMetric() {
}
public void getCompressionMetricForAnomaly() {
this.anomalyDifferenceSmoothed = anomalyDifferenceSmoothed;
}
public TimeSeries getCompressionTimeSeries(TimeSeries ts) throws ModelingException, ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
ModelingResult modelingResult = timeSeriesService.smoothTimeSeries(ts, "FTransform");
//ModelingResult modelingResult = timeSeriesService.smoothTimeSeries(ts, "FTransform");
ModelingResult modelingResult = getCompressionTimeSeriesWithFTransform(ts);
//var ts1 = fTransform.getForecast(ts, )
var timeSeriesAnomalyResult = anomalyDifferenceSmoothed.calculate(modelingResult.getTimeSeries());
var timeSeriesAnomaly = anomalyDifferenceSmoothed.calculate(ts);
System.out.println(ts.getLength()+"\t"
+timeSeriesAnomaly.getLength()+"\t"
+Math.abs(timeSeriesAnomaly.getLength() - timeSeriesAnomalyResult.getLength())+"\t"
+(double)modelingResult.getTimeSeries().getLength()/ts.getLength()+"\t"
+modelingResult.getScore().getDoubleValue());
System.out.println(timeSeriesAnomaly);
System.out.println(timeSeriesAnomalyResult);
System.out.println(utilService.getTimeSeriesToString(ts));
System.out.println(utilService.getTimeSeriesToString(modelingResult.getTimeSeries()));
return modelingResult.getTimeSeries();
}

View File

@ -0,0 +1,11 @@
package ru.ulstu.http;
import org.springframework.stereotype.Service;
@Service
public class HttpClientService {
public void get(){
}
}

View File

@ -13,11 +13,11 @@ import java.time.Duration;
import java.util.Optional;
@Service
public class HttpService {
private final Logger log = LoggerFactory.getLogger(HttpService.class);
public class WebClientService {
private final Logger log = LoggerFactory.getLogger(WebClientService.class);
private final WebClient client;
public HttpService(WebClient client) {
public WebClientService(WebClient client) {
this.client = client;
}

View File

@ -27,6 +27,6 @@ public class AnomalyCompressionScore extends ScoreMethod {
var timeSeriesAnomalyResult = anomalyDifferenceSmoothed.calculate(model);
var timeSeriesAnomaly = anomalyDifferenceSmoothed.calculate(tsValues);
// туду: добавить сравнение аномальных точек
return model.getLength()/tsValues.size() + Math.abs(timeSeriesAnomaly.getLength() - timeSeriesAnomalyResult.getLength());
return (double)model.getLength()/tsValues.size() + Math.abs(timeSeriesAnomaly.getLength() - timeSeriesAnomalyResult.getLength());
}
}

View File

@ -2,21 +2,34 @@ package ru.ulstu.service;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriComponentsBuilder;
import ru.ulstu.datamodel.ts.TimeSeries;
import ru.ulstu.http.HttpService;
import ru.ulstu.datamodel.ts.TimeSeriesValue;
import ru.ulstu.http.WebClientService;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
@Service
public class PrometheusService {
private static final String PROMETHEUS_API_URL = "http://prometheus.athene.tech/api/v1/";
private final HttpService httpService;
private final WebClientService httpService;
public PrometheusService(HttpService httpService) {
public PrometheusService(WebClientService httpService) {
this.httpService = httpService;
}
public TimeSeries executionQuery(String query){ // example: pve_cpu_usage_ratio{id="lxc/111"}[5h]
var timeSeries = new TimeSeries();
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(PROMETHEUS_API_URL + "query")
.queryParam("query",
"pve_cpu_usage_ratio{id='lxc/111'}[1h]");
var response = httpService.get(PROMETHEUS_API_URL + "query?query=" + query);
if (response.getJSONObject(0).getString("status").equals("success")){
@ -36,12 +49,20 @@ public class PrometheusService {
}
public TimeSeries executionQueryRange(String query){
int numberTimeSeries = 14;
var nowDateTime = LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS);
var timeSeries = new TimeSeries();
var response = httpService.get(PROMETHEUS_API_URL + "query_range?query=" + query +
"&start=2023-04-06T00:00:00.004Z&end=2023-04-07T00:00:00.004Z&step=15s");
if (response.getJSONObject(0).getString("status").equals("success")){
var array = response.getJSONObject(0).getJSONObject("data").getJSONArray("result");
int a = 5;
"&start=" + nowDateTime.minusHours(10).minusMinutes(100).toString()+".004Z" +
"&end=" + nowDateTime.minusHours(10).toString()+".004Z" +
"&step=15s");
var array = response.getJSONObject(0).getJSONObject("data").getJSONArray("result").getJSONObject(numberTimeSeries).getJSONArray("values").toList();
for (int i = 0; i < array.size(); i++) {
BigDecimal seconds = (BigDecimal)(((ArrayList) array.get(i)).get(0));
Double value = Double.parseDouble((String)(((ArrayList) array.get(i)).get(1)));
LocalDateTime dateTime =
LocalDateTime.ofInstant(Instant.ofEpochSecond(seconds.longValue()), ZoneId.systemDefault());
timeSeries.addValue(new TimeSeriesValue(dateTime, value));
}
return timeSeries;
}