---
# System prepended metadata

title: WB RT event cleanup

---

```typescript
import {getElasticClient} from "./get.elastic.client";
import * as console from "console";
import {SearchHit} from "@elastic/elasticsearch/lib/api/types";

const TIME_WINDOW = 10;
const WIGGLE_ROOM = 0.1;
const TIME_WINDOWS_MS = (TIME_WINDOW - WIGGLE_ROOM) * 60 * 1000;

type DocumentType = {
  "type": "WHITEBOARD_RT_CONTRIBUTION",
  "id": string,
  "name": string,
  "author": string,
  "space": string,
  "@timestamp": Date,
  "alkemio": boolean,
  "environment": "production"
}

const main = async () => {
  const elastic = getElasticClient();

  if (!elastic) {
    throw new Error('elastic not initialized');
  }

  const result = await elastic.search<DocumentType>({
    size: 10000,
    index: 'contribution-events',
    query: {
      "bool": {
        "filter": [
          {
            "match_phrase": {
              "type": "WHITEBOARD_RT_CONTRIBUTION"
            }
          },
        ],
      }
    }
  });
  const docs = result.hits.hits
  // convert @timestamp field to Date object
  .map(x => {
      if (x._source) {
        x._source['@timestamp'] = new Date(x._source['@timestamp']);
      }
      return x;
  });
  // group docs by the id field
  const map = new Map<string, SearchHit<DocumentType>[]>();
  docs.forEach(doc => {
    if (doc._source) {
      const id = doc._source.id;
      const arr = map.get(id) ?? [];
      arr.push(doc);
      map.set(id, arr);
    }
  });
  // for every entry in the map track if the time between two of the same values of author is less than 9 minutes
  // if it's less than 9 minutes - add the _id field to an array and print it at the end
  const duplicates: SearchHit<DocumentType>[] = [];
  map.forEach((hits, id) => {
    const sorted = hits.sort((a, b) => {
      if (a._source && b._source) {
        return a._source['@timestamp'].getTime() - b._source['@timestamp'].getTime();
      }
      return 0;
    });
    let lastTimestamp = 0;
    // for every unique value of author check if the time between it and the last occurrence is less than 9 minutes
    // if it is - add it to the duplicates array
    const map = new Map<string, number>();
    sorted.forEach(hit => {
      if (hit._source) {
        const author = hit._source.author;
        const lastTimeStamp = map.get(author);
        if (lastTimeStamp && hit._source['@timestamp'].getTime() - lastTimeStamp < TIME_WINDOWS_MS) {
          duplicates.push(hit);
        } else {
          map.set(author, hit._source['@timestamp'].getTime());
        }
      }
    });
  });

  const deleteResult = await elastic.deleteByQuery({
    index: 'contribution-events',
    human: true,
    wait_for_completion: true,
    query: {
      ids: {
        values: duplicates.map(x => x._id),
      },
    },
  });
  console.log(deleteResult);
};
main();
```