grafana/public/app/features/query-library/api/mappers.ts
Kristina 783ff71560
Explore: Allow saving to Query Library from query row (#89381)
* wip

* Add save functionality to query row

* add success conditional

* move around translations

* Add translations

* Add key to fix test

* Add key to the right spot

* define specific save button

* WIP - Use RowActionComponents to add action without modifying the core component

* Only add component once on render

* Move logic to main explore page

* Add keyed render actions to prevent redundancy, use this to add keyed action

* Overcome the forces of dayquil to attempt to make actual sense

* Add scoped actions to query action component

* Spaces not allowed in generateName
2024-07-30 13:56:44 -05:00

63 lines
1.9 KiB
TypeScript

import { AddQueryTemplateCommand, QueryTemplate } from '../types';
import { API_VERSION, QueryTemplateKinds } from './query';
import { CREATED_BY_KEY, DataQuerySpec, DataQuerySpecResponse, DataQueryTarget } from './types';
export const parseCreatedByValue = (value?: string) => {
// https://github.com/grafana/grafana/blob/main/pkg/services/user/identity.go#L194
/*if (value !== undefined && value !== '') {
const vals = value.split(':');
if (vals.length >= 2) {
if (vals[0] === 'anonymous' || vals[0] === 'render' || vals[0] === '') {
return undefined;
} else {
return {
userId: vals[1],
login: vals[2],
};
}
} else {
return undefined;
}
} else {
return undefined;
}*/
return !!value ? value : undefined;
};
export const convertDataQueryResponseToQueryTemplates = (result: DataQuerySpecResponse): QueryTemplate[] => {
if (!result.items) {
return [];
}
return result.items.map((spec) => {
return {
uid: spec.metadata.name || '',
title: spec.spec.title,
targets: spec.spec.targets.map((target: DataQueryTarget) => target.properties),
createdAtTimestamp: new Date(spec.metadata.creationTimestamp || '').getTime(),
user: parseCreatedByValue(spec.metadata?.annotations?.[CREATED_BY_KEY]),
};
});
};
export const convertAddQueryTemplateCommandToDataQuerySpec = (
addQueryTemplateCommand: AddQueryTemplateCommand
): DataQuerySpec => {
const { title, targets } = addQueryTemplateCommand;
return {
apiVersion: API_VERSION,
kind: QueryTemplateKinds.QueryTemplate,
metadata: {
generateName: 'A' + title.replaceAll(' ', '-'),
},
spec: {
title: title,
vars: [], // TODO: Detect variables in #86838
targets: targets.map((dataQuery) => ({
variables: {},
properties: dataQuery,
})),
},
};
};