How to decimate data from device bucket

Hello,

We have some devices which were sending data on 2 minutes in the past. As that is to fast, and we want to free some registers space, we would like to clean bucket by deleting everything except every fifth data point.

How to do this?

Thanks

Hi Branko,
You would need to build an analysis for that.

The following code can do that:

const { Account, Device, Utils, Analysis } = require('@tago-io/sdk');

async function startAnalysis(context) {
  // Replace the token with one generated from your profile at https://admin.tago.io/account/
  const account = new Account({ token: 'Your-Account-Token' });

  // Get your device list
  // Remove the filter param if you want to get all of them.
  const device_list = await account.devices.list({ 
    amount: 10000, 
    fields: ['id', 'name', 'tags'], 
    filter: { tags: [{ key: 'device_type', value: 'customer' }] } 
  });

  for (const dev_info of device_list) {
    context.log(`Removing Device data from ${dev_info.name} (${dev_info.id})`);
    const token = await Utils.getTokenByName(account, dev_info.id);
    const device = new Device({ token });
    
// set to skip the first 50 data, and then remove the next 10000 data.
    await device.deleteData({ skip: 50, qty: 10000 }).then(result => context.log(result)).catch(context.log);
  }
}

module.exports = new Analysis(startAnalysis);

It will only allow you to remove 10.000 data/device by each time this analysis runs.

Thanks for answer. With some small changes, it works great.

1 Like