elasticsearch/ts/elasticsearch.classes.elasticindex.ts

104 lines
3.0 KiB
TypeScript
Raw Normal View History

2023-07-04 07:46:04 +00:00
import * as plugins from './elasticsearch.plugins.js';
import { ElasticSearch } from './elasticsearch.classes.elasticsearch.js';
import { type ILogPackage } from '@pushrocks/smartlog-interfaces';
2018-11-09 22:15:11 +00:00
import { Stringmap } from '@pushrocks/lik';
export class ElasticIndex {
private stringmap = new Stringmap();
private elasticSearchRef: ElasticSearch<any>;
constructor(elasticSearchInstanceArg: ElasticSearch<ILogPackage>) {
this.elasticSearchRef = elasticSearchInstanceArg;
}
public async ensureIndex(indexArg: string) {
const done = plugins.smartpromise.defer();
2018-11-10 00:48:44 +00:00
if (this.stringmap.checkString(indexArg)) {
2018-11-25 21:02:57 +00:00
done.resolve();
2018-11-09 22:15:11 +00:00
return;
}
2018-11-10 00:48:44 +00:00
this.elasticSearchRef.client.cat.indices(
{
format: 'json',
2023-07-04 07:13:14 +00:00
bytes: 'm',
2018-11-10 00:48:44 +00:00
},
2018-11-26 11:24:37 +00:00
async (err, responseArg: any[]) => {
2019-11-02 23:49:35 +00:00
if (err) {
2018-11-26 11:24:37 +00:00
console.log(err);
return;
}
2018-11-10 00:48:44 +00:00
// lets delete indexes that violate the retention
2019-11-02 23:49:35 +00:00
if (Array.isArray(responseArg)) {
2023-07-04 07:13:14 +00:00
const filteredIndices = responseArg.filter((indexObjectArg) => {
2018-11-26 11:24:37 +00:00
return indexObjectArg.index.startsWith('smartlog');
});
2023-07-04 07:13:14 +00:00
const filteredIndexNames = filteredIndices.map((indexObjectArg) => {
2018-11-26 11:24:37 +00:00
return indexObjectArg.index;
});
this.deleteOldIndices(filteredIndexNames);
2018-11-10 00:48:44 +00:00
}
2018-11-09 22:15:11 +00:00
2018-11-26 11:24:37 +00:00
let index = null;
2019-11-02 23:49:35 +00:00
if (Array.isArray(responseArg)) {
2023-07-04 07:13:14 +00:00
index = responseArg.find((indexObject) => {
2018-11-26 11:24:37 +00:00
return indexObject.index === indexArg;
});
}
2018-11-10 00:48:44 +00:00
if (!index) {
const done2 = plugins.smartpromise.defer();
this.elasticSearchRef.client.indices.create(
{
2018-12-12 23:19:50 +00:00
waitForActiveShards: '1',
2023-07-04 07:13:14 +00:00
index: indexArg,
2018-11-10 00:48:44 +00:00
},
(error, response) => {
// console.lof(response)
done2.resolve();
}
);
await done2.promise;
}
this.stringmap.addString(indexArg);
done.resolve();
2018-11-09 22:15:11 +00:00
}
2018-11-10 00:48:44 +00:00
);
2018-11-09 22:15:11 +00:00
await done.promise;
}
2018-11-26 11:24:37 +00:00
2019-11-02 23:49:35 +00:00
public createNewIndex(indexNameArg: string) {}
2018-11-26 11:24:37 +00:00
public async deleteOldIndices(indicesArray: string[]) {
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) {
const regexResult = /^smartlog-([0-9]*)\.([0-9]*)\.([0-9]*)$/.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(
{
2023-07-04 07:13:14 +00:00
index: indexName,
2018-11-26 11:24:37 +00:00
},
(err2, response2) => {
if (err2) {
console.log(err2);
}
console.log(`deleted ${indexName}`);
done2.resolve();
}
);
await done2.promise;
}
}
}
2018-11-09 22:15:11 +00:00
}