diff --git a/pkg/api/dashboard_snapshot.go b/pkg/api/dashboard_snapshot.go index 635e1d14711..c4035e921d5 100644 --- a/pkg/api/dashboard_snapshot.go +++ b/pkg/api/dashboard_snapshot.go @@ -1,17 +1,27 @@ package api import ( + "bytes" + "encoding/json" + "io/ioutil" + "net/http" + "time" + "github.com/grafana/grafana/pkg/api/dtos" "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/metrics" "github.com/grafana/grafana/pkg/middleware" m "github.com/grafana/grafana/pkg/models" + "github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/util" ) 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 { c.JsonApiErr(500, "Failed to create snaphost", err) return @@ -19,7 +29,30 @@ func CreateDashboardSnapshot(c *middleware.Context, cmd m.CreateDashboardSnapsho 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) { diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 71a5aeaacf5..f6dab8c8043 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -21,8 +21,9 @@ var ( M_Api_Login_OAuth = NewComboCounterRef("api.login.oauth") 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") ) diff --git a/pkg/metrics/report_usage.go b/pkg/metrics/report_usage.go index f952e56fab6..4a4355c5deb 100644 --- a/pkg/metrics/report_usage.go +++ b/pkg/metrics/report_usage.go @@ -7,7 +7,9 @@ import ( "strings" "time" + "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/log" + m "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/setting" ) @@ -34,11 +36,11 @@ func sendUsageStats() { "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{}) { 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) data := bytes.NewBuffer(out) client := http.Client{Timeout: time.Duration(5 * time.Second)} - go client.Post("https://stats.grafana.org/grafana-usage-report", "application/json", data) } diff --git a/pkg/models/dashboard_snapshot.go b/pkg/models/dashboard_snapshot.go index 61abca12673..8f96b27f6ae 100644 --- a/pkg/models/dashboard_snapshot.go +++ b/pkg/models/dashboard_snapshot.go @@ -20,6 +20,7 @@ type DashboardSnapshot struct { type CreateDashboardSnapshotCommand struct { Dashboard map[string]interface{} `json:"dashboard" binding:"Required"` + External bool Key string `json:"-"` diff --git a/src/app/features/dashboard/shareSnapshotCtrl.js b/src/app/features/dashboard/shareSnapshotCtrl.js index c9a71f4230b..a3f279b9c64 100644 --- a/src/app/features/dashboard/shareSnapshotCtrl.js +++ b/src/app/features/dashboard/shareSnapshotCtrl.js @@ -22,7 +22,7 @@ function (angular) { }, 2000); }; - $scope.saveSnapshot = function(makePublic) { + $scope.saveSnapshot = function(external) { var dash = angular.copy($scope.dashboard); // change title dash.title = $scope.snapshot.name; @@ -40,22 +40,15 @@ function (angular) { 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; - 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() { $scope.loading = false; });