Skip to content

Commit

Permalink
avniproject#762 | DB and Collection are now fetched from DB if they a…
Browse files Browse the repository at this point in the history
…rent cached
  • Loading branch information
ombhardwajj committed Sep 8, 2024
1 parent 4f382cc commit 87c122f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

@JsonIgnoreProperties(ignoreUnknown = true)
public class FieldDetails {
public FieldDetails() {
}

public FieldDetails(String name) {
this.name = name;
}

@JsonProperty("id")
private int id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

@JsonIgnoreProperties(ignoreUnknown = true)
public class TableDetails {
public TableDetails() {
}

public TableDetails(String name) {
this.name = name;
}

@JsonProperty("description")
private String description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -54,6 +53,4 @@ public void createQuestionForTable(String tableName, String schema) throws Excep
);
databaseRepository.postForObject(metabaseApiUrl + "/card", requestBody.toJson(objectMapper), JsonNode.class);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ 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;

@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;
Expand All @@ -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<String> 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<List<String>> rows = datasetResponse.getData().getRows();

List<String> subjectTypeNames = new ArrayList<>();
Expand All @@ -86,17 +79,12 @@ public List<String> getSubjectTypeNames() {
return subjectTypeNames;
}




public List<String> 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<List<String>> rows = datasetResponse.getData().getRows();

List<String> programNames = new ArrayList<>();
Expand All @@ -112,76 +100,45 @@ public List<String> 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<String> 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<String> 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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 87c122f

Please sign in to comment.