Skip to content

Commit

Permalink
avniproject#762 | Enums Added
Browse files Browse the repository at this point in the history
  • Loading branch information
ombhardwajj committed Aug 28, 2024
1 parent 3f8968c commit 94650c8
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.avni.server.domain.metabase;

public enum BaseType {
INTEGER("type/Integer"),
TEXT("type/Text"),
BOOLEAN("type/Boolean");

private final String typeName;

BaseType(String typeName) {
this.typeName = typeName;
}

public String getTypeName() {
return typeName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.avni.server.domain.metabase;

public enum CardType {
QUESTION("question");

private final String type;

CardType(String type) {
this.type = type;
}

public String getType() {
return type;
}

public static CardType fromString(String typeName) {
for (CardType type : CardType.values()) {
if (type.getType().equalsIgnoreCase(typeName)) {
return type;
}
}
throw new IllegalArgumentException("Unknown card type: " + typeName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.avni.server.domain.metabase;

public enum ConditionType {
EQUAL("="),
NOT_EQUAL("!=");

private final String operator;

ConditionType(String operator) {
this.operator = operator;
}

public String getOperator() {
return operator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,29 @@

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;

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

public MetabaseJoin(String fields, String alias, int sourceTable, int joinFieldId1, int joinFieldId2, String tableName, ObjectMapper objectMapper) throws Exception {
this.fields = fields;
this.alias = alias;
this.sourceTable = sourceTable;
this.condition = createConditionNode(joinFieldId1, joinFieldId2, tableName, objectMapper);

this.condition = createConditionNode(joinFieldId1, joinFieldId2, tableName, BaseType.INTEGER, objectMapper);
}

private JsonNode createConditionNode(int joinFieldId1, int joinFieldId2, String tableName, ObjectMapper objectMapper) throws Exception {
return objectMapper.readTree(
"[\"=\", [\"field\", " + joinFieldId1 + ", {\"base-type\": \"type/Integer\"}], [\"field\", " + joinFieldId2 + ", {\"base-type\": \"type/Integer\", \"join-alias\": \"" + tableName + "\"}]]"
);
private JsonNode createConditionNode(int joinFieldId1, int joinFieldId2, String tableName, BaseType baseType, ObjectMapper objectMapper) throws Exception {
ArrayNode conditionNode = objectMapper.createArrayNode();
conditionNode.add(ConditionType.EQUAL.getOperator());
conditionNode.add(objectMapper.createArrayNode().add("field").add(joinFieldId1).add(objectMapper.createObjectNode().put("base-type", baseType.getTypeName())));
conditionNode.add(objectMapper.createArrayNode().add("field").add(joinFieldId2).add(objectMapper.createObjectNode().put("base-type", baseType.getTypeName()).put("join-alias", tableName)));
return conditionNode;
}

public ObjectNode toJson(ObjectMapper objectMapper) {
Expand All @@ -29,6 +33,7 @@ public ObjectNode toJson(ObjectMapper objectMapper) {
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
Expand Up @@ -8,7 +8,8 @@ public class MetabaseQuery {
private final int databaseId;
private final int sourceTable;
private final ArrayNode joins;
private final String type = "query";
private final QueryType type = QueryType.QUERY;


public MetabaseQuery(int databaseId,int sourceTable, ArrayNode joins) {
this.databaseId = databaseId;
Expand All @@ -24,7 +25,8 @@ public ObjectNode toJson(ObjectMapper objectMapper) {
ObjectNode queryNode = objectMapper.createObjectNode();
queryNode.put("source-table", sourceTable);
queryNode.set("joins", joins);
queryNode.put("type", type);
queryNode.put("type", type.toString());


return queryNode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@
public class MetabaseRequestBody {
private String name;
private MetabaseQuery datasetQuery;
private String display;
private VisualizationType display;
private String description;
private ObjectNode visualizationSettings;
private int collectionId;
private Integer collectionPosition;
private JsonNode resultMetadata;
private CardType cardType;

public MetabaseRequestBody(String name, MetabaseQuery datasetQuery, String display, String description, ObjectNode visualizationSettings, int collectionId) {
public MetabaseRequestBody(String name, MetabaseQuery datasetQuery, VisualizationType display, String description, ObjectNode visualizationSettings, int collectionId, CardType cardType) {
this.name = name;
this.datasetQuery = datasetQuery;
this.display = display;
this.description = description;
this.visualizationSettings = visualizationSettings;
this.collectionId = collectionId;
this.cardType = cardType;
}

public ObjectNode toJson(ObjectMapper objectMapper) {
Expand All @@ -33,7 +35,8 @@ public ObjectNode toJson(ObjectMapper objectMapper) {
datasetQueryNode.set("query", datasetQuery.toJson(objectMapper));

rootNode.set("dataset_query", datasetQueryNode);
rootNode.put("display", display);
rootNode.put("display", display.toString());
rootNode.put("type", cardType.getType());
rootNode.putNull("description");
rootNode.set("visualization_settings", visualizationSettings);
rootNode.put("collection_id", collectionId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.avni.server.domain.metabase;

public enum QueryType {
QUERY("query"),
DASHBOARD("dashboard");

private final String typeName;

QueryType(String typeName) {
this.typeName = typeName;
}

public String getTypeName() {
return typeName;
}

@Override
public String toString() {
return typeName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.avni.server.domain.metabase;

public enum SyncStatus {
COMPLETE("complete"),
INCOMPLETE("incomplete");

private final String status;

SyncStatus(String status) {
this.status = status;
}

public String getStatus() {
return status;
}

public static SyncStatus fromString(String status) {
for (SyncStatus s : SyncStatus.values()) {
if (s.getStatus().equalsIgnoreCase(status)) {
return s;
}
}
throw new IllegalArgumentException("Unknown sync status: " + status);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.avni.server.domain.metabase;

public enum VisualizationType {
TABLE("table"),
CHART("chart");

private final String typeName;

VisualizationType(String typeName) {
this.typeName = typeName;
}

public String getTypeName() {
return typeName;
}

@Override
public String toString() {
return typeName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
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.domain.metabase.MetabaseRequestBody;
import org.avni.server.domain.metabase.*;
import org.avni.server.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -103,9 +100,10 @@ public int getFieldIdByTableNameAndFieldName(String tableName, String fieldName)
return -1;
}

public String getInitialSyncStatus() {
public SyncStatus getInitialSyncStatus() {
JsonNode responseBody = databaseRepository.getInitialSyncStatus(getDatabaseId());
return responseBody.path("initial_sync_status").asText();
String status = responseBody.path("initial_sync_status").asText();
return SyncStatus.fromString(status);
}

private JsonNode getTableMetadata() {
Expand Down Expand Up @@ -184,7 +182,7 @@ private void createQuestionForTable(String tableName, String addressTableName, S
MetabaseQuery query = new MetabaseQuery(getDatabaseId(),addressTableId, joinsArray);

MetabaseRequestBody requestBody = new MetabaseRequestBody(
"Address + " + tableName, query, "table", null, objectMapper.createObjectNode(), getCollectionId());
"Address + " + tableName, query, VisualizationType.TABLE, null, objectMapper.createObjectNode(), getCollectionId(), CardType.QUESTION);

databaseRepository.postForObject(metabaseApiUrl + "/card", requestBody.toJson(objectMapper), JsonNode.class);
}
Expand Down Expand Up @@ -218,9 +216,8 @@ private void createQuestionForTable(String tableName, String schema) {
}

public void createQuestionsForSubjectTypes() throws Exception {

String syncStatus = getInitialSyncStatus();
if (!"complete".equals(syncStatus)) {
SyncStatus syncStatus = getInitialSyncStatus();
if (syncStatus != SyncStatus.COMPLETE) {
throw new RuntimeException("Database initial sync is not complete.");
}

Expand All @@ -232,8 +229,8 @@ public void createQuestionsForSubjectTypes() throws Exception {
}

public void createQuestionsForProgramsAndEncounters() throws Exception{
String syncStatus = getInitialSyncStatus();
if (!"complete".equals(syncStatus)) {
SyncStatus syncStatus = getInitialSyncStatus();
if (syncStatus != SyncStatus.COMPLETE) {
throw new RuntimeException("Database initial sync is not complete.");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package org.avni.server.web;
import org.avni.server.domain.metabase.SyncStatus;
import org.avni.server.service.metabase.DatabaseService;
import org.avni.server.domain.accessControl.PrivilegeType;
import org.avni.server.dao.metabase.MetabaseConnector;
Expand Down Expand Up @@ -35,7 +36,7 @@ public void createQuestions() throws Exception{
}

@GetMapping("/sync-status")
public String getSyncStatus() {
public SyncStatus getSyncStatus() {
return databaseService.getInitialSyncStatus();
}

Expand Down

0 comments on commit 94650c8

Please sign in to comment.