From 87c122f042092421fd716a86aa15e1f24b58a2bb Mon Sep 17 00:00:00 2001 From: Om Bhardwaj <115864495+ombhardwajj@users.noreply.github.com> Date: Sun, 8 Sep 2024 23:14:21 +0530 Subject: [PATCH] avniproject#762 | DB and Collection are now fetched from DB if they arent cached --- .../avni/server/domain/metabase/Database.java | 11 +- .../server/domain/metabase/FieldDetails.java | 6 + .../server/domain/metabase/TableDetails.java | 6 + .../AddressQuestionCreationService.java | 3 - .../service/metabase/DatabaseService.java | 105 ++++++------------ .../service/metabase/MetabaseService.java | 20 +--- 6 files changed, 57 insertions(+), 94 deletions(-) diff --git a/avni-server-api/src/main/java/org/avni/server/domain/metabase/Database.java b/avni-server-api/src/main/java/org/avni/server/domain/metabase/Database.java index 009f07762..21d388917 100644 --- a/avni-server-api/src/main/java/org/avni/server/domain/metabase/Database.java +++ b/avni-server-api/src/main/java/org/avni/server/domain/metabase/Database.java @@ -8,16 +8,21 @@ public class Database { private String name; private String engine; private DatabaseDetails details; - public Database() { + + } + + public Database(String name) { + this.name = name; } - + public Database(String name, String engine, DatabaseDetails details) { this(null,name,engine,details); } - public Database(Integer id,String name, String engine, DatabaseDetails details) { + + public Database(Integer id, String name, String engine, DatabaseDetails details) { this.id=id; this.name = name; this.engine = engine; diff --git a/avni-server-api/src/main/java/org/avni/server/domain/metabase/FieldDetails.java b/avni-server-api/src/main/java/org/avni/server/domain/metabase/FieldDetails.java index 5b723307b..9ae172d77 100644 --- a/avni-server-api/src/main/java/org/avni/server/domain/metabase/FieldDetails.java +++ b/avni-server-api/src/main/java/org/avni/server/domain/metabase/FieldDetails.java @@ -5,6 +5,12 @@ @JsonIgnoreProperties(ignoreUnknown = true) public class FieldDetails { + public FieldDetails() { + } + + public FieldDetails(String name) { + this.name = name; + } @JsonProperty("id") private int id; diff --git a/avni-server-api/src/main/java/org/avni/server/domain/metabase/TableDetails.java b/avni-server-api/src/main/java/org/avni/server/domain/metabase/TableDetails.java index f2178fbc3..7bc0965fb 100644 --- a/avni-server-api/src/main/java/org/avni/server/domain/metabase/TableDetails.java +++ b/avni-server-api/src/main/java/org/avni/server/domain/metabase/TableDetails.java @@ -5,6 +5,12 @@ @JsonIgnoreProperties(ignoreUnknown = true) public class TableDetails { + public TableDetails() { + } + + public TableDetails(String name) { + this.name = name; + } @JsonProperty("description") private String description; 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 b88db3481..306052614 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 @@ -31,7 +31,6 @@ public void createQuestionForTable(TableDetails tableDetails, TableDetails addre databaseRepository.createQuestionForTable(database, tableDetails, addressTableDetails, addressFieldDetails, tableFieldDetails); } - @Override public void createQuestionForTable(String tableName, String schema) throws Exception { Database database = databaseService.getGlobalDatabase(); @@ -54,6 +53,4 @@ public void createQuestionForTable(String tableName, String schema) throws Excep ); databaseRepository.postForObject(metabaseApiUrl + "/card", requestBody.toJson(objectMapper), JsonNode.class); } - - } 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 20f9e2e63..f19dfdf00 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 @@ -18,8 +18,6 @@ public class DatabaseService { private final ObjectMapper objectMapper; private final MetabaseService metabaseService; private final AddressQuestionCreationService addressQuestionCreationService; - private Integer databaseId; - private Integer collectionId; @Value("${metabase.api.url}") private String metabaseApiUrl; @@ -27,6 +25,8 @@ public class DatabaseService { @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) { this.databaseRepository = databaseRepository; @@ -36,39 +36,32 @@ public DatabaseService(DatabaseRepository databaseRepository, ObjectMapper objec } public Database getGlobalDatabase() { - Database globalDatabase = metabaseService.getGlobalDatabase(); - return databaseRepository.getDatabaseById(globalDatabase); - } - - public int getDatabaseId() { - if (databaseId == null) { - databaseId = metabaseService.getGlobalDatabaseId(); - } - return databaseId; + return metabaseService.getGlobalDatabase(); } public int getCollectionId() { - if (collectionId == null) { - collectionId = metabaseService.getGlobalCollectionId(); - } - return collectionId; + return metabaseService.getGlobalCollection().getIdAsInt(); } public SyncStatus getInitialSyncStatus() { - Database globalDatabase = metabaseService.getGlobalDatabase(); - DatabaseSyncStatus databaseSyncStatus = databaseRepository.getInitialSyncStatus(globalDatabase); + DatabaseSyncStatus databaseSyncStatus = databaseRepository.getInitialSyncStatus(getGlobalDatabase()); String status = databaseSyncStatus.getInitialSyncStatus(); return SyncStatus.fromString(status); } + private void ensureSyncComplete() { + SyncStatus syncStatus = getInitialSyncStatus(); + if (syncStatus != SyncStatus.COMPLETE) { + throw new RuntimeException("Database sync is not complete. Cannot create questions."); + } + } + public List getSubjectTypeNames() { - Database database = getGlobalDatabase(); - TableDetails metadataTable = new TableDetails(); - metadataTable.setName("table_metadata"); + TableDetails metadataTable = new TableDetails("table_metadata"); - TableDetails fetchedMetadataTable = databaseRepository.findTableDetailsByName(database, metadataTable); + TableDetails fetchedMetadataTable = databaseRepository.findTableDetailsByName(getGlobalDatabase(), metadataTable); - DatasetResponse datasetResponse = databaseRepository.findAll(fetchedMetadataTable, database); + DatasetResponse datasetResponse = databaseRepository.findAll(fetchedMetadataTable, getGlobalDatabase()); List> rows = datasetResponse.getData().getRows(); List subjectTypeNames = new ArrayList<>(); @@ -86,17 +79,12 @@ public List getSubjectTypeNames() { return subjectTypeNames; } - - - public List getProgramAndEncounterNames() { - Database database = getGlobalDatabase(); - TableDetails metadataTable = new TableDetails(); - metadataTable.setName("table_metadata"); + TableDetails metadataTable = new TableDetails("table_metadata"); - TableDetails fetchedMetadataTable = databaseRepository.findTableDetailsByName(database, metadataTable); + TableDetails fetchedMetadataTable = databaseRepository.findTableDetailsByName(getGlobalDatabase(), metadataTable); - DatasetResponse datasetResponse = databaseRepository.findAll(fetchedMetadataTable, database); + DatasetResponse datasetResponse = databaseRepository.findAll(fetchedMetadataTable, getGlobalDatabase()); List> rows = datasetResponse.getData().getRows(); List programNames = new ArrayList<>(); @@ -112,76 +100,45 @@ public List getProgramAndEncounterNames() { return programNames; } - public void createQuestionsForSubjectTypes() throws Exception { - SyncStatus syncStatus = getInitialSyncStatus(); - if (syncStatus != SyncStatus.COMPLETE) { - throw new RuntimeException("Database sync is not complete. Cannot create questions."); - } - - Database database = getGlobalDatabase(); + ensureSyncComplete(); List subjectTypeNames = getSubjectTypeNames(); - TableDetails addressTableDetails = new TableDetails(); - addressTableDetails.setName("Address"); - - TableDetails fetchedAddressTableDetails = databaseRepository.findTableDetailsByName(database, addressTableDetails); + TableDetails addressTableDetails = new TableDetails(ADDRESS_TABLE); + TableDetails fetchedAddressTableDetails = databaseRepository.findTableDetailsByName(getGlobalDatabase(), addressTableDetails); - FieldDetails addressFieldDetails = new FieldDetails(); - addressFieldDetails.setName("id"); + FieldDetails addressFieldDetails = new FieldDetails("id"); + FieldDetails subjectFieldDetails = new FieldDetails("address_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); - + TableDetails fetchedSubjectTableDetails = databaseRepository.findTableDetailsByName(getGlobalDatabase(), subjectTableDetails); addressQuestionCreationService.createQuestionForTable(fetchedSubjectTableDetails, fetchedAddressTableDetails, addressFieldDetails, subjectFieldDetails); } } - - public void createQuestionsForProgramsAndEncounters() throws Exception { - SyncStatus syncStatus = getInitialSyncStatus(); - if (syncStatus != SyncStatus.COMPLETE) { - throw new RuntimeException("Database sync is not complete. Cannot create questions."); - } - - Database database = getGlobalDatabase(); + ensureSyncComplete(); List programAndEncounterNames = getProgramAndEncounterNames(); - TableDetails addressTableDetails = new TableDetails(); - addressTableDetails.setName("Address"); - - TableDetails fetchedAddressTableDetails = databaseRepository.findTableDetailsByName(database, addressTableDetails); - - FieldDetails addressFieldDetails = new FieldDetails(); - addressFieldDetails.setName("id"); + TableDetails addressTableDetails = new TableDetails(ADDRESS_TABLE); + TableDetails fetchedAddressTableDetails = databaseRepository.findTableDetailsByName(getGlobalDatabase(), addressTableDetails); - FieldDetails programFieldDetails = new FieldDetails(); - programFieldDetails.setName("address_id"); + FieldDetails addressFieldDetails = new FieldDetails("id"); + FieldDetails programFieldDetails = new FieldDetails("address_id"); for (String programName : programAndEncounterNames) { TableDetails programTableDetails = new TableDetails(); programTableDetails.setName(programName); - - TableDetails fetchedProgramTableDetails = databaseRepository.findTableDetailsByName(database, programTableDetails); - + TableDetails fetchedProgramTableDetails = databaseRepository.findTableDetailsByName(getGlobalDatabase(), programTableDetails); addressQuestionCreationService.createQuestionForTable(fetchedProgramTableDetails, fetchedAddressTableDetails, addressFieldDetails, programFieldDetails); } } - - public void createQuestionsForIndividualTables() { - SyncStatus syncStatus = getInitialSyncStatus(); - if (syncStatus != SyncStatus.COMPLETE) { - throw new RuntimeException("Database sync is not complete. Cannot create questions."); - } + ensureSyncComplete(); Database database = getGlobalDatabase(); diff --git a/avni-server-api/src/main/java/org/avni/server/service/metabase/MetabaseService.java b/avni-server-api/src/main/java/org/avni/server/service/metabase/MetabaseService.java index 429e7ad3c..050ee873d 100644 --- a/avni-server-api/src/main/java/org/avni/server/service/metabase/MetabaseService.java +++ b/avni-server-api/src/main/java/org/avni/server/service/metabase/MetabaseService.java @@ -64,27 +64,19 @@ public void setupMetabase() { collectionPermissionsRepository.updateCollectionPermissions(collectionPermissions, metabaseGroup.getId(), metabaseCollection.getId()); } - public int getGlobalDatabaseId() { + public Database getGlobalDatabase() { if (globalDatabase == null) { Organisation currentOrganisation = organisationService.getCurrentOrganisation(); - Database database = new Database(); - database.setName(currentOrganisation.getName()); - globalDatabase = databaseRepository.getDatabaseByName(database); + globalDatabase = databaseRepository.getDatabaseByName(new Database(currentOrganisation.getName())); } - return globalDatabase.getId(); + return globalDatabase; } - public int getGlobalCollectionId() { + public CollectionInfoResponse getGlobalCollection() { if (globalCollection == null) { Organisation currentOrganisation = organisationService.getCurrentOrganisation(); - Database database = new Database(); - database.setName(currentOrganisation.getName()); - globalCollection = databaseRepository.getCollectionByName(database); + globalCollection = databaseRepository.getCollectionByName(new Database(currentOrganisation.getName())); } - return globalCollection.getIdAsInt(); - } - - public Database getGlobalDatabase() { - return globalDatabase; + return globalCollection; } }