From 8f067a5ed221157d1384975df8c57d888c57008d Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Tue, 19 Jan 2016 01:37:36 -0800 Subject: [PATCH 1/9] Added backend functionality for searching snapshots --- pkg/api/api.go | 11 +++++++++-- pkg/api/dashboard_snapshot.go | 22 +++++++++++++++++++++ pkg/api/index.go | 6 ++++++ pkg/models/dashboard_snapshot.go | 10 ++++++++++ pkg/services/sqlstore/dashboard_snapshot.go | 17 ++++++++++++++++ 5 files changed, 64 insertions(+), 2 deletions(-) diff --git a/pkg/api/api.go b/pkg/api/api.go index ea434ed71da..4f4a1ddd72f 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -68,9 +68,11 @@ func Register(r *macaron.Macaron) { r.Post("/api/user/password/reset", bind(dtos.ResetUserPasswordForm{}), wrap(ResetPassword)) // dashboard snapshots - r.Post("/api/snapshots/", bind(m.CreateDashboardSnapshotCommand{}), CreateDashboardSnapshot) - r.Get("/dashboard/snapshot/*", Index) + r.Get("/dashboard/snapshot/*", Index) + r.Get("/dashboard/snapshots/", reqSignedIn, Index) + // api for dashboard snapshots + r.Post("/api/snapshots/", bind(m.CreateDashboardSnapshotCommand{}), CreateDashboardSnapshot) r.Get("/api/snapshot/shared-options/", GetSharingOptions) r.Get("/api/snapshots/:key", GetDashboardSnapshot) r.Get("/api/snapshots-delete/:key", DeleteDashboardSnapshot) @@ -182,6 +184,11 @@ func Register(r *macaron.Macaron) { r.Get("/tags", GetDashboardTags) }) + // dashboard snapshots + r.Group("/dashboard/snapshots", func() { + r.Get("/", wrap(SearchDashboardSnapshots)) + }) + // Playlist r.Group("/playlists", func() { r.Get("/", wrap(SearchPlaylists)) diff --git a/pkg/api/dashboard_snapshot.go b/pkg/api/dashboard_snapshot.go index 521dee29a63..dddb5ed436a 100644 --- a/pkg/api/dashboard_snapshot.go +++ b/pkg/api/dashboard_snapshot.go @@ -98,3 +98,25 @@ func DeleteDashboardSnapshot(c *middleware.Context) { c.JSON(200, util.DynMap{"message": "Snapshot deleted. It might take an hour before it's cleared from a CDN cache."}) } + +func SearchDashboardSnapshots(c *middleware.Context) Response { + query := c.Query("query") + limit := c.QueryInt("limit") + + if limit == 0 { + limit = 1000 + } + + searchQuery := m.GetDashboardSnapshotsQuery{ + Name: query, + Limit: limit, + OrgId: c.OrgId, + } + + err := bus.Dispatch(&searchQuery) + if err != nil { + return ApiError(500, "Search failed", err) + } + + return Json(200, searchQuery.Result) +} diff --git a/pkg/api/index.go b/pkg/api/index.go index d3bdff12b09..ca2f9320215 100644 --- a/pkg/api/index.go +++ b/pkg/api/index.go @@ -60,6 +60,12 @@ func setIndexViewData(c *middleware.Context) (*dtos.IndexViewData, error) { Url: "/playlists", }) + data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{ + Text: "Snapshots", + Icon: "fa fa-fw fa-camera-retro", + Url: "/dashboard/snapshots", + }) + if c.OrgRole == m.ROLE_ADMIN { data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{ Text: "Data Sources", diff --git a/pkg/models/dashboard_snapshot.go b/pkg/models/dashboard_snapshot.go index e8f37e2a236..c9221f42815 100644 --- a/pkg/models/dashboard_snapshot.go +++ b/pkg/models/dashboard_snapshot.go @@ -47,3 +47,13 @@ type GetDashboardSnapshotQuery struct { Result *DashboardSnapshot } + +type DashboardSnapshots []*DashboardSnapshot + +type GetDashboardSnapshotsQuery struct { + Name string + Limit int + OrgId int64 + + Result DashboardSnapshots +} diff --git a/pkg/services/sqlstore/dashboard_snapshot.go b/pkg/services/sqlstore/dashboard_snapshot.go index f4611050a77..6b71a0e26b3 100644 --- a/pkg/services/sqlstore/dashboard_snapshot.go +++ b/pkg/services/sqlstore/dashboard_snapshot.go @@ -12,6 +12,7 @@ func init() { bus.AddHandler("sql", CreateDashboardSnapshot) bus.AddHandler("sql", GetDashboardSnapshot) bus.AddHandler("sql", DeleteDashboardSnapshot) + bus.AddHandler("sql", SearchDashboardSnapshots) } func CreateDashboardSnapshot(cmd *m.CreateDashboardSnapshotCommand) error { @@ -63,3 +64,19 @@ func GetDashboardSnapshot(query *m.GetDashboardSnapshotQuery) error { query.Result = &snapshot return nil } + +func SearchDashboardSnapshots(query *m.GetDashboardSnapshotsQuery) error { + var snapshots = make(m.DashboardSnapshots, 0) + + sess := x.Limit(query.Limit) + + if query.Name != "" { + sess.Where("name LIKE ?", query.Name) + } + + sess.Where("org_id = ?", query.OrgId) + err := sess.Find(&snapshots) + query.Result = snapshots + + return err +} From 1ab11540104c0036ef54527722f9da70a35a66c7 Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Tue, 19 Jan 2016 04:09:57 -0800 Subject: [PATCH 2/9] Optimized backend queries --- pkg/api/api.go | 3 +-- pkg/api/index.go | 2 +- pkg/services/sqlstore/dashboard_snapshot.go | 3 +-- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/pkg/api/api.go b/pkg/api/api.go index 4f4a1ddd72f..c42bb376df2 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -69,7 +69,6 @@ func Register(r *macaron.Macaron) { // dashboard snapshots r.Get("/dashboard/snapshot/*", Index) - r.Get("/dashboard/snapshots/", reqSignedIn, Index) // api for dashboard snapshots r.Post("/api/snapshots/", bind(m.CreateDashboardSnapshotCommand{}), CreateDashboardSnapshot) @@ -184,7 +183,7 @@ func Register(r *macaron.Macaron) { r.Get("/tags", GetDashboardTags) }) - // dashboard snapshots + // Dashboard snapshots r.Group("/dashboard/snapshots", func() { r.Get("/", wrap(SearchDashboardSnapshots)) }) diff --git a/pkg/api/index.go b/pkg/api/index.go index ca2f9320215..3423651922a 100644 --- a/pkg/api/index.go +++ b/pkg/api/index.go @@ -63,7 +63,7 @@ func setIndexViewData(c *middleware.Context) (*dtos.IndexViewData, error) { data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{ Text: "Snapshots", Icon: "fa fa-fw fa-camera-retro", - Url: "/dashboard/snapshots", + Url: "/snapshots", }) if c.OrgRole == m.ROLE_ADMIN { diff --git a/pkg/services/sqlstore/dashboard_snapshot.go b/pkg/services/sqlstore/dashboard_snapshot.go index 6b71a0e26b3..eef2898a7c0 100644 --- a/pkg/services/sqlstore/dashboard_snapshot.go +++ b/pkg/services/sqlstore/dashboard_snapshot.go @@ -68,7 +68,7 @@ func GetDashboardSnapshot(query *m.GetDashboardSnapshotQuery) error { func SearchDashboardSnapshots(query *m.GetDashboardSnapshotsQuery) error { var snapshots = make(m.DashboardSnapshots, 0) - sess := x.Limit(query.Limit) + sess := x.Cols("name,key,delete_key").Limit(query.Limit) if query.Name != "" { sess.Where("name LIKE ?", query.Name) @@ -77,6 +77,5 @@ func SearchDashboardSnapshots(query *m.GetDashboardSnapshotsQuery) error { sess.Where("org_id = ?", query.OrgId) err := sess.Find(&snapshots) query.Result = snapshots - return err } From bcb44b7b31f747fc936dee819c467000954d94d6 Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Tue, 19 Jan 2016 05:02:22 -0800 Subject: [PATCH 3/9] UI and backend connectivity implemented --- pkg/api/api.go | 1 + pkg/api/index.go | 2 +- pkg/services/sqlstore/dashboard_snapshot.go | 2 +- public/app/features/all.js | 1 + public/app/features/snapshot/all.js | 4 ++ .../features/snapshot/partials/snapshots.html | 39 +++++++++++++++++ public/app/features/snapshot/snapshot_ctrl.js | 43 +++++++++++++++++++ .../app/features/snapshot/snapshot_routes.js | 18 ++++++++ 8 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 public/app/features/snapshot/all.js create mode 100644 public/app/features/snapshot/partials/snapshots.html create mode 100644 public/app/features/snapshot/snapshot_ctrl.js create mode 100644 public/app/features/snapshot/snapshot_routes.js diff --git a/pkg/api/api.go b/pkg/api/api.go index c42bb376df2..034d9adc180 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -69,6 +69,7 @@ func Register(r *macaron.Macaron) { // dashboard snapshots r.Get("/dashboard/snapshot/*", Index) + r.Get("/dashboard/snapshots/", reqSignedIn, Index) // api for dashboard snapshots r.Post("/api/snapshots/", bind(m.CreateDashboardSnapshotCommand{}), CreateDashboardSnapshot) diff --git a/pkg/api/index.go b/pkg/api/index.go index 3423651922a..ca2f9320215 100644 --- a/pkg/api/index.go +++ b/pkg/api/index.go @@ -63,7 +63,7 @@ func setIndexViewData(c *middleware.Context) (*dtos.IndexViewData, error) { data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{ Text: "Snapshots", Icon: "fa fa-fw fa-camera-retro", - Url: "/snapshots", + Url: "/dashboard/snapshots", }) if c.OrgRole == m.ROLE_ADMIN { diff --git a/pkg/services/sqlstore/dashboard_snapshot.go b/pkg/services/sqlstore/dashboard_snapshot.go index eef2898a7c0..64e7e31898c 100644 --- a/pkg/services/sqlstore/dashboard_snapshot.go +++ b/pkg/services/sqlstore/dashboard_snapshot.go @@ -68,7 +68,7 @@ func GetDashboardSnapshot(query *m.GetDashboardSnapshotQuery) error { func SearchDashboardSnapshots(query *m.GetDashboardSnapshotsQuery) error { var snapshots = make(m.DashboardSnapshots, 0) - sess := x.Cols("name,key,delete_key").Limit(query.Limit) + sess := x.Limit(query.Limit) if query.Name != "" { sess.Where("name LIKE ?", query.Name) diff --git a/public/app/features/all.js b/public/app/features/all.js index d4436c9deaa..4ec4719ebdd 100644 --- a/public/app/features/all.js +++ b/public/app/features/all.js @@ -5,6 +5,7 @@ define([ './templating/templateSrv', './dashboard/all', './playlist/all', + './snapshot/all', './panel/all', './profile/profileCtrl', './profile/changePasswordCtrl', diff --git a/public/app/features/snapshot/all.js b/public/app/features/snapshot/all.js new file mode 100644 index 00000000000..45cb9eb594f --- /dev/null +++ b/public/app/features/snapshot/all.js @@ -0,0 +1,4 @@ +define([ + './snapshot_ctrl', + './snapshot_routes' +], function () {}); diff --git a/public/app/features/snapshot/partials/snapshots.html b/public/app/features/snapshot/partials/snapshots.html new file mode 100644 index 00000000000..58b6c872617 --- /dev/null +++ b/public/app/features/snapshot/partials/snapshots.html @@ -0,0 +1,39 @@ + + +
+
+ +

Available snapshots

+ + + + + + + + + + + + + + + + +
NameSnapshot url
+ {{snapshot.Name}} + + dashboard/snapshot/{{snapshot.Key}} + + + + View + + + + + +
+ +
+
diff --git a/public/app/features/snapshot/snapshot_ctrl.js b/public/app/features/snapshot/snapshot_ctrl.js new file mode 100644 index 00000000000..345638f4770 --- /dev/null +++ b/public/app/features/snapshot/snapshot_ctrl.js @@ -0,0 +1,43 @@ +define([ + 'angular', + 'lodash' +], +function (angular, _) { + 'use strict'; + + var module = angular.module('grafana.controllers'); + + module.controller('SnapshotsCtrl', function($scope, $location, backendSrv) { + backendSrv.get('/api/dashboard/snapshots') + .then(function(result) { + $scope.snapshots = result; + }); + + $scope.removeSnapshotConfirmed = function(snapshot) { + _.remove($scope.snapshots, {Key: snapshot.Key}); + + backendSrv.get('/api/snapshots-delete/' + snapshot.DeleteKey) + .then(function() { + $scope.appEvent('alert-success', ['Snapshot deleted', '']); + }, function() { + $scope.appEvent('alert-error', ['Unable to delete snapshot', '']); + $scope.snapshots.push(snapshot); + }); + }; + + $scope.removeSnapshot = function(snapshot) { + + $scope.appEvent('confirm-modal', { + title: 'Confirm delete snapshot', + text: 'Are you sure you want to delete snapshot ' + snapshot.Name + '?', + yesText: "Delete", + icon: "fa-warning", + onConfirm: function() { + $scope.removeSnapshotConfirmed(snapshot); + } + }); + + }; + + }); +}); diff --git a/public/app/features/snapshot/snapshot_routes.js b/public/app/features/snapshot/snapshot_routes.js new file mode 100644 index 00000000000..74de0634282 --- /dev/null +++ b/public/app/features/snapshot/snapshot_routes.js @@ -0,0 +1,18 @@ +define([ + 'angular', + 'app/core/config', + 'lodash' +], +function (angular) { + 'use strict'; + + var module = angular.module('grafana.routes'); + + module.config(function($routeProvider) { + $routeProvider + .when('/dashboard/snapshots', { + templateUrl: 'app/features/snapshot/partials/snapshots.html', + controller : 'SnapshotsCtrl' + }); + }); +}); From ca55d1f31526408ba9255578aa9b12f84984ae23 Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Tue, 19 Jan 2016 05:05:24 -0800 Subject: [PATCH 4/9] Minor bug fixes --- pkg/api/api.go | 16 +++++------ pkg/api/dashboard_snapshot.go | 30 ++++++++++----------- pkg/api/index.go | 10 +++---- pkg/models/dashboard_snapshot.go | 8 +++--- pkg/services/sqlstore/dashboard_snapshot.go | 20 +++++++------- 5 files changed, 42 insertions(+), 42 deletions(-) diff --git a/pkg/api/api.go b/pkg/api/api.go index 034d9adc180..9477288dca6 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -68,11 +68,11 @@ func Register(r *macaron.Macaron) { r.Post("/api/user/password/reset", bind(dtos.ResetUserPasswordForm{}), wrap(ResetPassword)) // dashboard snapshots - r.Get("/dashboard/snapshot/*", Index) - r.Get("/dashboard/snapshots/", reqSignedIn, Index) + r.Get("/dashboard/snapshot/*", Index) + r.Get("/dashboard/snapshots/", reqSignedIn, Index) - // api for dashboard snapshots - r.Post("/api/snapshots/", bind(m.CreateDashboardSnapshotCommand{}), CreateDashboardSnapshot) + // api for dashboard snapshots + r.Post("/api/snapshots/", bind(m.CreateDashboardSnapshotCommand{}), CreateDashboardSnapshot) r.Get("/api/snapshot/shared-options/", GetSharingOptions) r.Get("/api/snapshots/:key", GetDashboardSnapshot) r.Get("/api/snapshots-delete/:key", DeleteDashboardSnapshot) @@ -184,10 +184,10 @@ func Register(r *macaron.Macaron) { r.Get("/tags", GetDashboardTags) }) - // Dashboard snapshots - r.Group("/dashboard/snapshots", func() { - r.Get("/", wrap(SearchDashboardSnapshots)) - }) + // Dashboard snapshots + r.Group("/dashboard/snapshots", func() { + r.Get("/", wrap(SearchDashboardSnapshots)) + }) // Playlist r.Group("/playlists", func() { diff --git a/pkg/api/dashboard_snapshot.go b/pkg/api/dashboard_snapshot.go index 8eb9da60231..2d1fbce782c 100644 --- a/pkg/api/dashboard_snapshot.go +++ b/pkg/api/dashboard_snapshot.go @@ -101,23 +101,23 @@ func DeleteDashboardSnapshot(c *middleware.Context) { } func SearchDashboardSnapshots(c *middleware.Context) Response { - query := c.Query("query") - limit := c.QueryInt("limit") + query := c.Query("query") + limit := c.QueryInt("limit") - if limit == 0 { - limit = 1000 - } + if limit == 0 { + limit = 1000 + } - searchQuery := m.GetDashboardSnapshotsQuery{ - Name: query, - Limit: limit, - OrgId: c.OrgId, - } + searchQuery := m.GetDashboardSnapshotsQuery{ + Name: query, + Limit: limit, + OrgId: c.OrgId, + } - err := bus.Dispatch(&searchQuery) - if err != nil { - return ApiError(500, "Search failed", err) - } + err := bus.Dispatch(&searchQuery) + if err != nil { + return ApiError(500, "Search failed", err) + } - return Json(200, searchQuery.Result) + return Json(200, searchQuery.Result) } diff --git a/pkg/api/index.go b/pkg/api/index.go index ca2f9320215..09ef756f95a 100644 --- a/pkg/api/index.go +++ b/pkg/api/index.go @@ -60,11 +60,11 @@ func setIndexViewData(c *middleware.Context) (*dtos.IndexViewData, error) { Url: "/playlists", }) - data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{ - Text: "Snapshots", - Icon: "fa fa-fw fa-camera-retro", - Url: "/dashboard/snapshots", - }) + data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{ + Text: "Snapshots", + Icon: "fa fa-fw fa-camera-retro", + Url: "/dashboard/snapshots", + }) if c.OrgRole == m.ROLE_ADMIN { data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{ diff --git a/pkg/models/dashboard_snapshot.go b/pkg/models/dashboard_snapshot.go index 65163e6b119..8c1a0f47a06 100644 --- a/pkg/models/dashboard_snapshot.go +++ b/pkg/models/dashboard_snapshot.go @@ -52,9 +52,9 @@ type GetDashboardSnapshotQuery struct { type DashboardSnapshots []*DashboardSnapshot type GetDashboardSnapshotsQuery struct { - Name string - Limit int - OrgId int64 + Name string + Limit int + OrgId int64 - Result DashboardSnapshots + Result DashboardSnapshots } diff --git a/pkg/services/sqlstore/dashboard_snapshot.go b/pkg/services/sqlstore/dashboard_snapshot.go index b7ffa0b9912..fc94a91cce5 100644 --- a/pkg/services/sqlstore/dashboard_snapshot.go +++ b/pkg/services/sqlstore/dashboard_snapshot.go @@ -12,7 +12,7 @@ func init() { bus.AddHandler("sql", CreateDashboardSnapshot) bus.AddHandler("sql", GetDashboardSnapshot) bus.AddHandler("sql", DeleteDashboardSnapshot) - bus.AddHandler("sql", SearchDashboardSnapshots) + bus.AddHandler("sql", SearchDashboardSnapshots) } func CreateDashboardSnapshot(cmd *m.CreateDashboardSnapshotCommand) error { @@ -67,16 +67,16 @@ func GetDashboardSnapshot(query *m.GetDashboardSnapshotQuery) error { } func SearchDashboardSnapshots(query *m.GetDashboardSnapshotsQuery) error { - var snapshots = make(m.DashboardSnapshots, 0) + var snapshots = make(m.DashboardSnapshots, 0) - sess := x.Limit(query.Limit) + sess := x.Limit(query.Limit) - if query.Name != "" { - sess.Where("name LIKE ?", query.Name) - } + if query.Name != "" { + sess.Where("name LIKE ?", query.Name) + } - sess.Where("org_id = ?", query.OrgId) - err := sess.Find(&snapshots) - query.Result = snapshots - return err + sess.Where("org_id = ?", query.OrgId) + err := sess.Find(&snapshots) + query.Result = snapshots + return err } From 6e99eed417acb9279feefdb4d2ea6cedd39116bf Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Tue, 19 Jan 2016 17:00:54 -0800 Subject: [PATCH 5/9] Moved snapshot route to core routes --- public/app/core/routes/all.js | 4 ++++ public/app/features/snapshot/all.js | 1 - .../app/features/snapshot/snapshot_routes.js | 18 ------------------ 3 files changed, 4 insertions(+), 19 deletions(-) delete mode 100644 public/app/features/snapshot/snapshot_routes.js diff --git a/public/app/core/routes/all.js b/public/app/core/routes/all.js index cc4d73ef708..ba993134455 100644 --- a/public/app/core/routes/all.js +++ b/public/app/core/routes/all.js @@ -132,6 +132,10 @@ define([ templateUrl: 'app/partials/reset_password.html', controller : 'ResetPasswordCtrl', }) + .when('/dashboard/snapshots', { + templateUrl: 'app/features/snapshot/partials/snapshots.html', + controller : 'SnapshotsCtrl' + }) .when('/apps', { templateUrl: 'app/features/apps/partials/list.html', controller: 'AppListCtrl', diff --git a/public/app/features/snapshot/all.js b/public/app/features/snapshot/all.js index 45cb9eb594f..b0f66edc414 100644 --- a/public/app/features/snapshot/all.js +++ b/public/app/features/snapshot/all.js @@ -1,4 +1,3 @@ define([ './snapshot_ctrl', - './snapshot_routes' ], function () {}); diff --git a/public/app/features/snapshot/snapshot_routes.js b/public/app/features/snapshot/snapshot_routes.js deleted file mode 100644 index 74de0634282..00000000000 --- a/public/app/features/snapshot/snapshot_routes.js +++ /dev/null @@ -1,18 +0,0 @@ -define([ - 'angular', - 'app/core/config', - 'lodash' -], -function (angular) { - 'use strict'; - - var module = angular.module('grafana.routes'); - - module.config(function($routeProvider) { - $routeProvider - .when('/dashboard/snapshots', { - templateUrl: 'app/features/snapshot/partials/snapshots.html', - controller : 'SnapshotsCtrl' - }); - }); -}); From e26cd21048b631235c98e141184b9986d0fc2458 Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Tue, 19 Jan 2016 21:04:53 -0800 Subject: [PATCH 6/9] Converted ctrl to typescript --- public/app/core/routes/all.js | 3 +- public/app/features/snapshot/all.js | 3 -- public/app/features/snapshot/all.ts | 1 + .../features/snapshot/partials/snapshots.html | 6 +-- public/app/features/snapshot/snapshot_ctrl.js | 43 ------------------- public/app/features/snapshot/snapshot_ctrl.ts | 20 +++++++++ 6 files changed, 26 insertions(+), 50 deletions(-) delete mode 100644 public/app/features/snapshot/all.js create mode 100644 public/app/features/snapshot/all.ts delete mode 100644 public/app/features/snapshot/snapshot_ctrl.js create mode 100644 public/app/features/snapshot/snapshot_ctrl.ts diff --git a/public/app/core/routes/all.js b/public/app/core/routes/all.js index ba993134455..521e8008f98 100644 --- a/public/app/core/routes/all.js +++ b/public/app/core/routes/all.js @@ -134,7 +134,8 @@ define([ }) .when('/dashboard/snapshots', { templateUrl: 'app/features/snapshot/partials/snapshots.html', - controller : 'SnapshotsCtrl' + controller : 'SnapshotsCtrl', + controllerAs: 'ctrl', }) .when('/apps', { templateUrl: 'app/features/apps/partials/list.html', diff --git a/public/app/features/snapshot/all.js b/public/app/features/snapshot/all.js deleted file mode 100644 index b0f66edc414..00000000000 --- a/public/app/features/snapshot/all.js +++ /dev/null @@ -1,3 +0,0 @@ -define([ - './snapshot_ctrl', -], function () {}); diff --git a/public/app/features/snapshot/all.ts b/public/app/features/snapshot/all.ts new file mode 100644 index 00000000000..521c7a4c111 --- /dev/null +++ b/public/app/features/snapshot/all.ts @@ -0,0 +1 @@ +import './snapshot_ctrl'; diff --git a/public/app/features/snapshot/partials/snapshots.html b/public/app/features/snapshot/partials/snapshots.html index 58b6c872617..9d1b688fd35 100644 --- a/public/app/features/snapshot/partials/snapshots.html +++ b/public/app/features/snapshot/partials/snapshots.html @@ -1,7 +1,7 @@
-
+

Available snapshots

@@ -14,7 +14,7 @@ - + {{snapshot.Name}} @@ -28,7 +28,7 @@ - + diff --git a/public/app/features/snapshot/snapshot_ctrl.js b/public/app/features/snapshot/snapshot_ctrl.js deleted file mode 100644 index 345638f4770..00000000000 --- a/public/app/features/snapshot/snapshot_ctrl.js +++ /dev/null @@ -1,43 +0,0 @@ -define([ - 'angular', - 'lodash' -], -function (angular, _) { - 'use strict'; - - var module = angular.module('grafana.controllers'); - - module.controller('SnapshotsCtrl', function($scope, $location, backendSrv) { - backendSrv.get('/api/dashboard/snapshots') - .then(function(result) { - $scope.snapshots = result; - }); - - $scope.removeSnapshotConfirmed = function(snapshot) { - _.remove($scope.snapshots, {Key: snapshot.Key}); - - backendSrv.get('/api/snapshots-delete/' + snapshot.DeleteKey) - .then(function() { - $scope.appEvent('alert-success', ['Snapshot deleted', '']); - }, function() { - $scope.appEvent('alert-error', ['Unable to delete snapshot', '']); - $scope.snapshots.push(snapshot); - }); - }; - - $scope.removeSnapshot = function(snapshot) { - - $scope.appEvent('confirm-modal', { - title: 'Confirm delete snapshot', - text: 'Are you sure you want to delete snapshot ' + snapshot.Name + '?', - yesText: "Delete", - icon: "fa-warning", - onConfirm: function() { - $scope.removeSnapshotConfirmed(snapshot); - } - }); - - }; - - }); -}); diff --git a/public/app/features/snapshot/snapshot_ctrl.ts b/public/app/features/snapshot/snapshot_ctrl.ts new file mode 100644 index 00000000000..b194fbf9119 --- /dev/null +++ b/public/app/features/snapshot/snapshot_ctrl.ts @@ -0,0 +1,20 @@ +/// + +import angular from 'angular'; +import _ from 'lodash'; + +export class SnapshotsCtrl { + snapshots: any[]; + + /** @ngInject */ + constructor(private backendSrv: any) {} + + init() { + this.backendSrv.get('/api/dashboard/snapshots').then(snapshots => { + this.snapshots = snapshots; + }); + console.log(this.snapshots); + } +} + +angular.module('grafana.controllers').controller('SnapshotsCtrl', SnapshotsCtrl); From 281ec60085ef1d659360a03083ea7d95cb4893b3 Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Tue, 19 Jan 2016 21:52:13 -0800 Subject: [PATCH 7/9] UI and backend working --- pkg/api/dashboard_snapshot.go | 1 - .../features/snapshot/partials/snapshots.html | 6 +-- public/app/features/snapshot/snapshot_ctrl.ts | 37 +++++++++++++++---- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/pkg/api/dashboard_snapshot.go b/pkg/api/dashboard_snapshot.go index 2d1fbce782c..99c8d4dd85f 100644 --- a/pkg/api/dashboard_snapshot.go +++ b/pkg/api/dashboard_snapshot.go @@ -36,7 +36,6 @@ func CreateDashboardSnapshot(c *middleware.Context, cmd m.CreateDashboardSnapsho cmd.DeleteKey = util.GetRandomString(32) cmd.OrgId = c.OrgId cmd.UserId = c.UserId - cmd.Name = c.Name metrics.M_Api_Dashboard_Snapshot_Create.Inc(1) } diff --git a/public/app/features/snapshot/partials/snapshots.html b/public/app/features/snapshot/partials/snapshots.html index 9d1b688fd35..58b6c872617 100644 --- a/public/app/features/snapshot/partials/snapshots.html +++ b/public/app/features/snapshot/partials/snapshots.html @@ -1,7 +1,7 @@
-
+

Available snapshots

@@ -14,7 +14,7 @@ - + {{snapshot.Name}} @@ -28,7 +28,7 @@ - + diff --git a/public/app/features/snapshot/snapshot_ctrl.ts b/public/app/features/snapshot/snapshot_ctrl.ts index b194fbf9119..60b09bf892e 100644 --- a/public/app/features/snapshot/snapshot_ctrl.ts +++ b/public/app/features/snapshot/snapshot_ctrl.ts @@ -4,16 +4,39 @@ import angular from 'angular'; import _ from 'lodash'; export class SnapshotsCtrl { - snapshots: any[]; /** @ngInject */ - constructor(private backendSrv: any) {} + constructor(backendSrv, $scope) { + $scope.init = function() { + backendSrv.get('/api/dashboard/snapshots').then(function(result) { + $scope.snapshots = result; + }); + }; - init() { - this.backendSrv.get('/api/dashboard/snapshots').then(snapshots => { - this.snapshots = snapshots; - }); - console.log(this.snapshots); + $scope.removeSnapshot = function(snapshot) { + $scope.appEvent('confirm-modal', { + title: 'Confirm delete snapshot', + text: 'Are you sure you want to delete snapshot ' + snapshot.Name + '?', + yesText: "Delete", + icon: "fa-warning", + onConfirm: function() { + $scope.removeSnapshotConfirmed(snapshot); + } + }); + }; + + $scope.removeSnapshotConfirmed = function(snapshot) { + _.remove($scope.snapshots, {Key: snapshot.Key}); + backendSrv.get('/api/snapshots-delete/' + snapshot.DeleteKey) + .then(function() { + $scope.appEvent('alert-success', ['Snapshot deleted', '']); + }, function() { + $scope.appEvent('alert-error', ['Unable to delete snapshot', '']); + $scope.snapshots.push(snapshot); + }); + }; + + $scope.init(); } } From aa1d28835dfd1ef114fa151e3c1b20930d46dfde Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Tue, 19 Jan 2016 23:23:44 -0800 Subject: [PATCH 8/9] Fixed angularjs variable names --- pkg/api/dashboard_snapshot.go | 20 ++++++++++++++++++- pkg/models/dashboard_snapshot.go | 16 +++++++++++++++ .../features/snapshot/partials/snapshots.html | 6 +++--- public/app/features/snapshot/snapshot_ctrl.ts | 6 +++--- 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/pkg/api/dashboard_snapshot.go b/pkg/api/dashboard_snapshot.go index 99c8d4dd85f..8ed848a7b11 100644 --- a/pkg/api/dashboard_snapshot.go +++ b/pkg/api/dashboard_snapshot.go @@ -118,5 +118,23 @@ func SearchDashboardSnapshots(c *middleware.Context) Response { return ApiError(500, "Search failed", err) } - return Json(200, searchQuery.Result) + dtos := make([]*m.DashboardSnapshotDTO, len(searchQuery.Result)) + for i, snapshot := range searchQuery.Result { + dtos[i] = &m.DashboardSnapshotDTO{ + Id: snapshot.Id, + Name: snapshot.Name, + Key: snapshot.Key, + DeleteKey: snapshot.DeleteKey, + OrgId: snapshot.OrgId, + UserId: snapshot.UserId, + External: snapshot.External, + ExternalUrl: snapshot.ExternalUrl, + Expires: snapshot.Expires, + Created: snapshot.Created, + Updated: snapshot.Updated, + } + } + + return Json(200, dtos) + //return Json(200, searchQuery.Result) } diff --git a/pkg/models/dashboard_snapshot.go b/pkg/models/dashboard_snapshot.go index 8c1a0f47a06..9bfbd06c1ef 100644 --- a/pkg/models/dashboard_snapshot.go +++ b/pkg/models/dashboard_snapshot.go @@ -20,6 +20,22 @@ type DashboardSnapshot struct { Dashboard map[string]interface{} } +// DashboardSnapshotDTO without dashboard map +type DashboardSnapshotDTO struct { + Id int64 `json:"id"` + Name string `json:"name"` + Key string `json:"key"` + DeleteKey string `json:"deleteKey"` + OrgId int64 `json:"orgId"` + UserId int64 `json:"userId"` + External bool `json:"external"` + ExternalUrl string `json:"externalUrl"` + + Expires time.Time `json:"expires"` + Created time.Time `json:"created"` + Updated time.Time `json:"updated"` +} + // ----------------- // COMMANDS diff --git a/public/app/features/snapshot/partials/snapshots.html b/public/app/features/snapshot/partials/snapshots.html index 58b6c872617..e1fcf16f0ef 100644 --- a/public/app/features/snapshot/partials/snapshots.html +++ b/public/app/features/snapshot/partials/snapshots.html @@ -16,13 +16,13 @@ - {{snapshot.Name}} + {{snapshot.name}} - dashboard/snapshot/{{snapshot.Key}} + dashboard/snapshot/{{snapshot.key}} - + View diff --git a/public/app/features/snapshot/snapshot_ctrl.ts b/public/app/features/snapshot/snapshot_ctrl.ts index 60b09bf892e..5b27016f0aa 100644 --- a/public/app/features/snapshot/snapshot_ctrl.ts +++ b/public/app/features/snapshot/snapshot_ctrl.ts @@ -16,7 +16,7 @@ export class SnapshotsCtrl { $scope.removeSnapshot = function(snapshot) { $scope.appEvent('confirm-modal', { title: 'Confirm delete snapshot', - text: 'Are you sure you want to delete snapshot ' + snapshot.Name + '?', + text: 'Are you sure you want to delete snapshot ' + snapshot.name + '?', yesText: "Delete", icon: "fa-warning", onConfirm: function() { @@ -26,8 +26,8 @@ export class SnapshotsCtrl { }; $scope.removeSnapshotConfirmed = function(snapshot) { - _.remove($scope.snapshots, {Key: snapshot.Key}); - backendSrv.get('/api/snapshots-delete/' + snapshot.DeleteKey) + _.remove($scope.snapshots, {key: snapshot.key}); + backendSrv.get('/api/snapshots-delete/' + snapshot.deleteKey) .then(function() { $scope.appEvent('alert-success', ['Snapshot deleted', '']); }, function() { From a854f0c4d4a1392fd71c36d58ac206e8c9240b5a Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Wed, 20 Jan 2016 02:35:24 -0800 Subject: [PATCH 9/9] Switched snapshot ctrl to angularjs2 --- .../features/snapshot/partials/snapshots.html | 4 +- public/app/features/snapshot/snapshot_ctrl.ts | 63 +++++++++---------- 2 files changed, 33 insertions(+), 34 deletions(-) diff --git a/public/app/features/snapshot/partials/snapshots.html b/public/app/features/snapshot/partials/snapshots.html index e1fcf16f0ef..4a1d44d38a4 100644 --- a/public/app/features/snapshot/partials/snapshots.html +++ b/public/app/features/snapshot/partials/snapshots.html @@ -14,7 +14,7 @@ - + {{snapshot.name}} @@ -28,7 +28,7 @@ - + diff --git a/public/app/features/snapshot/snapshot_ctrl.ts b/public/app/features/snapshot/snapshot_ctrl.ts index 5b27016f0aa..7b38f9ad8eb 100644 --- a/public/app/features/snapshot/snapshot_ctrl.ts +++ b/public/app/features/snapshot/snapshot_ctrl.ts @@ -5,39 +5,38 @@ import _ from 'lodash'; export class SnapshotsCtrl { + snapshots: any; + /** @ngInject */ - constructor(backendSrv, $scope) { - $scope.init = function() { - backendSrv.get('/api/dashboard/snapshots').then(function(result) { - $scope.snapshots = result; - }); - }; - - $scope.removeSnapshot = function(snapshot) { - $scope.appEvent('confirm-modal', { - title: 'Confirm delete snapshot', - text: 'Are you sure you want to delete snapshot ' + snapshot.name + '?', - yesText: "Delete", - icon: "fa-warning", - onConfirm: function() { - $scope.removeSnapshotConfirmed(snapshot); - } - }); - }; - - $scope.removeSnapshotConfirmed = function(snapshot) { - _.remove($scope.snapshots, {key: snapshot.key}); - backendSrv.get('/api/snapshots-delete/' + snapshot.deleteKey) - .then(function() { - $scope.appEvent('alert-success', ['Snapshot deleted', '']); - }, function() { - $scope.appEvent('alert-error', ['Unable to delete snapshot', '']); - $scope.snapshots.push(snapshot); - }); - }; - - $scope.init(); + constructor(private $rootScope, private backendSrv) { + this.backendSrv.get('/api/dashboard/snapshots').then(result => { + this.snapshots = result; + }); } + + removeSnapshotConfirmed(snapshot) { + _.remove(this.snapshots, {key: snapshot.key}); + this.backendSrv.get('/api/snapshots-delete/' + snapshot.deleteKey) + .then(() => { + this.$rootScope.appEvent('alert-success', ['Snapshot deleted', '']); + }, () => { + this.$rootScope.appEvent('alert-error', ['Unable to delete snapshot', '']); + this.snapshots.push(snapshot); + }); + } + + removeSnapshot(snapshot) { + this.$rootScope.appEvent('confirm-modal', { + title: 'Confirm delete snapshot', + text: 'Are you sure you want to delete snapshot ' + snapshot.name + '?', + yesText: "Delete", + icon: "fa-warning", + onConfirm: () => { + this.removeSnapshotConfirmed(snapshot); + } + }); + } + } - + angular.module('grafana.controllers').controller('SnapshotsCtrl', SnapshotsCtrl);