1import store from 'app/core/store';
2import { filter, isArray, isNumber } from 'lodash';
3import config from 'app/core/config';
4
5export class ImpressionSrv {
6  constructor() {}
7
8  addDashboardImpression(dashboardId: number) {
9    const impressionsKey = this.impressionKey();
10    let impressions = [];
11    if (store.exists(impressionsKey)) {
12      impressions = JSON.parse(store.get(impressionsKey));
13      if (!isArray(impressions)) {
14        impressions = [];
15      }
16    }
17
18    impressions = impressions.filter((imp) => {
19      return dashboardId !== imp;
20    });
21
22    impressions.unshift(dashboardId);
23
24    if (impressions.length > 50) {
25      impressions.pop();
26    }
27    store.set(impressionsKey, JSON.stringify(impressions));
28  }
29
30  getDashboardOpened() {
31    let impressions = store.get(this.impressionKey()) || '[]';
32
33    impressions = JSON.parse(impressions);
34
35    impressions = filter(impressions, (el) => {
36      return isNumber(el);
37    });
38
39    return impressions;
40  }
41
42  impressionKey() {
43    return 'dashboard_impressions-' + config.bootData.user.orgId;
44  }
45}
46
47const impressionSrv = new ImpressionSrv();
48export default impressionSrv;
49