import { chain } from 'lodash' ; import moment from 'moment' ; import { LegacyAPICaller } from 'opensearch-dashboards/server' ; import { timePatternToWildcard } from './time_pattern_to_wildcard' ; import { callIndexAliasApi , IndicesAliasResponse } from './opensearch_api' ; param { Function } callCluster param { String } timePattern return { Promise < Object > } export async function resolveTimePattern ( callCluster , timePattern ) { const aliases = await callIndexAliasApi ( callCluster , timePatternToWildcard ( timePattern ) ) ; const allIndexDetails = chain < IndicesAliasResponse > ( aliases ) . reduce ( ( acc , index , indexName ) => acc . concat ( indexName , Object . keys ( index . aliases || { } ) ) , [ ] ) . sortBy ( ( indexName ) => indexName ) . sortedUniq ( ) . map ( ( indexName ) => { const parsed = moment ( indexName , timePattern , true ) ; if ( ! parsed . isValid ( ) ) { return { valid : false , indexName , order : indexName , isMatch : false , } ; } return { valid : true , indexName , order : parsed , isMatch : indexName === parsed . format ( timePattern ) , } ; } ) . orderBy ( [ 'valid' , 'order' ] , [ 'desc' , 'desc' ] ) . value ( ) ; return { all : allIndexDetails . map ( ( details ) => details . indexName ) , matches : allIndexDetails . filter ( ( details ) => details . isMatch ) . map ( ( details ) => details . indexName ) , } ; }