Version: 8.0.x

Read Metrics

To read existing metrics, you will need to make a TLS request to the OpenHIM API for the below method and endpoint. The two query parameters startDate and endDate have to be supplied (datetime format).

Read all metrics#

Method: GET
Endpoint: {openhim_url}:8080/metrics?startDate=<START_DATE>&endDate=<END_DATE>

Read metrics broken down by channel#

Method: GET
Endpoint: {openhim_url}:8080/metrics/channels?startDate=<START_DATE>&endDate=<END_DATE>

Read metrics for a specific channel#

Method: GET
Endpoint: {openhim_url}:8080/metrics/channels/:channelId?startDate=<START_DATE>&endDate=<END_DATE>

Read metrics in a specified timeSeries#

Method: GET
Endpoint: {openhim_url}:8080/metrics/timeseries/:timeSeries?startDate=<START_DATE>&endDate=<END_DATE>

Time series values are one of 'minute', 'hour', 'day', 'month', 'year'.

Read metrics broken down by channel in a specified timeSeries#

Method: GET
Endpoint: {openhim_url}:8080/metrics/timeseries/:timeSeries/channels?startDate=<START_DATE>&endDate=<END_DATE>

Read metrics for a specific channel in a specified timeSeries#

Method: GET
Endpoint: {openhim_url}:8080/metrics/timeseries/:timeSeries/channels/:channelId?startDate=<START_DATE>&endDate=<END_DATE>

The metrics API always returns a JSON array with the metrics. If the channel does not exist the api responds with a status of 401, and 200 on success.

Example#

Note: In the examples below, we are using the token authentication type to authenticate requests

Before we can send our request to the OpenHIM API we need to ensure that we construct our valid HTTP headers to successfully authenticate with the OpenHIM API.

Copy the below code at the bottom of your nodejs script that handles the authentication of the OpenHIM headers as described in the authentication section.

Replace the openhimOptions values with the correct implementation details

// append below code to the "openhim-api.js" script containing the authentication methods.
// This is described within the authentication section
(async () => {
const openhimOptions = {
apiURL: 'https://localhost:8080',
apiEndpoint: '/metrics',
username: 'root@openhim.org',
password: 'openhim-password',
rejectUnauthorized: false
}
const headers = await genAuthHeaders(openhimOptions)
const options = { method: 'GET',
url: `${openhimOptions.apiURL}${openhimOptions.apiEndpoint}`,
rejectUnauthorized: openhimOptions.rejectUnauthorized,
headers: headers,
qs: {
startDate: new Date(Date.now() - 3.154e+10), // 3.154e+10 is the number of milliseconds in a year
endDate: new Date()
}
}
request(options, (error, response, body) => {
if (error) throw new Error(error)
console.log({
statusCode: response.statusCode,
body
})
})
})()

The response status code will be 200 if successful.