mirror of https://github.com/grafana/grafana.git
				
				
				
			Some refinements to dashboard snapshots
This commit is contained in:
		
							parent
							
								
									c8687560d6
								
							
						
					
					
						commit
						9268ecf3e9
					
				|  | @ -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) { | ||||
|  |  | |||
|  | @ -22,6 +22,7 @@ var ( | |||
| 	M_Api_Org_Create        = NewComboCounterRef("api.org.create") | ||||
| 
 | ||||
| 	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") | ||||
|  |  | |||
|  | @ -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) | ||||
| } | ||||
|  |  | |||
|  | @ -20,6 +20,7 @@ type DashboardSnapshot struct { | |||
| 
 | ||||
| type CreateDashboardSnapshotCommand struct { | ||||
| 	Dashboard map[string]interface{} `json:"dashboard" binding:"Required"` | ||||
| 	External  bool | ||||
| 
 | ||||
| 	Key string `json:"-"` | ||||
| 
 | ||||
|  |  | |||
|  | @ -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; | ||||
| 
 | ||||
|         if (external) { | ||||
|           $scope.snapshotUrl = results.url; | ||||
|         } else { | ||||
|           var baseUrl = $location.absUrl().replace($location.url(), ""); | ||||
|         if (makePublic) { | ||||
|           baseUrl = 'http://snapshots.raintank.io'; | ||||
|         } | ||||
| 
 | ||||
|           $scope.snapshotUrl = baseUrl + '/dashboard/snapshots/' + results.key; | ||||
| 
 | ||||
|         } | ||||
|       }, function() { | ||||
|         $scope.loading = false; | ||||
|       }); | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue