forked from avniproject/avni-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
avniproject#762 | A working API has been successfully built to automa…
…te the creation of questions for the tables subject types and Address
- Loading branch information
1 parent
813146b
commit 25dbade
Showing
6 changed files
with
220 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
avni-server-api/src/main/java/org/avni/server/service/DatabaseService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package org.avni.server.service; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import org.avni.server.dao.metabase.DatabaseRepository; | ||
import org.avni.server.util.StringUtils; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@Service | ||
public class DatabaseService { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
private final DatabaseRepository databaseRepository; | ||
private final ObjectMapper objectMapper; | ||
private final MetabaseService metabaseService; | ||
|
||
@Value("${metabase.api.url}") | ||
private String metabaseApiUrl; | ||
|
||
@Value("${metabase.api.key}") | ||
private String apiKey; | ||
|
||
@Autowired | ||
public DatabaseService(DatabaseRepository databaseRepository, ObjectMapper objectMapper, MetabaseService metabaseService) { | ||
this.databaseRepository = databaseRepository; | ||
this.objectMapper = objectMapper; | ||
this.metabaseService = metabaseService; | ||
} | ||
|
||
public int getTableIdByName(int databaseId, String tableName) { | ||
JsonNode rootNode = databaseRepository.getDatabaseDetails(databaseId); | ||
JsonNode tablesArray = rootNode.path("tables"); | ||
for (JsonNode tableNode : tablesArray) { | ||
if (tableName.equals(tableNode.path("display_name").asText())) { | ||
return tableNode.path("id").asInt(); | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
public int getFieldIdByTableNameAndFieldName(int databaseId, String tableName, String fieldName) { | ||
JsonNode fieldsArray = databaseRepository.getFields(databaseId); | ||
String snakeCaseTableName = StringUtils.toSnakeCase(tableName); | ||
for (JsonNode fieldNode : fieldsArray) { | ||
if (snakeCaseTableName.equals(fieldNode.path("table_name").asText()) && fieldName.equals(fieldNode.path("name").asText())) { | ||
return fieldNode.path("id").asInt(); | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
public void waitForSyncCompletion(int databaseId) { | ||
while (true) { | ||
String syncStatus = getInitialSyncStatus(databaseId); | ||
if ("complete".equals(syncStatus)) { | ||
return; | ||
} | ||
try { | ||
TimeUnit.SECONDS.sleep(5); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw new RuntimeException("Thread was interrupted while waiting for sync completion", e); | ||
} | ||
} | ||
} | ||
|
||
public String getInitialSyncStatus(int databaseId) { | ||
JsonNode responseBody = databaseRepository.getInitialSyncStatus(databaseId); | ||
return responseBody.path("initial_sync_status").asText(); | ||
} | ||
|
||
public List<String> getSubjectTypeNames(int databaseId) { | ||
int tableId = getTableIdByName(databaseId, "Subject Type"); | ||
String requestBody = "{\"database\":" + databaseId + ",\"query\":{\"source-table\":" + tableId + "},\"type\":\"query\",\"parameters\":[]}"; | ||
|
||
JsonNode response = databaseRepository.getDataset(requestBody); | ||
|
||
JsonNode dataNode = response.path("data"); | ||
JsonNode rows = dataNode.path("rows"); | ||
|
||
List<String> subjectTypeNames = new ArrayList<>(); | ||
for (JsonNode row : rows) { | ||
String name = row.get(4).asText(); | ||
boolean isVoided = row.get(6).asBoolean(); | ||
if (!isVoided) { | ||
subjectTypeNames.add(name); | ||
} | ||
} | ||
return subjectTypeNames; | ||
} | ||
|
||
public void createQuestionsForSubjectTypes() { | ||
int databaseId = metabaseService.getGlobalDatabaseId(); | ||
int collectionId = metabaseService.getGlobalCollectionId(); | ||
|
||
waitForSyncCompletion(databaseId); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
vinayvenu
|
||
|
||
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
avni-server-api/src/main/java/org/avni/server/util/StringUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.avni.server.util; | ||
|
||
public class StringUtils { | ||
public static String toSnakeCase(String input) { | ||
if (input == null) { | ||
return null; | ||
} | ||
return input.trim().replaceAll(" +", "_").toLowerCase(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This feels a bit Generic. Maybe call it MetabaseDatabaseService, or put all these services under a metabase package?