Skip to content

Commit

Permalink
EPMRPP-88736 implement send analytics job
Browse files Browse the repository at this point in the history
  • Loading branch information
grabsefx committed Jun 27, 2024
1 parent 46f5f91 commit 134e2d7
Showing 1 changed file with 43 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String> getGa4UrlParameters() {
Map<String, String> map = new HashMap<>();
map.put("measurementId", measurementId);
Expand Down

0 comments on commit 134e2d7

Please sign in to comment.