Skip to content

Commit

Permalink
avniproject#762|Question creation for Programs and Encounters with Ad…
Browse files Browse the repository at this point in the history
…dress table automated
  • Loading branch information
ombhardwajj committed Aug 5, 2024
1 parent 55393b5 commit fdd1085
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,48 +92,102 @@ public void createQuestionsForSubjectTypes() {

List<String> subjectTypeNames = getSubjectTypeNames(databaseId);

int addressTableId = getTableIdByName(databaseId, "Address");
int joinFieldId1 = getFieldIdByTableNameAndFieldName(databaseId, "Address", "id");

for (String subjectTypeName : subjectTypeNames) {
int subjectTableId = getTableIdByName(databaseId, subjectTypeName);
int joinFieldId2 = getFieldIdByTableNameAndFieldName(databaseId, subjectTypeName, "address_id");

ObjectNode datasetQuery = objectMapper.createObjectNode();
datasetQuery.put("database", databaseId);
datasetQuery.put("type", "query");

ObjectNode query = objectMapper.createObjectNode();
query.put("source-table", addressTableId);

ArrayNode joins = objectMapper.createArrayNode();
ObjectNode join = objectMapper.createObjectNode();
join.put("fields", "all");
join.put("alias", subjectTypeName);

ArrayNode condition = objectMapper.createArrayNode();
condition.add("=");
condition.add(objectMapper.createArrayNode().add("field").add(joinFieldId1).add(objectMapper.createObjectNode().put("base-type", "type/Integer")));
condition.add(objectMapper.createArrayNode().add("field").add(joinFieldId2).add(objectMapper.createObjectNode().put("base-type", "type/Integer").put("join-alias", subjectTypeName)));

join.set("condition", condition);
join.put("source-table", subjectTableId);
joins.add(join);

query.set("joins", joins);
datasetQuery.set("query", query);

ObjectNode body = objectMapper.createObjectNode();
body.put("name", "Address + " + subjectTypeName);
body.set("dataset_query", datasetQuery);
body.put("display", "table");
body.putNull("description");
body.set("visualization_settings", objectMapper.createObjectNode());
body.put("collection_id", collectionId);
body.putNull("collection_position");
body.putNull("result_metadata");

databaseRepository.postForObject(metabaseApiUrl + "/card", body, JsonNode.class);
createQuestionForTable(databaseId, collectionId, subjectTypeName, "Address", "id", "address_id");
}
}

private List<String> getProgramNamesFromOperationalProgramsTable(int databaseId) {
int operationalProgramsTableId = getTableIdByName(databaseId, "All Operational Programs");

String requestBody = "{\"database\":" + databaseId + ",\"query\":{\"source-table\":" + operationalProgramsTableId + "},\"type\":\"query\",\"parameters\":[]}";
JsonNode response = databaseRepository.getDataset(requestBody);

List<String> programNames = new ArrayList<>();
JsonNode rows = response.path("data").path("rows");
for (JsonNode row : rows) {
String programName = row.get(1).asText(); // Assuming the program name is in the second column
programNames.add(programName);
}
return programNames;
}

private List<String> extractTableNames(JsonNode databaseDetails) {
List<String> tableNames = new ArrayList<>();
JsonNode tablesArray = databaseDetails.path("tables");
for (JsonNode tableNode : tablesArray) {
tableNames.add(tableNode.path("display_name").asText());
}
return tableNames;
}

private void createQuestionForTable(int databaseId, int collectionId, String tableName, String addressTableName, String addressField, String tableField) {
int addressTableId = getTableIdByName(databaseId, addressTableName);
int joinFieldId1 = getFieldIdByTableNameAndFieldName(databaseId, addressTableName, addressField);
int tableId = getTableIdByName(databaseId, tableName);
int joinFieldId2 = getFieldIdByTableNameAndFieldName(databaseId, tableName, tableField);

ObjectNode datasetQuery = objectMapper.createObjectNode();
datasetQuery.put("database", databaseId);
datasetQuery.put("type", "query");

ObjectNode query = objectMapper.createObjectNode();
query.put("source-table", addressTableId);

ArrayNode joins = objectMapper.createArrayNode();
ObjectNode join = objectMapper.createObjectNode();
join.put("fields", "all");
join.put("alias", tableName);

ArrayNode condition = objectMapper.createArrayNode();
condition.add("=");
condition.add(objectMapper.createArrayNode().add("field").add(joinFieldId1).add(objectMapper.createObjectNode().put("base-type", "type/Integer")));
condition.add(objectMapper.createArrayNode().add("field").add(joinFieldId2).add(objectMapper.createObjectNode().put("base-type", "type/Integer").put("join-alias", tableName)));

join.set("condition", condition);
join.put("source-table", tableId);
joins.add(join);

query.set("joins", joins);
datasetQuery.set("query", query);

ObjectNode body = objectMapper.createObjectNode();
body.put("name", "Address + " + tableName);
body.set("dataset_query", datasetQuery);
body.put("display", "table");
body.putNull("description");
body.set("visualization_settings", objectMapper.createObjectNode());
body.put("collection_id", collectionId);
body.putNull("collection_position");
body.putNull("result_metadata");

databaseRepository.postForObject(metabaseApiUrl + "/card", body, JsonNode.class);
}

