Compare commits

..

4 Commits

Author SHA1 Message Date
41f1758d46 2.0.4 2023-08-29 09:56:59 +02:00
2b7cd33996 fix(core): update 2023-08-29 09:56:58 +02:00
80a04ca893 2.0.3 2023-08-29 09:31:43 +02:00
93f739c79e fix(core): update 2023-08-29 09:31:42 +02:00
3 changed files with 36 additions and 26 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@apiclient.xyz/elasticsearch",
"version": "2.0.2",
"version": "2.0.4",
"private": false,
"description": "log to elasticsearch in a kibana compatible format",
"main": "dist_ts/index.js",

View File

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

View File

@ -71,8 +71,12 @@ export class ElasticDoc {
async startPipingSession(options: { onlyNew?: boolean }) {
this.sessionDocs.clear();
this.onlyNew = options.onlyNew;
if (this.onlyNew) {
// Retrieve and store the latest timestamp
// Check if index exists before attempting to search for the latest timestamp
const { body: indexExists } = await this.client.indices.exists({ index: this.index });
if (indexExists) {
const response = await this.client.search({
index: this.index,
sort: '@timestamp:desc',
@ -80,23 +84,21 @@ export class ElasticDoc {
});
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 {
`Working in "onlyNew" mode, but no documents found in index ${this.index}. Hence processing all documents now.`
console.log(`Working in "onlyNew" mode, but no documents found in index ${this.index}. Hence processing all documents now.`);
}
} else {
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 }) {
// If 'onlyNew' is true, compare the document timestamp with the latest timestamp
if (this.onlyNew) {
if (this.latestTimestamp && optionsArg.timestamp <= this.latestTimestamp) {
// Omit the document
return;
}
}
await this.ensureIndexExists(optionsArg.doc);
const documentBody = {
@ -104,11 +106,19 @@ export class ElasticDoc {
...(optionsArg.timestamp && { '@timestamp': optionsArg.timestamp }),
};
// If 'onlyNew' is true, compare the document timestamp with the latest timestamp
if (this.onlyNew) {
if (this.latestTimestamp && optionsArg.timestamp <= this.latestTimestamp) {
// Omit the document
return;
} else {
await this.client.index({
index: this.index,
id: optionsArg.docId,
body: documentBody,
});
}
}
this.sessionDocs.add(optionsArg.docId);
}