Skip to content

Commit

Permalink
Add ability to Metrics to get size data on reports, filtering with pa…
Browse files Browse the repository at this point in the history
…tientId, measureId, and/or reportId
  • Loading branch information
MontaltoNick committed Feb 23, 2024
1 parent 78ad84f commit 2055998
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.lantanagroup.link.Constants;
import com.lantanagroup.link.db.SharedService;
import com.lantanagroup.link.db.TenantService;
import com.lantanagroup.link.db.model.MetricData;
import com.lantanagroup.link.db.model.Metrics;
import com.lantanagroup.link.model.*;
Expand Down Expand Up @@ -401,4 +402,78 @@ private List<MetricData> getPeriodMetricData(String category, String task, List<
.collect(Collectors.toList());
}

@GetMapping("/size/{tenantId}")
public PatientMeasureReportSizeSummary getPatientMeasureReportSizeSummary(
@PathVariable String tenantId,
@RequestParam(name = "patientId", required = false) String patientId,
@RequestParam(name = "reportId", required = false) String reportId,
@RequestParam(name = "measureId", required = false) String measureId) {

var tenantService = TenantService.create(sharedService, tenantId);

var reportSizes = tenantService.getPatientMeasureReportSize(patientId, reportId, measureId);

var summary = new PatientMeasureReportSizeSummary();
Double sum = 0.0;
var measureCountMap = summary.getCountReportSizeByMeasureId();
var reportCountMap = summary.getCountReportSizeByReportId();
var patientCountMap = summary.getCountReportSizeByPatientId();

var averageReportSizeByMeasureId = summary.getAverageReportSizeByMeasureId();
var averageReportSizeByReportId = summary.getAverageReportSizeByReportId();
var averageReportSizeByPatientId= summary.getAverageReportSizeByPatientId();

for(var r : reportSizes)
{
var mId = r.getMeasureId();
var pId = r.getPatientId();
var rId = r.getReportId();

sum += r.getSizeKb();

if(measureCountMap.containsKey(mId))
measureCountMap.put(mId, measureCountMap.get(mId) + 1);
else {
measureCountMap.put(mId, 1);
averageReportSizeByMeasureId.put(mId, 0.0);
}
averageReportSizeByMeasureId.put(mId, averageReportSizeByMeasureId.get(mId) + r.getSizeKb());

if(reportCountMap.containsKey(rId))
reportCountMap.put(rId, reportCountMap.get(rId) + 1);
else {
reportCountMap.put(rId, 1);
averageReportSizeByReportId.put(rId, 0.0);
}

averageReportSizeByReportId.put(rId, averageReportSizeByReportId.get(rId) + r.getSizeKb());

if(patientCountMap.containsKey(pId))
patientCountMap.put(pId, patientCountMap.get(pId) + 1);
else {
patientCountMap.put(pId, 1);
averageReportSizeByPatientId.put(pId, 0.0);
}
averageReportSizeByPatientId.put(pId, averageReportSizeByPatientId.get(pId) + r.getSizeKb());
}

summary.setTotalSize(sum);
summary.setReports(reportSizes);
summary.setReportCount(reportSizes.size());
summary.setAverageReportSize(sum/reportSizes.size());

for(var kv : measureCountMap.entrySet()) {
averageReportSizeByMeasureId.put(kv.getKey(), averageReportSizeByMeasureId.get(kv.getKey())/kv.getValue());
}

for(var kv : reportCountMap.entrySet()) {
averageReportSizeByReportId.put(kv.getKey(), averageReportSizeByReportId.get(kv.getKey())/kv.getValue());
}

for(var kv : patientCountMap.entrySet()) {
averageReportSizeByPatientId.put(kv.getKey(), averageReportSizeByPatientId.get(kv.getKey())/kv.getValue());
}

return summary;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,14 @@ public List<PatientMeasureReport> getPatientMeasureReports(String reportId, Stri
return this.patientMeasureReports.findByReportIdAndMeasureId(reportId, measureId);
}

public PatientMeasureReportSize getPatientMeasureReportSize(String id) {
return patientMeasureReports.GetMeasureReportSizeById(id);
}

public List<PatientMeasureReportSize> getPatientMeasureReportSize(String patientId, String reportId, String measureId) {
return patientMeasureReports.GetMeasureReportSize(patientId, reportId, measureId);
}

public void savePatientMeasureReport(PatientMeasureReport patientMeasureReport) {
this.patientMeasureReports.save(patientMeasureReport);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ public Row(ResultSet resultSet) {
public String getString(String columnName) throws SQLException {
return resultSet.getNString(columnName);
}

public Double getDouble(String columnName) throws SQLException {
return resultSet.getDouble(columnName);
}
public Date getDate(String columnName) throws SQLException {
return resultSet.getTimestamp(columnName);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.lantanagroup.link.db.mappers;

import com.lantanagroup.link.db.model.PatientMeasureReportSize;

import java.sql.ResultSet;
import java.sql.SQLException;

public class PatientMeasureReportSizeMapper extends BaseMapper<PatientMeasureReportSize> {
@Override
protected PatientMeasureReportSize doToModel(ResultSet resultSet) throws SQLException {
Row row = new Row(resultSet);
var model = new PatientMeasureReportSize();
model.setReportId(row.getString("reportId"));
model.setMeasureId(row.getString("measureId"));
model.setPatientId(row.getString("patientId"));
model.setSizeKb(row.getDouble("sizeKb"));

return model;
}

@Override
protected Parameters doToParameters(PatientMeasureReportSize model) {
Parameters parameters = new Parameters();
parameters.addString("reportId", model.getReportId());
parameters.addString("measureId", model.getMeasureId());
parameters.addString("patientId", model.getPatientId());
parameters.addValue("sizeKb", model.getSizeKb());
return parameters;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.lantanagroup.link.db.model;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class PatientMeasureReportSize {
private String reportId;
private String measureId;
private String patientId;
private double SizeKb;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

import com.lantanagroup.link.StreamUtils;
import com.lantanagroup.link.db.mappers.PatientMeasureReportMapper;
import com.lantanagroup.link.db.mappers.PatientMeasureReportSizeMapper;
import com.lantanagroup.link.db.model.PatientMeasureReport;
import com.lantanagroup.link.db.model.PatientMeasureReportSize;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.support.TransactionTemplate;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PatientMeasureReportRepository {
private static final PatientMeasureReportMapper mapper = new PatientMeasureReportMapper();

private static final PatientMeasureReportSizeMapper sizeMapper = new PatientMeasureReportSizeMapper();
private final TransactionTemplate txTemplate;
private final NamedParameterJdbcTemplate jdbc;

Expand All @@ -32,6 +35,31 @@ public PatientMeasureReport findById(String id) {
.orElse(null);
}

public PatientMeasureReportSize GetMeasureReportSizeById(String id) {
String sql = "SELECT pmr.patientId,pmr.reportId,pmr.measureID, CAST(SUM(DATALENGTH(pmr.measureReport)) as FLOAT)/1024.0 as sizeKb FROM dbo.patientMeasureReport AS pmr WHERE id = :id GROUP BY pmr.patientId, pmr.reportId, pmr.measureId";

Map<String, ?> parameters = Map.of("id", id);

return jdbc.query(sql, parameters, sizeMapper).stream()
.reduce(StreamUtils::toOnlyElement)
.orElse(null);
}

public List<PatientMeasureReportSize> GetMeasureReportSize(String patientId, String reportId, String measureId) {
String sql = "SELECT pmr.patientId, pmr.reportId, pmr.measureID, CAST(SUM(DATALENGTH(pmr.measureReport)) as FLOAT)/1024.0 as sizeKb FROM dbo.patientMeasureReport AS pmr WHERE (ISNULL(:reportId, '') = '' OR reportId = :reportId) AND (ISNULL(:measureId, '') = '' OR measureId = :measureId) AND (ISNULL(:patientId, '') = '' OR patientId = :patientId) GROUP BY pmr.patientId, pmr.reportId, pmr.measureId";

if(reportId == null) reportId = "";
if(measureId == null) measureId = "";
if(patientId == null) patientId = "";

HashMap<String, String> parameters = new HashMap<String, String>();
parameters.put("reportId", reportId);
parameters.put("patientId", patientId);
parameters.put("measureId", measureId);

return jdbc.query(sql, parameters, sizeMapper);
}

public List<PatientMeasureReport> findByReportId(String reportId) {
String sql = "SELECT * FROM dbo.patientMeasureReport WHERE reportId = :reportId;";
Map<String, ?> parameters = Map.of("reportId", reportId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.lantanagroup.link.model;
import com.lantanagroup.link.db.model.PatientMeasureReportSize;

import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;
import java.util.List;

@Getter
@Setter
public class PatientMeasureReportSizeSummary{
private List<PatientMeasureReportSize> Reports;
private Double TotalSize;
private int ReportCount;
private Double AverageReportSize;

private HashMap<String, Integer> CountReportSizeByMeasureId= new HashMap<>();
private HashMap<String, Double> AverageReportSizeByMeasureId = new HashMap<>();

private HashMap<String, Integer> CountReportSizeByPatientId= new HashMap<>();
private HashMap<String, Double> AverageReportSizeByPatientId= new HashMap<>();

private HashMap<String, Integer> CountReportSizeByReportId= new HashMap<>();
private HashMap<String, Double> AverageReportSizeByReportId= new HashMap<>();
}

0 comments on commit 2055998

Please sign in to comment.