From 7ee77cd1269294621ea67c250a3ec45ce367ab46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Artur=20Ko=C5=82ecki?= Date: Wed, 8 Mar 2023 12:03:46 +0100 Subject: [PATCH] Changed process time to backend. --- .../r11/tools/controller/JsonController.java | 50 +++++++++++++------ Frontend/assets/scripts/tools/json.js | 26 +++++----- 2 files changed, 46 insertions(+), 30 deletions(-) diff --git a/Backend/tools-services/src/main/java/com/r11/tools/controller/JsonController.java b/Backend/tools-services/src/main/java/com/r11/tools/controller/JsonController.java index 581c5d8..1d465fd 100644 --- a/Backend/tools-services/src/main/java/com/r11/tools/controller/JsonController.java +++ b/Backend/tools-services/src/main/java/com/r11/tools/controller/JsonController.java @@ -24,35 +24,53 @@ public class JsonController implements RestController { @ScopedControllerManifest(method = HandlerType.POST, path = "/formatting") public void formatting(Request request, Response response) { + long startProcess = System.currentTimeMillis(); + JsonObject responseJson = new JsonObject(); + try { - JsonObject jsonObject = this.gson.fromJson(request.body(), JsonObject.class); + JsonObject requestJson = this.gson.fromJson(request.body(), JsonObject.class); + response.status(200); - response.body(this.prettyGson.toJson(jsonObject)); + + responseJson.addProperty("data", this.prettyGson.toJson(requestJson)); + responseJson.addProperty("time", System.currentTimeMillis() - startProcess); + + response.body(this.prettyGson.toJson(responseJson)); } catch (Exception e) { - response.status(500); Throwable cause = e.getCause(); - if (cause == null) { - response.body(e.getMessage()); - } else { - response.body(cause.getMessage()); - } + + response.status(500); + + responseJson.addProperty("data", cause == null ? e.getMessage() : cause.getMessage()); + responseJson.addProperty("time", System.currentTimeMillis() - startProcess); + + response.body(this.prettyGson.toJson(responseJson)); } } @ScopedControllerManifest(method = HandlerType.POST, path = "/minimize") public void minimize(Request request, Response response) { + long startProcess = System.currentTimeMillis(); + JsonObject responseJson = new JsonObject(); + try { - JsonObject jsonObject = this.prettyGson.fromJson(request.body(), JsonObject.class); + JsonObject requestJson = this.prettyGson.fromJson(request.body(), JsonObject.class); + response.status(200); - response.body(this.gson.toJson(jsonObject)); + + responseJson.addProperty("data", this.gson.toJson(requestJson)); + responseJson.addProperty("time", System.currentTimeMillis() - startProcess); + + response.body(this.gson.toJson(responseJson)); } catch (Exception e) { - response.status(500); Throwable cause = e.getCause(); - if (cause == null) { - response.body(e.getMessage()); - } else { - response.body(cause.getMessage()); - } + + response.status(500); + + responseJson.addProperty("data", cause == null ? e.getMessage() : cause.getMessage()); + responseJson.addProperty("time", System.currentTimeMillis() - startProcess); + + response.body(this.prettyGson.toJson(responseJson)); } } } diff --git a/Frontend/assets/scripts/tools/json.js b/Frontend/assets/scripts/tools/json.js index 00d68ae..1f5f09c 100644 --- a/Frontend/assets/scripts/tools/json.js +++ b/Frontend/assets/scripts/tools/json.js @@ -1,7 +1,6 @@ function formatAndValidateJson(errorElement) { const input = document.querySelector('#jsonBlock'); const processInfo = document.getElementById(errorElement); - const start = new Date(); const address = window.location.protocol + "//" + window.location.hostname + ":" + 8081 + "/json/formatting" @@ -10,22 +9,22 @@ function formatAndValidateJson(errorElement) { body: input.textContent }) .then(async (response) => { + const promise = response.json(); if (!response.ok) { - throw Error(await response.text()); + throw Error(await promise); } - return response.text(); + return promise; }) .then((data) => { - input.innerText = data; + input.innerText = data.data; processInfo.innerText = ""; hljs.highlightElement(input); - const end = new Date(); - processInfo.innerHTML = "Computed in " + (end.getMilliseconds() - start.getMilliseconds()) + "ms"; + processInfo.innerHTML = "Computed in " + data.time + "ms"; }) .catch((error) => { - processInfo.innerHTML = "" + error + ""; + processInfo.innerHTML = "" + error.data + ""; console.error('Error:', error); }); } @@ -34,7 +33,6 @@ function minimizeJson(errorElement) { const input = document.querySelector('#jsonBlock'); const processInfo = document.getElementById(errorElement); - const start = new Date(); const address = window.location.protocol + "//" + window.location.hostname + ":" + 8081 + "/json/minimize" fetch(address, { @@ -42,22 +40,22 @@ function minimizeJson(errorElement) { body: input.textContent }) .then(async (response) => { + const promise = response.json(); if (!response.ok) { - throw Error(await response.text()); + throw Error(await promise); } - return response.text(); + return promise; }) .then((data) => { - input.innerText = data; + input.innerText = data.data; processInfo.innerText = ""; hljs.highlightElement(input); - const end = new Date(); - processInfo.innerHTML = "Computed in " + (end.getMilliseconds() - start.getMilliseconds()) + "ms"; + processInfo.innerHTML = "Computed in " + data.time + "ms"; }) .catch((error) => { - processInfo.innerHTML = "" + error + ""; + processInfo.innerHTML = "" + error.data + ""; console.error('Error:', error); }); }