Skip to content

Commit

Permalink
avniproject#762 | MetabaseJoin And MetabaseQuery created
Browse files Browse the repository at this point in the history
  • Loading branch information
ombhardwajj committed Aug 24, 2024
1 parent b9dc476 commit 6df633a
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.avni.server.domain.metabase;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class MetabaseJoin {
private final String fields;
private final String alias;
private final int sourceTable;
private final JsonNode condition;

public MetabaseJoin(String fields, String alias, int sourceTable, JsonNode condition) {
this.fields = fields;
this.alias = alias;
this.sourceTable = sourceTable;
this.condition = condition;
}

public ObjectNode toJson(ObjectMapper objectMapper) {
ObjectNode joinNode = objectMapper.createObjectNode();
joinNode.put("fields", this.fields);
joinNode.put("alias", this.alias);
joinNode.put("source-table", this.sourceTable);
joinNode.set("condition", this.condition);
return joinNode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.avni.server.domain.metabase;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class MetabaseQuery {
private final int sourceTable;
private final ArrayNode joins;

public MetabaseQuery(int sourceTable, ArrayNode joins) {
this.sourceTable = sourceTable;
this.joins = joins;
}

public ObjectNode toJson(ObjectMapper objectMapper) {
ObjectNode queryNode = objectMapper.createObjectNode();
queryNode.put("source-table", this.sourceTable);
queryNode.set("joins", this.joins);
return queryNode;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
ckage org.avni.server.service.metabase;
package org.avni.server.service.metabase;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.avni.server.dao.metabase.DatabaseRepository;
import org.avni.server.domain.metabase.TableType;
import org.avni.server.domain.metabase.MetabaseJoin;
import org.avni.server.domain.metabase.MetabaseQuery;
import org.avni.server.util.StringUtils;
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;
import java.util.HashMap;

@Service
public class DatabaseService {
Expand Down Expand Up @@ -165,35 +169,26 @@ private List<String> extractTableNames(JsonNode databaseDetails) {
return tableNames;
}

private void createQuestionForTable(String tableName, String addressTableName, String addressField, String tableField) {
private void createQuestionForTable(String tableName, String addressTableName, String addressField, String tableField) throws Exception {
int addressTableId = getTableIdByName(addressTableName);
int joinFieldId1 = getFieldIdByTableNameAndFieldName(addressTableName, addressField);
int tableId = getTableIdByName(tableName);
int joinFieldId2 = getFieldIdByTableNameAndFieldName(tableName, tableField);

ObjectNode datasetQuery = objectMapper.createObjectNode();
datasetQuery.put("database", getDatabaseId());
datasetQuery.put("type", "query");

ObjectNode query = objectMapper.createObjectNode();
query.put("source-table", addressTableId);

ArrayNode joins = objectMapper.createArrayNode();
ObjectNode join = objectMapper.createObjectNode();
join.put("fields", "all");
join.put("alias", tableName);
JsonNode conditionNode = objectMapper.readTree(
"[\"=\", [\"field\", " + joinFieldId1 + ", {\"base-type\": \"type/Integer\"}], [\"field\", " + joinFieldId2 + ", {\"base-type\": \"type/Integer\", \"join-alias\": \"" + tableName + "\"}]]"
);
MetabaseJoin join = new MetabaseJoin("all", tableName, tableId, conditionNode); // Change: tableId as integer

ArrayNode condition = objectMapper.createArrayNode();
condition.add("=");
condition.add(objectMapper.createArrayNode().add("field").add(joinFieldId1).add(objectMapper.createObjectNode().put("base-type", "type/Integer")));
condition.add(objectMapper.createArrayNode().add("field").add(joinFieldId2).add(objectMapper.createObjectNode().put("base-type", "type/Integer").put("join-alias", tableName)));
ArrayNode joinsArray = objectMapper.createArrayNode();
joinsArray.add(join.toJson(objectMapper));

join.set("condition", condition);
join.put("source-table", tableId);
joins.add(join);
MetabaseQuery query = new MetabaseQuery(addressTableId, joinsArray); // Change: addressTableId as integer

query.set("joins", joins);
datasetQuery.set("query", query);
ObjectNode datasetQuery = objectMapper.createObjectNode();
datasetQuery.put("database", getDatabaseId());
datasetQuery.put("type", "query");
datasetQuery.set("query", query.toJson(objectMapper));

ObjectNode body = objectMapper.createObjectNode();
body.put("name", "Address + " + tableName);
Expand All @@ -208,6 +203,8 @@ 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);

Expand Down Expand Up @@ -236,7 +233,7 @@ private void createQuestionForTable(String tableName, String schema) {
);
}

public void createQuestionsForSubjectTypes() {
public void createQuestionsForSubjectTypes() throws Exception {

String syncStatus = getInitialSyncStatus();
if (!"complete".equals(syncStatus)) {
Expand All @@ -250,7 +247,7 @@ public void createQuestionsForSubjectTypes() {
}
}

public void createQuestionsForProgramsAndEncounters() {
public void createQuestionsForProgramsAndEncounters() throws Exception{
String syncStatus = getInitialSyncStatus();
if (!"complete".equals(syncStatus)) {
throw new RuntimeException("Database initial sync is not complete.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void setupMetabase() {
collectionPermissionsRepository.updateCollectionPermissions(collectionPermissions, metabaseGroup.getId(), metabaseCollection.getId());
}

public void createQuestionsForSubjectTypes() {
public void createQuestionsForSubjectTypes() throws Exception{
databaseService.createQuestionsForSubjectTypes();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void setupMetabase() {
}

@PostMapping("/create-questions")
public void createQuestions() {
public void createQuestions() throws Exception{
databaseService.createQuestionsForSubjectTypes();
databaseService.createQuestionsForProgramsAndEncounters();
databaseService.createQuestionsForIndivdualTables();
Expand Down

0 comments on commit 6df633a

Please sign in to comment.