elasticsearch/ts/elasticsearch.classes.elasticindex.ts

107 lines
3.1 KiB
TypeScript
Raw Normal View History

2018-11-09 22:15:11 +00:00
import * as plugins from './elasticsearch.plugins';
import { ElasticSearch } from './elasticsearch.classes.elasticsearch';
import { ILogPackage } from '@pushrocks/smartlog-interfaces';
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',
bytes: 'm'
},
2018-11-26 11:24:37 +00:00
async (err, responseArg: any[]) => {
if(err) {
console.log(err);
return;
}
2018-11-10 00:48:44 +00:00
// lets delete indexes that violate the retention
2018-11-26 11:24:37 +00:00
if(Array.isArray(responseArg)) {
const filteredIndices = responseArg.filter(indexObjectArg => {
return indexObjectArg.index.startsWith('smartlog');
});
const filteredIndexNames = filteredIndices.map(indexObjectArg => {
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;
if(Array.isArray(responseArg)) {
index = responseArg.find(indexObject => {
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',
2018-11-10 00:48:44 +00:00
index: indexArg
},
(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
public createNewIndex(indexNameArg: string) {
}
public async deleteOldIndices(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);
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(
{
index: indexName
},
(err2, response2) => {
if (err2) {
console.log(err2);
}
console.log(`deleted ${indexName}`);
done2.resolve();
}
);
await done2.promise;
}
}
}
2018-11-09 22:15:11 +00:00
}