fix(core): update

This commit is contained in:
Philipp Kunz 2023-08-29 09:56:58 +02:00
parent 80a04ca893
commit 2b7cd33996
2 changed files with 23 additions and 14 deletions

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@apiclient.xyz/elasticsearch', name: '@apiclient.xyz/elasticsearch',
version: '2.0.3', version: '2.0.4',
description: 'log to elasticsearch in a kibana compatible format' description: 'log to elasticsearch in a kibana compatible format'
} }

View File

@ -71,26 +71,34 @@ export class ElasticDoc {
async startPipingSession(options: { onlyNew?: boolean }) { async startPipingSession(options: { onlyNew?: boolean }) {
this.sessionDocs.clear(); this.sessionDocs.clear();
this.onlyNew = options.onlyNew; this.onlyNew = options.onlyNew;
if (this.onlyNew) { if (this.onlyNew) {
// Retrieve and store the latest timestamp // Check if index exists before attempting to search for the latest timestamp
const response = await this.client.search({ const { body: indexExists } = await this.client.indices.exists({ index: this.index });
index: this.index,
sort: '@timestamp:desc', if (indexExists) {
size: 1, const response = await this.client.search({
}); index: this.index,
const hit = response.body.hits.hits[0]; sort: '@timestamp:desc',
this.latestTimestamp = hit?._source?.['@timestamp'] || null; size: 1,
if (this.latestTimestamp) { });
console.log(`Working in "onlyNew" mode. Hence we are omitting documents prior to ${this.latestTimestamp}`); const hit = response.body.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 { } else {
`Working in "onlyNew" mode, but no documents found in index ${this.index}. Hence processing all documents now.` this.latestTimestamp = null;
console.log(`Index ${this.index} does not exist. Working in "onlyNew" mode, but will process all documents as the index is empty.`);
} }
} }
} }
async pipeDocument(optionsArg: { docId: string; timestamp?: string | number; doc: any }) { async pipeDocument(optionsArg: { docId: string; timestamp?: string | number; doc: any }) {
// If 'onlyNew' is true, compare the document timestamp with the latest timestamp
await this.ensureIndexExists(optionsArg.doc); await this.ensureIndexExists(optionsArg.doc);
const documentBody = { const documentBody = {
@ -98,6 +106,7 @@ export class ElasticDoc {
...(optionsArg.timestamp && { '@timestamp': optionsArg.timestamp }), ...(optionsArg.timestamp && { '@timestamp': optionsArg.timestamp }),
}; };
// If 'onlyNew' is true, compare the document timestamp with the latest timestamp
if (this.onlyNew) { if (this.onlyNew) {
if (this.latestTimestamp && optionsArg.timestamp <= this.latestTimestamp) { if (this.latestTimestamp && optionsArg.timestamp <= this.latestTimestamp) {
// Omit the document // Omit the document