Skip to content

Commit

Permalink
avniproject#762 | Automated questions for Address,Media,Sync Telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
ombhardwajj committed Aug 11, 2024
1 parent e8ec0c5 commit b5bc0c6
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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\":[]}";
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -200,4 +251,16 @@ public void createQuestionsForProgramsAndEncounters() {
}
}
}

public void createQuestionsForIndivdualTables() {
List<String> tablesToCreateQuestionsFor = Arrays.asList(
"Address",
"Media",
"Sync Telemetry"
);

for (String tableName : tablesToCreateQuestionsFor) {
createQuestionForTable(tableName, "!public");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public void setupMetabase() {
public void createQuestions() {
databaseService.createQuestionsForSubjectTypes();
databaseService.createQuestionsForProgramsAndEncounters();
databaseService.createQuestionsForIndivdualTables();
}

@GetMapping("/sync-status")
Expand Down

0 comments on commit b5bc0c6

Please sign in to comment.