Writing Clientside Purge Rules

@derick
Thinking of a batter way of clearing the data from the client devices using purging rules since they have become very slow
Wrote the code below, please review before I push to the server
app_settings.json
{
“//”: “other app_settings settings”,
“purge”: {
“fn”: “function(userCtx, contact, reports, messages) { const old = Date.now() - (1000 * 60 * 60 * 24 * 365);
return reports.filter(r => r.reported_date < old).map(r => r._id);}”,
“text_expression”: “at 6 pm on Friday”
}
}

purge.js
module.exports = {
text_expression: ‘at 9 am on Sunday’,
run_every_days: 7,
cron: ‘0 1 * * FRI’,
fn: function(userCtx, contact, reports, messages) {
const old = Date.now() - (1000 * 60 * 60 * 24 * 365);
const oldMessages = Date.now() - (1000 * 60 * 60 * 24 * 90);

    const reportsToPurge = reports
                             .filter(r => r.reported_date < old)
                             .map(r => r._id);
    const messagesToPurge = messages
                             .filter(m => m.reported_date < oldMessages)
                             .map(m => m._id);
  
    return [report.contact];
  } 

};

@oyierphil what do you want to purge? What rules to you want to implement?

I’d encourage you to upload this to a development server / local copy and evaluate the impact of the purging rules.

As implemented, your purge function will not purge any reports. The purge function should return a list of _id’s. Your current implementation returns an object.

A first step would be to change the return statement to
return [...reportsToPurge,...messagesToPurge].

You would still need to evaluate that that logic meets your needs.

1 Like

@derick
From our configuration, we store reports at the client side (Basically the case investigation form), we didn’t configure messaging, thus we don’t store messages at the client side

Our phones have become very very very slow, and the clients are loosing patience on the queue, automatic purging will solve our problem

@derick
We reviewed our hierarchy and updated the app_settings.json file to include only the contact_types in our hierarchy, especially the contact id and parent,
So far the speed has improved and the teams are working

Still figuring out the purging rules since I have issues with the ssl certificate on my test server, haven’t tried the codes for purging rules

@derick
I’ve been called today that our devices are very slow again, back to the drawing board.
The solution could be the purging rules, anyone who has tried client side purging?

Good evening
We managed to sort-out most challenges, now our devices are full and want to test and implement client-side and server-side purging, anyone who has tried any purging option to share the experience, thank you

Hi @oyierphil,

There are some sample purge rules in this guide Purging | Community Health Toolkit which are applicable to a wide range of use-cases. Please have a look and we’ll be happy to answer any questions you may have.