Skip to content

Commit

Permalink
avniproject#762 | Fixed question creation implementation for individu…
Browse files Browse the repository at this point in the history
…al tables : Its now using AddressQuestionCreationService
  • Loading branch information
ombhardwajj committed Sep 9, 2024
1 parent 87c122f commit dbef075
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,6 @@ public Database save(Database database) {
return database;
}

public Database getDatabaseById(Database database) {
int id = database.getId();
String url = metabaseApiUrl + "/database/" + id;
try {
String jsonResponse = getForObject(url, String.class);
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(jsonResponse, Database.class);
} catch (Exception e) {
throw new RuntimeException("Failed to retrieve database with ID " + id, e);
}
}

public Database getDatabaseByName(Database database) {
String url = metabaseApiUrl + "/database";

Expand Down Expand Up @@ -78,9 +66,9 @@ public CollectionInfoResponse getCollectionByName(Database database) {
}
}

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);
public void createQuestionForTable(Database database, TableDetails tableDetails, TableDetails addressTableDetails, FieldDetails originField, FieldDetails destinationField) {
FieldDetails joinField1 = getFieldDetailsByName(database, addressTableDetails, originField);
FieldDetails joinField2 = getFieldDetailsByName(database, tableDetails, destinationField);

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

public void createQuestionForIndividualTable(Database database, TableDetails tableDetails) {
MetabaseQuery query = new MetabaseQueryBuilder(database, objectMapper.createArrayNode(), objectMapper)
.forTable(tableDetails)
.build();

MetabaseRequestBody requestBody = new MetabaseRequestBody(
tableDetails.getDisplayName(),
query,
VisualizationType.TABLE,
null,
objectMapper.createObjectNode(),
getCollectionByName(database).getIdAsInt()
);

postForObject(metabaseApiUrl + "/card", requestBody.toJson(objectMapper), JsonNode.class);
}

public FieldDetails getFieldDetailsByName(Database database, TableDetails tableDetails, FieldDetails fieldDetails) {
List<FieldDetails> fieldsList = getFields(database);
String snakeCaseTableName = S.toSnakeCase(tableDetails.getName());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package org.avni.server.service.metabase;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.avni.server.dao.metabase.DatabaseRepository;
import org.avni.server.domain.metabase.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

Expand All @@ -14,43 +11,27 @@ public class AddressQuestionCreationService implements QuestionCreationService {

private final DatabaseService databaseService;
private final DatabaseRepository databaseRepository;
private final ObjectMapper objectMapper;
private final String metabaseApiUrl;

@Autowired
public AddressQuestionCreationService(@Lazy DatabaseService databaseService, DatabaseRepository databaseRepository, ObjectMapper objectMapper, @Value("${metabase.api.url}") String metabaseApiUrl) {
public AddressQuestionCreationService(@Lazy DatabaseService databaseService, DatabaseRepository databaseRepository) {
this.databaseService = databaseService;
this.databaseRepository = databaseRepository;
this.objectMapper = objectMapper;
this.metabaseApiUrl = metabaseApiUrl;
}

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

@Override
public void createQuestionForTable(String tableName, String schema) throws Exception {
public void createQuestionForTable(String tableName, String schema) {
Database database = databaseService.getGlobalDatabase();

TableDetails tableDetails = new TableDetails();
tableDetails.setName(tableName);
TableDetails fetchedTableDetails = databaseRepository.findTableDetailsByName(database, tableDetails);

MetabaseQuery query = new MetabaseQueryBuilder(database, objectMapper.createArrayNode(), objectMapper)
.forTable(fetchedTableDetails)
.build();

MetabaseRequestBody requestBody = new MetabaseRequestBody(
tableName,
query,
VisualizationType.TABLE,
null,
objectMapper.createObjectNode(),
databaseService.getCollectionId()
);
databaseRepository.postForObject(metabaseApiUrl + "/card", requestBody.toJson(objectMapper), JsonNode.class);
databaseRepository.createQuestionForIndividualTable(database, fetchedTableDetails);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package org.avni.server.service.metabase;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.avni.server.dao.metabase.DatabaseRepository;
import org.avni.server.domain.metabase.*;
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;
Expand All @@ -15,22 +12,14 @@
public class DatabaseService {

private final DatabaseRepository databaseRepository;
private final ObjectMapper objectMapper;
private final MetabaseService metabaseService;
private final AddressQuestionCreationService addressQuestionCreationService;

@Value("${metabase.api.url}")
private String metabaseApiUrl;

@Value("${metabase.api.key}")
private String apiKey;

private static final String ADDRESS_TABLE = "Address";

@Autowired
public DatabaseService(DatabaseRepository databaseRepository, ObjectMapper objectMapper, MetabaseService metabaseService, AddressQuestionCreationService addressQuestionCreationService) {
public DatabaseService(DatabaseRepository databaseRepository, MetabaseService metabaseService, AddressQuestionCreationService addressQuestionCreationService) {
this.databaseRepository = databaseRepository;
this.objectMapper = objectMapper;
this.metabaseService = metabaseService;
this.addressQuestionCreationService = addressQuestionCreationService;
}
Expand All @@ -39,8 +28,8 @@ public Database getGlobalDatabase() {
return metabaseService.getGlobalDatabase();
}

public int getCollectionId() {
return metabaseService.getGlobalCollection().getIdAsInt();
public CollectionInfoResponse getGlobalCollection() {
return metabaseService.getGlobalCollection();
}

public SyncStatus getInitialSyncStatus() {
Expand All @@ -57,9 +46,7 @@ private void ensureSyncComplete() {
}

public List<String> getSubjectTypeNames() {
TableDetails metadataTable = new TableDetails("table_metadata");

TableDetails fetchedMetadataTable = databaseRepository.findTableDetailsByName(getGlobalDatabase(), metadataTable);
TableDetails fetchedMetadataTable = databaseRepository.findTableDetailsByName(getGlobalDatabase(), new TableDetails("table_metadata"));

DatasetResponse datasetResponse = databaseRepository.findAll(fetchedMetadataTable, getGlobalDatabase());
List<List<String>> rows = datasetResponse.getData().getRows();
Expand All @@ -80,9 +67,7 @@ public List<String> getSubjectTypeNames() {
}

public List<String> getProgramAndEncounterNames() {
TableDetails metadataTable = new TableDetails("table_metadata");

TableDetails fetchedMetadataTable = databaseRepository.findTableDetailsByName(getGlobalDatabase(), metadataTable);
TableDetails fetchedMetadataTable = databaseRepository.findTableDetailsByName(getGlobalDatabase(), new TableDetails("table_metadata"));

DatasetResponse datasetResponse = databaseRepository.findAll(fetchedMetadataTable, getGlobalDatabase());
List<List<String>> rows = datasetResponse.getData().getRows();
Expand All @@ -100,7 +85,7 @@ public List<String> getProgramAndEncounterNames() {
return programNames;
}

public void createQuestionsForSubjectTypes() throws Exception {
public void createQuestionsForSubjectTypes(){
ensureSyncComplete();
List<String> subjectTypeNames = getSubjectTypeNames();

Expand All @@ -119,12 +104,11 @@ public void createQuestionsForSubjectTypes() throws Exception {
}
}

public void createQuestionsForProgramsAndEncounters() throws Exception {
public void createQuestionsForProgramsAndEncounters(){
ensureSyncComplete();
List<String> programAndEncounterNames = getProgramAndEncounterNames();

TableDetails addressTableDetails = new TableDetails(ADDRESS_TABLE);
TableDetails fetchedAddressTableDetails = databaseRepository.findTableDetailsByName(getGlobalDatabase(), addressTableDetails);
TableDetails fetchedAddressTableDetails = databaseRepository.findTableDetailsByName(getGlobalDatabase(), new TableDetails(ADDRESS_TABLE));

FieldDetails addressFieldDetails = new FieldDetails("id");
FieldDetails programFieldDetails = new FieldDetails("address_id");
Expand All @@ -137,36 +121,17 @@ public void createQuestionsForProgramsAndEncounters() throws Exception {
}
}

public void createQuestionsForIndividualTables() {
public void createQuestionsForIndividualTables(){
ensureSyncComplete();

Database database = getGlobalDatabase();

List<String> individualTables = Arrays.asList("address", "media", "sync_telemetry");

for (String tableName : individualTables) {
TableDetails tableDetails = new TableDetails();
tableDetails.setName(tableName);
TableDetails fetchedTableDetails = databaseRepository.findTableDetailsByName(database, tableDetails);

MetabaseQuery query = new MetabaseQueryBuilder(database, objectMapper.createArrayNode(), objectMapper)
.forTable(fetchedTableDetails)
.build();

MetabaseRequestBody requestBody = new MetabaseRequestBody(
tableName,
query,
VisualizationType.TABLE,
null,
objectMapper.createObjectNode(),
databaseRepository.getCollectionByName(database).getIdAsInt()
);

databaseRepository.postForObject(metabaseApiUrl + "/card", requestBody.toJson(objectMapper), JsonNode.class);
addressQuestionCreationService.createQuestionForTable(tableName, "!public");
}
}

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

createQuestionsForProgramsAndEncounters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class CreateCollectionRequestUtilTest {
public class CollectionUtilTest {
@Test
public void hasOnlyTrailingEmptyStrings() {
assertTrue(CollectionUtil.hasOnlyTrailingEmptyStrings(Arrays.asList("a", "b", "")));
Expand Down

0 comments on commit dbef075

Please sign in to comment.