|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import { get } from 'lodash'; |
| 7 | +import { VisualizeEmbeddable, Vis } from '../../visualizations/public'; |
| 8 | +import { ErrorEmbeddable } from '../../embeddable/public'; |
| 9 | +// eslint-disable-next-line @osd/eslint/no-restricted-paths |
| 10 | +import { timefilterServiceMock } from '../../data/public/query/timefilter/timefilter_service.mock'; |
| 11 | +import { EventVisEmbeddableItem } from './view_events_flyout'; |
| 12 | +import { |
| 13 | + VisLayer, |
| 14 | + VisLayerTypes, |
| 15 | + VisLayerErrorTypes, |
| 16 | + PointInTimeEventsVisLayer, |
| 17 | + PluginResource, |
| 18 | + PointInTimeEvent, |
| 19 | +} from './types'; |
| 20 | +import { AggConfigs, AggTypesRegistryStart, IndexPattern } from '../../data/common'; |
| 21 | +import { mockAggTypesRegistry } from '../../data/common/search/aggs/test_helpers'; |
| 22 | + |
| 23 | +export const VALID_CONFIG_STATES = [ |
| 24 | + { |
| 25 | + enabled: true, |
| 26 | + type: 'max', |
| 27 | + params: {}, |
| 28 | + schema: 'metric', |
| 29 | + }, |
| 30 | + { |
| 31 | + enabled: true, |
| 32 | + type: 'date_histogram', |
| 33 | + params: {}, |
| 34 | + schema: 'segment', |
| 35 | + }, |
| 36 | +]; |
| 37 | + |
| 38 | +export const STUB_INDEX_PATTERN_WITH_FIELDS = { |
| 39 | + id: '1234', |
| 40 | + title: 'logstash-*', |
| 41 | + fields: [ |
| 42 | + { |
| 43 | + name: 'response', |
| 44 | + type: 'number', |
| 45 | + esTypes: ['integer'], |
| 46 | + aggregatable: true, |
| 47 | + filterable: true, |
| 48 | + searchable: true, |
| 49 | + }, |
| 50 | + ], |
| 51 | +} as IndexPattern; |
| 52 | + |
| 53 | +export const TYPES_REGISTRY: AggTypesRegistryStart = mockAggTypesRegistry(); |
| 54 | + |
| 55 | +export const VALID_AGGS = new AggConfigs(STUB_INDEX_PATTERN_WITH_FIELDS, VALID_CONFIG_STATES, { |
| 56 | + typesRegistry: TYPES_REGISTRY, |
| 57 | +}); |
| 58 | + |
| 59 | +export const VALID_VIS = ({ |
| 60 | + type: {}, |
| 61 | + uiState: { |
| 62 | + on: jest.fn(), |
| 63 | + }, |
| 64 | + params: { |
| 65 | + type: 'line', |
| 66 | + seriesParams: [ |
| 67 | + { |
| 68 | + type: 'line', |
| 69 | + }, |
| 70 | + ], |
| 71 | + categoryAxes: [ |
| 72 | + { |
| 73 | + position: 'bottom', |
| 74 | + }, |
| 75 | + ], |
| 76 | + }, |
| 77 | + data: { |
| 78 | + aggs: VALID_AGGS, |
| 79 | + }, |
| 80 | +} as unknown) as Vis; |
| 81 | + |
| 82 | +const SAVED_OBJ_ID = 'test-saved-obj-id'; |
| 83 | +const VIS_TITLE = 'test-vis-title'; |
| 84 | +const ORIGIN_PLUGIN = 'test-plugin'; |
| 85 | +const PLUGIN_RESOURCE = { |
| 86 | + type: 'test-type', |
| 87 | + id: 'test-resource-id', |
| 88 | + name: 'test-resource-name', |
| 89 | + urlPath: 'test-url-path', |
| 90 | +} as PluginResource; |
| 91 | +const EVENT_COUNT = 3; |
| 92 | +const ERROR_MESSAGE = 'test-error-message'; |
| 93 | + |
| 94 | +export const createPluginResource = ( |
| 95 | + type: string = PLUGIN_RESOURCE.type, |
| 96 | + id: string = PLUGIN_RESOURCE.id, |
| 97 | + name: string = PLUGIN_RESOURCE.name, |
| 98 | + urlPath: string = PLUGIN_RESOURCE.urlPath |
| 99 | +): PluginResource => { |
| 100 | + return { |
| 101 | + type, |
| 102 | + id, |
| 103 | + name, |
| 104 | + urlPath, |
| 105 | + }; |
| 106 | +}; |
| 107 | + |
| 108 | +export const createMockErrorEmbeddable = (): ErrorEmbeddable => { |
| 109 | + return new ErrorEmbeddable('Oh no something has gone wrong', { id: ' 404' }); |
| 110 | +}; |
| 111 | + |
| 112 | +export const createMockVisEmbeddable = ( |
| 113 | + savedObjectId: string = SAVED_OBJ_ID, |
| 114 | + title: string = VIS_TITLE, |
| 115 | + validVis: boolean = true |
| 116 | +): VisualizeEmbeddable => { |
| 117 | + const mockTimeFilterService = timefilterServiceMock.createStartContract(); |
| 118 | + const mockTimeFilter = mockTimeFilterService.timefilter; |
| 119 | + const mockVis = validVis |
| 120 | + ? VALID_VIS |
| 121 | + : (({ |
| 122 | + type: {}, |
| 123 | + data: {}, |
| 124 | + uiState: { |
| 125 | + on: jest.fn(), |
| 126 | + }, |
| 127 | + params: { |
| 128 | + type: 'line', |
| 129 | + seriesParams: [], |
| 130 | + }, |
| 131 | + } as unknown) as Vis); |
| 132 | + const mockDeps = { |
| 133 | + start: jest.fn(), |
| 134 | + }; |
| 135 | + const mockConfiguration = { |
| 136 | + vis: mockVis, |
| 137 | + editPath: 'test-edit-path', |
| 138 | + editUrl: 'test-edit-url', |
| 139 | + editable: true, |
| 140 | + deps: mockDeps, |
| 141 | + }; |
| 142 | + const mockVisualizeInput = { id: 'test-id', savedObjectId }; |
| 143 | + |
| 144 | + const mockVisEmbeddable = new VisualizeEmbeddable( |
| 145 | + mockTimeFilter, |
| 146 | + mockConfiguration, |
| 147 | + mockVisualizeInput |
| 148 | + ); |
| 149 | + mockVisEmbeddable.getTitle = () => title; |
| 150 | + return mockVisEmbeddable; |
| 151 | +}; |
| 152 | + |
| 153 | +export const createPointInTimeEventsVisLayer = ( |
| 154 | + originPlugin: string = ORIGIN_PLUGIN, |
| 155 | + pluginResource: PluginResource = PLUGIN_RESOURCE, |
| 156 | + eventCount: number = EVENT_COUNT, |
| 157 | + error: boolean = false, |
| 158 | + errorMessage: string = ERROR_MESSAGE |
| 159 | +): PointInTimeEventsVisLayer => { |
| 160 | + const events = [] as PointInTimeEvent[]; |
| 161 | + for (let i = 0; i < eventCount; i++) { |
| 162 | + events.push({ |
| 163 | + timestamp: i, |
| 164 | + metadata: { |
| 165 | + pluginResourceId: pluginResource.id, |
| 166 | + }, |
| 167 | + } as PointInTimeEvent); |
| 168 | + } |
| 169 | + return { |
| 170 | + originPlugin, |
| 171 | + type: VisLayerTypes.PointInTimeEvents, |
| 172 | + pluginResource, |
| 173 | + events, |
| 174 | + error: error |
| 175 | + ? { |
| 176 | + type: VisLayerErrorTypes.FETCH_FAILURE, |
| 177 | + message: errorMessage, |
| 178 | + } |
| 179 | + : undefined, |
| 180 | + }; |
| 181 | +}; |
| 182 | + |
| 183 | +export const createMockEventVisEmbeddableItem = ( |
| 184 | + savedObjectId: string = SAVED_OBJ_ID, |
| 185 | + title: string = VIS_TITLE, |
| 186 | + originPlugin: string = ORIGIN_PLUGIN, |
| 187 | + pluginResource: PluginResource = PLUGIN_RESOURCE, |
| 188 | + eventCount: number = EVENT_COUNT |
| 189 | +): EventVisEmbeddableItem => { |
| 190 | + const visLayer = createPointInTimeEventsVisLayer(originPlugin, pluginResource, eventCount); |
| 191 | + const embeddable = createMockVisEmbeddable(savedObjectId, title); |
| 192 | + return { |
| 193 | + visLayer, |
| 194 | + embeddable, |
| 195 | + }; |
| 196 | +}; |
| 197 | + |
| 198 | +export const createVisLayer = ( |
| 199 | + type: any, |
| 200 | + error: boolean = false, |
| 201 | + errorMessage: string = 'some-error-message', |
| 202 | + resource?: { |
| 203 | + type?: string; |
| 204 | + id?: string; |
| 205 | + name?: string; |
| 206 | + urlPath?: string; |
| 207 | + } |
| 208 | +): VisLayer => { |
| 209 | + return { |
| 210 | + type, |
| 211 | + originPlugin: 'test-plugin', |
| 212 | + pluginResource: { |
| 213 | + type: get(resource, 'type', 'test-resource-type'), |
| 214 | + id: get(resource, 'id', 'test-resource-id'), |
| 215 | + name: get(resource, 'name', 'test-resource-name'), |
| 216 | + urlPath: get(resource, 'urlPath', 'test-resource-url-path'), |
| 217 | + }, |
| 218 | + error: error |
| 219 | + ? { |
| 220 | + type: VisLayerErrorTypes.FETCH_FAILURE, |
| 221 | + message: errorMessage, |
| 222 | + } |
| 223 | + : undefined, |
| 224 | + }; |
| 225 | +}; |
0 commit comments