Reading Audit Events with rwslib
In our last post we introduced rwslib, a Python client library for Rave Web Services (RWS). In this post we look a little closer at one of the components of RWS: the Clinical Audit Records web service and at a rwslib helper that makes it easy to capture key events from the Rave EDC System.
RWS provides several webservice endpoints to extract clinical data in CDISC ODM and CSV formats. This data is ideal for reporting since it represents the latest available values entered into the system. But what if you are trying to populate a data warehouse and want to be able to show point-in-time values or look at the pattern of changes to data over time?
In that case you want access to the audit trail which logs every change made to the data through its lifetime. Happily, RWS provides access to a large set of that data through its Clinical Audit Records service. The Clinical Audit Records (audits) service allows you to treat the audit trail as a kind of tape, reading from the first entry up to the present time. Typically the audit trail is a very large table of data so the output of the service is batched, each time you make a request of the service it includes a header in the response to let you know if there is more data to be read and what URL should be called to retrieve the next time-ordered batch. Many types of audits can be extracted from the system.
What does an example response look like? Here’s a snippet:
As you can see, it’s a CDISC ODM formatted XML document with one ClinicalData entry per audit trail entry. The contents of each ClinicalData element varies based on the type of audit entry. In our example above the first entry is related to the creation of a new subject record so there is no StudyEvent, Form or Item level data elements. The second entry relates to a change of a form fields’ value so it has this extra context information to allow the precise identification of its name and location.
Audits and rwslib
rwslib provides a helper for the audits service called AuditRecordsRequest which makes it easy to get access to this service:
AuditRecordsRequest doesn’t do much more than format the request URL and make the request for you, it doesn’t parse the resulting ODM XML or extract the next link url from the returned headers. It’s a building block to help you build your own integrations. But we can go a step further and make use of an additional library from the extras package in rwslib which offers a simpler way consume the audit event service.
audit_event builds on the facilities of rwslib and AuditRecordsRequest to provide an event-style interface to the AuditRecordsRequest and the ODM data it returns. ODMAdapter is a class that manages an RWSConnection to request and parse the ODM and request headers for you. All you have to do is define handlers for the audit events that you want to capture and set it running.
Let’s imagine you want to capture a list of all subjects created from the audit trail. First you need to create a class which will respond to the SubjectCreated event.
We will pass an instance of the above class to ODMAdapter and each time an ODM ClinicalData element with the SubjectCreated audit subcategory is processed the SubjectCreated method will be called with the data from the ODM passed in the context parameter (read about the structure of the context object). At the end of the run through the audits the subjects list will be populated with a list of context objects, one for each subject created.
Let’s see how that works:
The ODMAdapter class has a number of options which can be set to control the size of the ODM document retrieved (how many entries it includes) as well as where in the audit trail to start.
All you need to make use of audit_event is Python, a copy of rwslib from GitHub (this includes the audit_event in the extras module) and a Rave account. The ClinicalAuditRecords service will only return audits that you have permission to view.
Interested in building reporting or other data extraction solutions using RWS and/or rwslib? We have a developer program. Please get in touch!