Held results

Keep results whose case does not exist yet and write them once it does.

This page follows Results from FHIR in the Lab results series. A laboratory can report a result before the case has been registered in DHIS2, for instance when a facility enters its cases at the end of the day. The lookup by specimen ID then finds nothing, and the result is held until the case exists.

Holding a result

In dhis2.lab.write-result, the branch that handles a missing case stores the request in a cache hash under the specimen ID. The key of the hash comes from the held section of dhis2.ini:

        if case is None:
            cache_key = self.config.dhis2.held.cache_key
            held_request = dumps(request)
            self.cache.hset(cache_key, request.specimen_id, held_request)
            self.response.payload = {'is_found': False}
            return

dumps is json.dumps. The rest of the service is as shown in Write a result. Because the entire request is stored, the retry does not depend on the source the result came from, and HL7 v2 and FHIR results are held and retried in the same way.

Retrying held results

A scheduled service reads the hash and invokes dhis2.lab.write-result for each held result. A result whose case now exists is written and removed from the hash. A result whose case still does not exist is stored again by the branch above and stays held:

# -*- coding: utf-8 -*-

# stdlib
from json import loads

# Zato
from zato.server.service import Service

class RetryHeldLabResults(Service):
    name = 'dhis2.lab.retry-held'

    def handle(self) -> 'None':

        cache_key = self.config.dhis2.held.cache_key
        held_results = self.cache.hgetall(cache_key)

        for specimen_id, held_request in held_results.items():

            request = loads(held_request)
            response = self.invoke('dhis2.lab.write-result', request)

            # The case exists now and the result was written, so it is no longer held
            if response['is_found']:
                self.cache.hdel(cache_key, specimen_id)

The service runs from a scheduler job with dhis2.lab.retry-held as the service and an interval of fifteen minutes.

Job retrying held results

Next

Weekly counts reports the number of results per test and outcome to a DHIS2 data set each week.

See also

FeatureWhat it does
Write a resultThe service the held results are retried through
CacheHashes and the other cache operations
SchedulerInterval-based jobs

Learn more