Some refinements to dashboard snapshots

pull/1650/head
Torkel Ödegaard 10 years ago
parent c8687560d6
commit 9268ecf3e9
  1. 37
      pkg/api/dashboard_snapshot.go
  2. 5
      pkg/metrics/metrics.go
  3. 19
      pkg/metrics/report_usage.go
  4. 1
      pkg/models/dashboard_snapshot.go
  5. 21
      src/app/features/dashboard/shareSnapshotCtrl.js

@ -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) {

@ -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_Create = NewComboCounterRef("api.dashboard_snapshot.create")
M_Api_Dashboard_Snapshot_Get = NewComboCounterRef("api.dashboard_snapshot.get") 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")
) )

@ -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{} statsQuery := m.GetSystemStatsQuery{}
// if err := bus.Dispatch(&statsQuery); err != nil { if err := bus.Dispatch(&statsQuery); err != nil {
// log.Error(3, "Failed to get system stats", err) log.Error(3, "Failed to get system stats", err)
// return 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.dashboards.count"] = statsQuery.Result.DashboardCount
// metrics["stats.users.count"] = statsQuery.Result.UserCount metrics["stats.users.count"] = statsQuery.Result.UserCount
// metrics["stats.orgs.count"] = statsQuery.Result.OrgCount 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)
} }

@ -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:"-"`

@ -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'; backendSrv.post('/api/snapshots', {dashboard: dash, external: external}).then(function(results) {
if (makePublic) {
apiUrl = 'http://snapshots.raintank.io/api/snapshots';
}
backendSrv.post(apiUrl, {dashboard: dash}).then(function(results) {
$scope.loading = false; $scope.loading = false;
var baseUrl = $location.absUrl().replace($location.url(), ""); if (external) {
if (makePublic) { $scope.snapshotUrl = results.url;
baseUrl = 'http://snapshots.raintank.io'; } 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;
}); });

Loading…
Cancel
Save