|
| 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 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +/* eslint-disable @typescript-eslint/no-empty-interface */ |
| 8 | + |
| 9 | +import * as rt from 'io-ts'; |
| 10 | +import moment from 'moment'; |
| 11 | +import { pipe } from 'fp-ts/lib/pipeable'; |
| 12 | +import { chain } from 'fp-ts/lib/Either'; |
| 13 | + |
| 14 | +export const TimestampFromString = new rt.Type<number, string>( |
| 15 | + 'TimestampFromString', |
| 16 | + (input): input is number => typeof input === 'number', |
| 17 | + (input, context) => |
| 18 | + pipe( |
| 19 | + rt.string.validate(input, context), |
| 20 | + chain(stringInput => { |
| 21 | + const momentValue = moment(stringInput); |
| 22 | + return momentValue.isValid() |
| 23 | + ? rt.success(momentValue.valueOf()) |
| 24 | + : rt.failure(stringInput, context); |
| 25 | + }) |
| 26 | + ), |
| 27 | + output => new Date(output).toISOString() |
| 28 | +); |
| 29 | + |
| 30 | +/** |
| 31 | + * Stored source configuration as read from and written to saved objects |
| 32 | + */ |
| 33 | + |
| 34 | +const SavedSourceConfigurationFieldsRuntimeType = rt.partial({ |
| 35 | + container: rt.string, |
| 36 | + host: rt.string, |
| 37 | + pod: rt.string, |
| 38 | + tiebreaker: rt.string, |
| 39 | + timestamp: rt.string, |
| 40 | +}); |
| 41 | + |
| 42 | +export const SavedSourceConfigurationTimestampColumnRuntimeType = rt.type({ |
| 43 | + timestampColumn: rt.type({ |
| 44 | + id: rt.string, |
| 45 | + }), |
| 46 | +}); |
| 47 | + |
| 48 | +export const SavedSourceConfigurationMessageColumnRuntimeType = rt.type({ |
| 49 | + messageColumn: rt.type({ |
| 50 | + id: rt.string, |
| 51 | + }), |
| 52 | +}); |
| 53 | + |
| 54 | +export const SavedSourceConfigurationFieldColumnRuntimeType = rt.type({ |
| 55 | + fieldColumn: rt.type({ |
| 56 | + id: rt.string, |
| 57 | + field: rt.string, |
| 58 | + }), |
| 59 | +}); |
| 60 | + |
| 61 | +export const SavedSourceConfigurationColumnRuntimeType = rt.union([ |
| 62 | + SavedSourceConfigurationTimestampColumnRuntimeType, |
| 63 | + SavedSourceConfigurationMessageColumnRuntimeType, |
| 64 | + SavedSourceConfigurationFieldColumnRuntimeType, |
| 65 | +]); |
| 66 | + |
| 67 | +export const SavedSourceConfigurationRuntimeType = rt.partial({ |
| 68 | + name: rt.string, |
| 69 | + description: rt.string, |
| 70 | + metricAlias: rt.string, |
| 71 | + logAlias: rt.string, |
| 72 | + fields: SavedSourceConfigurationFieldsRuntimeType, |
| 73 | + logColumns: rt.array(SavedSourceConfigurationColumnRuntimeType), |
| 74 | +}); |
| 75 | + |
| 76 | +export interface InfraSavedSourceConfiguration |
| 77 | + extends rt.TypeOf<typeof SavedSourceConfigurationRuntimeType> {} |
| 78 | + |
| 79 | +export const pickSavedSourceConfiguration = ( |
| 80 | + value: InfraSourceConfiguration |
| 81 | +): InfraSavedSourceConfiguration => { |
| 82 | + const { name, description, metricAlias, logAlias, fields, logColumns } = value; |
| 83 | + const { container, host, pod, tiebreaker, timestamp } = fields; |
| 84 | + |
| 85 | + return { |
| 86 | + name, |
| 87 | + description, |
| 88 | + metricAlias, |
| 89 | + logAlias, |
| 90 | + fields: { container, host, pod, tiebreaker, timestamp }, |
| 91 | + logColumns, |
| 92 | + }; |
| 93 | +}; |
| 94 | + |
| 95 | +/** |
| 96 | + * Static source configuration as read from the configuration file |
| 97 | + */ |
| 98 | + |
| 99 | +const StaticSourceConfigurationFieldsRuntimeType = rt.partial({ |
| 100 | + ...SavedSourceConfigurationFieldsRuntimeType.props, |
| 101 | + message: rt.array(rt.string), |
| 102 | +}); |
| 103 | + |
| 104 | +export const StaticSourceConfigurationRuntimeType = rt.partial({ |
| 105 | + name: rt.string, |
| 106 | + description: rt.string, |
| 107 | + metricAlias: rt.string, |
| 108 | + logAlias: rt.string, |
| 109 | + fields: StaticSourceConfigurationFieldsRuntimeType, |
| 110 | + logColumns: rt.array(SavedSourceConfigurationColumnRuntimeType), |
| 111 | +}); |
| 112 | + |
| 113 | +export interface InfraStaticSourceConfiguration |
| 114 | + extends rt.TypeOf<typeof StaticSourceConfigurationRuntimeType> {} |
| 115 | + |
| 116 | +/** |
| 117 | + * Full source configuration type after all cleanup has been done at the edges |
| 118 | + */ |
| 119 | + |
| 120 | +const SourceConfigurationFieldsRuntimeType = rt.type({ |
| 121 | + ...StaticSourceConfigurationFieldsRuntimeType.props, |
| 122 | +}); |
| 123 | + |
| 124 | +export const SourceConfigurationRuntimeType = rt.type({ |
| 125 | + ...SavedSourceConfigurationRuntimeType.props, |
| 126 | + fields: SourceConfigurationFieldsRuntimeType, |
| 127 | + logColumns: rt.array(SavedSourceConfigurationColumnRuntimeType), |
| 128 | +}); |
| 129 | + |
| 130 | +export const SourceRuntimeType = rt.intersection([ |
| 131 | + rt.type({ |
| 132 | + id: rt.string, |
| 133 | + origin: rt.keyof({ |
| 134 | + fallback: null, |
| 135 | + internal: null, |
| 136 | + stored: null, |
| 137 | + }), |
| 138 | + configuration: SourceConfigurationRuntimeType, |
| 139 | + }), |
| 140 | + rt.partial({ |
| 141 | + version: rt.string, |
| 142 | + updatedAt: rt.number, |
| 143 | + }), |
| 144 | +]); |
| 145 | + |
| 146 | +export interface InfraSourceConfiguration |
| 147 | + extends rt.TypeOf<typeof SourceConfigurationRuntimeType> {} |
| 148 | + |
| 149 | +export interface InfraSource extends rt.TypeOf<typeof SourceRuntimeType> {} |
| 150 | + |
| 151 | +const SourceStatusFieldRuntimeType = rt.type({ |
| 152 | + name: rt.string, |
| 153 | + type: rt.string, |
| 154 | + searchable: rt.boolean, |
| 155 | + aggregatable: rt.boolean, |
| 156 | + displayable: rt.boolean, |
| 157 | +}); |
| 158 | + |
| 159 | +const SourceStatusRuntimeType = rt.type({ |
| 160 | + logIndicesExist: rt.boolean, |
| 161 | + metricIndicesExist: rt.boolean, |
| 162 | + indexFields: rt.array(SourceStatusFieldRuntimeType), |
| 163 | +}); |
| 164 | + |
| 165 | +export const SourceResponseRuntimeType = rt.type({ |
| 166 | + source: SourceRuntimeType, |
| 167 | + status: SourceStatusRuntimeType, |
| 168 | +}); |
| 169 | + |
| 170 | +export type SourceResponse = rt.TypeOf<typeof SourceResponseRuntimeType>; |
| 171 | + |
| 172 | +/** |
| 173 | + * Saved object type with metadata |
| 174 | + */ |
| 175 | + |
| 176 | +export const SourceConfigurationSavedObjectRuntimeType = rt.intersection([ |
| 177 | + rt.type({ |
| 178 | + id: rt.string, |
| 179 | + attributes: SavedSourceConfigurationRuntimeType, |
| 180 | + }), |
| 181 | + rt.partial({ |
| 182 | + version: rt.string, |
| 183 | + updated_at: TimestampFromString, |
| 184 | + }), |
| 185 | +]); |
| 186 | + |
| 187 | +export interface SourceConfigurationSavedObject |
| 188 | + extends rt.TypeOf<typeof SourceConfigurationSavedObjectRuntimeType> {} |
0 commit comments