Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
2b0003546a | |||
60617f2fca | |||
9c767d07e4 | |||
f3aa94dcb7 | |||
a0be0edd9d | |||
ad24ba2f5d | |||
b0cf4bb27f | |||
fd29ceab80 |
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@apiclient.xyz/elasticsearch",
|
||||
"version": "1.0.38",
|
||||
"version": "1.0.42",
|
||||
"private": false,
|
||||
"description": "log to elasticsearch in a kibana compatible format",
|
||||
"main": "dist_ts/index.js",
|
||||
@ -27,8 +27,7 @@
|
||||
"@pushrocks/smartdelay": "^3.0.1",
|
||||
"@pushrocks/smartlog-interfaces": "^3.0.0",
|
||||
"@pushrocks/smartpromise": "^4.0.2",
|
||||
"@pushrocks/smarttime": "^4.0.1",
|
||||
"@types/elasticsearch": "^5.0.40"
|
||||
"@pushrocks/smarttime": "^4.0.1"
|
||||
},
|
||||
"files": [
|
||||
"ts/**/*",
|
||||
|
7
pnpm-lock.yaml
generated
7
pnpm-lock.yaml
generated
@ -23,9 +23,6 @@ dependencies:
|
||||
'@pushrocks/smarttime':
|
||||
specifier: ^4.0.1
|
||||
version: 4.0.1
|
||||
'@types/elasticsearch':
|
||||
specifier: ^5.0.40
|
||||
version: 5.0.40
|
||||
|
||||
devDependencies:
|
||||
'@gitzone/tsbuild':
|
||||
@ -1244,10 +1241,6 @@ packages:
|
||||
resolution: {integrity: sha512-tpu0hp+AOIzwdAHyZPzLE5pCf9uT0pb+xZ76T4S7MrY2YTVq918Q7Q2VQ3KCVQqYxM7nxuCK/SL3X97jBEIeKQ==}
|
||||
dev: true
|
||||
|
||||
/@types/elasticsearch@5.0.40:
|
||||
resolution: {integrity: sha512-lhnbkC0XorAD7Dt7X+94cXUSHEdDNnEVk/DgFLHgIZQNhixV631Lj4+KpXunTT5rCHyj9RqK3TfO7QrOiwEeUQ==}
|
||||
dev: false
|
||||
|
||||
/@types/express-serve-static-core@4.17.35:
|
||||
resolution: {integrity: sha512-wALWQwrgiB2AWTT91CB62b6Yt0sNHpznUXeZEcnPU3DRdlDIz74x8Qg1UUYKSVFi+va5vKOLYRBI1bRKiLLKIg==}
|
||||
dependencies:
|
||||
|
@ -2,23 +2,21 @@ import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import { Qenv } from '@pushrocks/qenv';
|
||||
import * as elasticsearch from '../ts/index.js';
|
||||
|
||||
const testQenv = new Qenv('./', './.nogit/');
|
||||
|
||||
let testElasticLog: elasticsearch.ElasticSearch<any>;
|
||||
|
||||
tap.test('first test', async () => {
|
||||
testElasticLog = new elasticsearch.ElasticSearch({
|
||||
indexPrefix: 'smartlog',
|
||||
indexPrefix: 'testprefix',
|
||||
indexRetention: 7,
|
||||
domain: testQenv.getEnvVarOnDemand('ELK_DOMAIN'),
|
||||
port: parseInt(testQenv.getEnvVarOnDemand('ELK_PORT'), 10),
|
||||
ssl: true,
|
||||
node: 'http://localhost:9200',
|
||||
user: 'elastic',
|
||||
pass: 'YourPassword'
|
||||
});
|
||||
expect(testElasticLog).toBeInstanceOf(elasticsearch.ElasticSearch);
|
||||
});
|
||||
|
||||
tap.test('should send a message to Elasticsearch', async () => {
|
||||
testElasticLog.log({
|
||||
await testElasticLog.log({
|
||||
timestamp: Date.now(),
|
||||
type: 'increment',
|
||||
level: 'info',
|
||||
|
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@apiclient.xyz/elasticsearch',
|
||||
version: '1.0.38',
|
||||
version: '1.0.42',
|
||||
description: 'log to elasticsearch in a kibana compatible format'
|
||||
}
|
||||
|
@ -12,91 +12,74 @@ export class ElasticIndex {
|
||||
this.elasticSearchRef = elasticSearchInstanceArg;
|
||||
}
|
||||
|
||||
public async ensureIndex(indexArg: string) {
|
||||
const done = plugins.smartpromise.defer();
|
||||
if (this.stringmap.checkString(indexArg)) {
|
||||
done.resolve();
|
||||
public async ensureIndex(prefixArg: string, indexNameArg: string) {
|
||||
if (this.stringmap.checkString(indexNameArg)) {
|
||||
return;
|
||||
}
|
||||
this.elasticSearchRef.client.cat.indices(
|
||||
{
|
||||
const responseArg = await this.elasticSearchRef.client.cat.indices({
|
||||
format: 'json',
|
||||
bytes: 'm',
|
||||
},
|
||||
async (err, responseArg: any[]) => {
|
||||
if (err) {
|
||||
}).catch(err => {
|
||||
console.log(err);
|
||||
return;
|
||||
});
|
||||
|
||||
if (!responseArg) {
|
||||
throw new Error('Could not get valid response from elastic search');
|
||||
}
|
||||
|
||||
// lets delete indexes that violate the retention
|
||||
if (Array.isArray(responseArg)) {
|
||||
const filteredIndices = responseArg.filter((indexObjectArg) => {
|
||||
return indexObjectArg.index.startsWith('smartlog');
|
||||
if (Array.isArray(responseArg.body)) {
|
||||
const filteredIndices = responseArg.body.filter((indexObjectArg) => {
|
||||
return indexObjectArg.index.startsWith(prefixArg);
|
||||
});
|
||||
const filteredIndexNames = filteredIndices.map((indexObjectArg) => {
|
||||
return indexObjectArg.index;
|
||||
});
|
||||
this.deleteOldIndices(filteredIndexNames);
|
||||
await this.deleteOldIndices(prefixArg, filteredIndexNames);
|
||||
}
|
||||
|
||||
let index = null;
|
||||
|
||||
if (Array.isArray(responseArg)) {
|
||||
index = responseArg.find((indexObject) => {
|
||||
return indexObject.index === indexArg;
|
||||
if (Array.isArray(responseArg.body)) {
|
||||
index = responseArg.body.find((indexItemArg) => {
|
||||
return indexItemArg.index === indexNameArg;
|
||||
});
|
||||
}
|
||||
|
||||
if (!index) {
|
||||
const done2 = plugins.smartpromise.defer();
|
||||
this.elasticSearchRef.client.indices.create(
|
||||
{
|
||||
waitForActiveShards: '1',
|
||||
index: indexArg,
|
||||
},
|
||||
(error, response) => {
|
||||
// console.lof(response)
|
||||
done2.resolve();
|
||||
await this.createNewIndex(indexNameArg);
|
||||
}
|
||||
);
|
||||
await done2.promise;
|
||||
}
|
||||
this.stringmap.addString(indexArg);
|
||||
done.resolve();
|
||||
}
|
||||
);
|
||||
await done.promise;
|
||||
this.stringmap.addString(indexNameArg);
|
||||
return index;
|
||||
}
|
||||
|
||||
public createNewIndex(indexNameArg: string) {}
|
||||
public async createNewIndex(indexNameArg: string) {
|
||||
const response = await this.elasticSearchRef.client.indices.create({
|
||||
wait_for_active_shards: '1',
|
||||
index: indexNameArg,
|
||||
});
|
||||
}
|
||||
|
||||
public async deleteOldIndices(indicesArray: string[]) {
|
||||
public async deleteOldIndices(prefixArg: string, indicesArray: string[]) {
|
||||
const todayAsUnix: number = Date.now();
|
||||
const rententionPeriodAsUnix: number = plugins.smarttime.units.days(
|
||||
this.elasticSearchRef.indexRetention
|
||||
);
|
||||
for (const indexName of indicesArray) {
|
||||
const regexResult = /^smartlog-([0-9]*)\.([0-9]*)\.([0-9]*)$/.exec(indexName);
|
||||
if (!indexName.startsWith(prefixArg)) continue;
|
||||
const indexRegex = new RegExp(`^${prefixArg}-([0-9]*)-([0-9]*)-([0-9]*)$`)
|
||||
const regexResult = indexRegex.exec(indexName);
|
||||
const dateAsUnix: number = new Date(
|
||||
`${regexResult[1]}-${regexResult[2]}-${regexResult[3]}`
|
||||
).getTime();
|
||||
if (todayAsUnix - rententionPeriodAsUnix > dateAsUnix) {
|
||||
console.log(`found old index ${indexName}`);
|
||||
const done2 = plugins.smartpromise.defer();
|
||||
this.elasticSearchRef.client.indices.delete(
|
||||
const response = await this.elasticSearchRef.client.indices.delete(
|
||||
{
|
||||
index: indexName,
|
||||
},
|
||||
(err2, response2) => {
|
||||
if (err2) {
|
||||
console.log(err2);
|
||||
}
|
||||
console.log(`deleted ${indexName}`);
|
||||
done2.resolve();
|
||||
}
|
||||
);
|
||||
await done2.promise;
|
||||
}).catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
// interfaces
|
||||
import { Client as ElasticClient } from 'elasticsearch';
|
||||
import { Client as ElasticClient } from '@elastic/elasticsearch';
|
||||
import type { ILogContext, ILogPackage, ILogDestination } from '@pushrocks/smartlog-interfaces';
|
||||
|
||||
// other classes
|
||||
@ -14,9 +14,7 @@ export interface IStandardLogParams {
|
||||
export interface IElasticSearchConstructorOptions {
|
||||
indexPrefix: string;
|
||||
indexRetention: number;
|
||||
port: number;
|
||||
domain: string;
|
||||
ssl: boolean;
|
||||
node: string;
|
||||
user?: string;
|
||||
pass?: string;
|
||||
}
|
||||
@ -35,49 +33,30 @@ export class ElasticSearch<T> {
|
||||
*/
|
||||
constructor(optionsArg: IElasticSearchConstructorOptions) {
|
||||
this.client = new ElasticClient({
|
||||
host: this.computeHostString(optionsArg),
|
||||
// log: 'trace'
|
||||
node: optionsArg.node,
|
||||
auth: {
|
||||
username: optionsArg.user,
|
||||
password: optionsArg.pass,
|
||||
}
|
||||
});
|
||||
this.indexPrefix = optionsArg.indexPrefix;
|
||||
this.indexRetention = optionsArg.indexRetention;
|
||||
}
|
||||
|
||||
/**
|
||||
* computes the host string from the constructor options
|
||||
* @param optionsArg
|
||||
*/
|
||||
private computeHostString(optionsArg: IElasticSearchConstructorOptions): string {
|
||||
let hostString = `${optionsArg.domain}:${optionsArg.port}`;
|
||||
if (optionsArg.user && optionsArg.pass) {
|
||||
hostString = `${optionsArg.user}:${optionsArg.pass}@${hostString}`;
|
||||
}
|
||||
if (optionsArg.ssl) {
|
||||
hostString = `https://${hostString}`;
|
||||
} else {
|
||||
hostString = `http://${hostString}`;
|
||||
}
|
||||
console.log(hostString);
|
||||
return hostString;
|
||||
}
|
||||
|
||||
public async log(logPackageArg: ILogPackage, scheduleOverwrite = false) {
|
||||
const now = new Date();
|
||||
const indexToUse = `${this.indexPrefix}-${now.getFullYear()}.${(
|
||||
'0' +
|
||||
(now.getMonth() + 1)
|
||||
).slice(-2)}.${('0' + now.getDate()).slice(-2)}`;
|
||||
const indexToUse = `${this.indexPrefix}-${now.toISOString().split('T')[0]}`;
|
||||
|
||||
if (this.elasticScheduler.docsScheduled && !scheduleOverwrite) {
|
||||
this.elasticScheduler.scheduleDoc(logPackageArg);
|
||||
return;
|
||||
}
|
||||
|
||||
await this.elasticIndex.ensureIndex(indexToUse);
|
||||
|
||||
const response = await this.elasticIndex.ensureIndex(this.indexPrefix, indexToUse);
|
||||
console.log(response);
|
||||
this.client.index(
|
||||
{
|
||||
index: indexToUse,
|
||||
type: 'log',
|
||||
body: {
|
||||
'@timestamp': new Date(logPackageArg.timestamp).toISOString(),
|
||||
...logPackageArg,
|
||||
|
@ -1,4 +1,4 @@
|
||||
import * as elasticsearch from 'elasticsearch';
|
||||
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';
|
||||
|
Reference in New Issue
Block a user