From 3fa7d6623624d8ad9ba9c95fc41ad0cdc29c1a78 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Thu, 17 Aug 2023 19:21:26 +0200 Subject: [PATCH] fix(core): update --- ts/00_commitinfo_data.ts | 2 +- ...icindex.ts => els.classes.elasticindex.ts} | 0 ...ler.ts => els.classes.elasticscheduler.ts} | 0 ts/els.classes.fastpush.ts | 47 +++++++++++++++++++ ts/els.classes.smartlogdestination.ts | 4 +- ts/index.ts | 1 + 6 files changed, 51 insertions(+), 3 deletions(-) rename ts/{elasticsearch.classes.elasticindex.ts => els.classes.elasticindex.ts} (100%) rename ts/{elasticsearch.classes.elasticscheduler.ts => els.classes.elasticscheduler.ts} (100%) create mode 100644 ts/els.classes.fastpush.ts diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index f3233b6..b9fe829 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@apiclient.xyz/elasticsearch', - version: '1.0.51', + version: '1.0.52', description: 'log to elasticsearch in a kibana compatible format' } diff --git a/ts/elasticsearch.classes.elasticindex.ts b/ts/els.classes.elasticindex.ts similarity index 100% rename from ts/elasticsearch.classes.elasticindex.ts rename to ts/els.classes.elasticindex.ts diff --git a/ts/elasticsearch.classes.elasticscheduler.ts b/ts/els.classes.elasticscheduler.ts similarity index 100% rename from ts/elasticsearch.classes.elasticscheduler.ts rename to ts/els.classes.elasticscheduler.ts diff --git a/ts/els.classes.fastpush.ts b/ts/els.classes.fastpush.ts new file mode 100644 index 0000000..6dabbb8 --- /dev/null +++ b/ts/els.classes.fastpush.ts @@ -0,0 +1,47 @@ +import { Client as ElasticClient } from '@elastic/elasticsearch'; + +export class FastPush { + private client: ElasticClient; + + constructor(node: string, auth?: { username: string; password: string }) { + this.client = new ElasticClient({ + node: node, + ...(auth && { auth: auth }), + }); + } + + async pushToIndex(indexName: string, docArray: any[]) { + if (docArray.length === 0) return; + + // Check if index exists + const { body: indexExists } = await this.client.indices.exists({ index: indexName }); + + if (!indexExists) { + // Create index with mappings (for simplicity, we use dynamic mapping) + await this.client.indices.create({ + index: indexName, + body: { + mappings: { + dynamic: "true", + properties: { + // If there's a need for specific mappings, they can be added here + }, + }, + }, + }); + } + + // Bulk insert documents + const bulkBody = []; + for (const doc of docArray) { + bulkBody.push({ + index: { + _index: indexName, + }, + }); + bulkBody.push(doc); + } + + await this.client.bulk({ body: bulkBody }); + } +} diff --git a/ts/els.classes.smartlogdestination.ts b/ts/els.classes.smartlogdestination.ts index b318169..4e2d2c7 100644 --- a/ts/els.classes.smartlogdestination.ts +++ b/ts/els.classes.smartlogdestination.ts @@ -3,8 +3,8 @@ import { Client as ElasticClient } from '@elastic/elasticsearch'; import type { ILogContext, ILogPackage, ILogDestination } from '@pushrocks/smartlog-interfaces'; // other classes -import { ElasticScheduler } from './elasticsearch.classes.elasticscheduler.js'; -import { ElasticIndex } from './elasticsearch.classes.elasticindex.js'; +import { ElasticScheduler } from './els.classes.elasticscheduler.js'; +import { ElasticIndex } from './els.classes.elasticindex.js'; export interface IStandardLogParams { message: string; diff --git a/ts/index.ts b/ts/index.ts index f07583c..802bd6f 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -1,3 +1,4 @@ export * from './els.classes.smartlogdestination.js'; +export * from './els.classes.fastpush.js'; export * from './els.classes.elasticdoc.js'; export * from './els.classes.kvstore.js';