Compare commits

...

4 Commits

Author SHA1 Message Date
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
cc30f43a28 1.0.56 2023-08-25 16:06:53 +02:00
100a8fc12e fix(core): update 2023-08-25 16:06:52 +02:00
4 changed files with 65 additions and 10 deletions

View File

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

@ -50,9 +50,21 @@ 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('1', { name: 'doc1' }); await testElasticDoc.pipeDocument({
await testElasticDoc.pipeDocument('2', { name: 'doc2' }); docId: '1',
await testElasticDoc.pipeDocument('1', { name: 'updated doc1' }); timestamp: new Date().toISOString(),
doc: { name: 'doc1' }
});
await testElasticDoc.pipeDocument({
docId: '2',
timestamp: new Date().toISOString(),
doc: { name: 'doc2' }
});
await testElasticDoc.pipeDocument({
docId: '1',
timestamp: new Date().toISOString(),
doc: { name: 'updated doc1' }
});
}); });
tap.test('should delete documents not part of the piping session', async () => { tap.test('should delete documents not part of the piping session', async () => {

View File

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

View File

@ -20,7 +20,8 @@ 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 BATCH_SIZE = 1000; private BATCH_SIZE = 1000;
constructor(options: IElasticDocConstructorOptions) { constructor(options: IElasticDocConstructorOptions) {
@ -31,17 +32,59 @@ export class ElasticDoc {
this.index = options.index; this.index = options.index;
} }
private async ensureIndexExists(doc: any) {
if (!this.indexInitialized) {
const { body: indexExists } = await this.client.indices.exists({ index: this.index });
if (!indexExists) {
const mappings = this.createMappingsFromDoc(doc);
await this.client.indices.create({
index: this.index,
body: {
mappings,
settings: {
// You can define the settings according to your requirements here
},
},
});
}
this.indexInitialized = true;
}
}
private createMappingsFromDoc(doc: any): any {
const properties: any = {};
for (const key in doc) {
if (key === '@timestamp') {
properties[key] = { type: 'date' };
continue;
}
properties[key] = { type: typeof doc[key] === 'number' ? 'float' : 'text' };
}
return { properties };
}
async startPipingSession() { async startPipingSession() {
this.sessionDocs.clear(); this.sessionDocs.clear();
} }
async pipeDocument(docId: string, doc: any) { async pipeDocument(optionsArg: {
docId: string;
timestamp: string | number;
doc: any;
}) {
await this.ensureIndexExists(optionsArg.doc);
const documentBody = {
...optionsArg.doc,
...(optionsArg.timestamp && { '@timestamp': optionsArg.timestamp }),
};
await this.client.index({ await this.client.index({
index: this.index, index: this.index,
id: docId, id: optionsArg.docId,
body: doc, body: documentBody,
}); });
this.sessionDocs.add(docId); this.sessionDocs.add(optionsArg.docId);
} }
async endPipingSession() { async endPipingSession() {