Skip to content

Commit

Permalink
#837 | Added a metabase dashboard and advanced questions in it
Browse files Browse the repository at this point in the history
  • Loading branch information
ombhardwajj committed Jan 13, 2025
1 parent 568a05d commit 52a1f6d
Show file tree
Hide file tree
Showing 13 changed files with 353 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void createAdvancedQuestion(Database database) {
.withVisualization(VisualizationType.PIE);
MetabaseQuery query = createAdvancedQuery("individual", "subject_type", config, database);
postQuestion(
"Pie Chart : Count of Non Voided Individuals - Non Voided Subject Type",
QuestionName.QUESTION_1.getQuestionName(),
query,
config,
getCollectionForDatabase(database).getIdAsInt()
Expand All @@ -174,7 +174,7 @@ public void createAdvancedQuestion2(Database database) {
.withVisualization(VisualizationType.PIE);
MetabaseQuery query = createAdvancedQuery("program_enrolment", "program", config, database);
postQuestion(
"Pie Chart : Number of Non exited and Non voided Enrollments for Non Voided Program ",
QuestionName.QUESTION_2.getQuestionName(),
query,
config,
getCollectionForDatabase(database).getIdAsInt()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.avni.server.dao.metabase;

import org.avni.server.domain.metabase.GroupPermissionsBody;
import org.avni.server.util.ObjectMapperSingleton;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
Expand Down Expand Up @@ -38,9 +39,14 @@ protected <T> HttpEntity<T> createHttpEntity(T body) {
return new HttpEntity<>(body, headers);
}

protected void sendPutRequest(String url, Map<String, ?> requestBody) {
HttpEntity<Map<String, ?>> entity = createHttpEntity(requestBody);
restTemplate.exchange(url, HttpMethod.PUT, entity, Map.class);
protected void sendPutRequest(String url, Object requestBody) {
try {
String jsonBody = ObjectMapperSingleton.getObjectMapper().writeValueAsString(requestBody);
HttpEntity<String> entity = createHttpEntity(jsonBody);
restTemplate.exchange(url, HttpMethod.PUT, entity, String.class);
} catch (Exception e) {
throw new RuntimeException("Error serializing request body to JSON", e);
}
}

public <T> T postForObject(String url, Object request, Class<T> responseType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.avni.server.dao.metabase;

import org.avni.server.domain.metabase.*;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public class MetabaseDashboardRepository extends MetabaseConnector {
private final DatabaseRepository databaseRepository;

public MetabaseDashboardRepository(RestTemplateBuilder restTemplateBuilder , DatabaseRepository databaseRepository) {
super(restTemplateBuilder);
this.databaseRepository = databaseRepository;
}

public Dashboard save(CreateDashboardRequest createDashboardRequest) {
String url = metabaseApiUrl + "/dashboard";
return postForObject(url, createDashboardRequest, Dashboard.class);
}


public CollectionItem getDashboardByName(CollectionInfoResponse globalCollection) {
List<CollectionItem> items = databaseRepository.getExistingCollectionItems(globalCollection.getIdAsInt());

return items.stream()
.filter(item -> item.getName().equals(globalCollection.getName()))
.findFirst()
.orElse(null);
}

public void updateDashboard(int dashboardId, DashboardUpdateRequest request) {
String url = metabaseApiUrl + "/dashboard/" + dashboardId;
sendPutRequest(url, request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@

@JsonIgnoreProperties(ignoreUnknown = true)
public class CollectionItem {

private String name;

public CollectionItem() {
}

public CollectionItem(String name, int id) {
this.name = name;
this.id = id;
}

private int id;

public String getName() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.avni.server.domain.metabase;

public class CreateDashboardRequest {
private final String name;
private final String description;
private final Integer collection_id;

public CreateDashboardRequest(String name, String description, Integer collection_id) {
this.name = name;
this.description = description;
this.collection_id = collection_id;
}

public String getName() {
return name;
}

public String getDescription() {
return description;
}

public Integer getCollection_id() {
return collection_id;
}

@Override
public String toString() {
return "{" +
"name='" + name + '\'' +
", description='" + description + '\'' +
", collectionId=" + collection_id +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.avni.server.domain.metabase;

import org.springframework.stereotype.Component;

@Component
public class Dashboard {
private int id;
private String name;

public int getId() {
return id;
}

public String getName() {
return name;
}

@Override
public String toString() {
return "Dashboard{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.avni.server.domain.metabase;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties(ignoreUnknown = true)
public class DashboardResponse {

private String name;
private String id;

public DashboardResponse() {
}

public DashboardResponse(@JsonProperty("name") String name,
@JsonProperty("id") Object id) {
this.name = name;

if (id instanceof Integer) {
this.id = String.valueOf(id);
} else {
this.id = id.toString();
}
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getId() {
return id;
}

public int getIdAsInt() {
try {
return Integer.parseInt(id);
} catch (NumberFormatException e) {
throw new RuntimeException("Failed to convert id to integer: " + id, e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.avni.server.domain.metabase;

import java.util.List;

public class DashboardUpdateRequest {
private final List<Dashcard> dashcards;

public DashboardUpdateRequest(List<Dashcard> dashcards) {
this.dashcards = dashcards;
}


public List<Dashcard> getDashcards() {
return dashcards;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.avni.server.domain.metabase;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Collections;
import java.util.List;

public class Dashcard {
@JsonProperty("id")
private int dashboardId;

@JsonProperty("card_id")
private int cardId;

@JsonProperty("dashboard_tab_id")
private Integer dashboardTabId;

private int row;
private int col;

@JsonProperty("size_x")
private int sizeX;

@JsonProperty("size_y")
private int sizeY;

@JsonProperty("visualization_settings")
private Object visualizationSettings;

@JsonProperty("parameter_mappings")
private List<Object> parameterMappings;

public Dashcard(int dashboardId, int cardId, Integer dashboardTabId, int row, int col, int sizeX, int sizeY) {
this.dashboardId = dashboardId;
this.cardId = cardId;
this.dashboardTabId = dashboardTabId;
this.row = row;
this.col = col;
this.sizeX = sizeX;
this.sizeY = sizeY;
this.visualizationSettings = Collections.emptyMap();
this.parameterMappings = Collections.emptyList();
}

public int getDashboardId() {
return dashboardId;
}

public int getCardId() {
return cardId;
}

public Integer getDashboardTabId() {
return dashboardTabId;
}

public int getRow() {
return row;
}

public int getCol() {
return col;
}

public int getSizeX() {
return sizeX;
}

public int getSizeY() {
return sizeY;
}

@Override
public String toString() {
return "{" +
"dashboardId=" + dashboardId +
", cardId=" + cardId +
", dashboardTabId=" + dashboardTabId +
", row=" + row +
", col=" + col +
", sizeX=" + sizeX +
", sizeY=" + sizeY +
", visualizationSettings=" + visualizationSettings +
", parameterMappings=" + parameterMappings +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.avni.server.domain.metabase;

public enum QuestionName {
QUESTION_1("Pie Chart : Count of Non Voided Individuals - Non Voided Subject Type"),
QUESTION_2("Pie Chart : Number of Non exited and Non voided Enrollments for Non Voided Program");

private final String questionName;

QuestionName(String questionName) {
this.questionName = questionName;
}

public String getQuestionName() {
return questionName;
}
}
Loading

0 comments on commit 52a1f6d

Please sign in to comment.