fix(core): update

This commit is contained in:
Philipp Kunz 2023-08-29 11:11:25 +02:00
parent cdbf1fd316
commit 203444d1a6
8 changed files with 1805 additions and 538 deletions

View File

@ -14,20 +14,20 @@
"buildDocs": "tsdoc"
},
"devDependencies": {
"@gitzone/tsbuild": "^2.1.66",
"@gitzone/tsrun": "^1.2.42",
"@gitzone/tstest": "^1.0.74",
"@pushrocks/qenv": "^5.0.2",
"@pushrocks/tapbundle": "^5.0.8",
"@types/node": "^20.3.3"
"@git.zone/tsbuild": "^2.1.70",
"@git.zone/tsrun": "^1.2.46",
"@git.zone/tstest": "^1.0.80",
"@push.rocks/qenv": "^6.0.2",
"@push.rocks/tapbundle": "^5.0.15",
"@types/node": "^20.5.7"
},
"dependencies": {
"@elastic/elasticsearch": "7.17.11-patch.1",
"@pushrocks/lik": "^6.0.2",
"@pushrocks/smartdelay": "^3.0.1",
"@pushrocks/smartlog-interfaces": "^3.0.0",
"@pushrocks/smartpromise": "^4.0.2",
"@pushrocks/smarttime": "^4.0.1"
"@elastic/elasticsearch": "8.9.0",
"@push.rocks/lik": "^6.0.5",
"@push.rocks/smartdelay": "^3.0.5",
"@push.rocks/smartlog-interfaces": "^3.0.0",
"@push.rocks/smartpromise": "^4.0.2",
"@push.rocks/smarttime": "^4.0.5"
},
"files": [
"ts/**/*",

2203
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,5 @@
import { expect, tap } from '@pushrocks/tapbundle';
import { Qenv } from '@pushrocks/qenv';
import { expect, tap } from '@push.rocks/tapbundle';
import { Qenv } from '@push.rocks/qenv';
import * as elasticsearch from '../ts/index.js';
let testElasticLog: elasticsearch.ElsSmartlogDestination<any>;

View File

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

View File

@ -39,7 +39,7 @@ export class ElasticDoc {
private async ensureIndexExists(doc: any) {
if (!this.indexInitialized) {
const { body: indexExists } = await this.client.indices.exists({ index: this.index });
const indexExists = await this.client.indices.exists({ index: this.index });
if (!indexExists) {
const mappings = this.createMappingsFromDoc(doc);
await this.client.indices.create({
@ -81,7 +81,7 @@ export class ElasticDoc {
});
// If the search query succeeded, the index exists.
const hit = response.body.hits.hits[0];
const hit = response.hits.hits[0];
this.latestTimestamp = hit?._source?.['@timestamp'] || null;
if (this.latestTimestamp) {
@ -137,11 +137,11 @@ export class ElasticDoc {
size: this.BATCH_SIZE,
});
while (true) {
response.body.hits.hits.forEach((hit: any) => allDocIds.push(hit._id));
if (!response.body.hits.hits.length) {
response.hits.hits.forEach((hit: any) => allDocIds.push(hit._id));
if (!response.hits.hits.length) {
break;
}
response = await this.client.scroll({ scroll_id: response.body._scroll_id, scroll: '1m' });
response = await this.client.scroll({ scroll_id: response._scroll_id, scroll: '1m' });
}
for (const docId of allDocIds) {
@ -170,7 +170,7 @@ export class ElasticDoc {
async takeSnapshot(processIterator: SnapshotProcessor) {
const snapshotIndex = `${this.index}_snapshots`;
const { body: indexExists } = await this.client.indices.exists({ index: snapshotIndex });
const indexExists = await this.client.indices.exists({ index: snapshotIndex });
if (!indexExists) {
await this.client.indices.create({
index: snapshotIndex,
@ -199,7 +199,7 @@ export class ElasticDoc {
private async getLastSnapshot(): Promise<ISnapshot | null> {
const snapshotIndex = `${this.index}_snapshots`;
const { body: indexExists } = await this.client.indices.exists({ index: snapshotIndex });
const indexExists = await this.client.indices.exists({ index: snapshotIndex });
if (!indexExists) {
return null;
@ -211,11 +211,12 @@ export class ElasticDoc {
size: 1,
});
if (response.body.hits.hits.length > 0) {
const hit = response.body.hits.hits[0];
if (response.hits.hits.length > 0) {
const hit = response.hits.hits[0];
console.log(hit);
return {
date: hit._source.date,
aggregationData: hit._source.aggregationData,
date: hit._source['date'],
aggregationData: hit._source['aggregationData'],
};
} else {
return null;
@ -229,15 +230,15 @@ export class ElasticDoc {
size: this.BATCH_SIZE,
});
while (true) {
for (const hit of response.body.hits.hits) {
for (const hit of response.hits.hits) {
yield hit._source;
}
if (!response.body.hits.hits.length) {
if (!response.hits.hits.length) {
break;
}
response = await this.client.scroll({ scroll_id: response.body._scroll_id, scroll: '1m' });
response = await this.client.scroll({ scroll_id: response._scroll_id, scroll: '1m' });
}
}

View File

@ -1,7 +1,7 @@
import * as plugins from './els.plugins.js';
import { ElsSmartlogDestination } from './els.classes.smartlogdestination.js';
import { type ILogPackage } from '@pushrocks/smartlog-interfaces';
import { Stringmap } from '@pushrocks/lik';
import { type ILogPackage } from '@push.rocks/smartlog-interfaces';
import { Stringmap } from '@push.rocks/lik';
export class ElasticIndex {
private stringmap = new Stringmap();
@ -18,7 +18,7 @@ export class ElasticIndex {
const responseArg = await this.elasticSearchRef.client.cat.indices({
format: 'json',
bytes: 'm',
bytes: 'mb',
}).catch(err => {
console.log(err);
});
@ -27,8 +27,8 @@ export class ElasticIndex {
throw new Error('Could not get valid response from elastic search');
}
if (Array.isArray(responseArg.body)) {
const filteredIndices = responseArg.body.filter((indexObjectArg) => {
if (Array.isArray(responseArg)) {
const filteredIndices = responseArg.filter((indexObjectArg) => {
return indexObjectArg.index.startsWith(prefixArg);
});
const filteredIndexNames = filteredIndices.map((indexObjectArg) => {
@ -39,8 +39,8 @@ export class ElasticIndex {
let index = null;
if (Array.isArray(responseArg.body)) {
index = responseArg.body.find((indexItemArg) => {
if (Array.isArray(responseArg)) {
index = responseArg.find((indexItemArg) => {
return indexItemArg.index === indexNameArg;
});
}
@ -55,7 +55,7 @@ export class ElasticIndex {
public async createNewIndex(indexNameArg: string) {
const response = await this.elasticSearchRef.client.indices.create({
wait_for_active_shards: '1',
wait_for_active_shards: 1,
index: indexNameArg,
body: {
mappings: {

View File

@ -1,8 +1,5 @@
// interfaces
import { Client as ElasticClient } from '@elastic/elasticsearch';
import type { ILogContext, ILogPackage, ILogDestination } from '@pushrocks/smartlog-interfaces';
// other classes
import type { ILogContext, ILogPackage, ILogDestination } from '@push.rocks/smartlog-interfaces';
import { ElasticScheduler } from './els.classes.elasticscheduler.js';
import { ElasticIndex } from './els.classes.elasticindex.js';
@ -29,10 +26,6 @@ export class ElsSmartlogDestination<T> {
public indexPrefix: string;
public indexRetention: number;
/**
* sets up an instance of Elastic log
* @param optionsArg
*/
constructor(optionsArg: IElasticSearchConstructorOptions) {
this.client = new ElasticClient({
node: optionsArg.node,
@ -40,42 +33,27 @@ export class ElsSmartlogDestination<T> {
});
this.indexPrefix = `${optionsArg.indexPrefix}`;
this.indexRetention = optionsArg.indexRetention;
this.setupDataStream();
}
private async setupDataStream() {
// Define an index template
await this.client.indices.putIndexTemplate({
name: `${this.indexPrefix}_template`,
index_patterns: [`${this.indexPrefix}-*`],
data_stream: {},
});
}
public async log(logPackageArg: ILogPackage, scheduleOverwrite = false) {
const now = new Date();
const indexToUse = `${this.indexPrefix}-${now.toISOString().split('T')[0]}`;
const indexToUse = `${this.indexPrefix}-data-stream`; // Use data stream name
if (this.elasticScheduler.docsScheduled && !scheduleOverwrite) {
this.elasticScheduler.scheduleDoc(logPackageArg);
return;
}
// Make sure the index is created with a mapping for dynamic JSON
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(
{
index: indexToUse,
@ -83,15 +61,6 @@ export class ElsSmartlogDestination<T> {
'@timestamp': new Date(logPackageArg.timestamp).toISOString(),
...logPackageArg,
},
},
(error, response) => {
if (error) {
console.log('ElasticLog encountered an error:');
console.log(error);
this.elasticScheduler.addFailedDoc(logPackageArg);
} else {
// console.log(`ElasticLog: ${logPackageArg.message}`);
}
}
);
}

View File

@ -1,8 +1,8 @@
import * as elasticsearch from '@elastic/elasticsearch';
import * as lik from '@pushrocks/lik';
import * as smartdelay from '@pushrocks/smartdelay';
import * as smartlogInterfaces from '@pushrocks/smartlog-interfaces';
import * as smartpromise from '@pushrocks/smartpromise';
import * as smarttime from '@pushrocks/smarttime';
import * as lik from '@push.rocks/lik';
import * as smartdelay from '@push.rocks/smartdelay';
import * as smartlogInterfaces from '@push.rocks/smartlog-interfaces';
import * as smartpromise from '@push.rocks/smartpromise';
import * as smarttime from '@push.rocks/smarttime';
export { elasticsearch, lik, smartdelay, smartlogInterfaces, smartpromise, smarttime };