Skip to content

Commit

Permalink
Prometheus Mappings Fix
Browse files Browse the repository at this point in the history
Signed-off-by: Vamsi Manohar <[email protected]>
  • Loading branch information
vmmusings committed Nov 4, 2022
1 parent 001709a commit 3a4f58b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dashboards-observability/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class ObservabilityPlugin implements Plugin<ObservabilitySetup, Observabi
const pplService = new PPLService(coreStart.http);
const dslService = new DSLService(coreStart.http);
const savedObjects = new SavedObjects(coreStart.http);
const timestampUtils = new TimestampUtils(dslService);
const timestampUtils = new TimestampUtils(dslService, pplService);
const qm = new QueryManager();
return Observability(
coreStart,
Expand Down
51 changes: 50 additions & 1 deletion dashboards-observability/public/services/timestamp/timestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
import { isEmpty, isEqual, values, keys } from 'lodash';
import DSLService from '../requests/dsl';
import { IDefaultTimestampState } from '../../../common/types/explorer';
import PPLService from '../requests/ppl';

// eslint-disable-next-line import/no-default-export
export default class TimestampUtils {
constructor(private dslService: DSLService) {}
constructor(private dslService: DSLService, private pplService: PPLService) {}

isTimeField(type: string) {
return ['date', 'date_nanos'].some((dateTimeType) => isEqual(type, dateTimeType));
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 3a4f58b

Please sign in to comment.