From e5970e83ffccf7db3b8bd5532e11413f21ed7701 Mon Sep 17 00:00:00 2001 From: bergquist Date: Fri, 26 Feb 2016 10:09:38 +0100 Subject: [PATCH 1/2] feat(dashlist): list last x viewed dashboards closes #3896 --- public/app/features/dashboard/all.js | 1 + .../features/dashboard/dashboardLoaderSrv.js | 25 +++++++---- public/app/features/dashboard/impressions.js | 7 ++++ public/app/features/dashboard/impressions2.ts | 41 +++++++++++++++++++ public/app/plugins/panel/dashlist/module.html | 2 +- public/app/plugins/panel/dashlist/module.ts | 16 +++++++- public/dashboards/home.json | 4 +- 7 files changed, 84 insertions(+), 12 deletions(-) create mode 100644 public/app/features/dashboard/impressions.js create mode 100644 public/app/features/dashboard/impressions2.ts diff --git a/public/app/features/dashboard/all.js b/public/app/features/dashboard/all.js index 073ca2ae1d9..15b96f2a1b6 100644 --- a/public/app/features/dashboard/all.js +++ b/public/app/features/dashboard/all.js @@ -16,4 +16,5 @@ define([ './graphiteImportCtrl', './dynamicDashboardSrv', './importCtrl', + './impressions', ], function () {}); diff --git a/public/app/features/dashboard/dashboardLoaderSrv.js b/public/app/features/dashboard/dashboardLoaderSrv.js index f578a9d1075..5b775166381 100644 --- a/public/app/features/dashboard/dashboardLoaderSrv.js +++ b/public/app/features/dashboard/dashboardLoaderSrv.js @@ -5,8 +5,9 @@ define([ 'jquery', 'app/core/utils/kbn', 'app/core/utils/datemath', + './impressions', ], -function (angular, moment, _, $, kbn, dateMath) { +function (angular, moment, _, $, kbn, dateMath, impressions) { 'use strict'; var module = angular.module('grafana.services'); @@ -24,19 +25,27 @@ function (angular, moment, _, $, kbn, dateMath) { }; this.loadDashboard = function(type, slug) { - if (type === 'script') { - return this._loadScriptedDashboard(slug); - } + var promise; - if (type === 'snapshot') { - return backendSrv.get('/api/snapshots/' + $routeParams.slug).catch(function() { + if (type === 'script') { + promise = this._loadScriptedDashboard(slug); + } else if (type === 'snapshot') { + promise = backendSrv.get('/api/snapshots/' + $routeParams.slug).catch(function() { return {meta:{isSnapshot: true, canSave: false, canEdit: false}, dashboard: {title: 'Snapshot not found'}}; }); + } else { + promise = backendSrv.getDashboard($routeParams.type, $routeParams.slug) + .catch(function() { + return self._dashboardLoadFailed("Not found"); + }); } - return backendSrv.getDashboard($routeParams.type, $routeParams.slug).catch(function() { - return self._dashboardLoadFailed("Not found"); + promise.then(function(result) { + impressions.addImpression(slug); + return result; }); + + return promise; }; this._loadScriptedDashboard = function(file) { diff --git a/public/app/features/dashboard/impressions.js b/public/app/features/dashboard/impressions.js new file mode 100644 index 00000000000..2a83f4543a5 --- /dev/null +++ b/public/app/features/dashboard/impressions.js @@ -0,0 +1,7 @@ +define([ + './impressions2' +], function(impressions) { + 'use strict'; + // backward compatability hack; + return impressions.impressions; +}); diff --git a/public/app/features/dashboard/impressions2.ts b/public/app/features/dashboard/impressions2.ts new file mode 100644 index 00000000000..3a0ec574344 --- /dev/null +++ b/public/app/features/dashboard/impressions2.ts @@ -0,0 +1,41 @@ +/// + +import store from 'app/core/store'; +import _ from 'lodash'; + +export class Impressions { + constructor() {} + + addImpression(slug) { + var impressions = []; + if (store.exists("dashboard_impressions")) { + impressions = JSON.parse(store.get("dashboard_impressions")); + if (!_.isArray(impressions)) { + impressions = []; + } + } + + var exists = impressions.indexOf(slug); + if (exists >= 0) { + impressions.splice(exists, 1); + } + + impressions.unshift(slug); + + if (impressions.length > 20) { + impressions.shift(); + } + store.set("dashboard_impressions", JSON.stringify(impressions)); + } + + getImpressions() { + var k = store.get("dashboard_impressions"); + return JSON.parse(k); + } +} + +var impressions = new Impressions(); + +export { + impressions +}; diff --git a/public/app/plugins/panel/dashlist/module.html b/public/app/plugins/panel/dashlist/module.html index 455291409d5..79952d0032c 100644 --- a/public/app/plugins/panel/dashlist/module.html +++ b/public/app/plugins/panel/dashlist/module.html @@ -5,7 +5,7 @@ {{dash.title}} - + diff --git a/public/app/plugins/panel/dashlist/module.ts b/public/app/plugins/panel/dashlist/module.ts index a1c72434b38..46da2e4f519 100644 --- a/public/app/plugins/panel/dashlist/module.ts +++ b/public/app/plugins/panel/dashlist/module.ts @@ -3,6 +3,7 @@ import _ from 'lodash'; import config from 'app/core/config'; import {PanelCtrl} from 'app/plugins/sdk'; +import {impressions} from 'app/features/dashboard/impressions2'; // Set and populate defaults var panelDefaults = { @@ -31,7 +32,7 @@ class DashListCtrl extends PanelCtrl { initEditMode() { super.initEditMode(); - this.modes = ['starred', 'search']; + this.modes = ['starred', 'search', 'last viewed']; this.icon = "fa fa-star"; this.addEditorTab('Options', () => { return {templateUrl: 'public/app/plugins/panel/dashlist/editor.html'}; @@ -41,6 +42,19 @@ class DashListCtrl extends PanelCtrl { refresh() { var params: any = {limit: this.panel.limit}; + if (this.panel.mode === 'last viewed') { + var dashListNames = _.first(impressions.getImpressions(), this.panel.limit).map((dashboard) => { + return { + title: dashboard, + uri: 'db/' + dashboard + }; + }); + + this.dashList = dashListNames; + this.renderingCompleted(); + return; + } + if (this.panel.mode === 'starred') { params.starred = "true"; } else { diff --git a/public/dashboards/home.json b/public/dashboards/home.json index e05a882ef57..d54e276db5c 100644 --- a/public/dashboards/home.json +++ b/public/dashboards/home.json @@ -47,11 +47,11 @@ { "id": 3, "limit": 10, - "mode": "search", + "mode": "last viewed", "query": "", "span": 6, "tags": [], - "title": "Dashboards", + "title": "Last 10 viewed dashboards", "type": "dashlist" } ], From b79217be1e48d21b25b5cd1e04f8c73e63951b3c Mon Sep 17 00:00:00 2001 From: bergquist Date: Mon, 29 Feb 2016 14:37:09 +0100 Subject: [PATCH 2/2] feat(impressionStore): remove un needed js-ts bridge --- public/app/features/dashboard/all.js | 2 +- public/app/features/dashboard/dashboardLoaderSrv.js | 6 +++--- .../dashboard/{impressions2.ts => impressionStore.ts} | 8 ++++---- public/app/features/dashboard/impressions.js | 7 ------- public/app/plugins/panel/dashlist/module.ts | 4 ++-- 5 files changed, 10 insertions(+), 17 deletions(-) rename public/app/features/dashboard/{impressions2.ts => impressionStore.ts} (84%) delete mode 100644 public/app/features/dashboard/impressions.js diff --git a/public/app/features/dashboard/all.js b/public/app/features/dashboard/all.js index 15b96f2a1b6..d5cc13b4270 100644 --- a/public/app/features/dashboard/all.js +++ b/public/app/features/dashboard/all.js @@ -16,5 +16,5 @@ define([ './graphiteImportCtrl', './dynamicDashboardSrv', './importCtrl', - './impressions', + './impressionStore', ], function () {}); diff --git a/public/app/features/dashboard/dashboardLoaderSrv.js b/public/app/features/dashboard/dashboardLoaderSrv.js index 5b775166381..f975c47800f 100644 --- a/public/app/features/dashboard/dashboardLoaderSrv.js +++ b/public/app/features/dashboard/dashboardLoaderSrv.js @@ -5,9 +5,9 @@ define([ 'jquery', 'app/core/utils/kbn', 'app/core/utils/datemath', - './impressions', + './impressionStore', ], -function (angular, moment, _, $, kbn, dateMath, impressions) { +function (angular, moment, _, $, kbn, dateMath, impressionStore) { 'use strict'; var module = angular.module('grafana.services'); @@ -41,7 +41,7 @@ function (angular, moment, _, $, kbn, dateMath, impressions) { } promise.then(function(result) { - impressions.addImpression(slug); + impressionStore.impressions.addDashboardImpression(slug); return result; }); diff --git a/public/app/features/dashboard/impressions2.ts b/public/app/features/dashboard/impressionStore.ts similarity index 84% rename from public/app/features/dashboard/impressions2.ts rename to public/app/features/dashboard/impressionStore.ts index 3a0ec574344..61be1131cb6 100644 --- a/public/app/features/dashboard/impressions2.ts +++ b/public/app/features/dashboard/impressionStore.ts @@ -3,10 +3,10 @@ import store from 'app/core/store'; import _ from 'lodash'; -export class Impressions { +export class ImpressionsStore { constructor() {} - addImpression(slug) { + addDashboardImpression(slug) { var impressions = []; if (store.exists("dashboard_impressions")) { impressions = JSON.parse(store.get("dashboard_impressions")); @@ -28,13 +28,13 @@ export class Impressions { store.set("dashboard_impressions", JSON.stringify(impressions)); } - getImpressions() { + getDashboardOpened() { var k = store.get("dashboard_impressions"); return JSON.parse(k); } } -var impressions = new Impressions(); +var impressions = new ImpressionsStore(); export { impressions diff --git a/public/app/features/dashboard/impressions.js b/public/app/features/dashboard/impressions.js deleted file mode 100644 index 2a83f4543a5..00000000000 --- a/public/app/features/dashboard/impressions.js +++ /dev/null @@ -1,7 +0,0 @@ -define([ - './impressions2' -], function(impressions) { - 'use strict'; - // backward compatability hack; - return impressions.impressions; -}); diff --git a/public/app/plugins/panel/dashlist/module.ts b/public/app/plugins/panel/dashlist/module.ts index 46da2e4f519..fd0e905d923 100644 --- a/public/app/plugins/panel/dashlist/module.ts +++ b/public/app/plugins/panel/dashlist/module.ts @@ -3,7 +3,7 @@ import _ from 'lodash'; import config from 'app/core/config'; import {PanelCtrl} from 'app/plugins/sdk'; -import {impressions} from 'app/features/dashboard/impressions2'; +import {impressions} from 'app/features/dashboard/impressionStore'; // Set and populate defaults var panelDefaults = { @@ -43,7 +43,7 @@ class DashListCtrl extends PanelCtrl { var params: any = {limit: this.panel.limit}; if (this.panel.mode === 'last viewed') { - var dashListNames = _.first(impressions.getImpressions(), this.panel.limit).map((dashboard) => { + var dashListNames = _.first(impressions.getDashboardOpened(), this.panel.limit).map((dashboard) => { return { title: dashboard, uri: 'db/' + dashboard