public void createQuestionsForProgramsAndEncounters() {
int databaseId = metabaseService.getGlobalDatabaseId();
int collectionId = metabaseService.getGlobalCollectionId();

// Ensure sync is complete
String syncStatus = getInitialSyncStatus(databaseId);
if (!"complete".equals(syncStatus)) {
throw new RuntimeException("Database initial sync is not complete.");
}

// Get the list of program names
List<String> programNames = getProgramNamesFromOperationalProgramsTable(databaseId);

// Get all table names in the database
JsonNode databaseDetails = databaseRepository.getDatabaseDetails(databaseId);
List<String> allTableNames = extractTableNames(databaseDetails);

// Identify matching tables for each program and create questions
for (String programName : programNames) {
for (String tableName : allTableNames) {
if (tableName.contains(programName)) {
createQuestionForTable(databaseId, collectionId, tableName, "Address", "id", "address_id");
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ public void setupMetabase() {
@PostMapping("/create-questions")
public void createQuestions() {
databaseService.createQuestionsForSubjectTypes();
databaseService.createQuestionsForProgramsAndEncounters();
}

@GetMapping("/sync-status")
public String getSyncStatus() {
return databaseService.getInitialSyncStatus(metabaseService.getGlobalDatabaseId());
}

}
4 changes: 2 additions & 2 deletions avni-server-api/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ spring.jackson.serialization.fail-on-empty-beans=false
spring.jackson.mapper.accept-case-insensitive-enums=true

# Application
debug=false
debug=true
avni.defaultUserName=${OPENCHS_USER_NAME:admin}
spring.servlet.multipart.max-file-size=10028KB
spring.servlet.multipart.max-request-size=10028KB

# Network
server.tomcat.protocol-header=x-forwarded-proto
spring.security.require-ssl=true
server.port=${OPENCHS_SERVER_PORT:8021}
server.port=${OPENCHS_SERVER_PORT:8080}
server.compression.enabled=true
server.compression.min-response-size=2048
server.compression.mime-types=application/json,application/xml,text/html,text/xml,text/plain,application/hal+json,application/javascript
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
alter table group_privilege add column if not exists impl_version int not null default 1;

update group_privilege
set impl_version = 0
where is_voided = true;

create or replace function check_group_privilege_uniqueness(groupPrivilegeId int, groupId int, privilegeId int, subjectTypeId int, programId int, programEncounterTypeId int, encounterTypeId int, checklistDetailId int, implVersion int) returns boolean
language plpgsql
as
$$
declare
begin
if exists (select gp.*
from public.group_privilege gp
where gp.group_id = groupId
and gp.privilege_id = privilegeId
and (gp.subject_type_id = subjectTypeId or (gp.subject_type_id is null and subjectTypeId is null))
and (gp.program_id = programId or (gp.program_id is null and programId is null))
and (gp.program_encounter_type_id = programEncounterTypeId or (gp.program_encounter_type_id is null and programEncounterTypeId is null))
and (gp.encounter_type_id = encounterTypeId or (gp.encounter_type_id is null and encounterTypeId is null))
and (gp.checklist_detail_id = checklistDetailId or (gp.checklist_detail_id is null and checklistDetailId is null))
and gp.id <> groupPrivilegeId
and gp.impl_version = 1
and implVersion = 1
) then
raise 'Duplicate group privilege exists for: id: %, group_id: %, privilege_id: % subject_type_id: %, program_id: %, program_encounter_type_id: %, encounter_type_id: %, checklist_detail_id: %', groupPrivilegeId, groupId, privilegeId, subjectTypeId, programId, programEncounterTypeId, encounterTypeId, checklistDetailId;
end if;

return true;
end
$$;

alter table group_privilege
add constraint check_group_privilege_unique
check (check_group_privilege_uniqueness(id, group_id, privilege_id, subject_type_id, program_id, program_encounter_type_id, encounter_type_id, checklist_detail_id, impl_version));
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table organisation add column status varchar(255) not null default 'Live';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
UPDATE standard_report_card_type
SET name = 'Recent registrations', description = 'Recent registrations', last_modified_date_time = current_timestamp
WHERE name = 'Last 24 hours registrations';
UPDATE standard_report_card_type
SET name = 'Recent enrolments', description = 'Recent enrolments', last_modified_date_time = current_timestamp
WHERE name = 'Last 24 hours enrolments';
UPDATE standard_report_card_type
SET name = 'Recent visits', description = 'Recent visits', last_modified_date_time = current_timestamp
WHERE name = 'Last 24 hours visits';

0 comments on commit fdd1085

Please sign in to comment.