From b5bc0c68ee623c1723dfbd13a8776a75340cbeb1 Mon Sep 17 00:00:00 2001 From: Om Bhardwaj <115864495+ombhardwajj@users.noreply.github.com> Date: Sun, 11 Aug 2024 23:44:57 +0530 Subject: [PATCH] avniproject#762 | Automated questions for Address,Media,Sync Telemetry --- .../service/metabase/DatabaseService.java | 65 ++++++++++++++++++- .../avni/server/web/MetabaseController.java | 1 + 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/avni-server-api/src/main/java/org/avni/server/service/metabase/DatabaseService.java b/avni-server-api/src/main/java/org/avni/server/service/metabase/DatabaseService.java index 025681e15..533b547ee 100644 --- a/avni-server-api/src/main/java/org/avni/server/service/metabase/DatabaseService.java +++ b/avni-server-api/src/main/java/org/avni/server/service/metabase/DatabaseService.java @@ -9,7 +9,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; - +import java.util.Arrays; import java.util.ArrayList; import java.util.List; @@ -60,6 +60,29 @@ public int getTableIdByName(String tableName) { return -1; } + public int getTableIdByName(String tableName, String schema) { + JsonNode rootNode = databaseRepository.getDatabaseDetails( + getDatabaseId() + ); + JsonNode tablesArray = rootNode.path("tables"); + + for (JsonNode tableNode : tablesArray) { + String tableSchema = tableNode.path("schema").asText(); + + boolean schemaMatches = schema.equals("public") + ? "public".equals(tableSchema) + : !"public".equals(tableSchema); + + if ( + tableName.equals(tableNode.path("display_name").asText()) && + schemaMatches + ) { + return tableNode.path("id").asInt(); + } + } + return -1; + } + private String createRequestBodyForDataset(int sourceTableId) { return "{\"database\":" + getDatabaseId() + ",\"query\":{\"source-table\":" + sourceTableId + "},\"type\":\"query\",\"parameters\":[]}"; } @@ -167,6 +190,34 @@ private void createQuestionForTable(String tableName, String addressTableName, S databaseRepository.postForObject(metabaseApiUrl + "/card", body, JsonNode.class); } + private void createQuestionForTable(String tableName, String schema) { + int tableId = getTableIdByName(tableName, schema); + + ObjectNode datasetQuery = objectMapper.createObjectNode(); + datasetQuery.put("database", getDatabaseId()); + datasetQuery.put("type", "query"); + + ObjectNode query = objectMapper.createObjectNode(); + query.put("source-table", tableId); + datasetQuery.set("query", query); + + ObjectNode body = objectMapper.createObjectNode(); + body.put("name", tableName); + body.set("dataset_query", datasetQuery); + body.put("display", "table"); + body.putNull("description"); + body.set("visualization_settings", objectMapper.createObjectNode()); + body.put("collection_id", getCollectionId()); + body.putNull("collection_position"); + body.putNull("result_metadata"); + + databaseRepository.postForObject( + metabaseApiUrl + "/card", + body, + JsonNode.class + ); + } + public void createQuestionsForSubjectTypes() { String syncStatus = getInitialSyncStatus(); @@ -200,4 +251,16 @@ public void createQuestionsForProgramsAndEncounters() { } } } + + public void createQuestionsForIndivdualTables() { + List tablesToCreateQuestionsFor = Arrays.asList( + "Address", + "Media", + "Sync Telemetry" + ); + + for (String tableName : tablesToCreateQuestionsFor) { + createQuestionForTable(tableName, "!public"); + } + } } diff --git a/avni-server-api/src/main/java/org/avni/server/web/MetabaseController.java b/avni-server-api/src/main/java/org/avni/server/web/MetabaseController.java index 652b26fa8..6eb423fbf 100644 --- a/avni-server-api/src/main/java/org/avni/server/web/MetabaseController.java +++ b/avni-server-api/src/main/java/org/avni/server/web/MetabaseController.java @@ -31,6 +31,7 @@ public void setupMetabase() { public void createQuestions() { databaseService.createQuestionsForSubjectTypes(); databaseService.createQuestionsForProgramsAndEncounters(); + databaseService.createQuestionsForIndivdualTables(); } @GetMapping("/sync-status")