-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementation of --activePeriod
feature.
#152
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c1811e7
Implementation of the --activePeriod feature.
bashir2 582b934
fixed e2e and other refs to searchList
bashir2 ff6b787
review comments and active patients
bashir2 a60f82c
Merge branch 'master' into date_review
bashir2 85a7e79
Merge branch 'master' into date_review
bashir2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
86 changes: 86 additions & 0 deletions
86
batch/src/main/java/org/openmrs/analytics/FetchPatientHistory.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,86 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package org.openmrs.analytics; | ||
|
||
import java.util.List; | ||
|
||
import ca.uhn.fhir.rest.api.SummaryEnum; | ||
import com.google.common.base.Preconditions; | ||
import com.google.common.collect.Lists; | ||
import org.apache.avro.Schema; | ||
import org.apache.avro.generic.GenericRecord; | ||
import org.apache.beam.sdk.coders.AvroCoder; | ||
import org.apache.beam.sdk.transforms.PTransform; | ||
import org.apache.beam.sdk.transforms.ParDo; | ||
import org.apache.beam.sdk.values.KV; | ||
import org.apache.beam.sdk.values.PCollection; | ||
import org.apache.beam.sdk.values.PCollectionTuple; | ||
import org.apache.beam.sdk.values.TupleTagList; | ||
import org.hl7.fhir.r4.model.Bundle; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class FetchPatientHistory extends PTransform<PCollection<KV<String, Integer>>, PCollectionTuple> { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(FetchPatientHistory.class); | ||
|
||
private final FetchSearchPageFn<KV<String, Integer>> fetchSearchPageFn; | ||
|
||
private final Schema schema; | ||
|
||
FetchPatientHistory(FhirEtlOptions options, String resourceType, Schema schema) { | ||
Preconditions.checkState(!options.getActivePeriod().isEmpty()); | ||
List<String> dateRange = FhirSearchUtil.getDateRange(options.getActivePeriod()); | ||
|
||
int count = options.getBatchSize(); | ||
this.schema = schema; | ||
|
||
fetchSearchPageFn = new FetchSearchPageFn<KV<String, Integer>>(options, resourceType + "_history") { | ||
|
||
@ProcessElement | ||
public void ProcessElement(@Element KV<String, Integer> patientIdCount, MultiOutputReceiver out) { | ||
String patientId = patientIdCount.getKey(); | ||
log.info(String.format("Already fetched %d %s resources for patient %s", patientIdCount.getValue(), | ||
resourceType, patientId)); | ||
log.info(String.format("Fetching historical %s resources for patient %s", resourceType, patientId)); | ||
Bundle bundle = this.fhirSearchUtil.searchByPatientAndLastDate(resourceType, patientId, dateRange.get(0), | ||
count); | ||
processBundle(bundle, out); | ||
String nextUrl = this.fhirSearchUtil.getNextUrl(bundle); | ||
while (nextUrl != null) { | ||
bundle = this.fhirSearchUtil.searchByUrl(nextUrl, count, SummaryEnum.DATA); | ||
processBundle(bundle, out); | ||
nextUrl = this.fhirSearchUtil.getNextUrl(bundle); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public PCollectionTuple expand(PCollection<KV<String, Integer>> patientIdCounts) { | ||
PCollectionTuple records = patientIdCounts.apply(ParDo.of(fetchSearchPageFn) | ||
.withOutputTags(fetchSearchPageFn.avroTag, TupleTagList.of(Lists.newArrayList(fetchSearchPageFn.jsonTag)))); | ||
records.get(fetchSearchPageFn.avroTag).setCoder(AvroCoder.of(GenericRecord.class, schema)); | ||
return records; | ||
} | ||
|
||
public PCollection<GenericRecord> getAvroRecords(PCollectionTuple records) { | ||
return fetchSearchPageFn.getAvroRecords(records); | ||
} | ||
|
||
public PCollection<String> getJsonRecords(PCollectionTuple records) { | ||
return fetchSearchPageFn.getJsonRecords(records); | ||
} | ||
|
||
} |
133 changes: 133 additions & 0 deletions
133
batch/src/main/java/org/openmrs/analytics/FetchResources.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,133 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package org.openmrs.analytics; | ||
|
||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import ca.uhn.fhir.rest.api.SummaryEnum; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.collect.Lists; | ||
import org.apache.avro.Schema; | ||
import org.apache.avro.generic.GenericRecord; | ||
import org.apache.beam.sdk.coders.AvroCoder; | ||
import org.apache.beam.sdk.transforms.PTransform; | ||
import org.apache.beam.sdk.transforms.ParDo; | ||
import org.apache.beam.sdk.values.KV; | ||
import org.apache.beam.sdk.values.PCollection; | ||
import org.apache.beam.sdk.values.PCollectionTuple; | ||
import org.apache.beam.sdk.values.TupleTag; | ||
import org.apache.beam.sdk.values.TupleTagList; | ||
import org.hl7.fhir.r4.model.Base; | ||
import org.hl7.fhir.r4.model.Bundle; | ||
import org.hl7.fhir.r4.model.Bundle.BundleEntryComponent; | ||
import org.hl7.fhir.r4.model.Property; | ||
import org.hl7.fhir.r4.model.Reference; | ||
import org.hl7.fhir.r4.model.Resource; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* This function object fetches all input segments from the FHIR source and converts each resource | ||
* to JSON or Avro (or both). If a FHIR sink address is provided, those resources are passed to that | ||
* FHIR sink too. | ||
*/ | ||
// TODO: Add unit-tests for this class | ||
public class FetchResources extends PTransform<PCollection<SearchSegmentDescriptor>, PCollectionTuple> { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(FetchResources.class); | ||
|
||
private static final Pattern PATIENT_REFERENCE = Pattern.compile(".*Patient/([^/]+)"); | ||
|
||
final private Schema schema; | ||
|
||
final private FetchSearchPageFn<SearchSegmentDescriptor> fetchSearchPageFn; | ||
|
||
final private TupleTag<KV<String, Integer>> patientIdTag = new TupleTag<KV<String, Integer>>() {}; | ||
|
||
FetchResources(FhirEtlOptions options, String stageIdentifier, Schema schema) { | ||
this.schema = schema; | ||
fetchSearchPageFn = new FetchSearchPageFn<SearchSegmentDescriptor>(options, stageIdentifier) { | ||
|
||
@ProcessElement | ||
public void ProcessElement(@Element SearchSegmentDescriptor segment, MultiOutputReceiver out) { | ||
String searchUrl = segment.searchUrl(); | ||
log.info(String.format("Fetching %d resources for statge %s; URL= %s", segment.count(), this.stageIdentifier, | ||
searchUrl.substring(0, Math.min(200, searchUrl.length())))); | ||
long fetchStartTime = System.currentTimeMillis(); | ||
Bundle pageBundle = fhirSearchUtil.searchByUrl(searchUrl, segment.count(), SummaryEnum.DATA); | ||
addFetchTime(System.currentTimeMillis() - fetchStartTime); | ||
processBundle(pageBundle, out); | ||
if (pageBundle != null && pageBundle.getEntry() != null) { | ||
for (BundleEntryComponent entry : pageBundle.getEntry()) { | ||
String patientId = getSubjectPatientIdOrNull(entry.getResource()); | ||
if (patientId != null) { | ||
out.get(patientIdTag).output(KV.of(patientId, 1)); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
} | ||
|
||
@VisibleForTesting | ||
static String getSubjectPatientIdOrNull(Resource resource) { | ||
String patinetId = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT: patientId There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
Property subject = resource.getNamedProperty("subject"); | ||
if (subject != null) { | ||
List<Base> values = subject.getValues(); | ||
if (values.size() == 1) { | ||
Reference reference = (Reference) values.get(0); | ||
// TODO: Find a more generic way to check if this is a reference to a Patient. With the | ||
// current OpenMRS setup, reference.getType() is null so we cannot rely on that. | ||
String refStr = reference.getReference(); | ||
Matcher matcher = PATIENT_REFERENCE.matcher(refStr); | ||
if (matcher.matches()) { | ||
patinetId = matcher.group(1); | ||
} | ||
if (patinetId == null) { | ||
log.warn(String.format("Ignoring subject of %s with id %s because it is not a Patient reference: %s", | ||
resource.getResourceType(), resource.getId(), refStr)); | ||
} | ||
} | ||
if (values.size() > 1) { | ||
log.warn(String.format("Unexpected multiple values for subject of %s with id %s", resource.getResourceType(), | ||
resource.getId())); | ||
} | ||
} | ||
return patinetId; | ||
} | ||
|
||
@Override | ||
public PCollectionTuple expand(PCollection<SearchSegmentDescriptor> segments) { | ||
PCollectionTuple records = segments.apply(ParDo.of(fetchSearchPageFn).withOutputTags(fetchSearchPageFn.avroTag, | ||
TupleTagList.of(Lists.newArrayList(fetchSearchPageFn.jsonTag, patientIdTag)))); | ||
records.get(fetchSearchPageFn.avroTag).setCoder(AvroCoder.of(GenericRecord.class, schema)); | ||
return records; | ||
} | ||
|
||
public PCollection<GenericRecord> getAvroRecords(PCollectionTuple records) { | ||
return fetchSearchPageFn.getAvroRecords(records); | ||
} | ||
|
||
public PCollection<String> getJsonRecords(PCollectionTuple records) { | ||
return fetchSearchPageFn.getJsonRecords(records); | ||
} | ||
|
||
public PCollection<KV<String, Integer>> getPatientIds(PCollectionTuple records) { | ||
return records.get(patientIdTag); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to change docker-compose files e.g here https://github.com/GoogleCloudPlatform/openmrs-fhir-analytics/blob/353cb17d30ee0e74816a58d1d56eddfecb8c77a3/docker/docker-compose.yaml#L32
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done and thanks for catching this. Also updated the JDBC code-paths for consistency.