mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Remove user lookup
This commit is contained in:
parent
22fb692a79
commit
442ea2e53f
@ -1,10 +1,7 @@
|
||||
import { uniq } from 'lodash';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import React from 'react';
|
||||
|
||||
import { getBackendSrv } from '@grafana/runtime/src/services/backendSrv';
|
||||
import { EmptyState, Spinner } from '@grafana/ui';
|
||||
import { useAllQueryTemplatesQuery } from 'app/features/query-library';
|
||||
import { User } from 'app/features/query-library/api/types';
|
||||
import { QueryTemplate } from 'app/features/query-library/types';
|
||||
|
||||
import { getDatasourceSrv } from '../../plugins/datasource_srv';
|
||||
@ -12,59 +9,8 @@ import { getDatasourceSrv } from '../../plugins/datasource_srv';
|
||||
import QueryTemplatesTable from './QueryTemplatesTable';
|
||||
import { QueryTemplateRow } from './QueryTemplatesTable/types';
|
||||
|
||||
const getQueryTemplateRows = async (data: QueryTemplate[]): Promise<QueryTemplateRow[]> => {
|
||||
const userQtList = uniq(data.map((qt) => qt.user).filter((user): user is User => !!user));
|
||||
|
||||
const userData = Promise.all(
|
||||
userQtList.map((user) => {
|
||||
if (user.userId) {
|
||||
return getBackendSrv().get(`api/users/${user.userId}`);
|
||||
} else if (user.login) {
|
||||
return getBackendSrv().get(`api/users/lookup/${user.login}`);
|
||||
} else {
|
||||
// should never happen
|
||||
return Promise.resolve();
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
return userData.then((ud) => {
|
||||
return data.map((queryTemplate: QueryTemplate, index: number) => {
|
||||
const datasourceRef = queryTemplate.targets[0]?.datasource;
|
||||
const datasourceType = getDatasourceSrv().getInstanceSettings(datasourceRef)?.meta.name || '';
|
||||
|
||||
const user =
|
||||
queryTemplate.user?.userId !== undefined
|
||||
? ud.find((user) => user.id === queryTemplate.user?.userId)
|
||||
: ud.find((user) => user.login === queryTemplate.user?.login);
|
||||
|
||||
return {
|
||||
index: index.toString(),
|
||||
datasourceRef,
|
||||
datasourceType,
|
||||
createdAtTimestamp: queryTemplate?.createdAtTimestamp || 0,
|
||||
query: queryTemplate.targets[0],
|
||||
description: queryTemplate.title,
|
||||
user: user,
|
||||
};
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export function QueryTemplatesList() {
|
||||
const { data, isLoading, error } = useAllQueryTemplatesQuery();
|
||||
const [queryTemplateData, setQueryTemplateData] = useState<QueryTemplateRow[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
const setQTData = async () => {
|
||||
const queryTemplateData = await getQueryTemplateRows(data!);
|
||||
setQueryTemplateData(queryTemplateData);
|
||||
};
|
||||
|
||||
if (isLoading === false && data !== undefined) {
|
||||
setQTData();
|
||||
}
|
||||
}, [data, isLoading]);
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
@ -90,5 +36,19 @@ export function QueryTemplatesList() {
|
||||
);
|
||||
}
|
||||
|
||||
return <QueryTemplatesTable queryTemplateRows={queryTemplateData} />;
|
||||
const queryTemplateRows: QueryTemplateRow[] = data.map((queryTemplate: QueryTemplate, index: number) => {
|
||||
const datasourceRef = queryTemplate.targets[0]?.datasource;
|
||||
const datasourceType = getDatasourceSrv().getInstanceSettings(datasourceRef)?.meta.name || '';
|
||||
return {
|
||||
index: index.toString(),
|
||||
datasourceRef,
|
||||
datasourceType,
|
||||
createdAtTimestamp: queryTemplate?.createdAtTimestamp || 0,
|
||||
query: queryTemplate.targets[0],
|
||||
description: queryTemplate.title,
|
||||
user: queryTemplate.user,
|
||||
};
|
||||
});
|
||||
|
||||
return <QueryTemplatesTable queryTemplateRows={queryTemplateRows} />;
|
||||
}
|
||||
|
@ -1,21 +1,17 @@
|
||||
import React from 'react';
|
||||
|
||||
import { Avatar } from '@grafana/ui';
|
||||
import { UserDTO } from 'app/types';
|
||||
import { User } from 'app/features/query-library/api/types';
|
||||
|
||||
import { useQueryLibraryListStyles } from './styles';
|
||||
|
||||
type AddedByCellProps = {
|
||||
user?: UserDTO;
|
||||
user?: User;
|
||||
};
|
||||
export function AddedByCell(props: AddedByCellProps) {
|
||||
const styles = useQueryLibraryListStyles();
|
||||
|
||||
return (
|
||||
<div>
|
||||
<span className={styles.logo}>
|
||||
<Avatar src={props.user?.avatarUrl || 'https://secure.gravatar.com/avatar'} alt="unknown" />
|
||||
</span>
|
||||
<span className={styles.otherText}>{props.user?.login || 'Unknown'}</span>
|
||||
</div>
|
||||
);
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { DataQuery, DataSourceRef } from '@grafana/schema';
|
||||
import { UserDTO } from 'app/types';
|
||||
import { User } from 'app/features/query-library/api/types';
|
||||
|
||||
export type QueryTemplateRow = {
|
||||
index: string;
|
||||
@ -8,5 +8,5 @@ export type QueryTemplateRow = {
|
||||
datasourceRef?: DataSourceRef | null;
|
||||
datasourceType?: string;
|
||||
createdAtTimestamp?: number;
|
||||
user?: UserDTO;
|
||||
user?: User;
|
||||
};
|
||||
|
@ -83,7 +83,7 @@ export function setupExplore(options?: SetupOptions): {
|
||||
}
|
||||
return of({ data });
|
||||
}),
|
||||
get: jest.fn().mockResolvedValue({}),
|
||||
get: jest.fn(),
|
||||
patch: jest.fn().mockRejectedValue(undefined),
|
||||
post: jest.fn(),
|
||||
put: jest.fn().mockRejectedValue(undefined),
|
||||
|
Loading…
Reference in New Issue
Block a user