2023-09-21 16:24:08 -03:00
|
|
|
import { DataSourceWithBackend } from '@grafana/runtime';
|
2022-09-22 09:35:04 -03:00
|
|
|
import { getConfig } from 'app/core/config';
|
2023-09-21 16:24:08 -03:00
|
|
|
import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
|
2022-09-22 09:35:04 -03:00
|
|
|
import { VariableModel } from 'app/features/variables/types';
|
|
|
|
|
|
2022-12-01 10:02:10 -06:00
|
|
|
import { PanelModel } from '../../../state';
|
2023-09-04 18:19:22 -03:00
|
|
|
import { shareDashboardType } from '../utils';
|
2022-12-01 10:02:10 -06:00
|
|
|
|
|
|
|
|
import { supportedDatasources } from './SupportedPubdashDatasources';
|
|
|
|
|
|
2023-02-28 09:02:23 -03:00
|
|
|
export enum PublicDashboardShareType {
|
|
|
|
|
PUBLIC = 'public',
|
|
|
|
|
EMAIL = 'email',
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-24 12:36:29 -03:00
|
|
|
export interface PublicDashboardSettings {
|
2022-10-21 13:42:14 -06:00
|
|
|
annotationsEnabled: boolean;
|
2022-09-22 09:35:04 -03:00
|
|
|
isEnabled: boolean;
|
2023-01-18 10:54:19 -03:00
|
|
|
timeSelectionEnabled: boolean;
|
2022-09-22 09:35:04 -03:00
|
|
|
}
|
|
|
|
|
|
2023-02-24 12:36:29 -03:00
|
|
|
export interface PublicDashboard extends PublicDashboardSettings {
|
|
|
|
|
accessToken?: string;
|
|
|
|
|
uid: string;
|
|
|
|
|
dashboardUid: string;
|
|
|
|
|
timeSettings?: object;
|
2023-02-28 09:02:23 -03:00
|
|
|
share: PublicDashboardShareType;
|
2023-03-02 19:15:56 -03:00
|
|
|
recipients?: Array<{ uid: string; recipient: string }>;
|
2022-09-22 09:35:04 -03:00
|
|
|
}
|
|
|
|
|
|
2023-04-27 14:20:03 -03:00
|
|
|
export interface SessionDashboard {
|
|
|
|
|
dashboardTitle: string;
|
|
|
|
|
dashboardUid: string;
|
|
|
|
|
publicDashboardAccessToken: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface SessionUser {
|
|
|
|
|
email: string;
|
|
|
|
|
firstSeenAtAge: string;
|
2023-11-08 11:11:02 -03:00
|
|
|
lastSeenAtAge: string;
|
2023-04-27 14:20:03 -03:00
|
|
|
totalDashboards: number;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-22 09:35:04 -03:00
|
|
|
// Instance methods
|
|
|
|
|
export const dashboardHasTemplateVariables = (variables: VariableModel[]): boolean => {
|
|
|
|
|
return variables.length > 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const publicDashboardPersisted = (publicDashboard?: PublicDashboard): boolean => {
|
|
|
|
|
return publicDashboard?.uid !== '' && publicDashboard?.uid !== undefined;
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-01 10:02:10 -06:00
|
|
|
/**
|
|
|
|
|
* Get unique datasource names from all panels that are not currently supported by public dashboards.
|
|
|
|
|
*/
|
2023-09-21 16:24:08 -03:00
|
|
|
export const getUnsupportedDashboardDatasources = async (panels: PanelModel[]): Promise<string[]> => {
|
2022-12-01 10:02:10 -06:00
|
|
|
let unsupportedDS = new Set<string>();
|
|
|
|
|
|
|
|
|
|
for (const panel of panels) {
|
|
|
|
|
for (const target of panel.targets) {
|
2023-09-21 16:24:08 -03:00
|
|
|
const dsType = target?.datasource?.type;
|
|
|
|
|
if (dsType) {
|
|
|
|
|
if (!supportedDatasources.has(dsType)) {
|
|
|
|
|
unsupportedDS.add(dsType);
|
|
|
|
|
} else {
|
|
|
|
|
const ds = await getDatasourceSrv().get(target.datasource);
|
|
|
|
|
if (!(ds instanceof DataSourceWithBackend)) {
|
|
|
|
|
unsupportedDS.add(dsType);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-01 10:02:10 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Array.from(unsupportedDS).sort();
|
|
|
|
|
};
|
|
|
|
|
|
2022-09-22 09:35:04 -03:00
|
|
|
/**
|
|
|
|
|
* Generate the public dashboard url. Uses the appUrl from the Grafana boot config, so urls will also be correct
|
|
|
|
|
* when Grafana is hosted on a subpath.
|
|
|
|
|
*
|
|
|
|
|
* All app urls from the Grafana boot config end with a slash.
|
|
|
|
|
*
|
2023-04-27 14:20:03 -03:00
|
|
|
* @param accessToken
|
2022-09-22 09:35:04 -03:00
|
|
|
*/
|
2023-04-27 14:20:03 -03:00
|
|
|
export const generatePublicDashboardUrl = (accessToken: string): string => {
|
|
|
|
|
return `${getConfig().appUrl}public-dashboards/${accessToken}`;
|
2022-09-22 09:35:04 -03:00
|
|
|
};
|
2023-02-28 09:02:23 -03:00
|
|
|
|
2023-05-08 16:51:42 -03:00
|
|
|
export const generatePublicDashboardConfigUrl = (dashboardUid: string): string => {
|
2023-09-04 18:19:22 -03:00
|
|
|
return `/d/${dashboardUid}?shareView=${shareDashboardType.publicDashboard}`;
|
2023-05-08 16:51:42 -03:00
|
|
|
};
|
|
|
|
|
|
2023-02-28 09:02:23 -03:00
|
|
|
export const validEmailRegex = /^[A-Z\d._%+-]+@[A-Z\d.-]+\.[A-Z]{2,}$/i;
|