import * as _ from 'lodash'; import { sendItemsToEsApi } from './bulk-api'; export interface IEsApiQueueOptions { indexName: string; batch?: number; key: string; esKey: string; queueName?: string; } export class AEsApiQueue { list: any = []; indexName; batch; key; esKey; queueName; constructor ({ indexName, batch = 1, key = 'url', esKey, queueName = '', }: IEsApiQueueOptions) { this.indexName = indexName; this.batch = batch; this.key = key; this.esKey = esKey; this.queueName = queueName; } async add(items) { this.list = [...this.list, ...items]; if (this.list.length >= this.batch) { const toSend = [...this.list]; this.list = []; await this.send(toSend); } } async send(items = this.list) { if (items.length > 0) { // console.info(`\n%c---------EsApiQueue send ${items.length} ${this.queueName} --------- \n`, 'background:yellow; color:blue; font-weight:600;\n'); await sendItemsToEsApi(this.indexName, this.key, items); const keys = items.map((item) => item[this.key]); this.list = this.list.filter(i => keys.includes(i[this.key])); } } }