Obtaining the metric type fields of a specific data source


#1

Hello, I have an application, and I have a UI where I show information about a selected metric type field.

The thing is that those fields of type metric are loaded in a dropdown list once a new data source is selected in another dropdown list.

What I am doing, to be more specific, is that when I select a new data source, I automatically call the getDatasourceMetadata(source, provider) function and get the meta.objectFields that the function returns, finally I filter them to show in the dropdown list only fields of type metric (‘NUMBER’, ‘INTEGER’, ‘MONEY’, ‘PERCENT’).

Is the way I’m doing it right or is there a more efficient way?


#2

Hi @oliver89, using the getDatasourceMetadata function is a way to get all the fields and then later you can filter them by the data types that correspond to the metrics. But there is a more direct way and it is by calling the getDatasourceMetrics(source, provider) function, which directly returns the fields that are of type metric.

Note that in order to get the fields correctly from this function, you have to have previously gotten the metadata from the provider using the getDatasourceMetadata function as you have been using it, you just have to call it the first time.

Something like this:

if (cf.getProviderByName('Elasticsearch')._metadata['index-name']) {
    await cf.getDatasourceMetadata('index-name', 'Elasticsearch');
}

const metricFields = cf.getDatasourceMetrics('index-name', 'Elasticsearch');
console.log(metricFields);

Let me know if it works for you.