elasticsearch/ts/els.classes.elasticindex.ts

104 lines
3.0 KiB
TypeScript
Raw Normal View History

2023-08-14 08:58:28 +00:00
import * as plugins from './els.plugins.js';
2023-08-01 10:24:22 +00:00
import { ElsSmartlogDestination } from './els.classes.smartlogdestination.js';
2023-08-29 09:11:25 +00:00
import { type ILogPackage } from '@push.rocks/smartlog-interfaces';
import { Stringmap } from '@push.rocks/lik';
2018-11-09 22:15:11 +00:00
export class ElasticIndex {
private stringmap = new Stringmap();
2023-08-01 10:24:22 +00:00
private elasticSearchRef: ElsSmartlogDestination<any>;
2018-11-09 22:15:11 +00:00
2023-08-01 10:24:22 +00:00
constructor(elasticSearchInstanceArg: ElsSmartlogDestination<ILogPackage>) {
2018-11-09 22:15:11 +00:00
this.elasticSearchRef = elasticSearchInstanceArg;
}
2023-07-05 13:31:00 +00:00
public async ensureIndex(prefixArg: string, indexNameArg: string) {
if (this.stringmap.checkString(indexNameArg)) {
2023-07-05 21:45:48 +00:00
return indexNameArg;
2018-11-09 22:15:11 +00:00
}
2023-08-01 10:24:22 +00:00
2023-07-05 13:31:00 +00:00
const responseArg = await this.elasticSearchRef.client.cat.indices({
format: 'json',
2023-08-29 09:11:25 +00:00
bytes: 'mb',
2023-07-05 13:31:00 +00:00
}).catch(err => {
console.log(err);
});
2018-11-26 11:24:37 +00:00
2023-07-05 13:31:00 +00:00
if (!responseArg) {
throw new Error('Could not get valid response from elastic search');
}
2018-11-09 22:15:11 +00:00
2023-08-29 09:11:25 +00:00
if (Array.isArray(responseArg)) {
const filteredIndices = responseArg.filter((indexObjectArg) => {
2023-07-05 13:31:00 +00:00
return indexObjectArg.index.startsWith(prefixArg);
});
const filteredIndexNames = filteredIndices.map((indexObjectArg) => {
return indexObjectArg.index;
});
await this.deleteOldIndices(prefixArg, filteredIndexNames);
}
2019-11-02 23:49:35 +00:00
2023-07-05 13:31:00 +00:00
let index = null;
2018-11-10 00:48:44 +00:00
2023-08-29 09:11:25 +00:00
if (Array.isArray(responseArg)) {
index = responseArg.find((indexItemArg) => {
2023-07-05 13:31:00 +00:00
return indexItemArg.index === indexNameArg;
});
}
if (!index) {
await this.createNewIndex(indexNameArg);
}
2023-08-01 10:24:22 +00:00
2023-07-05 13:31:00 +00:00
this.stringmap.addString(indexNameArg);
return index;
2018-11-09 22:15:11 +00:00
}
2018-11-26 11:24:37 +00:00
2023-07-05 13:31:00 +00:00
public async createNewIndex(indexNameArg: string) {
const response = await this.elasticSearchRef.client.indices.create({
2023-08-29 09:11:25 +00:00
wait_for_active_shards: 1,
2023-07-05 13:31:00 +00:00
index: indexNameArg,
2023-08-01 10:24:22 +00:00
body: {
mappings: {
properties: {
'@timestamp': {
type: 'date',
},
logPackageArg: {
properties: {
payload: {
type: 'object',
dynamic: true
}
}
},
},
},
},
2023-07-05 13:31:00 +00:00
});
}
2018-11-26 11:24:37 +00:00
2023-07-05 13:31:00 +00:00
public async deleteOldIndices(prefixArg: string, indicesArray: string[]) {
2018-11-26 11:24:37 +00:00
const todayAsUnix: number = Date.now();
2019-11-02 23:49:35 +00:00
const rententionPeriodAsUnix: number = plugins.smarttime.units.days(
this.elasticSearchRef.indexRetention
);
2018-11-26 11:24:37 +00:00
for (const indexName of indicesArray) {
2023-07-05 13:31:00 +00:00
if (!indexName.startsWith(prefixArg)) continue;
const indexRegex = new RegExp(`^${prefixArg}-([0-9]*)-([0-9]*)-([0-9]*)$`)
const regexResult = indexRegex.exec(indexName);
2018-11-26 11:24:37 +00:00
const dateAsUnix: number = new Date(
`${regexResult[1]}-${regexResult[2]}-${regexResult[3]}`
).getTime();
if (todayAsUnix - rententionPeriodAsUnix > dateAsUnix) {
console.log(`found old index ${indexName}`);
2023-07-05 13:31:00 +00:00
const response = await this.elasticSearchRef.client.indices.delete(
2018-11-26 11:24:37 +00:00
{
2023-07-04 07:13:14 +00:00
index: indexName,
2023-07-05 13:31:00 +00:00
}).catch(err => {
console.log(err);
});
2018-11-26 11:24:37 +00:00
}
}
}
2018-11-09 22:15:11 +00:00
}