import { sortBy } from 'lodash' ; import { SnapshotDetails , SnapshotDetailsEs , SnapshotConfig , SnapshotConfigEs , SnapshotRetention , SnapshotRetentionEs , } from '../types' ; import { deserializeTime , serializeTime } from './time_serialization' ; import { csvToArray } from './utils' ; export function deserializeSnapshotDetails ( snapshotDetailsEs , managedRepository ? , successfulSnapshots ? ) { if ( ! snapshotDetailsEs || typeof snapshotDetailsEs !== 'object' ) { throw new Error ( 'Unable to deserialize snapshot details' ) ; } const { snapshot , uuid , repository , version_id : versionId , version , indices = [ ] , data_streams : dataStreams = [ ] , include_global_state : includeGlobalState , state , start_time : startTime , start_time_in_millis : startTimeInMillis , end_time : endTime , end_time_in_millis : endTimeInMillis , duration_in_millis : durationInMillis , failures = [ ] , shards , metadata : { policy : policyName } = { policy : undefined } , } = snapshotDetailsEs ; const indexToFailuresMap = failures . reduce ( ( map , failure ) => { const { index , ... rest } = failure ; if ( ! map [ index ] ) { map [ index ] = { index , failures : [ ] , } ; } map [ index ] . failures . push ( rest ) ; return map ; } , { } ) ; Object . keys ( indexToFailuresMap ) . forEach ( ( index ) => { indexToFailuresMap [ index ] . failures = sortBy ( indexToFailuresMap [ index ] . failures , ( { shard } ) => shard ) ; } ) ; const indexFailures = sortBy ( Object . values ( indexToFailuresMap ) , ( { index } ) => index ) ; const snapshotDetails = { repository , snapshot , uuid , versionId , version , indices : [ ... indices ] . sort ( ) , dataStreams : [ ... dataStreams ] . sort ( ) , includeGlobalState , state , startTime , startTimeInMillis , endTime , endTimeInMillis , durationInMillis , indexFailures , shards , managedRepository , } ; if ( successfulSnapshots && successfulSnapshots . length ) { snapshotDetails . isLastSuccessfulSnapshot = successfulSnapshots [ 0 ] . snapshot === snapshot ; } if ( policyName ) { snapshotDetails . policyName = policyName ; } return snapshotDetails ; } export function deserializeSnapshotConfig ( snapshotConfigEs ) { const { indices , ignore_unavailable : ignoreUnavailable , include_global_state : includeGlobalState , partial , metadata , } = snapshotConfigEs ; const snapshotConfig = { indices , ignoreUnavailable , includeGlobalState , partial , metadata , } ; return Object . entries ( snapshotConfig ) . reduce ( ( config , [ key , value ] ) => { if ( value !== undefined ) { config [ key ] = value ; } return config ; } , { } ) ; } export function serializeSnapshotConfig ( snapshotConfig ) { const { indices , ignoreUnavailable , includeGlobalState , partial , metadata } = snapshotConfig ; const maybeIndicesArray = csvToArray ( indices ) ; const snapshotConfigEs = { indices : maybeIndicesArray , ignore_unavailable : ignoreUnavailable , include_global_state : includeGlobalState , partial , metadata , } ; return Object . entries ( snapshotConfigEs ) . reduce ( ( config , [ key , value ] ) => { if ( value !== undefined ) { config [ key ] = value ; } return config ; } , { } ) ; } export function deserializeSnapshotRetention ( snapshotRetentionEs ) { const { expire_after : expireAfter , max_count : maxCount , min_count : minCount , } = snapshotRetentionEs ; let expireAfterValue ; let expireAfterUnit ; if ( expireAfter ) { const { timeValue , timeUnit } = deserializeTime ( expireAfter ) ; if ( timeValue && timeUnit ) { expireAfterValue = timeValue ; expireAfterUnit = timeUnit ; } } const snapshotRetention = { expireAfterValue , expireAfterUnit , maxCount , minCount , } ; return Object . entries ( snapshotRetention ) . reduce ( ( retention , [ key , value ] ) => { if ( value !== undefined ) { retention [ key ] = value ; } return retention ; } , { } ) ; } export function serializeSnapshotRetention ( snapshotRetention ) { const { expireAfterValue , expireAfterUnit , minCount , maxCount } = snapshotRetention ; const snapshotRetentionEs = { expire_after : expireAfterValue && expireAfterUnit ? serializeTime ( expireAfterValue , expireAfterUnit ) : undefined , min_count : ! minCount ? undefined : minCount , max_count : ! maxCount ? undefined : maxCount , } ; const flattenedSnapshotRetentionEs = Object . entries ( snapshotRetentionEs ) . reduce ( ( retention , [ key , value ] ) => { if ( value !== undefined ) { retention [ key ] = value ; } return retention ; } , { } ) ; return Object . entries ( flattenedSnapshotRetentionEs ) . length ? flattenedSnapshotRetentionEs : undefined ; }