|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import { cloneDeep } from 'lodash'; |
| 7 | +import { i18n } from '@osd/i18n'; |
| 8 | +import { |
| 9 | + ExpressionFunctionDefinition, |
| 10 | + OpenSearchDashboardsDatatable, |
| 11 | + OpenSearchDashboardsDatatableColumn, |
| 12 | +} from '../../../expressions/public'; |
| 13 | +import { VegaVisualizationDependencies } from '../plugin'; |
| 14 | +import { VislibDimensions, VisParams } from '../../../visualizations/public'; |
| 15 | + |
| 16 | +type Input = OpenSearchDashboardsDatatable; |
| 17 | +type Output = Promise<string>; |
| 18 | + |
| 19 | +interface Arguments { |
| 20 | + visLayers: string | null; |
| 21 | + visParams: string; |
| 22 | + dimensions: string; |
| 23 | +} |
| 24 | + |
| 25 | +export type LineVegaSpecExpressionFunctionDefinition = ExpressionFunctionDefinition< |
| 26 | + 'line_vega_spec', |
| 27 | + Input, |
| 28 | + Arguments, |
| 29 | + Output |
| 30 | +>; |
| 31 | + |
| 32 | +// TODO: move this to the visualization plugin that has VisParams once all of these parameters have been better defined |
| 33 | +interface ValueAxis { |
| 34 | + id: string; |
| 35 | + labels: { |
| 36 | + filter: boolean; |
| 37 | + rotate: number; |
| 38 | + show: boolean; |
| 39 | + truncate: number; |
| 40 | + }; |
| 41 | + name: string; |
| 42 | + position: string; |
| 43 | + scale: { |
| 44 | + mode: string; |
| 45 | + type: string; |
| 46 | + }; |
| 47 | + show: true; |
| 48 | + style: any; |
| 49 | + title: { |
| 50 | + text: string; |
| 51 | + }; |
| 52 | + type: string; |
| 53 | +} |
| 54 | + |
| 55 | +// Get the first xaxis field as only 1 setup of X Axis will be supported and |
| 56 | +// there won't be support for split series and split chart |
| 57 | +const getXAxisId = (dimensions: any, columns: OpenSearchDashboardsDatatableColumn[]): string => { |
| 58 | + return columns.filter((column) => column.name === dimensions.x.label)[0].id; |
| 59 | +}; |
| 60 | + |
| 61 | +export const cleanString = (rawString: string): string => { |
| 62 | + return rawString.replaceAll('"', ''); |
| 63 | +}; |
| 64 | + |
| 65 | +export const formatDataTable = ( |
| 66 | + datatable: OpenSearchDashboardsDatatable |
| 67 | +): OpenSearchDashboardsDatatable => { |
| 68 | + datatable.columns.forEach((column) => { |
| 69 | + // clean quotation marks from names in columns |
| 70 | + column.name = cleanString(column.name); |
| 71 | + }); |
| 72 | + return datatable; |
| 73 | +}; |
| 74 | + |
| 75 | +export const setupConfig = (visParams: VisParams) => { |
| 76 | + const legendPosition = visParams.legendPosition; |
| 77 | + return { |
| 78 | + view: { |
| 79 | + stroke: null, |
| 80 | + }, |
| 81 | + concat: { |
| 82 | + spacing: 0, |
| 83 | + }, |
| 84 | + legend: { |
| 85 | + orient: legendPosition, |
| 86 | + }, |
| 87 | + }; |
| 88 | +}; |
| 89 | + |
| 90 | +export const buildLayerMark = (seriesParams: { |
| 91 | + type: string; |
| 92 | + interpolate: string; |
| 93 | + lineWidth: number; |
| 94 | + showCircles: boolean; |
| 95 | +}) => { |
| 96 | + return { |
| 97 | + // Possible types are: line, area, histogram. The eligibility checker will |
| 98 | + // prevent area and histogram (though area works in vega-lite) |
| 99 | + type: seriesParams.type, |
| 100 | + // Possible types: linear, cardinal, step-after. All of these types work in vega-lite |
| 101 | + interpolate: seriesParams.interpolate, |
| 102 | + // The possible values is any number, which matches what vega-lite supports |
| 103 | + strokeWidth: seriesParams.lineWidth, |
| 104 | + // this corresponds to showing the dots in the visbuilder for each data point |
| 105 | + point: seriesParams.showCircles, |
| 106 | + }; |
| 107 | +}; |
| 108 | + |
| 109 | +export const buildXAxis = ( |
| 110 | + xAxisTitle: string, |
| 111 | + xAxisId: string, |
| 112 | + startTime: number, |
| 113 | + endTime: number, |
| 114 | + visParams: VisParams |
| 115 | +) => { |
| 116 | + return { |
| 117 | + axis: { |
| 118 | + title: xAxisTitle, |
| 119 | + grid: visParams.grid.categoryLines, |
| 120 | + }, |
| 121 | + field: xAxisId, |
| 122 | + // Right now, the line charts can only set the x-axis value to be a date attribute, so |
| 123 | + // this should always be of type temporal |
| 124 | + type: 'temporal', |
| 125 | + scale: { |
| 126 | + domain: [startTime, endTime], |
| 127 | + }, |
| 128 | + }; |
| 129 | +}; |
| 130 | + |
| 131 | +export const buildYAxis = ( |
| 132 | + column: OpenSearchDashboardsDatatableColumn, |
| 133 | + valueAxis: ValueAxis, |
| 134 | + visParams: VisParams |
| 135 | +) => { |
| 136 | + return { |
| 137 | + axis: { |
| 138 | + title: cleanString(valueAxis.title.text) || column.name, |
| 139 | + grid: visParams.grid.valueAxis, |
| 140 | + orient: valueAxis.position, |
| 141 | + labels: valueAxis.labels.show, |
| 142 | + labelAngle: valueAxis.labels.rotate, |
| 143 | + }, |
| 144 | + field: column.id, |
| 145 | + type: 'quantitative', |
| 146 | + }; |
| 147 | +}; |
| 148 | + |
| 149 | +export const createSpecFromDatatable = ( |
| 150 | + datatable: OpenSearchDashboardsDatatable, |
| 151 | + visParams: VisParams, |
| 152 | + dimensions: VislibDimensions |
| 153 | +): object => { |
| 154 | + // TODO: we can try to use VegaSpec type but it is currently very outdated, where many |
| 155 | + // of the fields and sub-fields don't have other optional params that we want for customizing. |
| 156 | + // For now, we make this more loosely-typed by just specifying it as a generic object. |
| 157 | + const spec = {} as any; |
| 158 | + |
| 159 | + spec.$schema = 'https://vega.github.io/schema/vega-lite/v5.json'; |
| 160 | + spec.data = { |
| 161 | + values: datatable.rows, |
| 162 | + }; |
| 163 | + spec.config = setupConfig(visParams); |
| 164 | + |
| 165 | + // Get the valueAxes data and generate a map to easily fetch the different valueAxes data |
| 166 | + const valueAxis = new Map(); |
| 167 | + visParams?.valueAxes?.forEach((yAxis: ValueAxis) => { |
| 168 | + valueAxis.set(yAxis.id, yAxis); |
| 169 | + }); |
| 170 | + |
| 171 | + spec.layer = [] as any[]; |
| 172 | + |
| 173 | + if (datatable.rows.length > 0 && dimensions.x !== null) { |
| 174 | + const xAxisId = getXAxisId(dimensions, datatable.columns); |
| 175 | + const xAxisTitle = cleanString(dimensions.x.label); |
| 176 | + // get x-axis bounds for the chart |
| 177 | + const startTime = new Date(dimensions.x.params.bounds.min).valueOf(); |
| 178 | + const endTime = new Date(dimensions.x.params.bounds.max).valueOf(); |
| 179 | + let skip = 0; |
| 180 | + datatable.columns.forEach((column, index) => { |
| 181 | + // Check if it's not xAxis column data |
| 182 | + if (column.meta?.aggConfigParams?.interval !== undefined) { |
| 183 | + skip++; |
| 184 | + } else { |
| 185 | + const currentSeriesParams = visParams.seriesParams[index - skip]; |
| 186 | + const currentValueAxis = valueAxis.get(currentSeriesParams.valueAxis.toString()); |
| 187 | + let tooltip: Array<{ field: string; type: string; title: string }> = []; |
| 188 | + if (visParams.addTooltip) { |
| 189 | + tooltip = [ |
| 190 | + { field: xAxisId, type: 'temporal', title: xAxisTitle }, |
| 191 | + { field: column.id, type: 'quantitative', title: column.name }, |
| 192 | + ]; |
| 193 | + } |
| 194 | + spec.layer.push({ |
| 195 | + mark: buildLayerMark(currentSeriesParams), |
| 196 | + encoding: { |
| 197 | + x: buildXAxis(xAxisTitle, xAxisId, startTime, endTime, visParams), |
| 198 | + y: buildYAxis(column, currentValueAxis, visParams), |
| 199 | + tooltip, |
| 200 | + color: { |
| 201 | + // This ensures all the different metrics have their own distinct and unique color |
| 202 | + datum: column.name, |
| 203 | + }, |
| 204 | + }, |
| 205 | + }); |
| 206 | + } |
| 207 | + }); |
| 208 | + } |
| 209 | + |
| 210 | + if (visParams.addTimeMarker) { |
| 211 | + spec.transform = [ |
| 212 | + { |
| 213 | + calculate: 'now()', |
| 214 | + as: 'now_field', |
| 215 | + }, |
| 216 | + ]; |
| 217 | + |
| 218 | + spec.layer.push({ |
| 219 | + mark: 'rule', |
| 220 | + encoding: { |
| 221 | + x: { |
| 222 | + type: 'temporal', |
| 223 | + field: 'now_field', |
| 224 | + }, |
| 225 | + // The time marker on vislib is red, so keeping this consistent |
| 226 | + color: { |
| 227 | + value: 'red', |
| 228 | + }, |
| 229 | + size: { |
| 230 | + value: 1, |
| 231 | + }, |
| 232 | + }, |
| 233 | + }); |
| 234 | + } |
| 235 | + |
| 236 | + if (visParams.thresholdLine.show as boolean) { |
| 237 | + const layer = { |
| 238 | + mark: { |
| 239 | + type: 'rule', |
| 240 | + color: visParams.thresholdLine.color, |
| 241 | + strokeDash: [1, 0], |
| 242 | + }, |
| 243 | + encoding: { |
| 244 | + y: { |
| 245 | + datum: visParams.thresholdLine.value, |
| 246 | + }, |
| 247 | + }, |
| 248 | + }; |
| 249 | + |
| 250 | + // Can only support making a threshold line with full or dashed style, but not dot-dashed |
| 251 | + // due to vega-lite limitations |
| 252 | + if (visParams.thresholdLine.style !== 'full') { |
| 253 | + layer.mark.strokeDash = [8, 8]; |
| 254 | + } |
| 255 | + |
| 256 | + spec.layer.push(layer); |
| 257 | + } |
| 258 | + |
| 259 | + return spec; |
| 260 | +}; |
| 261 | + |
| 262 | +export const createLineVegaSpecFn = ( |
| 263 | + dependencies: VegaVisualizationDependencies |
| 264 | +): LineVegaSpecExpressionFunctionDefinition => ({ |
| 265 | + name: 'line_vega_spec', |
| 266 | + type: 'string', |
| 267 | + inputTypes: ['opensearch_dashboards_datatable'], |
| 268 | + help: i18n.translate('visTypeVega.function.help', { |
| 269 | + defaultMessage: 'Construct line vega spec', |
| 270 | + }), |
| 271 | + args: { |
| 272 | + visLayers: { |
| 273 | + types: ['string', 'null'], |
| 274 | + default: '', |
| 275 | + help: '', |
| 276 | + }, |
| 277 | + visParams: { |
| 278 | + types: ['string'], |
| 279 | + default: '""', |
| 280 | + help: '', |
| 281 | + }, |
| 282 | + dimensions: { |
| 283 | + types: ['string'], |
| 284 | + default: '""', |
| 285 | + help: '', |
| 286 | + }, |
| 287 | + }, |
| 288 | + async fn(input, args, context) { |
| 289 | + const table = cloneDeep(input); |
| 290 | + |
| 291 | + // creating initial vega spec from table |
| 292 | + const spec = createSpecFromDatatable( |
| 293 | + formatDataTable(table), |
| 294 | + JSON.parse(args.visParams), |
| 295 | + JSON.parse(args.dimensions) |
| 296 | + ); |
| 297 | + return JSON.stringify(spec); |
| 298 | + }, |
| 299 | +}); |
0 commit comments