Skip to content

Commit

Permalink
adding metrics (#5301)
Browse files Browse the repository at this point in the history
* hapi metrics

* step 1

* metrics service

* adding metrics service

* adding documentation

* refactor

* refactoring

* cleanup

* changing

* adding changelog

* some refactoring

* format

* review fixes part 1

* review points

* review fixes

* updates for code review

* updates for code review

* bumping versions

---------

Co-authored-by: leif stawnyczy <[email protected]>
Co-authored-by: leif stawnyczy <[email protected]>
  • Loading branch information
3 people committed Sep 29, 2023
1 parent c3ff3da commit 7e0fa98
Show file tree
Hide file tree
Showing 120 changed files with 1,955 additions and 121 deletions.
2 changes: 1 addition & 1 deletion hapi-deployable-pom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir</artifactId>
<version>6.9.8-SNAPSHOT</version>
<version>6.9.9-SNAPSHOT</version>

<relativePath>../pom.xml</relativePath>
</parent>
Expand Down
2 changes: 1 addition & 1 deletion hapi-fhir-android/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-deployable-pom</artifactId>
<version>6.9.8-SNAPSHOT</version>
<version>6.9.9-SNAPSHOT</version>

<relativePath>../hapi-deployable-pom/pom.xml</relativePath>
</parent>
Expand Down
2 changes: 1 addition & 1 deletion hapi-fhir-base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-deployable-pom</artifactId>
<version>6.9.8-SNAPSHOT</version>
<version>6.9.9-SNAPSHOT</version>

<relativePath>../hapi-deployable-pom/pom.xml</relativePath>
</parent>
Expand Down
4 changes: 2 additions & 2 deletions hapi-fhir-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
<modelVersion>4.0.0</modelVersion>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-bom</artifactId>
<version>6.9.8-SNAPSHOT</version>
<version>6.9.9-SNAPSHOT</version>

<packaging>pom</packaging>
<name>HAPI FHIR BOM</name>

<parent>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-deployable-pom</artifactId>
<version>6.9.8-SNAPSHOT</version>
<version>6.9.9-SNAPSHOT</version>

<relativePath>../hapi-deployable-pom/pom.xml</relativePath>
</parent>
Expand Down
2 changes: 1 addition & 1 deletion hapi-fhir-checkstyle/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir</artifactId>
<version>6.9.8-SNAPSHOT</version>
<version>6.9.9-SNAPSHOT</version>

<relativePath>../pom.xml</relativePath>
</parent>
Expand Down
2 changes: 1 addition & 1 deletion hapi-fhir-cli/hapi-fhir-cli-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-deployable-pom</artifactId>
<version>6.9.8-SNAPSHOT</version>
<version>6.9.9-SNAPSHOT</version>

<relativePath>../../hapi-deployable-pom/pom.xml</relativePath>
</parent>
Expand Down
2 changes: 1 addition & 1 deletion hapi-fhir-cli/hapi-fhir-cli-app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-cli</artifactId>
<version>6.9.8-SNAPSHOT</version>
<version>6.9.9-SNAPSHOT</version>

<relativePath>../pom.xml</relativePath>
</parent>
Expand Down
2 changes: 1 addition & 1 deletion hapi-fhir-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir</artifactId>
<version>6.9.8-SNAPSHOT</version>
<version>6.9.9-SNAPSHOT</version>

<relativePath>../pom.xml</relativePath>
</parent>
Expand Down
2 changes: 1 addition & 1 deletion hapi-fhir-client-okhttp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-deployable-pom</artifactId>
<version>6.9.8-SNAPSHOT</version>
<version>6.9.9-SNAPSHOT</version>

<relativePath>../hapi-deployable-pom/pom.xml</relativePath>
</parent>
Expand Down
2 changes: 1 addition & 1 deletion hapi-fhir-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-deployable-pom</artifactId>
<version>6.9.8-SNAPSHOT</version>
<version>6.9.9-SNAPSHOT</version>

<relativePath>../hapi-deployable-pom/pom.xml</relativePath>
</parent>
Expand Down
2 changes: 1 addition & 1 deletion hapi-fhir-converter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-deployable-pom</artifactId>
<version>6.9.8-SNAPSHOT</version>
<version>6.9.9-SNAPSHOT</version>

<relativePath>../hapi-deployable-pom/pom.xml</relativePath>
</parent>
Expand Down
2 changes: 1 addition & 1 deletion hapi-fhir-dist/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir</artifactId>
<version>6.9.8-SNAPSHOT</version>
<version>6.9.9-SNAPSHOT</version>

<relativePath>../pom.xml</relativePath>
</parent>
Expand Down
2 changes: 1 addition & 1 deletion hapi-fhir-docs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-deployable-pom</artifactId>
<version>6.9.8-SNAPSHOT</version>
<version>6.9.9-SNAPSHOT</version>

<relativePath>../hapi-deployable-pom/pom.xml</relativePath>
</parent>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
type: add
issue: 5274
title: "Added a service for generating metrics on mdm links and resources.
This includes JPA queries and updated indices.
"
2 changes: 1 addition & 1 deletion hapi-fhir-jacoco/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-deployable-pom</artifactId>
<version>6.9.8-SNAPSHOT</version>
<version>6.9.9-SNAPSHOT</version>

<relativePath>../hapi-deployable-pom/pom.xml</relativePath>
</parent>
Expand Down
2 changes: 1 addition & 1 deletion hapi-fhir-jaxrsserver-base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-deployable-pom</artifactId>
<version>6.9.8-SNAPSHOT</version>
<version>6.9.9-SNAPSHOT</version>

<relativePath>../hapi-deployable-pom/pom.xml</relativePath>
</parent>
Expand Down
2 changes: 1 addition & 1 deletion hapi-fhir-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-deployable-pom</artifactId>
<version>6.9.8-SNAPSHOT</version>
<version>6.9.9-SNAPSHOT</version>

<relativePath>../hapi-deployable-pom/pom.xml</relativePath>
</parent>
Expand Down
2 changes: 1 addition & 1 deletion hapi-fhir-jpaserver-base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-deployable-pom</artifactId>
<version>6.9.8-SNAPSHOT</version>
<version>6.9.9-SNAPSHOT</version>

<relativePath>../hapi-deployable-pom/pom.xml</relativePath>
</parent>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ca.uhn.fhir.jpa.dao.data;

import ca.uhn.fhir.jpa.entity.MdmLink;
import ca.uhn.fhir.mdm.api.MdmLinkSourceEnum;
import ca.uhn.fhir.mdm.api.MdmMatchResultEnum;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("metricsRepository")
public interface IMdmLinkJpaMetricsRepository extends JpaRepository<MdmLink, Long>, IHapiFhirJpaRepository {

@Query("SELECT ml.myMatchResult AS match_result, ml.myLinkSource AS link_source, count(*) AS c "
+ "FROM MdmLink ml "
+ "WHERE ml.myMdmSourceType = :resourceName "
+ "AND ml.myLinkSource in (:linkSource) "
+ "AND ml.myMatchResult in (:matchResult) "
+ "GROUP BY match_result, link_source "
+ "ORDER BY match_result")
Object[][] generateMetrics(
@Param("resourceName") String theResourceType,
@Param("linkSource") List<MdmLinkSourceEnum> theLinkSources,
@Param("matchResult") List<MdmMatchResultEnum> theMatchTypes);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package ca.uhn.fhir.jpa.dao.mdm;

import ca.uhn.fhir.jpa.api.dao.DaoRegistry;
import ca.uhn.fhir.jpa.dao.data.IMdmLinkJpaMetricsRepository;
import ca.uhn.fhir.mdm.api.BaseMdmMetricSvc;
import ca.uhn.fhir.mdm.api.MdmLinkSourceEnum;
import ca.uhn.fhir.mdm.api.MdmMatchResultEnum;
import ca.uhn.fhir.mdm.api.params.GenerateMdmMetricsParameters;
import ca.uhn.fhir.mdm.model.MdmLinkMetrics;
import ca.uhn.fhir.mdm.model.MdmLinkScoreMetrics;
import ca.uhn.fhir.mdm.model.MdmMetrics;
import ca.uhn.fhir.mdm.model.MdmResourceMetrics;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

public class MdmMetricSvcJpaImpl extends BaseMdmMetricSvc {

private final IMdmLinkJpaMetricsRepository myJpaRepository;

private final EntityManagerFactory myEntityManagerFactory;

public MdmMetricSvcJpaImpl(
IMdmLinkJpaMetricsRepository theRepository,
DaoRegistry theDaoRegistry,
EntityManagerFactory theEntityManagerFactory) {
super(theDaoRegistry);
myJpaRepository = theRepository;
myEntityManagerFactory = theEntityManagerFactory;
}

protected MdmLinkMetrics generateLinkMetrics(GenerateMdmMetricsParameters theParameters) {
List<MdmLinkSourceEnum> linkSources = theParameters.getLinkSourceFilters();
List<MdmMatchResultEnum> matchResults = theParameters.getMatchResultFilters();

if (linkSources.isEmpty()) {
linkSources = Arrays.asList(MdmLinkSourceEnum.values());
}
if (matchResults.isEmpty()) {
matchResults = Arrays.asList(MdmMatchResultEnum.values());
}

Object[][] data = myJpaRepository.generateMetrics(theParameters.getResourceType(), linkSources, matchResults);
MdmLinkMetrics metrics = new MdmLinkMetrics();
metrics.setResourceType(theParameters.getResourceType());
for (Object[] row : data) {
MdmMatchResultEnum matchResult = (MdmMatchResultEnum) row[0];
MdmLinkSourceEnum source = (MdmLinkSourceEnum) row[1];
long count = (Long) row[2];
metrics.addMetric(matchResult, source, count);
}
return metrics;
}

protected MdmLinkScoreMetrics generateLinkScoreMetrics(GenerateMdmMetricsParameters theParameters) {
String resourceType = theParameters.getResourceType();

List<MdmMatchResultEnum> matchResultTypes = theParameters.getMatchResultFilters();

// if no result type filter, add all result types
if (matchResultTypes.isEmpty()) {
matchResultTypes = Arrays.asList(MdmMatchResultEnum.values());
}

String sql = "SELECT %s FROM MPI_LINK ml WHERE ml.TARGET_TYPE = :resourceType "
+ "AND ml.MATCH_RESULT in (:matchResult)";

StringBuilder sb = new StringBuilder();
sb.append("sum(case when ml.SCORE is null then 1 else 0 end) as B_" + NULL_VALUE);

for (int i = 0; i < BUCKETS; i++) {
double bucket = getBucket(i + 1);
sb.append(",\n");
if (i == 0) {
// score <= .01
sb.append(String.format("sum(case when ml.SCORE <= %.2f then 1 else 0 end) as B%d", bucket, i));
} else {
// score > i/100 && score <= i/100
sb.append(String.format(
"sum(case when ml.score > %.2f and ml.SCORE <= %.2f then 1 else 0 end) as B%d",
getBucket(i), bucket, i));
}
}

EntityManager em = myEntityManagerFactory.createEntityManager();

Query nativeQuery = em.createNativeQuery(String.format(sql, sb.toString()));

org.hibernate.query.Query<?> hibernateQuery = (org.hibernate.query.Query<?>) nativeQuery;

hibernateQuery.setParameter("resourceType", resourceType);
hibernateQuery.setParameter(
"matchResult", matchResultTypes.stream().map(Enum::ordinal).collect(Collectors.toList()));

List<?> results = hibernateQuery.getResultList();

em.close();

MdmLinkScoreMetrics metrics = new MdmLinkScoreMetrics();

// we only get one row back
Object[] row = (Object[]) results.get(0);
int length = row.length;
for (int i = 0; i < length; i++) {
// if there's nothing in the db, these values will all be null
BigInteger bi = row[i] != null ? (BigInteger) row[i] : BigInteger.valueOf(0);
double bucket = getBucket(i);
if (i == 0) {
metrics.addScore(NULL_VALUE, bi.longValue());
} else if (i == 1) {
metrics.addScore(String.format(FIRST_BUCKET, bucket), bi.longValue());
} else {
metrics.addScore(String.format(NTH_BUCKET, getBucket(i - 1), bucket), bi.longValue());
}
}

return metrics;
}

@Transactional
@Override
public MdmMetrics generateMdmMetrics(GenerateMdmMetricsParameters theParameters) {
MdmResourceMetrics resourceMetrics = generateResourceMetrics(theParameters);
MdmLinkMetrics linkMetrics = generateLinkMetrics(theParameters);
MdmLinkScoreMetrics scoreMetrics = generateLinkScoreMetrics(theParameters);

MdmMetrics metrics = MdmMetrics.fromSeperableMetrics(resourceMetrics, linkMetrics, scoreMetrics);
return metrics;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@
@Index(name = "IDX_EMPI_MATCH_TGT_VER", columnList = "MATCH_RESULT, TARGET_PID, VERSION"),
// v---- this one
@Index(name = "IDX_EMPI_GR_TGT", columnList = "GOLDEN_RESOURCE_PID, TARGET_PID"),
@Index(name = "FK_EMPI_LINK_TARGET", columnList = "TARGET_PID")
@Index(name = "FK_EMPI_LINK_TARGET", columnList = "TARGET_PID"),
// indexes for metrics
@Index(name = "IDX_EMPI_TGT_MR_LS", columnList = "TARGET_TYPE, MATCH_RESULT, LINK_SOURCE"),
@Index(name = "IDX_EMPI_TGT_MR_SCORE", columnList = "TARGET_TYPE, MATCH_RESULT, SCORE")
})
@Audited
// This is the table name generated by default by envers, but we set it explicitly for clarity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,23 @@ public HapiFhirJpaMigrationTasks(Set<String> theFlags) {
init640_after_20230126();
init660();
init680();
init700();
}

protected void init700() {
Builder version = forVersion(VersionEnum.V7_0_0);

// new indices on MdmLink
Builder.BuilderWithTableName mdmLinkTable = version.onTable("MPI_LINK");

mdmLinkTable
.addIndex("20230911.1", "IDX_EMPI_TGT_MR_LS")
.unique(false)
.withColumns("TARGET_TYPE", "MATCH_RESULT", "LINK_SOURCE");
mdmLinkTable
.addIndex("20230911.2", "IDX_EMPi_TGT_MR_SCore")
.unique(false)
.withColumns("TARGET_TYPE", "MATCH_RESULT", "SCORE");
}

protected void init680() {
Expand Down
2 changes: 1 addition & 1 deletion hapi-fhir-jpaserver-elastic-test-utilities/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-deployable-pom</artifactId>
<version>6.9.8-SNAPSHOT</version>
<version>6.9.9-SNAPSHOT</version>
<relativePath>../hapi-deployable-pom/pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion hapi-fhir-jpaserver-hfql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-deployable-pom</artifactId>
<version>6.9.8-SNAPSHOT</version>
<version>6.9.9-SNAPSHOT</version>
<relativePath>../hapi-deployable-pom/pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion hapi-fhir-jpaserver-ips/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-deployable-pom</artifactId>
<version>6.9.8-SNAPSHOT</version>
<version>6.9.9-SNAPSHOT</version>

<relativePath>../hapi-deployable-pom/pom.xml</relativePath>
</parent>
Expand Down
Loading

0 comments on commit 7e0fa98

Please sign in to comment.