2022-08-08 11:13:19 +02:00
|
|
|
import { filter, isArray, isNumber, isString } from 'lodash';
|
2022-04-22 14:33:13 +01:00
|
|
|
|
2022-08-08 11:13:19 +02:00
|
|
|
import { getBackendSrv } from '@grafana/runtime';
|
2017-12-20 12:33:33 +01:00
|
|
|
import config from 'app/core/config';
|
2022-04-22 14:33:13 +01:00
|
|
|
import store from 'app/core/store';
|
2016-02-26 10:09:38 +01:00
|
|
|
|
2017-11-24 16:18:56 +01:00
|
|
|
export class ImpressionSrv {
|
2016-02-26 10:09:38 +01:00
|
|
|
constructor() {}
|
|
|
|
|
|
2022-08-08 11:13:19 +02:00
|
|
|
addDashboardImpression(dashboardUID: string) {
|
2019-04-28 09:58:12 +02:00
|
|
|
const impressionsKey = this.impressionKey();
|
2022-08-08 11:13:19 +02:00
|
|
|
let impressions: string[] = [];
|
2016-03-16 11:52:25 +01:00
|
|
|
if (store.exists(impressionsKey)) {
|
|
|
|
|
impressions = JSON.parse(store.get(impressionsKey));
|
2021-04-21 08:38:00 +01:00
|
|
|
if (!isArray(impressions)) {
|
2016-02-26 10:09:38 +01:00
|
|
|
impressions = [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-20 07:59:48 +01:00
|
|
|
impressions = impressions.filter((imp) => {
|
2022-08-08 11:13:19 +02:00
|
|
|
return dashboardUID !== imp;
|
2016-02-29 19:51:35 +01:00
|
|
|
});
|
2016-02-26 10:09:38 +01:00
|
|
|
|
2022-08-08 11:13:19 +02:00
|
|
|
impressions.unshift(dashboardUID);
|
2016-02-26 10:09:38 +01:00
|
|
|
|
2016-03-05 12:26:21 +01:00
|
|
|
if (impressions.length > 50) {
|
|
|
|
|
impressions.pop();
|
2016-02-26 10:09:38 +01:00
|
|
|
}
|
2016-03-16 11:52:25 +01:00
|
|
|
store.set(impressionsKey, JSON.stringify(impressions));
|
2016-02-26 10:09:38 +01:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 11:13:19 +02:00
|
|
|
private async convertToUIDs() {
|
|
|
|
|
let impressions = this.getImpressions();
|
|
|
|
|
const ids = filter(impressions, (el) => isNumber(el));
|
|
|
|
|
if (!ids.length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-03-07 11:04:02 +01:00
|
|
|
|
2022-08-08 11:13:19 +02:00
|
|
|
const convertedUIDs = await getBackendSrv().get<string[]>(`/api/dashboards/ids/${ids.join(',')}`);
|
|
|
|
|
store.set(this.impressionKey(), JSON.stringify([...filter(impressions, (el) => isString(el)), ...convertedUIDs]));
|
|
|
|
|
}
|
2016-03-07 11:04:02 +01:00
|
|
|
|
2022-08-08 11:13:19 +02:00
|
|
|
private getImpressions() {
|
|
|
|
|
const impressions = store.get(this.impressionKey()) || '[]';
|
|
|
|
|
|
|
|
|
|
return JSON.parse(impressions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Returns an array of internal (string) dashboard UIDs */
|
|
|
|
|
async getDashboardOpened(): Promise<string[]> {
|
|
|
|
|
// TODO should be removed after UID migration
|
|
|
|
|
try {
|
|
|
|
|
await this.convertToUIDs();
|
|
|
|
|
} catch (_) {}
|
2016-03-07 11:04:02 +01:00
|
|
|
|
2022-08-08 11:13:19 +02:00
|
|
|
const result = filter(this.getImpressions(), (el) => isString(el));
|
|
|
|
|
return result;
|
2016-02-26 10:09:38 +01:00
|
|
|
}
|
2016-03-16 11:52:25 +01:00
|
|
|
|
2019-04-28 09:58:12 +02:00
|
|
|
impressionKey() {
|
2017-12-20 12:33:33 +01:00
|
|
|
return 'dashboard_impressions-' + config.bootData.user.orgId;
|
2016-03-16 11:52:25 +01:00
|
|
|
}
|
2016-02-26 10:09:38 +01:00
|
|
|
}
|
|
|
|
|
|
2017-11-24 16:18:56 +01:00
|
|
|
const impressionSrv = new ImpressionSrv();
|
|
|
|
|
export default impressionSrv;
|