Bladeren bron

Some refinements to dashboard snapshots

Torkel Ödegaard 10 jaren geleden
bovenliggende
commit
9268ecf3e9

+ 35 - 2
pkg/api/dashboard_snapshot.go

@@ -1,17 +1,27 @@
 package api
 package api
 
 
 import (
 import (
+	"bytes"
+	"encoding/json"
+	"io/ioutil"
+	"net/http"
+	"time"
+
 	"github.com/grafana/grafana/pkg/api/dtos"
 	"github.com/grafana/grafana/pkg/api/dtos"
 	"github.com/grafana/grafana/pkg/bus"
 	"github.com/grafana/grafana/pkg/bus"
 	"github.com/grafana/grafana/pkg/metrics"
 	"github.com/grafana/grafana/pkg/metrics"
 	"github.com/grafana/grafana/pkg/middleware"
 	"github.com/grafana/grafana/pkg/middleware"
 	m "github.com/grafana/grafana/pkg/models"
 	m "github.com/grafana/grafana/pkg/models"
+	"github.com/grafana/grafana/pkg/setting"
 	"github.com/grafana/grafana/pkg/util"
 	"github.com/grafana/grafana/pkg/util"
 )
 )
 
 
 func CreateDashboardSnapshot(c *middleware.Context, cmd m.CreateDashboardSnapshotCommand) {
 func CreateDashboardSnapshot(c *middleware.Context, cmd m.CreateDashboardSnapshotCommand) {
-	cmd.Key = util.GetRandomString(32)
+	if cmd.External {
+		createExternalSnapshot(c, cmd)
+	}
 
 
+	cmd.Key = util.GetRandomString(32)
 	if err := bus.Dispatch(&cmd); err != nil {
 	if err := bus.Dispatch(&cmd); err != nil {
 		c.JsonApiErr(500, "Failed to create snaphost", err)
 		c.JsonApiErr(500, "Failed to create snaphost", err)
 		return
 		return
@@ -19,7 +29,30 @@ func CreateDashboardSnapshot(c *middleware.Context, cmd m.CreateDashboardSnapsho
 
 
 	metrics.M_Api_Dashboard_Snapshot_Create.Inc(1)
 	metrics.M_Api_Dashboard_Snapshot_Create.Inc(1)
 
 
-	c.JSON(200, util.DynMap{"key": cmd.Key})
+	c.JSON(200, util.DynMap{"key": cmd.Key, "url": setting.ToAbsUrl("/dashboard/snapshots")})
+}
+
+func createExternalSnapshot(c *middleware.Context, cmd m.CreateDashboardSnapshotCommand) {
+	metrics.M_Api_Dashboard_Snapshot_External.Inc(1)
+
+	json, _ := json.Marshal(cmd)
+	jsonData := bytes.NewBuffer(json)
+
+	client := http.Client{Timeout: time.Duration(5 * time.Second)}
+	resp, err := client.Post("http://snapshots-origin.raintank.io/api/snapshots", "application/json", jsonData)
+
+	if err != nil {
+		c.JsonApiErr(500, "Failed to publish external snapshot", err)
+		return
+	}
+
+	c.Header().Set("Content-Type", resp.Header.Get("Content-Type"))
+	c.WriteHeader(resp.StatusCode)
+
+	if resp.ContentLength > 0 {
+		bytes, _ := ioutil.ReadAll(resp.Body)
+		c.Write(bytes)
+	}
 }
 }
 
 
 func GetDashboardSnapshot(c *middleware.Context) {
 func GetDashboardSnapshot(c *middleware.Context) {

+ 3 - 2
pkg/metrics/metrics.go

@@ -21,8 +21,9 @@ var (
 	M_Api_Login_OAuth       = NewComboCounterRef("api.login.oauth")
 	M_Api_Login_OAuth       = NewComboCounterRef("api.login.oauth")
 	M_Api_Org_Create        = NewComboCounterRef("api.org.create")
 	M_Api_Org_Create        = NewComboCounterRef("api.org.create")
 
 
-	M_Api_Dashboard_Snapshot_Create = NewComboCounterRef("api.dashboard_snapshot.create")
-	M_Api_Dashboard_Snapshot_Get    = NewComboCounterRef("api.dashboard_snapshot.get")
+	M_Api_Dashboard_Snapshot_Create   = NewComboCounterRef("api.dashboard_snapshot.create")
+	M_Api_Dashboard_Snapshot_External = NewComboCounterRef("api.dashboard_snapshot.external")
+	M_Api_Dashboard_Snapshot_Get      = NewComboCounterRef("api.dashboard_snapshot.get")
 
 
 	M_Models_Dashboard_Insert = NewComboCounterRef("models.dashboard.insert")
 	M_Models_Dashboard_Insert = NewComboCounterRef("models.dashboard.insert")
 )
 )

+ 10 - 9
pkg/metrics/report_usage.go

@@ -7,7 +7,9 @@ import (
 	"strings"
 	"strings"
 	"time"
 	"time"
 
 
+	"github.com/grafana/grafana/pkg/bus"
 	"github.com/grafana/grafana/pkg/log"
 	"github.com/grafana/grafana/pkg/log"
+	m "github.com/grafana/grafana/pkg/models"
 	"github.com/grafana/grafana/pkg/setting"
 	"github.com/grafana/grafana/pkg/setting"
 )
 )
 
 
@@ -34,11 +36,11 @@ func sendUsageStats() {
 		"metrics": metrics,
 		"metrics": metrics,
 	}
 	}
 
 
-	// statsQuery := m.GetSystemStatsQuery{}
-	// if err := bus.Dispatch(&statsQuery); err != nil {
-	// 	log.Error(3, "Failed to get system stats", err)
-	// 	return
-	// }
+	statsQuery := m.GetSystemStatsQuery{}
+	if err := bus.Dispatch(&statsQuery); err != nil {
+		log.Error(3, "Failed to get system stats", err)
+		return
+	}
 
 
 	UsageStats.Each(func(name string, i interface{}) {
 	UsageStats.Each(func(name string, i interface{}) {
 		switch metric := i.(type) {
 		switch metric := i.(type) {
@@ -50,14 +52,13 @@ func sendUsageStats() {
 		}
 		}
 	})
 	})
 
 
-	// metrics["stats.dashboards.count"] = statsQuery.Result.DashboardCount
-	// metrics["stats.users.count"] = statsQuery.Result.UserCount
-	// metrics["stats.orgs.count"] = statsQuery.Result.OrgCount
+	metrics["stats.dashboards.count"] = statsQuery.Result.DashboardCount
+	metrics["stats.users.count"] = statsQuery.Result.UserCount
+	metrics["stats.orgs.count"] = statsQuery.Result.OrgCount
 
 
 	out, _ := json.Marshal(report)
 	out, _ := json.Marshal(report)
 	data := bytes.NewBuffer(out)
 	data := bytes.NewBuffer(out)
 
 
 	client := http.Client{Timeout: time.Duration(5 * time.Second)}
 	client := http.Client{Timeout: time.Duration(5 * time.Second)}
-
 	go client.Post("https://stats.grafana.org/grafana-usage-report", "application/json", data)
 	go client.Post("https://stats.grafana.org/grafana-usage-report", "application/json", data)
 }
 }

+ 1 - 0
pkg/models/dashboard_snapshot.go

@@ -20,6 +20,7 @@ type DashboardSnapshot struct {
 
 
 type CreateDashboardSnapshotCommand struct {
 type CreateDashboardSnapshotCommand struct {
 	Dashboard map[string]interface{} `json:"dashboard" binding:"Required"`
 	Dashboard map[string]interface{} `json:"dashboard" binding:"Required"`
+	External  bool
 
 
 	Key string `json:"-"`
 	Key string `json:"-"`
 
 

+ 7 - 14
src/app/features/dashboard/shareSnapshotCtrl.js

@@ -22,7 +22,7 @@ function (angular) {
       }, 2000);
       }, 2000);
     };
     };
 
 
