Skip to content

Commit

Permalink
avniproject#762 | Dataset Request Body Created
Browse files Browse the repository at this point in the history
  • Loading branch information
ombhardwajj committed Sep 8, 2024
1 parent 74ec09d commit 19b66fd
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ public CollectionInfoResponse getCollectionByName(String collectionName) {
}
}

public void createQuestionForTable(Database database, TableDetails tableDetails, TableDetails addressTableDetails, String addressField, String tableField) {
FieldDetails joinField1 = getFieldDetailsByName(database, addressTableDetails.getName(), addressField);
FieldDetails joinField2 = getFieldDetailsByName(database, tableDetails.getName(), tableField);
public void createQuestionForTable(Database database, TableDetails tableDetails, TableDetails addressTableDetails, FieldDetails addressField, FieldDetails tableField) {
FieldDetails joinField1 = getFieldDetailsByName(database, addressTableDetails, addressField);
FieldDetails joinField2 = getFieldDetailsByName(database, tableDetails, tableField);

ArrayNode joinsArray = objectMapper.createArrayNode();
MetabaseQuery query = new MetabaseQueryBuilder(database, joinsArray, objectMapper)
Expand All @@ -101,14 +101,14 @@ public void createQuestionForTable(Database database, TableDetails tableDetails,
postForObject(metabaseApiUrl + "/card", requestBody.toJson(objectMapper), JsonNode.class);
}

public FieldDetails getFieldDetailsByName(Database database, String tableName, String fieldName) {
public FieldDetails getFieldDetailsByName(Database database, TableDetails tableDetails, FieldDetails fieldDetails) {
List<FieldDetails> fieldsList = getFields(database);
String snakeCaseTableName = S.toSnakeCase(tableName);
String snakeCaseTableName = S.toSnakeCase(tableDetails.getName());

return fieldsList.stream()
.filter(field -> snakeCaseTableName.equals(field.getTableName()) && fieldName.equals(field.getName()))
.filter(field -> snakeCaseTableName.equals(field.getTableName()) && fieldDetails.getName().equals(field.getName()))
.findFirst()
.orElseThrow(() -> new RuntimeException("Field " + fieldName + " not found in table " + tableName));
.orElseThrow(() -> new RuntimeException("Field " + fieldDetails.getName() + " not found in table " + tableDetails.getName()));
}

public MetabaseDatabaseInfo getDatabaseDetails(Database database) {
Expand Down Expand Up @@ -166,7 +166,8 @@ public DatasetResponse findAll(TableDetails table, Database database) {
return getDataset(requestBody);
}

private String createRequestBodyForDataset(Database database,TableDetails table) {
return "{\"database\":" + database.getId() + ",\"query\":{\"source-table\":" + table.getId() + "},\"type\":\"query\",\"parameters\":[]}";
private String createRequestBodyForDataset(Database database, TableDetails table) {
DatasetRequestBody requestBody = new DatasetRequestBody(database, table);
return requestBody.toJson(objectMapper).toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.avni.server.domain.metabase;

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

public class DatasetRequestBody {

private final Database database;
private final TableDetails table;

public DatasetRequestBody(Database database, TableDetails table) {
this.database = database;
this.table = table;
}

public ObjectNode toJson(ObjectMapper objectMapper) {
ObjectNode rootNode = objectMapper.createObjectNode();
rootNode.put("database", database.getId());

ObjectNode queryNode = objectMapper.createObjectNode();
queryNode.put("source-table", table.getId());

rootNode.set("query", queryNode);
rootNode.put("type", "query");
rootNode.set("parameters", objectMapper.createArrayNode());

return rootNode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,17 @@ public class FieldDetails {
@JsonProperty("id")
private int id;

public void setId(int id) {
this.id = id;
}

@JsonProperty("name")
private String name;

public void setName(String name) {
this.name = name;
}

@JsonProperty("display_name")
private String displayName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public AddressQuestionCreationService(@Lazy DatabaseService databaseService, Dat
}

@Override
public void createQuestionForTable(TableDetails tableDetails, TableDetails addressTableDetails, String addressField, String tableField) throws Exception {
public void createQuestionForTable(TableDetails tableDetails, TableDetails addressTableDetails, FieldDetails addressFieldDetails, FieldDetails tableFieldDetails) throws Exception {
Database database = databaseService.getGlobalDatabase();
databaseRepository.createQuestionForTable(database, tableDetails, addressTableDetails, addressField, tableField);
databaseRepository.createQuestionForTable(database, tableDetails, addressTableDetails, addressFieldDetails, tableFieldDetails);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,19 @@ public void createQuestionsForSubjectTypes() throws Exception {

TableDetails fetchedAddressTableDetails = databaseRepository.findTableDetailsByName(database, addressTableDetails);

FieldDetails addressFieldDetails = new FieldDetails();
addressFieldDetails.setName("id");

FieldDetails subjectFieldDetails = new FieldDetails();
subjectFieldDetails.setName("address_id");

for (String subjectTypeName : subjectTypeNames) {
TableDetails subjectTableDetails = new TableDetails();
subjectTableDetails.setName(subjectTypeName);

TableDetails fetchedSubjectTableDetails = databaseRepository.findTableDetailsByName(database, subjectTableDetails);

addressQuestionCreationService.createQuestionForTable(fetchedSubjectTableDetails, fetchedAddressTableDetails, "id", "address_id");
addressQuestionCreationService.createQuestionForTable(fetchedSubjectTableDetails, fetchedAddressTableDetails, addressFieldDetails, subjectFieldDetails);
}
}

Expand All @@ -153,13 +159,19 @@ public void createQuestionsForProgramsAndEncounters() throws Exception {

TableDetails fetchedAddressTableDetails = databaseRepository.findTableDetailsByName(database, addressTableDetails);

FieldDetails addressFieldDetails = new FieldDetails();
addressFieldDetails.setName("id");

FieldDetails programFieldDetails = new FieldDetails();
programFieldDetails.setName("address_id");

for (String programName : programAndEncounterNames) {
TableDetails programTableDetails = new TableDetails();
programTableDetails.setName(programName);

TableDetails fetchedProgramTableDetails = databaseRepository.findTableDetailsByName(database, programTableDetails);

addressQuestionCreationService.createQuestionForTable(fetchedProgramTableDetails, fetchedAddressTableDetails, "id", "address_id");
addressQuestionCreationService.createQuestionForTable(fetchedProgramTableDetails, fetchedAddressTableDetails, addressFieldDetails, programFieldDetails);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.avni.server.service.metabase;
import org.avni.server.domain.metabase.FieldDetails;
import org.avni.server.domain.metabase.TableDetails;

public interface QuestionCreationService {
void createQuestionForTable(TableDetails tableDetails, TableDetails addressTableDetails, String addressField, String tableField) throws Exception;
void createQuestionForTable(TableDetails tableDetails, TableDetails addressTableDetails, FieldDetails addressFieldDetails, FieldDetails tableFieldDetails) throws Exception;
void createQuestionForTable(String tableName, String schema) throws Exception;
}

0 comments on commit 19b66fd

Please sign in to comment.