From d6a291d8d475100172d5ad20ebdba6b305a9eec0 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Tue, 29 Aug 2023 11:15:22 +0200 Subject: [PATCH] fix(core): update --- ts/00_commitinfo_data.ts | 2 +- ts/els.classes.elasticdoc.ts | 46 ++++++++++++++---------------------- ts/els.classes.fastpush.ts | 2 +- ts/els.classes.kvstore.ts | 4 ++-- 4 files changed, 22 insertions(+), 32 deletions(-) diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index c979b8a..3069287 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@apiclient.xyz/elasticsearch', - version: '2.0.7', + version: '2.0.8', description: 'log to elasticsearch in a kibana compatible format' } diff --git a/ts/els.classes.elasticdoc.ts b/ts/els.classes.elasticdoc.ts index 621e4fd..f1e318f 100644 --- a/ts/els.classes.elasticdoc.ts +++ b/ts/els.classes.elasticdoc.ts @@ -71,34 +71,25 @@ export class ElasticDoc { async startPipingSession(options: { onlyNew?: boolean }) { this.sessionDocs.clear(); this.onlyNew = options.onlyNew; - - if (this.onlyNew) { - try { - const response = await this.client.search({ - index: this.index, - sort: '@timestamp:desc', - size: 1, - }); - - // If the search query succeeded, the index exists. - const hit = response.hits.hits[0]; - this.latestTimestamp = hit?._source?.['@timestamp'] || null; - - if (this.latestTimestamp) { - console.log(`Working in "onlyNew" mode. Hence we are omitting documents prior to ${this.latestTimestamp}`); - } else { - console.log(`Working in "onlyNew" mode, but no documents found in index ${this.index}. Hence processing all documents now.`); - } - } catch (e) { - // If the search query failed, the index likely doesn't exist or some other error occurred. - if (e.meta && e.meta.statusCode === 404) { - console.log(`Index ${this.index} does not exist. Working in "onlyNew" mode, but will process all documents as the index is empty.`); - } else { - console.log(`An error occurred while trying to retrieve the latest timestamp: ${e}`); - } - - this.latestTimestamp = null; + const indexExists = await this.client.indices.exists({ index: this.index }); + if (this.onlyNew && indexExists) { + const response = await this.client.search({ + index: this.index, + sort: '@timestamp:desc', + size: 1, + }); + + // If the search query succeeded, the index exists. + const hit = response.hits.hits[0]; + this.latestTimestamp = hit?._source?.['@timestamp'] || null; + + if (this.latestTimestamp) { + console.log(`Working in "onlyNew" mode. Hence we are omitting documents prior to ${this.latestTimestamp}`); + } else { + console.log(`Working in "onlyNew" mode, but no documents found in index ${this.index}. Hence processing all documents now.`); } + } else if (this.onlyNew && !indexExists) { + console.log(`Working in "onlyNew" mode, but index ${this.index} does not exist. Hence processing all documents now.`); } } @@ -213,7 +204,6 @@ export class ElasticDoc { if (response.hits.hits.length > 0) { const hit = response.hits.hits[0]; - console.log(hit); return { date: hit._source['date'], aggregationData: hit._source['aggregationData'], diff --git a/ts/els.classes.fastpush.ts b/ts/els.classes.fastpush.ts index badea88..908c9a1 100644 --- a/ts/els.classes.fastpush.ts +++ b/ts/els.classes.fastpush.ts @@ -18,7 +18,7 @@ export class FastPush { async pushToIndex(indexName: string, docArray: any[], options?: FastPushOptions) { if (docArray.length === 0) return; - const { body: indexExists } = await this.client.indices.exists({ index: indexName }); + const indexExists = await this.client.indices.exists({ index: indexName }); if (indexExists) { if (options?.deleteIndex) { diff --git a/ts/els.classes.kvstore.ts b/ts/els.classes.kvstore.ts index 8f62939..3c4335d 100644 --- a/ts/els.classes.kvstore.ts +++ b/ts/els.classes.kvstore.ts @@ -27,7 +27,7 @@ export class ElasticKVStore { private async setupIndex() { try { - const { body: indexExists } = await this.client.indices.exists({ index: this.index }); + const indexExists = await this.client.indices.exists({ index: this.index }); if (!indexExists) { await this.client.indices.create({ @@ -72,7 +72,7 @@ export class ElasticKVStore { index: this.index, id: key }); - return response.body._source.value; + return response._source['value']; } catch (error) { if (error.meta && error.meta.statusCode === 404) { return null;