mirror of
https://github.com/SigNoz/signoz.git
synced 2025-12-25 11:30:08 +00:00
* feat: metrics builder metrics name suggestion UX changes * feat: metrics builder metric name and single value selection * feat: code cleanup * feat: improved ts typings Co-authored-by: Srikanth Chekuri <srikanth.chekuri92@gmail.com>
53 lines
1.2 KiB
TypeScript
53 lines
1.2 KiB
TypeScript
import { QueryData } from 'types/api/widgets/getQuery';
|
|
|
|
const getLabelName = (
|
|
metric: QueryData['metric'],
|
|
query: string,
|
|
legends: string,
|
|
): string => {
|
|
if (metric === undefined) {
|
|
return '';
|
|
}
|
|
|
|
const keysArray = Object.keys(metric);
|
|
if (legends.length !== 0) {
|
|
const variables = legends
|
|
.split('{{')
|
|
.filter((e) => e)
|
|
.map((e) => e.split('}}')[0]);
|
|
|
|
const results = variables.map((variable) => metric[variable]);
|
|
|
|
let endResult = legends;
|
|
|
|
variables.forEach((e, index) => {
|
|
endResult = endResult.replace(`{{${e}}}`, results[index]);
|
|
});
|
|
|
|
return endResult;
|
|
}
|
|
|
|
const index = keysArray.findIndex((e) => e === '__name__');
|
|
|
|
const preArray = index !== -1 ? keysArray.slice(0, index) : [];
|
|
const postArray = keysArray.slice(index + 1, keysArray.length);
|
|
|
|
if (index === undefined && preArray.length === 0 && postArray.length) {
|
|
return query;
|
|
}
|
|
|
|
const post = postArray.map((e) => `${e}="${metric[e]}"`).join(',');
|
|
const pre = preArray.map((e) => `${e}="${metric[e]}"`).join(',');
|
|
|
|
const value = metric[keysArray[index]];
|
|
|
|
const result = `${value === undefined ? '' : value}`;
|
|
|
|
if (post.length === 0 && pre.length === 0) {
|
|
return result;
|
|
}
|
|
return `${result}{${pre}${post}}`;
|
|
};
|
|
|
|
export default getLabelName;
|