fix(clean up old indices): update
This commit is contained in:
@ -14,32 +14,68 @@ export class ElasticIndex {
|
||||
|
||||
public async ensureIndex(indexArg: string) {
|
||||
const done = plugins.smartpromise.defer();
|
||||
if(this.stringmap.checkString(indexArg)) {
|
||||
if (this.stringmap.checkString(indexArg)) {
|
||||
return;
|
||||
}
|
||||
this.elasticSearchRef.client.cat.indices({
|
||||
format: 'json',
|
||||
bytes: 'm'
|
||||
}, async (err, response: any[]) => {
|
||||
// console.log(response);
|
||||
const index = response.find(indexObject => {
|
||||
return indexObject.index === indexArg;
|
||||
});
|
||||
|
||||
if(!index) {
|
||||
const done2 = plugins.smartpromise.defer();
|
||||
this.elasticSearchRef.client.indices.create({
|
||||
waitForActiveShards: '2',
|
||||
index: indexArg
|
||||
}, (error, response) => {
|
||||
// console.lof(response)
|
||||
done2.resolve();
|
||||
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');
|
||||
});
|
||||
await done2.promise;
|
||||
const filteredIndexNames = filteredIndices.map(indexObjectArg => {
|
||||
return indexObjectArg.name;
|
||||
});
|
||||
const todayAsUnix: number = Date.now();
|
||||
const rententionPeriodAsUnix: number = plugins.smarttime.units.days(
|
||||
this.elasticSearchRef.indexRetention
|
||||
);
|
||||
for (const indexName of filteredIndexNames) {
|
||||
const regexResult = /^smartlog-([0-9]*)\.([0-9]*)\.([0-9]*)$/;
|
||||
const dateAsUnix: number = new Date(
|
||||
`${regexResult[1]}-${regexResult[2]}-${regexResult[3]}`
|
||||
).getTime();
|
||||
if (todayAsUnix - rententionPeriodAsUnix > dateAsUnix) {
|
||||
const done2 = plugins.smartpromise.defer();
|
||||
this.elasticSearchRef.client.indices.delete({
|
||||
index: indexName
|
||||
}, (err2, response2) => {
|
||||
if(err2) {
|
||||
console.log(err2);
|
||||
}
|
||||
done.resolve();
|
||||
});
|
||||
await done2.promise;
|
||||
}
|
||||
}
|
||||
|
||||
// console.log(response);
|
||||
const index = response.find(indexObject => {
|
||||
return indexObject.index === indexArg;
|
||||
});
|
||||
|
||||
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();
|
||||
}
|
||||
this.stringmap.addString(indexArg);
|
||||
done.resolve();
|
||||
});
|
||||
);
|
||||
await done.promise;
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,9 @@ export interface IStandardLogParams {
|
||||
severity: string;
|
||||
}
|
||||
|
||||
export interface IElasticLogConstructorOptions {
|
||||
export interface IElasticSearchConstructorOptions {
|
||||
indexPrefix: string;
|
||||
indexRetention: number;
|
||||
port: number;
|
||||
domain: string;
|
||||
ssl: boolean;
|
||||
@ -24,22 +26,27 @@ export class ElasticSearch<T> {
|
||||
public elasticScheduler = new ElasticScheduler(this);
|
||||
public elasticIndex: ElasticIndex = new ElasticIndex(this);
|
||||
|
||||
public indexPrefix: string;
|
||||
public indexRetention: number;
|
||||
|
||||
/**
|
||||
* sets up an instance of Elastic log
|
||||
* @param optionsArg
|
||||
*/
|
||||
constructor(optionsArg: IElasticLogConstructorOptions) {
|
||||
constructor(optionsArg: IElasticSearchConstructorOptions) {
|
||||
this.client = new ElasticClient({
|
||||
host: this.computeHostString(optionsArg),
|
||||
log: 'trace'
|
||||
});
|
||||
this.indexPrefix = optionsArg.indexPrefix;
|
||||
this.indexRetention = optionsArg.indexRetention;
|
||||
}
|
||||
|
||||
/**
|
||||
* computes the host string from the constructor options
|
||||
* @param optionsArg
|
||||
*/
|
||||
private computeHostString(optionsArg: IElasticLogConstructorOptions): string {
|
||||
private computeHostString(optionsArg: IElasticSearchConstructorOptions): string {
|
||||
let hostString = `${optionsArg.domain}:${optionsArg.port}`;
|
||||
if (optionsArg.user && optionsArg.pass) {
|
||||
hostString = `${optionsArg.user}:${optionsArg.pass}@${hostString}`;
|
||||
@ -54,7 +61,7 @@ export class ElasticSearch<T> {
|
||||
|
||||
public async log(logPackageArg: ILogPackage, scheduleOverwrite = false) {
|
||||
const now = new Date();
|
||||
const indexToUse = `smartlog-${now.getFullYear()}.${('0' + (now.getMonth() + 1)).slice(-2)}.${(
|
||||
const indexToUse = `${this.indexPrefix}-${now.getFullYear()}.${('0' + (now.getMonth() + 1)).slice(-2)}.${(
|
||||
'0' + now.getDate()
|
||||
).slice(-2)}`;
|
||||
|
||||
|
@ -2,5 +2,6 @@ import * as elasticsearch from 'elasticsearch';
|
||||
import * as smartdelay from '@pushrocks/smartdelay';
|
||||
import * as smartlogInterfaces from '@pushrocks/smartlog-interfaces';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as smarttime from '@pushrocks/smarttime';
|
||||
|
||||
export { elasticsearch, smartdelay, smartlogInterfaces, smartpromise };
|
||||
export { elasticsearch, smartdelay, smartlogInterfaces, smartpromise, smarttime };
|
||||
|
Reference in New Issue
Block a user