diff --git a/.betterer.results b/.betterer.results index 35c44194c85..70347d3110f 100644 --- a/.betterer.results +++ b/.betterer.results @@ -3992,9 +3992,6 @@ exports[`better eslint`] = { [0, 0, 0, "No untranslated strings. Wrap text with <Trans />", "5"], [0, 0, 0, "No untranslated strings. Wrap text with <Trans />", "6"] ], - "public/app/features/explore/QueryLibrary/QueryTemplatesTable/AddedByCell.tsx:5381": [ - [0, 0, 0, "No untranslated strings. Wrap text with <Trans />", "0"] - ], "public/app/features/explore/QueryLibrary/QueryTemplatesTable/QueryDescriptionCell.tsx:5381": [ [0, 0, 0, "No untranslated strings. Wrap text with <Trans />", "0"] ], diff --git a/public/app/features/explore/QueryLibrary/QueryTemplatesList.tsx b/public/app/features/explore/QueryLibrary/QueryTemplatesList.tsx index f57e0a4b9de..eed3e100131 100644 --- a/public/app/features/explore/QueryLibrary/QueryTemplatesList.tsx +++ b/public/app/features/explore/QueryLibrary/QueryTemplatesList.tsx @@ -45,6 +45,7 @@ export function QueryTemplatesList() { createdAtTimestamp: queryTemplate?.createdAtTimestamp || 0, query: queryTemplate.targets[0], description: queryTemplate.title, + user: queryTemplate.user, }; }); diff --git a/public/app/features/explore/QueryLibrary/QueryTemplatesTable/AddedByCell.tsx b/public/app/features/explore/QueryLibrary/QueryTemplatesTable/AddedByCell.tsx index 34d63e256c7..b9903195d47 100644 --- a/public/app/features/explore/QueryLibrary/QueryTemplatesTable/AddedByCell.tsx +++ b/public/app/features/explore/QueryLibrary/QueryTemplatesTable/AddedByCell.tsx @@ -1,16 +1,14 @@ -import { Avatar } from '@grafana/ui'; - import { useQueryLibraryListStyles } from './styles'; -export function AddedByCell() { +type AddedByCellProps = { + user?: string; +}; +export function AddedByCell(props: AddedByCellProps) { const styles = useQueryLibraryListStyles(); return ( <div> - <span className={styles.logo}> - <Avatar src="https://secure.gravatar.com/avatar" alt="unknown" /> - </span> - <span className={styles.otherText}>Unknown</span> + <span className={styles.otherText}>{props.user || 'Unknown'}</span> </div> ); } diff --git a/public/app/features/explore/QueryLibrary/QueryTemplatesTable/index.tsx b/public/app/features/explore/QueryLibrary/QueryTemplatesTable/index.tsx index a3dd020eec7..376e7457dd8 100644 --- a/public/app/features/explore/QueryLibrary/QueryTemplatesTable/index.tsx +++ b/public/app/features/explore/QueryLibrary/QueryTemplatesTable/index.tsx @@ -18,7 +18,7 @@ const timestampSort: SortByFn<QueryTemplateRow> = (rowA, rowB, _, desc) => { const columns: Array<Column<QueryTemplateRow>> = [ { id: 'description', header: 'Data source and query', cell: QueryDescriptionCell }, - { id: 'addedBy', header: 'Added by', cell: AddedByCell }, + { id: 'addedBy', header: 'Added by', cell: ({ row: { original } }) => <AddedByCell user={original.user} /> }, { id: 'datasourceType', header: 'Datasource type', cell: DatasourceTypeCell, sortType: 'string' }, { id: 'createdAtTimestamp', header: 'Date added', cell: DateAddedCell, sortType: timestampSort }, { diff --git a/public/app/features/explore/QueryLibrary/QueryTemplatesTable/types.ts b/public/app/features/explore/QueryLibrary/QueryTemplatesTable/types.ts index 59cc7b24b6a..b9ca932e980 100644 --- a/public/app/features/explore/QueryLibrary/QueryTemplatesTable/types.ts +++ b/public/app/features/explore/QueryLibrary/QueryTemplatesTable/types.ts @@ -7,5 +7,6 @@ export type QueryTemplateRow = { datasourceRef?: DataSourceRef | null; datasourceType?: string; createdAtTimestamp?: number; + user?: string; uid?: string; }; diff --git a/public/app/features/query-library/api/mappers.test.ts b/public/app/features/query-library/api/mappers.test.ts new file mode 100644 index 00000000000..46620ef5166 --- /dev/null +++ b/public/app/features/query-library/api/mappers.test.ts @@ -0,0 +1,18 @@ +import { parseCreatedByValue } from './mappers'; + +describe.skip('mappers', () => { + describe('parseCreatedByValue', () => { + it.each` + value | expected + ${''} | ${undefined} + ${'api-key:1'} | ${{ userId: 1 }} + ${'service-account:1:admin'} | ${{ userId: 1, login: 'admin' }} + ${'user:1:admin'} | ${{ userId: 1, login: 'admin' }} + ${'anonymous:0'} | ${undefined} + ${'render:0'} | ${undefined} + ${':0'} | ${undefined} + `("parsing '$value' should be '$expected'", ({ value, expected }) => { + expect(parseCreatedByValue(value)).toEqual(expected); + }); + }); +}); diff --git a/public/app/features/query-library/api/mappers.ts b/public/app/features/query-library/api/mappers.ts index 3a6e3bf836b..d3a90d6e497 100644 --- a/public/app/features/query-library/api/mappers.ts +++ b/public/app/features/query-library/api/mappers.ts @@ -1,7 +1,29 @@ import { AddQueryTemplateCommand, QueryTemplate } from '../types'; import { API_VERSION, QueryTemplateKinds } from './query'; -import { DataQuerySpec, DataQuerySpecResponse, DataQueryTarget } from './types'; +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) { @@ -13,6 +35,7 @@ export const convertDataQueryResponseToQueryTemplates = (result: DataQuerySpecRe 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]), }; }); }; diff --git a/public/app/features/query-library/api/testdata/testQueryList.ts b/public/app/features/query-library/api/testdata/testQueryList.ts index 2a941716b7d..474210ef5d9 100644 --- a/public/app/features/query-library/api/testdata/testQueryList.ts +++ b/public/app/features/query-library/api/testdata/testQueryList.ts @@ -16,6 +16,7 @@ export const getTestQueryList = () => ({ uid: '65327fce-c545-489d-ada5-16f909453d12', resourceVersion: '1783293341664808960', creationTimestamp: '2024-04-25T20:32:58Z', + annotations: { 'grafana.app/createdBy': 'user:1:admin' }, }, spec: { title: 'Elastic Query Template', @@ -62,6 +63,7 @@ export const getTestQueryList = () => ({ uid: '3e71de65-efa7-40e3-8f23-124212cca455', resourceVersion: '1783214217151647744', creationTimestamp: '2024-04-25T11:05:55Z', + annotations: { 'grafana.app/createdBy': 'user:1:admin' }, }, spec: { title: 'Loki Query Template', diff --git a/public/app/features/query-library/api/types.ts b/public/app/features/query-library/api/types.ts index e2feb99057f..bf10cbc936e 100644 --- a/public/app/features/query-library/api/types.ts +++ b/public/app/features/query-library/api/types.ts @@ -12,6 +12,7 @@ export type DataQuerySpec = { generateName: string; name?: string; creationTimestamp?: string; + annotations?: { [key: string]: string }; }; spec: { title: string; @@ -24,3 +25,5 @@ export type DataQuerySpecResponse = { apiVersion: string; items: DataQuerySpec[]; }; + +export const CREATED_BY_KEY = 'grafana.app/createdBy'; diff --git a/public/app/features/query-library/types.ts b/public/app/features/query-library/types.ts index 9be092869d4..66a92b085a2 100644 --- a/public/app/features/query-library/types.ts +++ b/public/app/features/query-library/types.ts @@ -5,6 +5,7 @@ export type QueryTemplate = { title: string; targets: DataQuery[]; createdAtTimestamp: number; + user?: string; }; export type AddQueryTemplateCommand = {