Compare commits

...

6 Commits

Author SHA1 Message Date
cdbf1fd316 2.0.6 2023-08-29 10:03:38 +02:00
10108d8338 fix(core): update 2023-08-29 10:03:37 +02:00
36abb2c7c0 2.0.5 2023-08-29 10:00:43 +02:00
8f00d90bb1 fix(core): update 2023-08-29 10:00:42 +02:00
41f1758d46 2.0.4 2023-08-29 09:56:59 +02:00
2b7cd33996 fix(core): update 2023-08-29 09:56:58 +02:00
3 changed files with 31 additions and 16 deletions

View File

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

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.6',
description: 'log to elasticsearch in a kibana compatible format' description: 'log to elasticsearch in a kibana compatible format'
} }

View File

@ -71,26 +71,40 @@ 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 try {
const response = await this.client.search({ const response = await this.client.search({
index: this.index, index: this.index,
sort: '@timestamp:desc', sort: '@timestamp:desc',
size: 1, size: 1,
}); });
// If the search query succeeded, the index exists.
const hit = response.body.hits.hits[0]; const hit = response.body.hits.hits[0];
this.latestTimestamp = hit?._source?.['@timestamp'] || null; this.latestTimestamp = hit?._source?.['@timestamp'] || null;
if (this.latestTimestamp) { if (this.latestTimestamp) {
console.log(`Working in "onlyNew" mode. Hence we are omitting documents prior to ${this.latestTimestamp}`); console.log(`Working in "onlyNew" mode. Hence we are omitting documents prior to ${this.latestTimestamp}`);
} else { } 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.`);
}
} 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;
} }
} }
} }
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 +112,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