Skip to content

Commit

Permalink
avniproject#762 | Added FieldAttributes Enum , used it in MQB , Renam…
Browse files Browse the repository at this point in the history
…ed createQuestionForIndividualTable method , Deleted MetabaseJoin
  • Loading branch information
ombhardwajj committed Sep 10, 2024
1 parent dbef075 commit 381d921
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void createQuestionForTable(Database database, TableDetails tableDetails,
postForObject(metabaseApiUrl + "/card", requestBody.toJson(objectMapper), JsonNode.class);
}

public void createQuestionForIndividualTable(Database database, TableDetails tableDetails) {
public void createQuestionForASingleTable(Database database, TableDetails tableDetails) {
MetabaseQuery query = new MetabaseQueryBuilder(database, objectMapper.createArrayNode(), objectMapper)
.forTable(tableDetails)
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.avni.server.domain.metabase;

//Refer Documentation : https://www.metabase.com/docs/latest/api/card#post-apicard
public enum CardType {
QUESTION("question");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.avni.server.domain.metabase;

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

public enum FieldAttribute {
FIELD("field"),
FIELDS("fields"),
SOURCE_TABLE("source-table"),
ALIAS("alias"),
CONDITION("condition"),
JOINS("joins"),
JOIN_ALIAS("join-alias"),
BASE_TYPE("base-type");

private final String attributeName;

FieldAttribute(String attributeName) {
this.attributeName = attributeName;
}

public String getAttributeName() {
return attributeName;
}

public ObjectNode toJson(ObjectMapper objectMapper, Object value) {
ObjectNode attributeNode = objectMapper.createObjectNode();
attributeNode.put(attributeName, value.toString());
return attributeNode;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package org.avni.server.domain.metabase;

public enum BaseType {
// Refer Documentation here : https://www.metabase.com/docs/latest/data-modeling/field-types

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

private final String typeName;

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

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,34 @@ public MetabaseQueryBuilder(Database database, ArrayNode joinsArray, ObjectMappe
}

public MetabaseQueryBuilder forTable(TableDetails tableDetails) {
queryNode.put("source-table", tableDetails.getId());
queryNode.put(FieldAttribute.SOURCE_TABLE.getAttributeName(), tableDetails.getId());
return this;
}

public MetabaseQueryBuilder joinWith(TableDetails addressTable, FieldDetails joinField1, FieldDetails joinField2) {
ObjectNode joinNode = objectMapper.createObjectNode();
joinNode.put("fields", "all");
joinNode.put("alias", addressTable.getName());
joinNode.put("source-table", addressTable.getId());
joinNode.put(FieldAttribute.FIELDS.getAttributeName(), "all");
joinNode.put(FieldAttribute.ALIAS.getAttributeName(), addressTable.getName());

ArrayNode conditionArray = objectMapper.createArrayNode();
conditionArray.add("=");
conditionArray.add(ConditionType.EQUAL.getOperator());

ArrayNode leftField = objectMapper.createArrayNode();
leftField.add("field");
leftField.add(FieldAttribute.FIELD.getAttributeName());
leftField.add(joinField2.getId());
leftField.add(objectMapper.createObjectNode().put("base-type", "type/Integer"));
leftField.add(objectMapper.createObjectNode().put(FieldAttribute.BASE_TYPE.getAttributeName(), FieldType.INTEGER.getTypeName()));
conditionArray.add(leftField);

ArrayNode rightField = objectMapper.createArrayNode();
rightField.add("field");
rightField.add(FieldAttribute.FIELD.getAttributeName());
rightField.add(joinField1.getId());
rightField.add(objectMapper.createObjectNode().put("base-type", "type/Integer").put("join-alias", addressTable.getName()));
rightField.add(objectMapper.createObjectNode().put(FieldAttribute.BASE_TYPE.getAttributeName(), FieldType.INTEGER.getTypeName()).put(FieldAttribute.JOIN_ALIAS.getAttributeName(), addressTable.getName()));
conditionArray.add(rightField);

joinNode.set("condition", conditionArray);
joinNode.set(FieldAttribute.CONDITION.getAttributeName(), conditionArray);
joinNode.put(FieldAttribute.SOURCE_TABLE.getAttributeName(), addressTable.getId());
joinsArray.add(joinNode);
queryNode.set("joins", joinsArray);
queryNode.set(FieldAttribute.JOINS.getAttributeName(), joinsArray);

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

// Refer docs : https://www.metabase.com/docs/latest/api/card#post-apicard
public class MetabaseRequestBody {
private String name;
private MetabaseQuery datasetQuery;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.avni.server.domain.metabase;

//Refer documentation : https://www.metabase.com/docs/latest/questions/sharing/visualizing-results
public enum VisualizationType {
TABLE("table"),
CHART("chart");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ public void createQuestionForTable(String tableName, String schema) {
tableDetails.setName(tableName);
TableDetails fetchedTableDetails = databaseRepository.findTableDetailsByName(database, tableDetails);

databaseRepository.createQuestionForIndividualTable(database, fetchedTableDetails);
databaseRepository.createQuestionForASingleTable(database, fetchedTableDetails);
}
}

0 comments on commit 381d921

Please sign in to comment.