By Clay Li on Friday, 07 August 2020
Category: Feature

How to Fetch Data Efficiently via REST API

This blog discusses options to keep a local copy of Calem data up to date via Calem REST API. The use case is as follows:

Three options are discussed below. It is recommended that you implement option 2 or 3 if the data you fetch from Calem is significant (tens of thousands of records). You may get by with Option 1 if the number of records fetched is insignificant (less than a few thousands).


Option 1. Fetch All Data from Calem

The first option is to use a task scheduler such as crontab in Linux to fetch all data from Calem periodically. For instance, every 6 minutes, a cron task is launched to fetch asset, location and work orders from Calem.

A maximum limit of number of rows returned from an API fetch call on any table is enforced by Calem. It allows Calem to perform consistently and prevents a client extracting tens of thousands records in one API call which could put a excessive stress on network and server resources.


One needs to code a loop to fetch all data based on the configuration above with the limit clause in the API search (parameter "l" in the API). Here is a demonstration of the loop in pseudo PHP code.

Option 2. Fetch Data by Changes Periodically

Option 1 above fetches all data needed periodically (such as every 6 minutes). It has disadvantages:

There are ways to improve Option 1. One option is to take advantage of the bookkeeping fields for each record in Calem (Calem Offline is built based on those fields). 

We will use "SyncAnchor" to indicate the timestamp of the local data. It is object specific. For instance, you have a SyncAnchor each for asset, location, and work order.


Here is the flow to fetch data by changes based on SyncAnchor. This process works better for both initial fetch and incremental fetches. 

 Option 3. Fetch Data by Changes Periodically with a Fetch-All

It is possible to modify the Option 2 to fetch data by changes with a fetch-all once in a while. It is done by adding a logic in SyncAnchor and periodically reset the SyncAnchor. We will present an option below to do daily fetch-all at mid-night. You may add your own logic to fetch all data such as every 100 fetch by changes.



Additional Resources