diff --git a/avni-server-api/src/main/java/org/avni/server/dao/metabase/DatabaseRepository.java b/avni-server-api/src/main/java/org/avni/server/dao/metabase/DatabaseRepository.java index 066860a6e..d8cd74566 100644 --- a/avni-server-api/src/main/java/org/avni/server/dao/metabase/DatabaseRepository.java +++ b/avni-server-api/src/main/java/org/avni/server/dao/metabase/DatabaseRepository.java @@ -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"; @@ -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) @@ -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 fieldsList = getFields(database); String snakeCaseTableName = S.toSnakeCase(tableDetails.getName()); diff --git a/avni-server-api/src/main/java/org/avni/server/service/metabase/AddressQuestionCreationService.java b/avni-server-api/src/main/java/org/avni/server/service/metabase/AddressQuestionCreationService.java index 306052614..676b3192c 100644 --- a/avni-server-api/src/main/java/org/avni/server/service/metabase/AddressQuestionCreationService.java +++ b/avni-server-api/src/main/java/org/avni/server/service/metabase/AddressQuestionCreationService.java @@ -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; @@ -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); } } diff --git a/avni-server-api/src/main/java/org/avni/server/service/metabase/DatabaseService.java b/avni-server-api/src/main/java/org/avni/server/service/metabase/DatabaseService.java index f19dfdf00..784936d15 100644 --- a/avni-server-api/src/main/java/org/avni/server/service/metabase/DatabaseService.java +++ b/avni-server-api/src/main/java/org/avni/server/service/metabase/DatabaseService.java @@ -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; @@ -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; } @@ -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() { @@ -57,9 +46,7 @@ private void ensureSyncComplete() { } public List 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> rows = datasetResponse.getData().getRows(); @@ -80,9 +67,7 @@ public List getSubjectTypeNames() { } public List 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> rows = datasetResponse.getData().getRows(); @@ -100,7 +85,7 @@ public List getProgramAndEncounterNames() { return programNames; } - public void createQuestionsForSubjectTypes() throws Exception { + public void createQuestionsForSubjectTypes(){ ensureSyncComplete(); List subjectTypeNames = getSubjectTypeNames(); @@ -119,12 +104,11 @@ public void createQuestionsForSubjectTypes() throws Exception { } } - public void createQuestionsForProgramsAndEncounters() throws Exception { + public void createQuestionsForProgramsAndEncounters(){ ensureSyncComplete(); List 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"); @@ -137,36 +121,17 @@ public void createQuestionsForProgramsAndEncounters() throws Exception { } } - public void createQuestionsForIndividualTables() { + public void createQuestionsForIndividualTables(){ ensureSyncComplete(); - Database database = getGlobalDatabase(); - List 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(); diff --git a/avni-server-api/src/test/java/org/avni/server/util/CreateCollectionRequestUtilTest.java b/avni-server-api/src/test/java/org/avni/server/util/CollectionUtilTest.java similarity index 93% rename from avni-server-api/src/test/java/org/avni/server/util/CreateCollectionRequestUtilTest.java rename to avni-server-api/src/test/java/org/avni/server/util/CollectionUtilTest.java index b0928ecb8..d6219aa2f 100644 --- a/avni-server-api/src/test/java/org/avni/server/util/CreateCollectionRequestUtilTest.java +++ b/avni-server-api/src/test/java/org/avni/server/util/CollectionUtilTest.java @@ -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", "")));