Compare commits

...

14 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
80a04ca893 2.0.3 2023-08-29 09:31:43 +02:00
93f739c79e fix(core): update 2023-08-29 09:31:42 +02:00
a1b5bf5c0c 2.0.2 2023-08-29 09:18:16 +02:00
084c5d137c fix(core): update 2023-08-29 09:18:15 +02:00
4aa0592bd5 2.0.1 2023-08-25 16:07:53 +02:00
0313e5045a fix(core): update 2023-08-25 16:07:52 +02:00
7442d93f58 2.0.0 2023-08-25 16:07:17 +02:00
35e70aae62 BREAKING CHANGE(core): update 2023-08-25 16:07:16 +02:00
4 changed files with 79 additions and 31 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@apiclient.xyz/elasticsearch", "name": "@apiclient.xyz/elasticsearch",
"version": "1.0.56", "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

@ -49,7 +49,7 @@ tap.test('should create an ElasticDoc instance', async () => {
}); });
tap.test('should add and update documents in a piping session', async () => { tap.test('should add and update documents in a piping session', async () => {
await testElasticDoc.startPipingSession(); await testElasticDoc.startPipingSession({});
await testElasticDoc.pipeDocument({ await testElasticDoc.pipeDocument({
docId: '1', docId: '1',
timestamp: new Date().toISOString(), timestamp: new Date().toISOString(),

View File

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

View File

@ -14,13 +14,18 @@ export interface ISnapshot {
aggregationData: any; aggregationData: any;
} }
export type SnapshotProcessor = (iterator: AsyncIterable<any>, prevSnapshot: ISnapshot | null) => Promise<ISnapshot>; export type SnapshotProcessor = (
iterator: AsyncIterable<any>,
prevSnapshot: ISnapshot | null
) => Promise<ISnapshot>;
export class ElasticDoc { export class ElasticDoc {
public client: ElasticClient; public client: ElasticClient;
public index: string; public index: string;
private sessionDocs: Set<string> = new Set(); private sessionDocs: Set<string> = new Set();
private indexInitialized: boolean = false; private indexInitialized: boolean = false;
private latestTimestamp: string | null = null; // Store the latest timestamp
private onlyNew: boolean = false; // Whether to only pipe new docs
private BATCH_SIZE = 1000; private BATCH_SIZE = 1000;
@ -63,15 +68,43 @@ export class ElasticDoc {
return { properties }; return { properties };
} }
async startPipingSession() { async startPipingSession(options: { onlyNew?: boolean }) {
this.sessionDocs.clear(); 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.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; async pipeDocument(optionsArg: { docId: string; timestamp?: string | number; doc: any }) {
doc: any;
}) {
await this.ensureIndexExists(optionsArg.doc); await this.ensureIndexExists(optionsArg.doc);
const documentBody = { const documentBody = {
@ -79,18 +112,30 @@ export class ElasticDoc {
...(optionsArg.timestamp && { '@timestamp': optionsArg.timestamp }), ...(optionsArg.timestamp && { '@timestamp': optionsArg.timestamp }),
}; };
await this.client.index({ // If 'onlyNew' is true, compare the document timestamp with the latest timestamp
index: this.index, if (this.onlyNew) {
id: optionsArg.docId, if (this.latestTimestamp && optionsArg.timestamp <= this.latestTimestamp) {
body: documentBody, // Omit the document
}); return;
} else {
await this.client.index({
index: this.index,
id: optionsArg.docId,
body: documentBody,
});
}
}
this.sessionDocs.add(optionsArg.docId); this.sessionDocs.add(optionsArg.docId);
} }
async endPipingSession() { async endPipingSession() {
const allDocIds: string[] = []; const allDocIds: string[] = [];
const responseQueue = []; const responseQueue = [];
let response = await this.client.search({ index: this.index, scroll: '1m', size: this.BATCH_SIZE }); let response = await this.client.search({
index: this.index,
scroll: '1m',
size: this.BATCH_SIZE,
});
while (true) { while (true) {
response.body.hits.hits.forEach((hit: any) => allDocIds.push(hit._id)); response.body.hits.hits.forEach((hit: any) => allDocIds.push(hit._id));
if (!response.body.hits.hits.length) { if (!response.body.hits.hits.length) {
@ -133,15 +178,15 @@ export class ElasticDoc {
mappings: { mappings: {
properties: { properties: {
date: { date: {
type: 'date' type: 'date',
}, },
aggregationData: { aggregationData: {
type: 'object', type: 'object',
enabled: true enabled: true,
} },
} },
} },
} },
}); });
} }
@ -152,7 +197,7 @@ export class ElasticDoc {
await this.storeSnapshot(newSnapshot); await this.storeSnapshot(newSnapshot);
} }
private async getLastSnapshot(): Promise<ISnapshot | null> { private async getLastSnapshot(): Promise<ISnapshot | null> {
const snapshotIndex = `${this.index}_snapshots`; const snapshotIndex = `${this.index}_snapshots`;
const { body: indexExists } = await this.client.indices.exists({ index: snapshotIndex }); const { body: indexExists } = await this.client.indices.exists({ index: snapshotIndex });
@ -163,7 +208,7 @@ private async getLastSnapshot(): Promise<ISnapshot | null> {
const response = await this.client.search({ const response = await this.client.search({
index: snapshotIndex, index: snapshotIndex,
sort: 'date:desc', sort: 'date:desc',
size: 1 size: 1,
}); });
if (response.body.hits.hits.length > 0) { if (response.body.hits.hits.length > 0) {
@ -177,9 +222,12 @@ private async getLastSnapshot(): Promise<ISnapshot | null> {
} }
} }
private async *getDocumentIterator() { private async *getDocumentIterator() {
let response = await this.client.search({ index: this.index, scroll: '1m', size: this.BATCH_SIZE }); let response = await this.client.search({
index: this.index,
scroll: '1m',
size: this.BATCH_SIZE,
});
while (true) { while (true) {
for (const hit of response.body.hits.hits) { for (const hit of response.body.hits.hits) {
yield hit._source; yield hit._source;