SnapshotListCtrl.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import _ from 'lodash';
  2. import { NavModelSrv } from 'app/core/core';
  3. import { ILocationService } from 'angular';
  4. import { BackendSrv } from '@grafana/runtime';
  5. export class SnapshotListCtrl {
  6. navModel: any;
  7. snapshots: any;
  8. /** @ngInject */
  9. constructor(
  10. private $rootScope: any,
  11. private backendSrv: BackendSrv,
  12. navModelSrv: NavModelSrv,
  13. private $location: ILocationService
  14. ) {
  15. this.navModel = navModelSrv.getNav('dashboards', 'snapshots', 0);
  16. this.backendSrv.get('/api/dashboard/snapshots').then((result: any) => {
  17. const baseUrl = this.$location.absUrl().replace($location.url(), '');
  18. this.snapshots = result.map((snapshot: any) => ({
  19. ...snapshot,
  20. url: snapshot.externalUrl || `${baseUrl}/dashboard/snapshot/${snapshot.key}`,
  21. }));
  22. });
  23. }
  24. removeSnapshotConfirmed(snapshot: any) {
  25. _.remove(this.snapshots, { key: snapshot.key });
  26. this.backendSrv.delete('/api/snapshots/' + snapshot.key).then(
  27. () => {},
  28. () => {
  29. this.snapshots.push(snapshot);
  30. }
  31. );
  32. }
  33. removeSnapshot(snapshot: any) {
  34. this.$rootScope.appEvent('confirm-modal', {
  35. title: 'Delete',
  36. text: 'Are you sure you want to delete snapshot ' + snapshot.name + '?',
  37. yesText: 'Delete',
  38. icon: 'fa-trash',
  39. onConfirm: () => {
  40. this.removeSnapshotConfirmed(snapshot);
  41. },
  42. });
  43. }
  44. }