|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +import { ElasticsearchClient } from '@kbn/core/server'; |
| 9 | +import { Logger } from '@kbn/logging'; |
| 10 | +import { IndexRequest, IndicesCreateRequest } from '@elastic/elasticsearch/lib/api/types'; |
| 11 | +import { PrepareIndicesForEvaluations } from '../../prepare_indices_for_evalutations'; |
| 12 | +import { indicesCreateRequests } from './indices_create_requests'; |
| 13 | +import { indexRequests } from './index_requests'; |
| 14 | + |
| 15 | +const ENVIRONMENTS = ['production', 'staging', 'development']; |
| 16 | +export class PrepareIndicesForAssistantGraphEvaluations extends PrepareIndicesForEvaluations { |
| 17 | + constructor({ esClient, logger }: { esClient: ElasticsearchClient; logger: Logger }) { |
| 18 | + super({ |
| 19 | + esClient, |
| 20 | + indicesCreateRequests: PrepareIndicesForAssistantGraphEvaluations.hydrateRequestTemplate( |
| 21 | + Object.values(indicesCreateRequests) |
| 22 | + ), |
| 23 | + indexRequests: PrepareIndicesForAssistantGraphEvaluations.hydrateRequestTemplate( |
| 24 | + Object.values(indexRequests) |
| 25 | + ), |
| 26 | + logger, |
| 27 | + }); |
| 28 | + } |
| 29 | + |
| 30 | + static hydrateRequestTemplate<T extends IndicesCreateRequest | IndexRequest>(requests: T[]): T[] { |
| 31 | + return requests |
| 32 | + .map((request) => { |
| 33 | + return ENVIRONMENTS.map((environment) => { |
| 34 | + return { |
| 35 | + ...request, |
| 36 | + index: request.index |
| 37 | + .replace(/\[environment\]/g, environment) |
| 38 | + .replace(/\[date\]/g, this.getRandomDate()), |
| 39 | + } as T; |
| 40 | + }); |
| 41 | + }) |
| 42 | + .flat(); |
| 43 | + } |
| 44 | + |
| 45 | + async cleanup() { |
| 46 | + this.logger.debug('Deleting assistant indices for evaluations'); |
| 47 | + |
| 48 | + const requests = [...Object.values(indicesCreateRequests), ...Object.values(indexRequests)]; |
| 49 | + const indexPatternsToDelete = Object.values(requests).map((index) => |
| 50 | + index.index.replace(/\[environment\]/g, '*').replace(/\[date\]/g, '*') |
| 51 | + ); |
| 52 | + |
| 53 | + const indicesResolveIndexResponses = await Promise.all( |
| 54 | + indexPatternsToDelete.map(async (indexPattern) => |
| 55 | + this.esClient.indices.resolveIndex({ |
| 56 | + name: indexPattern, |
| 57 | + expand_wildcards: 'open', |
| 58 | + }) |
| 59 | + ) |
| 60 | + ); |
| 61 | + |
| 62 | + const indicesToDelete = indicesResolveIndexResponses |
| 63 | + .flatMap((response) => response.indices) |
| 64 | + .map((index) => index.name); |
| 65 | + |
| 66 | + const dataStreamsToDelete = indicesResolveIndexResponses |
| 67 | + .flatMap((response) => response.data_streams) |
| 68 | + .map((dataStream) => dataStream.name); |
| 69 | + |
| 70 | + if (indicesToDelete.length > 0) { |
| 71 | + this.logger.info('Deleting indices'); |
| 72 | + await this.esClient.indices.delete({ index: indicesToDelete }); |
| 73 | + } |
| 74 | + |
| 75 | + if (dataStreamsToDelete.length > 0) { |
| 76 | + this.logger.info('Deleting data streams'); |
| 77 | + await this.esClient.indices.deleteDataStream({ name: dataStreamsToDelete }); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + static getRandomDate() { |
| 82 | + const year = Math.floor(Math.random() * (2050 - 2000 + 1)) + 2000; |
| 83 | + const month = String(Math.floor(Math.random() * 12) + 1).padStart(2, '0'); |
| 84 | + const day = String(Math.floor(Math.random() * 28) + 1).padStart(2, '0'); |
| 85 | + return `${year}.${month}.${day}`; |
| 86 | + } |
| 87 | +} |
0 commit comments