elasticsearch/ts/elasticsearch.classes.elasticindex.ts

89 lines
2.8 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'
},
async (err, response: any[]) => {
// lets delete indexes that violate the retention
const filteredIndices = response.filter(indexObjectArg => {
return indexObjectArg.index.startsWith('smartlog');
});
const filteredIndexNames = filteredIndices.map(indexObjectArg => {
2018-11-25 21:02:57 +00:00
return indexObjectArg.index;
2018-11-10 00:48:44 +00:00
});
const todayAsUnix: number = Date.now();
const rententionPeriodAsUnix: number = plugins.smarttime.units.days(
this.elasticSearchRef.indexRetention
);
2018-11-25 21:02:57 +00:00
console.log(filteredIndexNames);
2018-11-10 00:48:44 +00:00
for (const indexName of filteredIndexNames) {
2018-11-25 21:02:57 +00:00
const regexResult = /^smartlog-([0-9]*)\.([0-9]*)\.([0-9]*)$/.exec(indexName);
2018-11-10 00:48:44 +00:00
const dateAsUnix: number = new Date(
`${regexResult[1]}-${regexResult[2]}-${regexResult[3]}`
).getTime();
if (todayAsUnix - rententionPeriodAsUnix > dateAsUnix) {
2018-11-25 21:02:57 +00:00
console.log(`found old index ${indexName}`);
2018-11-10 00:48:44 +00:00
const done2 = plugins.smartpromise.defer();
2018-11-25 21:06:25 +00:00
this.elasticSearchRef.client.indices.delete(
{
index: indexName
},
(err2, response2) => {
if (err2) {
console.log(err2);
}
console.log(`deleted ${indexName}`);
done2.resolve();
2018-11-10 00:48:44 +00:00
}
2018-11-25 21:06:25 +00:00
);
2018-11-10 00:48:44 +00:00
await done2.promise;
}
}
2018-11-09 22:15:11 +00:00
2018-11-10 00:48:44 +00:00
// console.log(response);
const index = response.find(indexObject => {
return indexObject.index === indexArg;
2018-11-09 22:15:11 +00:00
});
2018-11-10 00:48:44 +00:00
if (!index) {
const done2 = plugins.smartpromise.defer();
this.elasticSearchRef.client.indices.create(
{
waitForActiveShards: '2',
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;
}
}