From 134e2d772eac1cbb03263365f21ff7815101c9f3 Mon Sep 17 00:00:00 2001 From: siarhei_hrabko Date: Thu, 27 Jun 2024 11:49:03 +0300 Subject: [PATCH] EPMRPP-88736 implement send analytics job --- .../statistics/ManualLaunchStatisticsJob.java | 84 ++++++++++--------- 1 file changed, 43 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/epam/reportportal/jobs/statistics/ManualLaunchStatisticsJob.java b/src/main/java/com/epam/reportportal/jobs/statistics/ManualLaunchStatisticsJob.java index dfeab27..89cde53 100644 --- a/src/main/java/com/epam/reportportal/jobs/statistics/ManualLaunchStatisticsJob.java +++ b/src/main/java/com/epam/reportportal/jobs/statistics/ManualLaunchStatisticsJob.java @@ -26,7 +26,6 @@ import java.util.Set; import net.javacrumbs.shedlock.spring.annotation.SchedulerLock; import org.apache.commons.lang3.RandomUtils; -import org.apache.commons.lang3.StringUtils; import org.json.JSONArray; import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; @@ -98,9 +97,9 @@ public void execute() { MapSqlParameterSource queryParams = new MapSqlParameterSource(); queryParams.addValue(DATE_BEFORE, dateBefore); - JSONObject requestBody = new JSONObject(); - namedParameterJdbcTemplate.query(SELECT_ANALYZER_MANUAL_START_QUERY, queryParams, rs -> { + JSONObject requestBody = new JSONObject(); + int autoAnalyzed = 0; int userAnalyzed = 0; String version = null; @@ -130,55 +129,58 @@ public void execute() { } while (rs.next()); - if (autoAnalyzed + userAnalyzed > 0) { - var instanceID = jdbcTemplate.queryForObject(SELECT_INSTANCE_ID_QUERY, String.class); - var params = new JSONObject(); - params.put("category", "analyzer"); - params.put("instanceID", instanceID); - params.put("timestamp", now.toEpochMilli()); - params.put("version", version); // get from table - params.put("type", analyzerEnabled ? "is_analyzer" : "not_analyzer"); - if (analyzerEnabled) { - params.put("number", autoAnalyzed + "#" + userAnalyzed); - params.put("auto_analysis", String.join("#", autoAnalysisState)); - params.put("status", String.join("#", status)); - } - - var event = new JSONObject(); - event.put("name", "analyze_analyzer"); - event.put("params", params); + var instanceID = jdbcTemplate.queryForObject(SELECT_INSTANCE_ID_QUERY, String.class); + var params = new JSONObject(); + params.put("category", "analyzer"); + params.put("instanceID", instanceID); + params.put("timestamp", now.toEpochMilli()); + params.put("version", version); // get from table + params.put("type", analyzerEnabled ? "is_analyzer" : "not_analyzer"); + if (analyzerEnabled) { + params.put("number", autoAnalyzed + "#" + userAnalyzed); + params.put("auto_analysis", String.join("#", autoAnalysisState)); + params.put("status", String.join("#", status)); + } - JSONArray events = new JSONArray(); - events.put(event); + var event = new JSONObject(); + event.put("name", "analyze_analyzer"); + event.put("params", params); - requestBody.put("client_id", - now.toEpochMilli() + "." + RandomUtils.nextInt(100_000, 999_999)); - requestBody.put("events", events); + JSONArray events = new JSONArray(); + events.put(event); - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Sending statistics data with measurementId: {} and body: {}", measurementId, - requestBody); - } + requestBody.put("client_id", + now.toEpochMilli() + "." + RandomUtils.nextInt(100_000, 999_999)); + requestBody.put("events", events); - try { - var response = restTemplate.exchange(GA_URL, HttpMethod.POST, - new HttpEntity<>(requestBody), Object.class, getGa4UrlParameters()); - if (response.getStatusCodeValue() != 204) { - LOGGER.error("Failed to send statistics: {}", response); - } - response.getStatusCode(); - } catch (Exception e) { - LOGGER.error("Failed to send statistics", e); - } finally { - jdbcTemplate.execute(DELETE_ANALYZER_MANUAL_START_QUERY); - } + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Sending statistics data with measurementId: {} and body: {}", measurementId, + requestBody); } + + sendRequest(requestBody); + }); LOGGER.info("Completed analyzer manual start item statistics job"); } + private void sendRequest(JSONObject requestBody) { + try { + var response = restTemplate.exchange(GA_URL, HttpMethod.POST, new HttpEntity<>(requestBody), + Object.class, getGa4UrlParameters()); + if (response.getStatusCodeValue() != 204) { + LOGGER.error("Failed to send statistics: {}", response); + } + response.getStatusCode(); + } catch (Exception e) { + LOGGER.error("Failed to send statistics", e); + } finally { + jdbcTemplate.execute(DELETE_ANALYZER_MANUAL_START_QUERY); + } + } + private Map getGa4UrlParameters() { Map map = new HashMap<>(); map.put("measurementId", measurementId);