Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove old events from Thingsboard?

Tags:

thingsboard

What should I do to properly remove 'event' entries from Thingsboard?

As far as I know, the current API does not provide a way to remove events. It seems like the only way is to directly delete the records in DB.

By the way, I'm using PostgreSQL as DB.

like image 829
skangmy Avatar asked Feb 12 '19 05:02

skangmy


1 Answers

After two hours of research in Thingsboard source code, I found the solution.

The date is contained in the uid_event field in V1 UUID format.

So first, you need to write a function uuid_timestampin order to convert the UUID to a timestamp. I found the solution, here: https://stackoverflow.com/a/24191574/5300212

CREATE FUNCTION uuid_timestamp(id uuid) RETURNS timestamptz AS $$
  select TIMESTAMP WITH TIME ZONE 'epoch' +
      (((('x' || lpad(split_part(id::text, '-', 1), 16, '0'))::bit(64)::bigint) +
      (('x' || lpad(split_part(id::text, '-', 2), 16, '0'))::bit(64)::bigint << 32) +
      ((('x' || lpad(split_part(id::text, '-', 3), 16, '0'))::bit(64)::bigint&4095) << 48) - 122192928000000000) / 10000000 ) * INTERVAL '1 second';    
$$ LANGUAGE SQL
  IMMUTABLE
  RETURNS NULL ON NULL INPUT;

After that, to delete all events older than 30 days ago, you can run a query like:

DELETE FROM public.event WHERE uuid_timestamp(event_uid::uuid) < now() - '30 days'::interval;
like image 94
David Dorchies Avatar answered Oct 13 '22 09:10

David Dorchies