dashboard

This commit is contained in:
string 2025-11-01 14:09:33 +05:30
parent 7eb045a95a
commit 63a0f69100
9 changed files with 1508 additions and 616 deletions

View File

@ -38,6 +38,9 @@ public class Dashbord1_Line extends dashbord_Who_collumn {
@Column(length = 5000)
private String Model;
@Column(length = 5000)
private String common_filter;
@JsonBackReference
@ManyToOne
private Dashbord_Header dashbord_Header;

View File

@ -23,44 +23,35 @@ public class HeaderService {
return headerRepository.save(dashbord_Header);
}
public List<Dashbord_Header> getdetails() {
// TODO Auto-generated method stub
return (List<Dashbord_Header>) headerRepository.findAll();
}
public Dashbord_Header getdetailsbyId(int id) {
// TODO Auto-generated method stub
return headerRepository.findById(id);
}
public void delete_by_id(int id) {
// TODO Auto-generated method stub
headerRepository.deleteById(id);
headerRepository.deleteById(id);
}
public Dashbord_Header update_dashboard_header(Dashbord_Header dashbord_Header) {
return headerRepository.save(dashbord_Header);
}
public Dashbord1_Line update_Dashbord1_Line(Dashbord1_Line dashbord1_Line) {
// TODO Auto-generated method stub
return dashboard_lineRepository.save(dashbord1_Line);
}
public List<Dashbord_Header> get_by_module_id(int module_id) {
// TODO Auto-generated method stub
return (List<Dashbord_Header>) headerRepository.findbydashboardmodule(module_id);
}
public List<Dashbord1_Line> get_all_lines() {
// TODO Auto-generated method stub
return (List<Dashbord1_Line>) dashboard_lineRepository.findAll();
@ -78,22 +69,16 @@ public class HeaderService {
public Dashbord1_Line update_Dashbord1_Lineby_id(int id, Dashbord1_Line dashbord1_Line) {
Dashbord1_Line oldline= dashboard_lineRepository.findById(id);
Dashbord1_Line oldline = dashboard_lineRepository.findById(id);
// .orElseThrow(() -> new ResourceNotFoundException(Constant.NOT_FOUND_EXCEPTION + " :" + id));
oldline.setAccountId(dashbord1_Line.getAccountId());
oldline.setHeader_id(dashbord1_Line.getHeader_id());
oldline.setModel(dashbord1_Line.getModel());
final Dashbord1_Line newline= dashboard_lineRepository.save(oldline);
oldline.setCommon_filter(dashbord1_Line.getCommon_filter());
final Dashbord1_Line newline = dashboard_lineRepository.save(oldline);
return newline;
}
}

View File

@ -11,9 +11,11 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
@ -31,6 +33,8 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.realnet.SureConnect.Entities.Sure_Connect;
import com.realnet.SureConnect.Service.SureService;
@ -112,96 +116,74 @@ public class ChartBuilder {
return tableData;
}
//...........................22.07.2023.............................//
@GetMapping(value = "/getValue")
public ResponseEntity<?> getValue(@RequestParam String apiUrl, @RequestParam Integer sureId,
@RequestParam(required = false) String key) throws IOException {
// @GetMapping(value = "/getdashjson/{job_type}")
// public ResponseEntity<?> jsonretun(@RequestParam String tableName, @PathVariable String job_type,
// @RequestParam String xAxis, @RequestParam String yAxis) throws IOException {
//
// List<Map<String, Object>> tableData = getAllDataFromTable(tableName); // Retrieve all data from the table
//
// List<Object> yAxisValues = new ArrayList<>();
// List<String> xAxisValues = new ArrayList<>();
//
// for (Map<String, Object> row : tableData) {
// for (Entry<String, Object> entry : row.entrySet()) {
// String key = entry.getKey();
// Object value = entry.getValue();
//
// if (key.equalsIgnoreCase(xAxis)) {
// xAxisValues.add(value.toString());
// } else if (key.equalsIgnoreCase(yAxis)) {
// yAxisValues.add(value);
// }
// }
// }
//
// StringBuilder jsonmap = new StringBuilder();
//
// if (job_type.equalsIgnoreCase("Bar Chart")) {
// jsonmap.append("[\n");
// } else if (job_type.equalsIgnoreCase("Line Chart")) {
// jsonmap.append(" {\r\n" + " \"chartData\": [\r\n" + " { \"data\": [");
// } else if (job_type.equalsIgnoreCase("Doughnut Chart")) {
// jsonmap.append("{\"chartData\": [[");
// }
//
// for (int i = 0; i < xAxisValues.size(); i++) {
// String xValue = xAxisValues.get(i);
// Object yValue = yAxisValues.get(i);
//
// if (job_type.equalsIgnoreCase("Bar Chart")) {
// jsonmap.append("{\"name\": \"" + xValue + "\", \"progress\":\"" + yValue + "\"},\n");
// } else if (job_type.equalsIgnoreCase("Line Chart")) {
// jsonmap.append(yValue + ",");
// } else if (job_type.equalsIgnoreCase("Doughnut Chart")) {
// jsonmap.append(yValue + ",");
// }
// }
//
// if (!xAxisValues.isEmpty() && !yAxisValues.isEmpty()) {
// jsonmap.deleteCharAt(jsonmap.lastIndexOf(","));
// }
//
// if (job_type.equalsIgnoreCase("Bar Chart")) {
// jsonmap.append("]");
// } else if (job_type.equalsIgnoreCase("Line Chart")) {
// jsonmap.append("], \"label\": \"" + yAxis + "\" }\r\n" + " ],\r\n" + " \"chartLabels\": [ ");
// } else if (job_type.equalsIgnoreCase("Doughnut Chart")) {
// jsonmap.append("]],\r\n" + " \"chartLabels\": [");
// }
//
// for (String xValue : xAxisValues) {
// jsonmap.append("\"" + xValue + "\",");
// }
//
// if (!xAxisValues.isEmpty()) {
// jsonmap.deleteCharAt(jsonmap.lastIndexOf(","));
// }
//
// if (job_type.equalsIgnoreCase("Line Chart")) {
// jsonmap.append("] \n }\n");
// } else if (job_type.equalsIgnoreCase("Doughnut Chart")) {
// jsonmap.append("]\n" + "}");
// }
//
// return new ResponseEntity<>(jsonmap.toString(), HttpStatus.CREATED);
// }
System.out.println(" value get..");
// Step 1: Fetch all table data from API
List<Map<String, Object>> tableData = getAllDataFromApi(apiUrl, sureId);
// Step 2: Handle null or empty key
if (key == null || key.trim().isEmpty()) {
return ResponseEntity.badRequest().body("Key parameter is required");
}
// Step 3: Extract unique values for that key
Set<Object> uniqueValues = tableData.stream().map(row -> row.get(key)) // get value by key
.filter(Objects::nonNull) // remove nulls
.collect(Collectors.toCollection(LinkedHashSet::new)); // keep unique + order
// Step 4: Convert to List
List<Object> resultList = new ArrayList<>(uniqueValues);
// Step 5: Return as ResponseEntity
return ResponseEntity.ok(resultList);
}
@GetMapping(value = "/getdashjson/{job_type}")
public ResponseEntity<?> jsonretun2(@PathVariable String job_type, @RequestParam String tableName,
public ResponseEntity<?> jsonretun(@PathVariable String job_type, @RequestParam String tableName,
@RequestParam(required = false) String xAxis, @RequestParam(required = false) List<String> yAxes,
@RequestParam Integer sureId, @RequestParam(required = false) String parameter,
@RequestParam(required = false) String parameterValue) throws IOException {
@RequestParam(required = false) String parameterValue, @RequestParam(required = false) String filters)
throws IOException {
System.out.println(" chart data getting...");
List<Map<String, Object>> tableData = getAllDataFromApi(tableName, sureId); // Retrieve all data from the table
// Filter table data if parameter and parameterValue are provided
if (parameter != null && !parameter.trim().isEmpty() && parameterValue != null
&& !parameterValue.trim().isEmpty()) {
tableData = tableData.stream().filter(row -> {
Object paramVal = row.get(parameter);
return paramVal != null && paramVal.toString().equalsIgnoreCase(parameterValue);
}).collect(Collectors.toList());
}
ObjectMapper mapper = new ObjectMapper();
// Parse filters JSON (supports both string and array values)
if (filters != null && !filters.trim().isEmpty()) {
Map<String, Object> filtersMap;
try {
filtersMap = mapper.readValue(filters, new TypeReference<Map<String, Object>>() {
});
} catch (Exception e) {
return new ResponseEntity<>("Invalid filters JSON format", HttpStatus.BAD_REQUEST);
}
// Call reusable filter method
tableData = applyFilters(tableData, filtersMap);
}
StringBuilder jsonmap = new StringBuilder();
if (job_type.equalsIgnoreCase("Grid")) {
List<Map<String, Object>> allData = getAllDataFromApi(tableName, sureId);
jsonmap.append("[\n");
for (Map<String, Object> row : allData) {
for (Map<String, Object> row : tableData) {
jsonmap.append("{\n");
int colCount = 0;
@ -220,7 +202,7 @@ public class ChartBuilder {
jsonmap.append("}");
if (!row.equals(allData.get(allData.size() - 1))) {
if (!row.equals(tableData.get(tableData.size() - 1))) {
jsonmap.append(", ");
}
}
@ -231,12 +213,11 @@ public class ChartBuilder {
}
if (job_type.equalsIgnoreCase("Todo List") && yAxes != null && !yAxes.isEmpty()) {
List<Map<String, Object>> allData = getAllDataFromApi(tableName, sureId);
String listName = yAxes.get(0); // Assuming the first column in yAxes to be the list name
List<Object> listData = new ArrayList<>();
for (Map<String, Object> row : allData) {
for (Map<String, Object> row : tableData) {
Object value = row.get(listName);
if (value != null) {
@ -251,16 +232,6 @@ public class ChartBuilder {
return new ResponseEntity<>(response, HttpStatus.CREATED);
}
List<Map<String, Object>> tableData = getAllDataFromApi(tableName, sureId); // Retrieve all data from the table
// Filter table data if parameter and parameterValue are provided
if (parameter != null && !parameter.trim().isEmpty() && parameterValue != null
&& !parameterValue.trim().isEmpty()) {
tableData = tableData.stream().filter(row -> {
Object paramVal = row.get(parameter);
return paramVal != null && paramVal.toString().equalsIgnoreCase(parameterValue);
}).collect(Collectors.toList());
}
List<List<Object>> yAxisValuesList = new ArrayList<>();
List<String> xAxisValues = new ArrayList<>();
List<String> parameterValues = new ArrayList<>();
@ -289,404 +260,47 @@ public class ChartBuilder {
jsonmap = getJson(jsonmap, yAxes, xAxisValues, yAxisValuesList, parameterValues);
// if (job_type.equalsIgnoreCase("Bar Chart") || job_type.equalsIgnoreCase("Bar")) {
// jsonmap.append("{\n \"barChartData\": [\n");
//
// for (int j = 0; j < yAxes.size(); j++) {
// String yAxis = yAxes.get(j);
//
// jsonmap.append("{");
// jsonmap.append("\"data\": [");
//
// for (int i = 0; i < xAxisValues.size(); i++) {
//// Object yValue = yAxisValuesList.get(j).get(i);
//
// List<Object> list = yAxisValuesList.get(j);
// if (list.isEmpty()) {
// continue;
//
// }
// Object yValue = list.get(i);
// // Accept Integer, Long, Double, Float, or numeric Strings
// if (yValue instanceof Number) {
// jsonmap.append(yValue);
// } else if (yValue instanceof String) {
// String yStr = ((String) yValue).trim();
// try {
// // Parse and append only if numeric
// Double num = Double.parseDouble(yStr);
// jsonmap.append(num);
// } catch (NumberFormatException e) {
// // not numeric skip
// continue;
// }
// } else {
// continue; // skip non-numeric values
// }
//
// if (i < xAxisValues.size() - 1) {
// jsonmap.append(",");
// }
// }
//
// // 🧹 Remove trailing comma if any
// int lastIndex = jsonmap.lastIndexOf(",");
// if (lastIndex == jsonmap.length() - 1) {
// jsonmap.deleteCharAt(lastIndex);
// }
// jsonmap.append("],");
// jsonmap.append("\"label\": \"" + yAxis + "\"");
// jsonmap.append("}");
//
// if (j < yAxes.size() - 1) {
// jsonmap.append(",");
// }
// }
//
// jsonmap.append("],\n \"barChartLabels\": [ ");
//
// for (String xValue : xAxisValues) {
// jsonmap.append("\"" + xValue + "\",");
// }
//
// if (!xAxisValues.isEmpty()) {
// jsonmap.deleteCharAt(jsonmap.lastIndexOf(","));
// }
//
// jsonmap.append("] \n }\n");
// }
//
// else if (job_type.equalsIgnoreCase("Line Chart") || job_type.equalsIgnoreCase("Line")) {
// jsonmap.append("{\n \"chartData\": [\n");
//
// for (int j = 0; j < yAxes.size(); j++) {
// String yAxis = yAxes.get(j);
//
// jsonmap.append("{");
// jsonmap.append("\"data\": [");
//
// for (int i = 0; i < xAxisValues.size(); i++) {
// List<Object> list = yAxisValuesList.get(j);
// if (list.isEmpty()) {
// continue;
//
// }
// Object yValue = list.get(i);
// // Accept Integer, Long, Double, Float, or numeric Strings
// if (yValue instanceof Number) {
// jsonmap.append(yValue);
// } else if (yValue instanceof String) {
// String yStr = ((String) yValue).trim();
// try {
// // Parse and append only if numeric
// Double num = Double.parseDouble(yStr);
// jsonmap.append(num);
// } catch (NumberFormatException e) {
// // not numeric skip
// continue;
// }
// } else {
// continue; // skip non-numeric values
// }
//
// if (i < xAxisValues.size() - 1) {
// jsonmap.append(",");
// }
// }
//
// // 🧹 Remove trailing comma if any
// int lastIndex = jsonmap.lastIndexOf(",");
// if (lastIndex == jsonmap.length() - 1) {
// jsonmap.deleteCharAt(lastIndex);
// }
//
// jsonmap.append("],");
// jsonmap.append("\"label\": \"" + yAxis + "\"");
// jsonmap.append("}");
//
// if (j < yAxes.size() - 1) {
// jsonmap.append(",");
// }
// }
//
// jsonmap.append("],\n \"chartLabels\": [ ");
//
// for (String xValue : xAxisValues) {
// jsonmap.append("\"" + xValue + "\",");
// }
//
// if (!xAxisValues.isEmpty()) {
// jsonmap.deleteCharAt(jsonmap.lastIndexOf(","));
// }
//
// jsonmap.append("] \n }\n");
// } else if (job_type.equalsIgnoreCase("Doughnut Chart") || job_type.equalsIgnoreCase("Doughnut")) {
// jsonmap.append("{\"chartData\": [\n");
//
// for (int j = 0; j < yAxes.size(); j++) {
// String yAxis = yAxes.get(j);
//
// jsonmap.append("[");
//
// for (int i = 0; i < xAxisValues.size(); i++) {
// Object yValue = yAxisValuesList.get(j).get(i);
// jsonmap.append(yValue);
//
// if (i < xAxisValues.size() - 1) {
// jsonmap.append(",");
// }
// }
//
// jsonmap.append("]");
//
// if (j < yAxes.size() - 1) {
// jsonmap.append(",");
// }
// }
//
// jsonmap.append("],\n \"chartLabels\": [");
//
// for (String xValue : xAxisValues) {
// jsonmap.append("\"" + xValue + "\",");
// }
//
// if (!xAxisValues.isEmpty()) {
// jsonmap.deleteCharAt(jsonmap.lastIndexOf(","));
// }
//
// jsonmap.append("]\n}");
// }
//
// else if (job_type.equalsIgnoreCase("Radar Chart") || job_type.equalsIgnoreCase("Radar")) {
// jsonmap.append("{\n \"radarChartData\": [\n");
//
// for (int j = 0; j < yAxes.size(); j++) {
// String yAxis = yAxes.get(j);
//
// jsonmap.append("{");
// jsonmap.append("\"data\": [");
//
// for (int i = 0; i < xAxisValues.size(); i++) {
// Object yValue = yAxisValuesList.get(j).get(i);
// jsonmap.append(yValue);
//
// if (i < xAxisValues.size() - 1) {
// jsonmap.append(",");
// }
// }
//
// jsonmap.append("],");
// jsonmap.append("\"label\": \"" + yAxis + "\"");
// jsonmap.append("}");
//
// if (j < yAxes.size() - 1) {
// jsonmap.append(",");
// }
// }
//
// jsonmap.append("],\n \"radarChartLabels\": [ ");
//
// for (String xValue : xAxisValues) {
// jsonmap.append("\"" + xValue + "\",");
// }
//
// if (!xAxisValues.isEmpty()) {
// jsonmap.deleteCharAt(jsonmap.lastIndexOf(","));
// }
//
// jsonmap.append("] \n }\n");
// }
//
// else if (job_type.equalsIgnoreCase("PolarArea Chart") || job_type.equalsIgnoreCase("PolarArea")) {
// jsonmap.append("{\n \"polarAreaChartData\": [\n");
//
// for (int j = 0; j < yAxes.size(); j++) {
// String yAxis = yAxes.get(j);
//
// jsonmap.append("{");
// jsonmap.append("\"data\": [");
//
// for (int i = 0; i < xAxisValues.size(); i++) {
// Object yValue = yAxisValuesList.get(j).get(i);
// jsonmap.append(yValue);
//
// if (i < xAxisValues.size() - 1) {
// jsonmap.append(",");
// }
// }
//
// jsonmap.append("],");
// jsonmap.append("\"label\": \"" + yAxis + "\"");
// jsonmap.append("}");
//
// if (j < yAxes.size() - 1) {
// jsonmap.append(",");
// }
// }
//
// jsonmap.append("],\n \"polarAreaChartLabels\": [ ");
//
// for (String xValue : xAxisValues) {
// jsonmap.append("\"" + xValue + "\",");
// }
//
// if (!xAxisValues.isEmpty()) {
// jsonmap.deleteCharAt(jsonmap.lastIndexOf(","));
// }
//
// jsonmap.append("] \n }\n");
// }
//
// if (job_type.equalsIgnoreCase("Pie Chart") || job_type.equalsIgnoreCase("Pie")) {
// jsonmap.append("{\n \"pieChartData\": [");
//
// for (int i = 0; i < yAxisValuesList.get(0).size(); i++) { // Assuming "mark" is the first item in yAxes
// jsonmap.append(yAxisValuesList.get(0).get(i)); // Use the y-axis values
//
// if (i < yAxisValuesList.get(0).size() - 1) {
// jsonmap.append(",");
// }
// }
//
// jsonmap.append("],\n \"pieChartLabels\": [ ");
//
// for (int i = 0; i < xAxisValues.size(); i++) { // Assuming "name" is the x-axis
// jsonmap.append("\"" + xAxisValues.get(i) + "\""); // Use the x-axis values
//
// if (i < xAxisValues.size() - 1) {
// jsonmap.append(",");
// }
// }
//
// jsonmap.append("] \n }\n");
// }
//
// else if (job_type.equalsIgnoreCase("Bubble Chart") || job_type.equalsIgnoreCase("Bubble")) {
// jsonmap.append("{\n \"bubbleChartData\": [\n");
//
// for (int j = 0; j < yAxes.size(); j++) {
// String yAxis = yAxes.get(j);
//
// jsonmap.append("{");
// jsonmap.append("\"data\": [");
//
// for (int i = 0; i < xAxisValues.size(); i++) {
// Object xValue = xAxisValues.get(i);
// Object yValue = yAxisValuesList.get(j).get(i);
// int radius = 5 + (i % 3) * 3; // Adjust the radius as needed
//
// jsonmap.append("{ \"x\": " + xValue + ", \"y\": " + yValue + ", \"r\": " + radius + "}");
//
// if (i < xAxisValues.size() - 1) {
// jsonmap.append(",");
// }
// }
//
// jsonmap.append("],");
// jsonmap.append("\"label\": \"" + yAxis + "\"");
// jsonmap.append("}");
//
// if (j < yAxes.size() - 1) {
// jsonmap.append(",");
// }
// }
//
// jsonmap.append("],\n \"bubbleChartLabels\": [ ");
//
// for (String label : xAxisValues) {
// jsonmap.append("\"" + label + "\",");
// }
//
// if (!xAxisValues.isEmpty()) {
// jsonmap.deleteCharAt(jsonmap.lastIndexOf(","));
// }
//
// jsonmap.append("] \n }\n");
// } else if (job_type.equalsIgnoreCase("Scatter Chart") || job_type.equalsIgnoreCase("Scatter")) {
// jsonmap.append("{\n \"scatterChartData\": [\n");
//
// for (int j = 0; j < yAxes.size(); j++) {
// String yAxis = yAxes.get(j);
//
// jsonmap.append("{");
// jsonmap.append("\"data\": [");
//
// for (int i = 0; i < xAxisValues.size(); i++) {
// Object xValue = xAxisValues.get(i);
// Object yValue = yAxisValuesList.get(j).get(i);
//
// jsonmap.append("{ \"x\": " + xValue + ", \"y\": " + yValue + "}");
//
// if (i < xAxisValues.size() - 1) {
// jsonmap.append(",");
// }
// }
//
// jsonmap.append("],");
// jsonmap.append("\"label\": \"" + yAxis + "\"");
// jsonmap.append("}");
//
// if (j < yAxes.size() - 1) {
// jsonmap.append(",");
// }
// }
//
// jsonmap.append("],\n \"scatterChartLabels\": [ ");
//
// for (String label : xAxisValues) {
// jsonmap.append("\"" + label + "\",");
// }
//
// if (!xAxisValues.isEmpty()) {
// jsonmap.deleteCharAt(jsonmap.lastIndexOf(","));
// }
//
// jsonmap.append("] \n }\n");
// }
//
// else if (job_type.equalsIgnoreCase("Dynamic Chart") || job_type.equalsIgnoreCase("Dynamic")) {
// jsonmap.append("{\n \"dynamicChartData\": [\n");
//
// for (int j = 0; j < yAxes.size(); j++) {
// String yAxis = yAxes.get(j);
//
// jsonmap.append("{");
// jsonmap.append("\"data\": [");
//
// for (int i = 0; i < xAxisValues.size(); i++) {
// Object yValue = yAxisValuesList.get(j).get(i);
// jsonmap.append(yValue);
//
// if (i < xAxisValues.size() - 1) {
// jsonmap.append(",");
// }
// }
//
// jsonmap.append("],");
// jsonmap.append("\"label\": \"" + yAxis + "\"");
// jsonmap.append("}");
//
// if (j < yAxes.size() - 1) {
// jsonmap.append(",");
// }
// }
//
// jsonmap.append("],\n \"dynamicChartLabels\": [ ");
//
// for (String xValue : xAxisValues) {
// jsonmap.append("\"" + xValue + "\",");
// }
//
// if (!xAxisValues.isEmpty()) {
// jsonmap.deleteCharAt(jsonmap.lastIndexOf(","));
// }
//
// jsonmap.append("] \n }\n");
// }
return new ResponseEntity<>(jsonmap.toString(), HttpStatus.CREATED);
}
/**
* Reusable filter method Supports both single-value and multi-value filters
*/
private List<Map<String, Object>> applyFilters(List<Map<String, Object>> tableData,
Map<String, Object> filtersMap) {
if (filtersMap == null || filtersMap.isEmpty())
return tableData;
return tableData.stream().filter(row -> {
for (Map.Entry<String, Object> entry : filtersMap.entrySet()) {
Object filterValue = entry.getValue();
Object rowValue = row.get(entry.getKey());
// skip empty filters
if (filterValue == null)
continue;
// Multi-value filter (List or Array)
if (filterValue instanceof List<?>) {
List<?> filterList = (List<?>) filterValue;
if (filterList.isEmpty())
continue;
if (rowValue == null
|| !filterList.stream().anyMatch(v -> v.toString().equalsIgnoreCase(rowValue.toString()))) {
return false;
}
}
// Single-value filter
else {
if (rowValue == null || !rowValue.toString().equalsIgnoreCase(filterValue.toString())) {
return false;
}
}
}
return true;
}).collect(Collectors.toList());
}
public StringBuilder getJson(StringBuilder jsonmap, List<String> yAxes, List<String> xAxisValues,
List<List<Object>> yAxisValuesList, List<String> parameterValues) {
@ -770,77 +384,6 @@ public class ChartBuilder {
return jsonmap;
}
// get json data
// public StringBuilder getJson(StringBuilder jsonmap, List<String> yAxes, List<String> xAxisValues,
// List<List<Object>> yAxisValuesList, List<String> parameterValues) {
// jsonmap.append("{\n \"chartData\": [\n");
//
// for (int j = 0; j < yAxes.size(); j++) {
// String yAxis = yAxes.get(j);
//
// jsonmap.append("{");
// jsonmap.append("\"data\": [");
//
// for (int i = 0; i < xAxisValues.size(); i++) {
// List<Object> list = yAxisValuesList.get(j);
// if (list.isEmpty()) {
// continue;
//
// }
// Object yValue = list.get(i);
// // Accept Integer, Long, Double, Float, or numeric Strings
// if (yValue instanceof Number) {
// jsonmap.append(yValue);
// } else if (yValue instanceof String) {
// String yStr = ((String) yValue).trim();
// try {
// // Parse and append only if numeric
// Double num = Double.parseDouble(yStr);
// jsonmap.append(num);
// } catch (NumberFormatException e) {
// // not numeric skip
// continue;
// }
// } else {
// continue; // skip non-numeric values
// }
//
// if (i < xAxisValues.size() - 1) {
// jsonmap.append(",");
// }
// }
//
// // 🧹 Remove trailing comma if any
// int lastIndex = jsonmap.lastIndexOf(",");
// if (lastIndex == jsonmap.length() - 1) {
// jsonmap.deleteCharAt(lastIndex);
// }
//
// jsonmap.append("],");
// jsonmap.append("\"label\": \"" + yAxis + "\"");
// jsonmap.append("}");
//
// if (j < yAxes.size() - 1) {
// jsonmap.append(",");
// }
// }
//
// jsonmap.append("],\n \"chartLabels\": [ ");
//
// for (String xValue : xAxisValues) {
// jsonmap.append("\"" + xValue + "\",");
// }
//
// if (!xAxisValues.isEmpty()) {
// jsonmap.deleteCharAt(jsonmap.lastIndexOf(","));
// }
//
// jsonmap.append("] \n }\n");
//
// return jsonmap;
// }
//
@GetMapping("/getAllKeys")
public Set<String> getAllKeys(@RequestParam String apiUrl, @RequestParam Integer sureId) {
List<Map<String, Object>> apiData = getAllKeyFromApi(apiUrl, sureId);

View File

@ -0,0 +1,49 @@
package com.realnet.Dashboard_builder.Controllers;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDate;
import java.util.*;
@RestController
public class DashboardMockController {
@GetMapping("/api/getDummyDashboardData")
public ResponseEntity<List<Map<String, Object>>> getDummyDashboardData() {
List<Map<String, Object>> dataList = new ArrayList<>();
String[] salesReps = { "Gaurav", "Ravi", "Ankit", "Neha", "Kiran" };
String[] partners = { "IBM", "Microsoft", "TCS", "Infosys", "Wipro" };
String[] regions = { "Asia", "Europe", "North America", "South America", "Africa" };
String[] channels = { "Online", "Retail", "Direct", "Distributor", "Reseller" };
String[] products = { "Laptops", "Servers", "Networking", "Cloud", "AI" };
for (int i = 1; i <= 50; i++) {
Map<String, Object> row = new LinkedHashMap<>();
// 🧩 Fixed pattern numeric data
row.put("leadCount", 100 + (i * 10));
row.put("dealValue", 5000 + (i * 750));
row.put("conversionRate", (i % 100));
row.put("activeUsers", 50 + (i * 5));
row.put("visits", 200 + (i * 15));
// 🧠 Repeated pattern text data
row.put("salesRep", salesReps[i % salesReps.length]);
row.put("partner", partners[i % partners.length]);
row.put("region", regions[i % regions.length]);
row.put("channel", channels[i % channels.length]);
row.put("productLine", products[i % products.length]);
// 📅 Sequential date data
row.put("createdDate", LocalDate.of(2024, (i % 12) + 1, ((i % 28) + 1)));
row.put("lastUpdated", LocalDate.of(2025, ((i + 2) % 12) + 1, ((i + 3) % 28) + 1));
dataList.add(row);
}
return ResponseEntity.ok(dataList);
}
}

View File

@ -92,12 +92,12 @@ public class Data_lakeController {
}
// json Updtaed
// json Upadted
@PutMapping("/Data_lake/json/{id}")
public Data_lake update(@PathVariable Integer id) throws JsonProcessingException {
Data_lake update = Service.Updatejson(id);
System.out.println("json update..." + update);
Data_lake update = Service.applyCalculation(id);
System.out.println("clculation applied..." + update);
return update;
}

View File

@ -42,10 +42,16 @@ public class Data_lake extends Extension {
private String url_endpoint;
private Integer ref_datalake_id;
@Lob
@Column(columnDefinition = "TEXT")
private String calculated_field_json;
@Lob
@Column(columnDefinition = "TEXT")
private String groupby_json;
private Boolean iscalculatedfield;
}

View File

@ -18,4 +18,7 @@ public interface Data_lakeRepository extends JpaRepository<Data_lake, Integer> {
@Query(value = "select * from data_lake where created_by=?1", nativeQuery = true)
Page<Data_lake> findAll(Long creayedBy, Pageable page);
@Query(value = "select * from data_lake where created_by=?1 && ref_datalake_id=?2", nativeQuery = true)
List<Data_lake> findAllByRefDatlakeId(Long creayedBy, Integer ref_datalake_id);
}

View File

@ -2,10 +2,15 @@ package com.realnet.DataLake.Services;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
@ -126,24 +131,61 @@ public class Data_lakeService {
old.setIscalculatedfield(data.getIscalculatedfield());
}
if (data.getGroupby_json() != null) {
old.setGroupby_json(data.getGroupby_json());
}
if (data.getRef_datalake_id() != null) {
old.setRef_datalake_id(data.getRef_datalake_id());
}
final Data_lake test = Repository.save(old);
return test;
}
public Data_lake Updatejson(Integer id) throws JsonProcessingException {
public Data_lake applyCalculation(Integer id) throws JsonProcessingException {
Data_lake old = Repository.findById(id).get();
String url = old.getUrl();
ResponseEntity<String> response = GETAsString(url);
String rawBody = response.getBody();
List<Data_lake> datlakes = Repository.findAllByRefDatlakeId(getUser().getUserId(), id);
datlakes.forEach(lake -> {
try {
Updatejson(lake.getId(), rawBody);
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
Data_lake saved = Updatejson(id, rawBody);
System.out.println(" json updated..");
return saved;
}
public Data_lake Updatejson(Integer id, String rawBody) throws JsonProcessingException {
Data_lake old = Repository.findById(id).get();
// String url = old.getUrl();
//
// ResponseEntity<String> response = GETAsString(url);
// String rawBody = response.getBody();
// Convert the JSON object (ArrayList, Map, etc.) to a String
// Object responseBody = response.getBody();
ObjectMapper mapper = new ObjectMapper();
String rawBody = response.getBody();
// String jsonString = mapper.writeValueAsString(rawBody);
ObjectMapper jsonMapper = new ObjectMapper();
Object responseBody;
@ -176,7 +218,7 @@ public class Data_lakeService {
// Handle calculated fields before batch processing
if (Boolean.TRUE.equals(old.getIscalculatedfield()) && old.getCalculated_field_json() != null) {
try {
responseBody = applyCalculatedFields(responseBody, old.getCalculated_field_json(), mapper);
responseBody = applyCalculatedFields(responseBody, old.getCalculated_field_json(), mapper, old);
System.out.println("Calculated fields applied successfully.");
} catch (Exception e) {
System.err.println("Failed to process calculated fields: " + e.getMessage());
@ -204,56 +246,201 @@ public class Data_lakeService {
}
@SuppressWarnings("unchecked")
private Object applyCalculatedFields(Object responseBody, String calculatedFieldJson, ObjectMapper mapper)
throws JsonProcessingException {
private Object applyCalculatedFields(Object responseBody, String calculatedFieldJson, ObjectMapper mapper,
Data_lake old) throws JsonProcessingException {
// Parse the calculated field JSON
List<Map<String, Object>> calcFields = mapper.readValue(calculatedFieldJson, List.class);
if (!(responseBody instanceof List)) {
// Wrap single object into a list for consistent processing
responseBody = Arrays.asList(responseBody);
}
List<Map<String, Object>> records = (List<Map<String, Object>>) responseBody;
for (Map<String, Object> record : records) {
for (Map<String, Object> calc : calcFields) {
String type = (String) calc.get("type");
if ("groupby".equalsIgnoreCase(type)) {
// Handle group-by aggregation
List<Map<String, Object>> groupbyrecords = applyGroupBy(records, calc);
String groupByRecord = mapper.writeValueAsString(groupbyrecords);
for (Map<String, Object> calc : calcFields) {
String fieldName = (String) calc.get("fieldName");
String operation = (String) calc.get("operation");
List<Map<String, Object>> components = (List<Map<String, Object>>) calc.get("fieldComponents");
old.setGroupby_json(groupByRecord);
// Create constant fields separately
for (Map<String, Object> comp : components) {
String subField = (String) comp.get("field");
Boolean isConstant = comp.get("isConstant") != null && (Boolean) comp.get("isConstant");
Object constantValue = comp.get("constant");
} else {
// Handle calculated / complex operations (as before)
for (Map<String, Object> record : records) {
String fieldName = (String) calc.get("fieldName");
String operation = (String) calc.get("operation");
List<Map<String, Object>> components = (List<Map<String, Object>>) calc.get("fieldComponents");
if (isConstant && subField != null) {
record.put(subField, parseNumberOrString(constantValue));
// Handle constant fields
if (components != null) {
for (Map<String, Object> comp : components) {
String subField = (String) comp.get("field");
Boolean isConstant = comp.get("isConstant") != null && (Boolean) comp.get("isConstant");
Object constantValue = comp.get("constant");
if (isConstant && subField != null) {
record.put(subField, parseNumberOrString(constantValue));
}
}
}
}
// Collect operand values
List<Object> values = new ArrayList<>();
for (Map<String, Object> comp : components) {
Boolean isConstant = comp.get("isConstant") != null && (Boolean) comp.get("isConstant");
Object val = isConstant ? comp.get("constant") : record.get(comp.get("field"));
if (val != null) {
values.add(val);
// Handle complex expression
if ("complex".equalsIgnoreCase(operation) && calc.get("complexEquation") != null) {
String equation = (String) calc.get("complexEquation");
Object result = evaluateComplexExpression(equation, record);
record.put(fieldName, result);
continue;
}
}
// Compute final value
Object result = performOperation(values, operation);
record.put(fieldName, result);
// Handle normal arithmetic
List<Object> values = new ArrayList<>();
if (components != null) {
for (Map<String, Object> comp : components) {
Boolean isConstant = comp.get("isConstant") != null && (Boolean) comp.get("isConstant");
Object val = isConstant ? comp.get("constant") : record.get(comp.get("field"));
if (val != null) {
values.add(val);
}
}
}
Object result = performOperation(values, operation);
record.put(fieldName, result);
}
}
}
return records;
}
@SuppressWarnings("unchecked")
private List<Map<String, Object>> applyGroupBy(List<Map<String, Object>> records, Map<String, Object> groupConfig) {
List<String> groupFields = (List<String>) groupConfig.get("groupFields");
List<Map<String, Object>> aggregations = (List<Map<String, Object>>) groupConfig.get("aggregations");
// Group records by key
Map<String, List<Map<String, Object>>> grouped = new LinkedHashMap<>();
for (Map<String, Object> record : records) {
String key = groupFields.stream().map(f -> String.valueOf(record.getOrDefault(f, "")))
.collect(Collectors.joining("|"));
grouped.computeIfAbsent(key, k -> new ArrayList<>()).add(record);
}
// Aggregate results per group
List<Map<String, Object>> result = new ArrayList<>();
for (Map.Entry<String, List<Map<String, Object>>> entry : grouped.entrySet()) {
Map<String, Object> groupRecord = new LinkedHashMap<>();
// Add group fields
String[] parts = entry.getKey().split("\\|");
for (int i = 0; i < groupFields.size(); i++) {
groupRecord.put(groupFields.get(i), parts[i]);
}
List<Map<String, Object>> groupRows = entry.getValue();
// Apply each aggregation
for (Map<String, Object> agg : aggregations) {
String field = (String) agg.get("field");
String operation = ((String) agg.get("operation")).toLowerCase();
List<Double> numericValues = groupRows.stream().map(r -> toDouble(r.get(field)))
.filter(v -> !Double.isNaN(v)).collect(Collectors.toList());
Object aggResult;
switch (operation) {
case "count":
aggResult = groupRows.size();
break;
case "sum":
aggResult = numericValues.stream().mapToDouble(Double::doubleValue).sum();
break;
case "average":
aggResult = numericValues.isEmpty() ? 0.0
: numericValues.stream().mapToDouble(Double::doubleValue).average().orElse(0.0);
break;
case "minimum":
aggResult = numericValues.isEmpty() ? null
: numericValues.stream().mapToDouble(Double::doubleValue).min().orElse(0.0);
break;
case "maximum":
aggResult = numericValues.isEmpty() ? null
: numericValues.stream().mapToDouble(Double::doubleValue).max().orElse(0.0);
break;
case "median":
aggResult = calculateMedian(numericValues);
break;
case "mode":
aggResult = calculateMode(numericValues);
break;
case "standard deviation":
aggResult = calculateStdDev(numericValues);
break;
default:
aggResult = null;
}
groupRecord.put(field + "_" + operation, aggResult);
}
result.add(groupRecord);
}
return result;
}
// --- Statistical helpers ---
private Double calculateMedian(List<Double> values) {
if (values == null || values.isEmpty())
return null;
List<Double> sorted = new ArrayList<>(values);
Collections.sort(sorted);
int n = sorted.size();
if (n % 2 == 1) {
return sorted.get(n / 2);
} else {
return (sorted.get(n / 2 - 1) + sorted.get(n / 2)) / 2.0;
}
}
private Double calculateMode(List<Double> values) {
if (values == null || values.isEmpty())
return null;
Map<Double, Long> freq = values.stream().collect(Collectors.groupingBy(v -> v, Collectors.counting()));
return freq.entrySet().stream().max(Map.Entry.comparingByValue()).map(Map.Entry::getKey).orElse(null);
}
private Double calculateStdDev(List<Double> values) {
if (values == null || values.isEmpty())
return null;
double mean = values.stream().mapToDouble(Double::doubleValue).average().orElse(0.0);
double variance = values.stream().mapToDouble(v -> Math.pow(v - mean, 2)).average().orElse(0.0);
return Math.sqrt(variance);
}
private Object evaluateComplexExpression(String expression, Map<String, Object> record) {
try {
// Replace field names with values dynamically
for (Map.Entry<String, Object> e : record.entrySet()) {
String field = e.getKey();
Object val = e.getValue();
expression = expression.replaceAll("\\b" + field + "\\b", String.valueOf(val != null ? val : 0));
}
// Evaluate using ScriptEngine (works on Java 1.8)
javax.script.ScriptEngine engine = new javax.script.ScriptEngineManager().getEngineByName("JavaScript");
Object result = engine.eval(expression);
return result;
} catch (Exception e) {
System.err.println("Complex expression error: " + e.getMessage());
return null;
}
}
// perform normal operation
private Object performOperation(List<Object> values, String operation) {
if (values.isEmpty())
return null;
@ -301,6 +488,60 @@ public class Data_lakeService {
}
}
/**
* Evaluates complex equations using JavaScript engine Supports math
* operations, parentheses, and string concatenation.
*/
private Object evaluateComplexEquation(String expression, Map<String, Object> record,
List<Map<String, Object>> fieldComponents) {
if (expression == null || expression.trim().isEmpty())
return null;
ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
try {
// 🔹 Replace constants
if (fieldComponents != null) {
for (Map<String, Object> comp : fieldComponents) {
String field = (String) comp.get("field");
Object constant = comp.get("constant");
Boolean isConstant = comp.get("isConstant") != null && (Boolean) comp.get("isConstant");
if (isConstant && constant != null) {
String constVal = constant.toString();
expression = expression.replaceAll("\\b" + field + "\\b", constVal);
}
}
}
// 🔹 Replace field values from record
for (Map.Entry<String, Object> entry : record.entrySet()) {
String key = entry.getKey();
Object val = entry.getValue();
if (val != null) {
String safeValue = val.toString();
// If it's a string containing letters, wrap it in quotes for JS
if (!safeValue.matches("^-?\\d+(\\.\\d+)?$")) {
safeValue = "'" + safeValue.replace("'", "\\'") + "'";
}
expression = expression.replaceAll("\\b" + key + "\\b", safeValue);
}
}
// Evaluate the expression safely
Object result = engine.eval(expression);
System.out.println(" exoression is : " + expression + " and " + result);
return result;
} catch (Exception e) {
System.err.println("❌ Error evaluating complex equation: " + e.getMessage());
return null;
}
}
private double toDouble(Object val) {
if (val == null)
return 0.0;