From 9c87f5ee5ed0e5fd0036144db6a8492364ad67cc Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Mon, 26 Nov 2018 12:24:37 +0100 Subject: [PATCH] fix(core): update --- ts/elasticsearch.classes.elasticindex.ts | 95 ++++++++++++++---------- 1 file changed, 56 insertions(+), 39 deletions(-) diff --git a/ts/elasticsearch.classes.elasticindex.ts b/ts/elasticsearch.classes.elasticindex.ts index 0cfaa3f..799a694 100644 --- a/ts/elasticsearch.classes.elasticindex.ts +++ b/ts/elasticsearch.classes.elasticindex.ts @@ -23,47 +23,30 @@ export class ElasticIndex { 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 => { - return indexObjectArg.index; - }); - const todayAsUnix: number = Date.now(); - const rententionPeriodAsUnix: number = plugins.smarttime.units.days( - this.elasticSearchRef.indexRetention - ); - console.log(filteredIndexNames); - for (const indexName of filteredIndexNames) { - 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; - } + async (err, responseArg: any[]) => { + if(err) { + console.log(err); + return; } - // console.log(response); - const index = response.find(indexObject => { - return indexObject.index === indexArg; - }); + // lets delete indexes that violate the retention + 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); + } + + let index = null; + + if(Array.isArray(responseArg)) { + index = responseArg.find(indexObject => { + return indexObject.index === indexArg; + }); + } if (!index) { const done2 = plugins.smartpromise.defer(); @@ -85,4 +68,38 @@ export class ElasticIndex { ); await done.promise; } + + 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; + } + } + } }