From 3a4f58bb4044e8c807ab03116d02fb505ed546a6 Mon Sep 17 00:00:00 2001 From: Vamsi Manohar Date: Thu, 3 Nov 2022 14:56:40 -0700 Subject: [PATCH] Prometheus Mappings Fix Signed-off-by: Vamsi Manohar --- dashboards-observability/public/plugin.ts | 2 +- .../public/services/timestamp/timestamp.ts | 51 ++++++++++++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/dashboards-observability/public/plugin.ts b/dashboards-observability/public/plugin.ts index a14131546..1e3189e57 100644 --- a/dashboards-observability/public/plugin.ts +++ b/dashboards-observability/public/plugin.ts @@ -47,7 +47,7 @@ export class ObservabilityPlugin implements Plugin isEqual(type, dateTimeType)); @@ -60,6 +61,54 @@ export default class TimestampUtils { } async getIndexMappings(index: string) { + const myArray = index.split('.'); + if (myArray.length > 1) { + if (await this.isPrometheusCatalog(myArray[0])) { + const mappings = await this.pplService.fetch({ + query: 'describe ' + index + ' | fields COLUMN_NAME, DATA_TYPE', + format: 'jdbc', + }); + return this.convertToMappings(index, mappings); + } + } return await this.dslService.fetchFields(index); } + + async isPrometheusCatalog(catalog: string) { + const catalogs = await this.pplService.fetch({ + query: "show datasources | where CONNECTOR_TYPE='PROMETHEUS' | fields DATASOURCE_NAME", + format: 'viz', + }); + if ( + catalogs.data && + catalogs.data.CATALOG_NAME && + catalogs.data.CATALOG_NAME.includes(catalog) + ) { + return true; + } else { + return false; + } + } + + private convertToMappings(index: string, { datarows }: object) { + const result = {}; + result[index] = { + mappings: { + properties: {}, + }, + }; + if (datarows) { + for (const s of datarows) { + const key = s[0]; + let datatype = s[1]; + if (datatype === 'timestamp') { + datatype = 'date'; + } + result[index].mappings.properties[key] = { + type: datatype, + }; + } + } + return result; + } }