mirror of
https://github.com/grafana/grafana.git
synced 2026-08-26 13:27:30 -05:00
* Add basic button for adding a query template * Add hook to create a template * Handle notifications * Add tags to invalidate cache * Generate translations * Updates types * Add tests * Simplify code * Add user to type * Add a better default title * bring in piotrs pr and try to add user data * Move out of metadata (reserved in k8s) and make new values exportable * Show user data * Fix bad merge * WIP * Add annotation data to FE * add (failing) test * Fix types and test * Cleanup * Enhance user data and send to component for display * Fix type * Fix expected values * fix betterer * Fix test * Remove user lookup * testing slug usage for api * Revert "testing slug usage for api" This reverts commitcc4556c3b7. * change types, display userid if login isnt returned * Simply display whatever is in property * skip test on removed logic * Try waiting for query to finish before eval * Revert "Try waiting for query to finish before eval" This reverts commit6220cabd17. * Handle attribute not existing when storage type is file --------- Co-authored-by: Piotr Jamroz <pm.jamroz@gmail.com>
63 lines
1.8 KiB
TypeScript
63 lines
1.8 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,
|
|
},
|
|
spec: {
|
|
title: title,
|
|
vars: [], // TODO: Detect variables in #86838
|
|
targets: targets.map((dataQuery) => ({
|
|
variables: {},
|
|
properties: dataQuery,
|
|
})),
|
|
},
|
|
};
|
|
};
|