2022-08-08 04:13:19 -05:00
|
|
|
import { filter, isArray, isNumber, isString } from 'lodash';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
2022-08-08 04:13:19 -05:00
|
|
|
import { getBackendSrv } from '@grafana/runtime';
|
2017-12-20 05:33:33 -06:00
|
|
|
import config from 'app/core/config';
|
2022-04-22 08:33:13 -05:00
|
|
|
import store from 'app/core/store';
|
2016-02-26 03:09:38 -06:00
|
|
|
|
2017-11-24 09:18:56 -06:00
|
|
|
export class ImpressionSrv {
|
2016-02-26 03:09:38 -06:00
|
|
|
constructor() {}
|
|
|
|
|
2022-08-08 04:13:19 -05:00
|
|
|
addDashboardImpression(dashboardUID: string) {
|
2019-04-28 02:58:12 -05:00
|
|
|
const impressionsKey = this.impressionKey();
|
2022-08-08 04:13:19 -05:00
|
|
|
let impressions: string[] = [];
|
2016-03-16 05:52:25 -05:00
|
|
|
if (store.exists(impressionsKey)) {
|
|
|
|
impressions = JSON.parse(store.get(impressionsKey));
|
2021-04-21 02:38:00 -05:00
|
|
|
if (!isArray(impressions)) {
|
2016-02-26 03:09:38 -06:00
|
|
|
impressions = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 00:59:48 -06:00
|
|
|
impressions = impressions.filter((imp) => {
|
2022-08-08 04:13:19 -05:00
|
|
|
return dashboardUID !== imp;
|
2016-02-29 12:51:35 -06:00
|
|
|
});
|
2016-02-26 03:09:38 -06:00
|
|
|
|
2022-08-08 04:13:19 -05:00
|
|
|
impressions.unshift(dashboardUID);
|
2016-02-26 03:09:38 -06:00
|
|
|
|
2016-03-05 05:26:21 -06:00
|
|
|
if (impressions.length > 50) {
|
|
|
|
impressions.pop();
|
2016-02-26 03:09:38 -06:00
|
|
|
}
|
2016-03-16 05:52:25 -05:00
|
|
|
store.set(impressionsKey, JSON.stringify(impressions));
|
2016-02-26 03:09:38 -06:00
|
|
|
}
|
|
|
|
|
2022-08-08 04:13:19 -05:00
|
|
|
private async convertToUIDs() {
|
|
|
|
let impressions = this.getImpressions();
|
|
|
|
const ids = filter(impressions, (el) => isNumber(el));
|
|
|
|
if (!ids.length) {
|
|
|
|
return;
|
|
|
|
}
|
2016-03-07 04:04:02 -06:00
|
|
|
|
2022-08-08 04:13:19 -05: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 04:04:02 -06:00
|
|
|
|
2022-08-08 04:13:19 -05: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 04:04:02 -06:00
|
|
|
|
2022-08-08 04:13:19 -05:00
|
|
|
const result = filter(this.getImpressions(), (el) => isString(el));
|
|
|
|
return result;
|
2016-02-26 03:09:38 -06:00
|
|
|
}
|
2016-03-16 05:52:25 -05:00
|
|
|
|
2019-04-28 02:58:12 -05:00
|
|
|
impressionKey() {
|
2017-12-20 05:33:33 -06:00
|
|
|
return 'dashboard_impressions-' + config.bootData.user.orgId;
|
2016-03-16 05:52:25 -05:00
|
|
|
}
|
2016-02-26 03:09:38 -06:00
|
|
|
}
|
|
|
|
|
2017-11-24 09:18:56 -06:00
|
|
|
const impressionSrv = new ImpressionSrv();
|
|
|
|
export default impressionSrv;
|