Compare commits
18 Commits
Author | SHA1 | Date | |
---|---|---|---|
bc9de8e4d6 | |||
3fa7d66236 | |||
2a0b0b2478 | |||
35e99663a4 | |||
2cc5855206 | |||
8f9f2fdf05 | |||
7ef36b5c40 | |||
67a8f3fe4d | |||
5ae2c37519 | |||
fcb67ec878 | |||
9e25494f8f | |||
dd8ba4736a | |||
d395310410 | |||
49233ce45f | |||
fb93dce8bc | |||
30cbc05aa2 | |||
2a595a1a9a | |||
d62b18e93c |
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@apiclient.xyz/elasticsearch",
|
"name": "@apiclient.xyz/elasticsearch",
|
||||||
"version": "1.0.43",
|
"version": "1.0.52",
|
||||||
"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",
|
||||||
|
0
test/00tapwrap.ts
Normal file
0
test/00tapwrap.ts
Normal file
@ -2,10 +2,11 @@ import { expect, tap } from '@pushrocks/tapbundle';
|
|||||||
import { Qenv } from '@pushrocks/qenv';
|
import { Qenv } from '@pushrocks/qenv';
|
||||||
import * as elasticsearch from '../ts/index.js';
|
import * as elasticsearch from '../ts/index.js';
|
||||||
|
|
||||||
let testElasticLog: elasticsearch.ElasticSearch<any>;
|
let testElasticLog: elasticsearch.ElsSmartlogDestination<any>;
|
||||||
|
let testElasticDoc: elasticsearch.ElasticDoc;
|
||||||
|
|
||||||
tap.test('first test', async () => {
|
tap.test('first test', async () => {
|
||||||
testElasticLog = new elasticsearch.ElasticSearch({
|
testElasticLog = new elasticsearch.ElsSmartlogDestination({
|
||||||
indexPrefix: 'testprefix',
|
indexPrefix: 'testprefix',
|
||||||
indexRetention: 7,
|
indexRetention: 7,
|
||||||
node: 'http://localhost:9200',
|
node: 'http://localhost:9200',
|
||||||
@ -14,7 +15,7 @@ tap.test('first test', async () => {
|
|||||||
password: 'YourPassword'
|
password: 'YourPassword'
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
expect(testElasticLog).toBeInstanceOf(elasticsearch.ElasticSearch);
|
expect(testElasticLog).toBeInstanceOf(elasticsearch.ElsSmartlogDestination);
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should send a message to Elasticsearch', async () => {
|
tap.test('should send a message to Elasticsearch', async () => {
|
||||||
@ -35,4 +36,42 @@ tap.test('should send a message to Elasticsearch', async () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tap.test('should create an ElasticDoc instance', async () => {
|
||||||
|
testElasticDoc = new elasticsearch.ElasticDoc({
|
||||||
|
index: 'testindex',
|
||||||
|
node: 'http://localhost:9200',
|
||||||
|
auth: {
|
||||||
|
username: 'elastic',
|
||||||
|
password: 'YourPassword'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
expect(testElasticDoc).toBeInstanceOf(elasticsearch.ElasticDoc);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should add and update documents in a piping session', async () => {
|
||||||
|
await testElasticDoc.startPipingSession();
|
||||||
|
await testElasticDoc.pipeDocument('1', { name: 'doc1' });
|
||||||
|
await testElasticDoc.pipeDocument('2', { name: 'doc2' });
|
||||||
|
await testElasticDoc.pipeDocument('1', { name: 'updated doc1' });
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should delete documents not part of the piping session', async () => {
|
||||||
|
await testElasticDoc.endPipingSession();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should take and store snapshot', async () => {
|
||||||
|
await testElasticDoc.takeSnapshot(async (iterator, prevSnapshot) => {
|
||||||
|
const aggregationData = [];
|
||||||
|
for await (const doc of iterator) {
|
||||||
|
// Sample aggregation: counting documents
|
||||||
|
aggregationData.push(doc);
|
||||||
|
}
|
||||||
|
const snapshot = {
|
||||||
|
date: new Date().toISOString(),
|
||||||
|
aggregationData,
|
||||||
|
};
|
||||||
|
return snapshot;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@apiclient.xyz/elasticsearch',
|
name: '@apiclient.xyz/elasticsearch',
|
||||||
version: '1.0.43',
|
version: '1.0.52',
|
||||||
description: 'log to elasticsearch in a kibana compatible format'
|
description: 'log to elasticsearch in a kibana compatible format'
|
||||||
}
|
}
|
||||||
|
@ -1,44 +0,0 @@
|
|||||||
import { ElasticSearch, type IStandardLogParams } from './elasticsearch.classes.elasticsearch.js';
|
|
||||||
|
|
||||||
export class ElasticScheduler {
|
|
||||||
elasticSearchRef: ElasticSearch<any>;
|
|
||||||
docsScheduled = false;
|
|
||||||
docsStorage: any[] = [];
|
|
||||||
|
|
||||||
constructor(elasticLogRefArg: ElasticSearch<any>) {
|
|
||||||
this.elasticSearchRef = elasticLogRefArg;
|
|
||||||
}
|
|
||||||
|
|
||||||
public addFailedDoc(objectArg: any | IStandardLogParams) {
|
|
||||||
this.docsStorage.push(objectArg);
|
|
||||||
this.setRetry();
|
|
||||||
}
|
|
||||||
public scheduleDoc(logObject: any) {
|
|
||||||
this.docsStorage.push(logObject);
|
|
||||||
}
|
|
||||||
|
|
||||||
public setRetry() {
|
|
||||||
setTimeout(() => {
|
|
||||||
const oldStorage = this.docsStorage;
|
|
||||||
this.docsStorage = [];
|
|
||||||
for (let logObject of oldStorage) {
|
|
||||||
this.elasticSearchRef.log(logObject, true);
|
|
||||||
}
|
|
||||||
if (this.docsStorage.length === 0) {
|
|
||||||
console.log('ElasticLog retry success!!!');
|
|
||||||
this.docsScheduled = false;
|
|
||||||
} else {
|
|
||||||
console.log('ElasticLog retry failed');
|
|
||||||
this.setRetry();
|
|
||||||
}
|
|
||||||
}, 5000);
|
|
||||||
}
|
|
||||||
|
|
||||||
public deferSend() {
|
|
||||||
if (!this.docsScheduled) {
|
|
||||||
console.log('Retry ElasticLog in 5 seconds!');
|
|
||||||
this.docsScheduled = true;
|
|
||||||
this.setRetry();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
159
ts/els.classes.elasticdoc.ts
Normal file
159
ts/els.classes.elasticdoc.ts
Normal file
@ -0,0 +1,159 @@
|
|||||||
|
import { Client as ElasticClient } from '@elastic/elasticsearch';
|
||||||
|
|
||||||
|
export interface IElasticDocConstructorOptions {
|
||||||
|
index: string;
|
||||||
|
node: string;
|
||||||
|
auth?: {
|
||||||
|
username: string;
|
||||||
|
password: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ISnapshot {
|
||||||
|
date: string;
|
||||||
|
aggregationData: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type SnapshotProcessor = (iterator: AsyncIterable<any>, prevSnapshot: ISnapshot | null) => Promise<ISnapshot>;
|
||||||
|
|
||||||
|
export class ElasticDoc {
|
||||||
|
public client: ElasticClient;
|
||||||
|
public index: string;
|
||||||
|
private sessionDocs: Set<string> = new Set();
|
||||||
|
|
||||||
|
private BATCH_SIZE = 1000;
|
||||||
|
|
||||||
|
constructor(options: IElasticDocConstructorOptions) {
|
||||||
|
this.client = new ElasticClient({
|
||||||
|
node: options.node,
|
||||||
|
...(options.auth && { auth: options.auth }),
|
||||||
|
});
|
||||||
|
this.index = options.index;
|
||||||
|
}
|
||||||
|
|
||||||
|
async startPipingSession() {
|
||||||
|
this.sessionDocs.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
async pipeDocument(docId: string, doc: any) {
|
||||||
|
await this.client.index({
|
||||||
|
index: this.index,
|
||||||
|
id: docId,
|
||||||
|
body: doc,
|
||||||
|
});
|
||||||
|
this.sessionDocs.add(docId);
|
||||||
|
}
|
||||||
|
|
||||||
|
async endPipingSession() {
|
||||||
|
const allDocIds: string[] = [];
|
||||||
|
const responseQueue = [];
|
||||||
|
let response = await this.client.search({ index: this.index, scroll: '1m', size: this.BATCH_SIZE });
|
||||||
|
while (true) {
|
||||||
|
response.body.hits.hits.forEach((hit: any) => allDocIds.push(hit._id));
|
||||||
|
if (!response.body.hits.hits.length) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
response = await this.client.scroll({ scroll_id: response.body._scroll_id, scroll: '1m' });
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const docId of allDocIds) {
|
||||||
|
if (!this.sessionDocs.has(docId)) {
|
||||||
|
responseQueue.push({
|
||||||
|
delete: {
|
||||||
|
_index: this.index,
|
||||||
|
_id: docId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (responseQueue.length >= this.BATCH_SIZE) {
|
||||||
|
await this.client.bulk({ refresh: true, body: responseQueue });
|
||||||
|
responseQueue.length = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (responseQueue.length > 0) {
|
||||||
|
await this.client.bulk({ refresh: true, body: responseQueue });
|
||||||
|
}
|
||||||
|
|
||||||
|
this.sessionDocs.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
async takeSnapshot(processIterator: SnapshotProcessor) {
|
||||||
|
const snapshotIndex = `${this.index}_snapshots`;
|
||||||
|
|
||||||
|
const { body: indexExists } = await this.client.indices.exists({ index: snapshotIndex });
|
||||||
|
if (!indexExists) {
|
||||||
|
await this.client.indices.create({
|
||||||
|
index: snapshotIndex,
|
||||||
|
body: {
|
||||||
|
mappings: {
|
||||||
|
properties: {
|
||||||
|
date: {
|
||||||
|
type: 'date'
|
||||||
|
},
|
||||||
|
aggregationData: {
|
||||||
|
type: 'object',
|
||||||
|
enabled: true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const documentIterator = this.getDocumentIterator();
|
||||||
|
|
||||||
|
const newSnapshot = await processIterator(documentIterator, await this.getLastSnapshot());
|
||||||
|
|
||||||
|
await this.storeSnapshot(newSnapshot);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async getLastSnapshot(): Promise<ISnapshot | null> {
|
||||||
|
const snapshotIndex = `${this.index}_snapshots`;
|
||||||
|
const { body: indexExists } = await this.client.indices.exists({ index: snapshotIndex });
|
||||||
|
|
||||||
|
if (!indexExists) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await this.client.search({
|
||||||
|
index: snapshotIndex,
|
||||||
|
sort: 'date:desc',
|
||||||
|
size: 1
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.body.hits.hits.length > 0) {
|
||||||
|
const hit = response.body.hits.hits[0];
|
||||||
|
return {
|
||||||
|
date: hit._source.date,
|
||||||
|
aggregationData: hit._source.aggregationData,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private async *getDocumentIterator() {
|
||||||
|
let response = await this.client.search({ index: this.index, scroll: '1m', size: this.BATCH_SIZE });
|
||||||
|
while (true) {
|
||||||
|
for (const hit of response.body.hits.hits) {
|
||||||
|
yield hit._source;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!response.body.hits.hits.length) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
response = await this.client.scroll({ scroll_id: response.body._scroll_id, scroll: '1m' });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async storeSnapshot(snapshot: ISnapshot) {
|
||||||
|
await this.client.index({
|
||||||
|
index: `${this.index}_snapshots`,
|
||||||
|
body: snapshot,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -1,21 +1,21 @@
|
|||||||
import * as plugins from './elasticsearch.plugins.js';
|
import * as plugins from './els.plugins.js';
|
||||||
import { ElasticSearch } from './elasticsearch.classes.elasticsearch.js';
|
import { ElsSmartlogDestination } from './els.classes.smartlogdestination.js';
|
||||||
import { type ILogPackage } from '@pushrocks/smartlog-interfaces';
|
import { type ILogPackage } from '@pushrocks/smartlog-interfaces';
|
||||||
|
|
||||||
import { Stringmap } from '@pushrocks/lik';
|
import { Stringmap } from '@pushrocks/lik';
|
||||||
|
|
||||||
export class ElasticIndex {
|
export class ElasticIndex {
|
||||||
private stringmap = new Stringmap();
|
private stringmap = new Stringmap();
|
||||||
private elasticSearchRef: ElasticSearch<any>;
|
private elasticSearchRef: ElsSmartlogDestination<any>;
|
||||||
|
|
||||||
constructor(elasticSearchInstanceArg: ElasticSearch<ILogPackage>) {
|
constructor(elasticSearchInstanceArg: ElsSmartlogDestination<ILogPackage>) {
|
||||||
this.elasticSearchRef = elasticSearchInstanceArg;
|
this.elasticSearchRef = elasticSearchInstanceArg;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async ensureIndex(prefixArg: string, indexNameArg: string) {
|
public async ensureIndex(prefixArg: string, indexNameArg: string) {
|
||||||
if (this.stringmap.checkString(indexNameArg)) {
|
if (this.stringmap.checkString(indexNameArg)) {
|
||||||
return;
|
return indexNameArg;
|
||||||
}
|
}
|
||||||
|
|
||||||
const responseArg = await this.elasticSearchRef.client.cat.indices({
|
const responseArg = await this.elasticSearchRef.client.cat.indices({
|
||||||
format: 'json',
|
format: 'json',
|
||||||
bytes: 'm',
|
bytes: 'm',
|
||||||
@ -27,7 +27,6 @@ export class ElasticIndex {
|
|||||||
throw new Error('Could not get valid response from elastic search');
|
throw new Error('Could not get valid response from elastic search');
|
||||||
}
|
}
|
||||||
|
|
||||||
// lets delete indexes that violate the retention
|
|
||||||
if (Array.isArray(responseArg.body)) {
|
if (Array.isArray(responseArg.body)) {
|
||||||
const filteredIndices = responseArg.body.filter((indexObjectArg) => {
|
const filteredIndices = responseArg.body.filter((indexObjectArg) => {
|
||||||
return indexObjectArg.index.startsWith(prefixArg);
|
return indexObjectArg.index.startsWith(prefixArg);
|
||||||
@ -49,6 +48,7 @@ export class ElasticIndex {
|
|||||||
if (!index) {
|
if (!index) {
|
||||||
await this.createNewIndex(indexNameArg);
|
await this.createNewIndex(indexNameArg);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.stringmap.addString(indexNameArg);
|
this.stringmap.addString(indexNameArg);
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
@ -57,6 +57,23 @@ export class ElasticIndex {
|
|||||||
const response = await this.elasticSearchRef.client.indices.create({
|
const response = await this.elasticSearchRef.client.indices.create({
|
||||||
wait_for_active_shards: '1',
|
wait_for_active_shards: '1',
|
||||||
index: indexNameArg,
|
index: indexNameArg,
|
||||||
|
body: {
|
||||||
|
mappings: {
|
||||||
|
properties: {
|
||||||
|
'@timestamp': {
|
||||||
|
type: 'date',
|
||||||
|
},
|
||||||
|
logPackageArg: {
|
||||||
|
properties: {
|
||||||
|
payload: {
|
||||||
|
type: 'object',
|
||||||
|
dynamic: true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
63
ts/els.classes.elasticscheduler.ts
Normal file
63
ts/els.classes.elasticscheduler.ts
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
import { ElsSmartlogDestination, type IStandardLogParams } from './els.classes.smartlogdestination.js';
|
||||||
|
|
||||||
|
export class ElasticScheduler {
|
||||||
|
elasticSearchRef: ElsSmartlogDestination<any>;
|
||||||
|
docsScheduled = false;
|
||||||
|
docsStorage: any[] = [];
|
||||||
|
|
||||||
|
// maximum size of the buffer
|
||||||
|
maxBufferSize = 500;
|
||||||
|
|
||||||
|
constructor(elasticLogRefArg: ElsSmartlogDestination<any>) {
|
||||||
|
this.elasticSearchRef = elasticLogRefArg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public addFailedDoc(objectArg: any | IStandardLogParams) {
|
||||||
|
this.addToStorage(objectArg);
|
||||||
|
this.setRetry();
|
||||||
|
}
|
||||||
|
|
||||||
|
public scheduleDoc(logObject: any) {
|
||||||
|
this.addToStorage(logObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
private addToStorage(logObject: any) {
|
||||||
|
this.docsStorage.push(logObject);
|
||||||
|
|
||||||
|
// if buffer is full, send logs immediately
|
||||||
|
if (this.docsStorage.length >= this.maxBufferSize) {
|
||||||
|
this.flushLogsToElasticSearch();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private flushLogsToElasticSearch() {
|
||||||
|
const oldStorage = this.docsStorage;
|
||||||
|
this.docsStorage = [];
|
||||||
|
|
||||||
|
for (let logObject of oldStorage) {
|
||||||
|
this.elasticSearchRef.log(logObject, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public setRetry() {
|
||||||
|
setTimeout(() => {
|
||||||
|
this.flushLogsToElasticSearch();
|
||||||
|
|
||||||
|
if (this.docsStorage.length === 0) {
|
||||||
|
console.log('ElasticLog retry success!!!');
|
||||||
|
this.docsScheduled = false;
|
||||||
|
} else {
|
||||||
|
console.log('ElasticLog retry failed');
|
||||||
|
this.setRetry();
|
||||||
|
}
|
||||||
|
}, 5000);
|
||||||
|
}
|
||||||
|
|
||||||
|
public deferSend() {
|
||||||
|
if (!this.docsScheduled) {
|
||||||
|
console.log('Retry ElasticLog in 5 seconds!');
|
||||||
|
this.docsScheduled = true;
|
||||||
|
this.setRetry();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
47
ts/els.classes.fastpush.ts
Normal file
47
ts/els.classes.fastpush.ts
Normal file
@ -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 });
|
||||||
|
}
|
||||||
|
}
|
111
ts/els.classes.kvstore.ts
Normal file
111
ts/els.classes.kvstore.ts
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
import * as plugins from './els.plugins.js';
|
||||||
|
import { Client as ElasticClient } from '@elastic/elasticsearch';
|
||||||
|
|
||||||
|
export interface IElasticKVStoreConstructorOptions {
|
||||||
|
index: string;
|
||||||
|
node: string;
|
||||||
|
auth?: {
|
||||||
|
username: string;
|
||||||
|
password: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export class ElasticKVStore {
|
||||||
|
public client: ElasticClient;
|
||||||
|
public index: string;
|
||||||
|
private readyDeferred: any;
|
||||||
|
|
||||||
|
constructor(options: IElasticKVStoreConstructorOptions) {
|
||||||
|
this.client = new ElasticClient({
|
||||||
|
node: options.node,
|
||||||
|
...(options.auth && { auth: options.auth }),
|
||||||
|
});
|
||||||
|
this.index = options.index;
|
||||||
|
this.readyDeferred = plugins.smartpromise.defer();
|
||||||
|
this.setupIndex();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async setupIndex() {
|
||||||
|
try {
|
||||||
|
const { body: indexExists } = await this.client.indices.exists({ index: this.index });
|
||||||
|
|
||||||
|
if (!indexExists) {
|
||||||
|
await this.client.indices.create({
|
||||||
|
index: this.index,
|
||||||
|
body: {
|
||||||
|
mappings: {
|
||||||
|
properties: {
|
||||||
|
key: {
|
||||||
|
type: 'keyword'
|
||||||
|
},
|
||||||
|
value: {
|
||||||
|
type: 'text'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
this.readyDeferred.resolve();
|
||||||
|
} catch (err) {
|
||||||
|
this.readyDeferred.reject(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async set(key: string, value: string) {
|
||||||
|
await this.readyDeferred.promise;
|
||||||
|
await this.client.index({
|
||||||
|
index: this.index,
|
||||||
|
id: key,
|
||||||
|
body: {
|
||||||
|
key,
|
||||||
|
value
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async get(key: string): Promise<string | null> {
|
||||||
|
await this.readyDeferred.promise;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await this.client.get({
|
||||||
|
index: this.index,
|
||||||
|
id: key
|
||||||
|
});
|
||||||
|
return response.body._source.value;
|
||||||
|
} catch (error) {
|
||||||
|
if (error.meta && error.meta.statusCode === 404) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async delete(key: string) {
|
||||||
|
await this.readyDeferred.promise;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await this.client.delete({
|
||||||
|
index: this.index,
|
||||||
|
id: key
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
if (error.meta && error.meta.statusCode !== 404) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async clear() {
|
||||||
|
await this.readyDeferred.promise;
|
||||||
|
|
||||||
|
await this.client.deleteByQuery({
|
||||||
|
index: this.index,
|
||||||
|
body: {
|
||||||
|
query: {
|
||||||
|
match_all: {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -3,8 +3,8 @@ import { Client as ElasticClient } from '@elastic/elasticsearch';
|
|||||||
import type { ILogContext, ILogPackage, ILogDestination } from '@pushrocks/smartlog-interfaces';
|
import type { ILogContext, ILogPackage, ILogDestination } from '@pushrocks/smartlog-interfaces';
|
||||||
|
|
||||||
// other classes
|
// other classes
|
||||||
import { ElasticScheduler } from './elasticsearch.classes.elasticscheduler.js';
|
import { ElasticScheduler } from './els.classes.elasticscheduler.js';
|
||||||
import { ElasticIndex } from './elasticsearch.classes.elasticindex.js';
|
import { ElasticIndex } from './els.classes.elasticindex.js';
|
||||||
|
|
||||||
export interface IStandardLogParams {
|
export interface IStandardLogParams {
|
||||||
message: string;
|
message: string;
|
||||||
@ -21,7 +21,7 @@ export interface IElasticSearchConstructorOptions {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ElasticSearch<T> {
|
export class ElsSmartlogDestination<T> {
|
||||||
public client: ElasticClient;
|
public client: ElasticClient;
|
||||||
public elasticScheduler = new ElasticScheduler(this);
|
public elasticScheduler = new ElasticScheduler(this);
|
||||||
public elasticIndex: ElasticIndex = new ElasticIndex(this);
|
public elasticIndex: ElasticIndex = new ElasticIndex(this);
|
||||||
@ -38,7 +38,7 @@ export class ElasticSearch<T> {
|
|||||||
node: optionsArg.node,
|
node: optionsArg.node,
|
||||||
...(optionsArg.auth && { auth: optionsArg.auth }),
|
...(optionsArg.auth && { auth: optionsArg.auth }),
|
||||||
});
|
});
|
||||||
this.indexPrefix = optionsArg.indexPrefix;
|
this.indexPrefix = `${optionsArg.indexPrefix}`;
|
||||||
this.indexRetention = optionsArg.indexRetention;
|
this.indexRetention = optionsArg.indexRetention;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,8 +51,31 @@ export class ElasticSearch<T> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await this.elasticIndex.ensureIndex(this.indexPrefix, indexToUse);
|
// Make sure the index is created with a mapping for dynamic JSON
|
||||||
console.log(response);
|
const indexExists = await this.client.indices.exists({ index: indexToUse });
|
||||||
|
if (!indexExists.body) {
|
||||||
|
await this.client.indices.create({
|
||||||
|
index: indexToUse,
|
||||||
|
body: {
|
||||||
|
mappings: {
|
||||||
|
properties: {
|
||||||
|
'@timestamp': {
|
||||||
|
type: 'date',
|
||||||
|
},
|
||||||
|
logPackageArg: {
|
||||||
|
properties: {
|
||||||
|
payload: {
|
||||||
|
type: 'object',
|
||||||
|
dynamic: true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
this.client.index(
|
this.client.index(
|
||||||
{
|
{
|
||||||
index: indexToUse,
|
index: indexToUse,
|
@ -1 +1,4 @@
|
|||||||
export * from './elasticsearch.classes.elasticsearch.js';
|
export * from './els.classes.smartlogdestination.js';
|
||||||
|
export * from './els.classes.fastpush.js';
|
||||||
|
export * from './els.classes.elasticdoc.js';
|
||||||
|
export * from './els.classes.kvstore.js';
|
||||||
|
Reference in New Issue
Block a user