Compare commits

...

6 Commits

Author SHA1 Message Date
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
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 43 additions and 27 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@apiclient.xyz/elasticsearch",
"version": "2.0.2",
"version": "2.0.5",
"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.5',
description: 'log to elasticsearch in a kibana compatible format'
}

View File

@ -71,32 +71,40 @@ export class ElasticDoc {
async startPipingSession(options: { onlyNew?: boolean }) {
this.sessionDocs.clear();
this.onlyNew = options.onlyNew;
if (this.onlyNew) {
// Retrieve and store the latest timestamp
const response = await this.client.search({
index: this.index,
sort: '@timestamp:desc',
size: 1,
});
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.`
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.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.`);
}
} 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 }) {
// 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 +112,19 @@ export class ElasticDoc {
...(optionsArg.timestamp && { '@timestamp': optionsArg.timestamp }),
};
await this.client.index({
index: this.index,
id: optionsArg.docId,
body: documentBody,
});
// 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);
}