-    $scope.saveSnapshot = function(makePublic) {
+    $scope.saveSnapshot = function(external) {
       var dash = angular.copy($scope.dashboard);
       var dash = angular.copy($scope.dashboard);
       // change title
       // change title
       dash.title = $scope.snapshot.name;
       dash.title = $scope.snapshot.name;
@@ -40,22 +40,15 @@ function (angular) {
         delete panel.snapshotData;
         delete panel.snapshotData;
       });
       });
 
 
-      var apiUrl = '/api/snapshots';
-
-      if (makePublic) {
-        apiUrl = 'http://snapshots.raintank.io/api/snapshots';
-      }
-
-      backendSrv.post(apiUrl, {dashboard: dash}).then(function(results) {
+      backendSrv.post('/api/snapshots', {dashboard: dash, external: external}).then(function(results) {
         $scope.loading = false;
         $scope.loading = false;
 
 
-        var baseUrl = $location.absUrl().replace($location.url(), "");
-        if (makePublic) {
-          baseUrl = 'http://snapshots.raintank.io';
+        if (external) {
+          $scope.snapshotUrl = results.url;
+        } else {
+          var baseUrl = $location.absUrl().replace($location.url(), "");
+          $scope.snapshotUrl = baseUrl + '/dashboard/snapshots/' + results.key;
         }
         }
-
-        $scope.snapshotUrl = baseUrl + '/dashboard/snapshots/' + results.key;
-
       }, function() {
       }, function() {
         $scope.loading = false;
         $scope.loading = false;
       });
       });