| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2015-02-03 22:04:35 +08:00
										 |  |  | 	"encoding/json" | 
					
						
							|  |  |  | 	"os" | 
					
						
							|  |  |  | 	"path" | 
					
						
							| 
									
										
										
										
											2015-06-20 02:42:55 +08:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2015-02-03 22:04:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-05 17:37:13 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/dtos" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/bus" | 
					
						
							| 
									
										
										
										
											2015-03-23 03:14:00 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/metrics" | 
					
						
							| 
									
										
										
										
											2015-02-05 17:37:13 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/middleware" | 
					
						
							|  |  |  | 	m "github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2015-06-05 14:15:38 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/search" | 
					
						
							| 
									
										
										
										
											2015-02-05 17:37:13 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/setting" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/util" | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-01 02:52:57 +08:00
										 |  |  | func isDashboardStarredByUser(c *middleware.Context, dashId int64) (bool, error) { | 
					
						
							| 
									
										
										
										
											2015-02-02 18:32:32 +08:00
										 |  |  | 	if !c.IsSignedIn { | 
					
						
							|  |  |  | 		return false, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	query := m.IsStarredByUserQuery{UserId: c.UserId, DashboardId: dashId} | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&query); err != nil { | 
					
						
							|  |  |  | 		return false, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return query.Result, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | func GetDashboard(c *middleware.Context) { | 
					
						
							| 
									
										
										
										
											2015-03-23 03:14:00 +08:00
										 |  |  | 	metrics.M_Api_Dashboard_Get.Inc(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-20 02:42:55 +08:00
										 |  |  | 	slug := strings.ToLower(c.Params(":slug")) | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 	query := m.GetDashboardQuery{Slug: slug, OrgId: c.OrgId} | 
					
						
							| 
									
										
										
										
											2014-12-29 20:58:06 +08:00
										 |  |  | 	err := bus.Dispatch(&query) | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		c.JsonApiErr(404, "Dashboard not found", nil) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-01 02:52:57 +08:00
										 |  |  | 	isStarred, err := isDashboardStarredByUser(c, query.Result.Id) | 
					
						
							| 
									
										
										
										
											2015-02-02 18:32:32 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		c.JsonApiErr(500, "Error while checking if dashboard was starred by user", err) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-29 19:10:34 +08:00
										 |  |  | 	dash := query.Result | 
					
						
							| 
									
										
										
										
											2015-12-18 19:38:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-29 01:53:19 +08:00
										 |  |  | 	// Finding creator and last updater of the dashboard
 | 
					
						
							| 
									
										
										
										
											2016-01-28 13:55:54 +08:00
										 |  |  | 	updater, creator := "Anonymous", "Anonymous" | 
					
						
							|  |  |  | 	if dash.UpdatedBy > 0 { | 
					
						
							| 
									
										
										
										
											2016-01-28 14:00:24 +08:00
										 |  |  | 		updater = getUserLogin(dash.UpdatedBy) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if dash.CreatedBy > 0 { | 
					
						
							|  |  |  | 		creator = getUserLogin(dash.CreatedBy) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-12-18 19:38:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-04 14:36:44 +08:00
										 |  |  | 	dto := dtos.DashboardFullWithMeta{ | 
					
						
							|  |  |  | 		Dashboard: dash.Data, | 
					
						
							| 
									
										
										
										
											2015-05-12 23:39:56 +08:00
										 |  |  | 		Meta: dtos.DashboardMeta{ | 
					
						
							| 
									
										
										
										
											2016-02-02 15:26:11 +08:00
										 |  |  | 			IsStarred: isStarred, | 
					
						
							|  |  |  | 			Slug:      slug, | 
					
						
							|  |  |  | 			Type:      m.DashTypeDB, | 
					
						
							|  |  |  | 			CanStar:   c.IsSignedIn, | 
					
						
							|  |  |  | 			CanSave:   c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR, | 
					
						
							|  |  |  | 			CanEdit:   canEditDashboard(c.OrgRole), | 
					
						
							|  |  |  | 			Created:   dash.Created, | 
					
						
							|  |  |  | 			Updated:   dash.Updated, | 
					
						
							|  |  |  | 			UpdatedBy: updater, | 
					
						
							|  |  |  | 			CreatedBy: creator, | 
					
						
							|  |  |  | 			Version:   dash.Version, | 
					
						
							| 
									
										
										
										
											2015-05-12 23:39:56 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2015-01-29 19:10:34 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-29 19:10:34 +08:00
										 |  |  | 	c.JSON(200, dto) | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-28 13:55:54 +08:00
										 |  |  | func getUserLogin(userId int64) string { | 
					
						
							| 
									
										
										
										
											2016-01-28 14:00:24 +08:00
										 |  |  | 	query := m.GetUserByIdQuery{Id: userId} | 
					
						
							|  |  |  | 	err := bus.Dispatch(&query) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return "Anonymous" | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		user := query.Result | 
					
						
							|  |  |  | 		return user.Login | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-01-28 13:55:54 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | func DeleteDashboard(c *middleware.Context) { | 
					
						
							|  |  |  | 	slug := c.Params(":slug") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 	query := m.GetDashboardQuery{Slug: slug, OrgId: c.OrgId} | 
					
						
							| 
									
										
										
										
											2015-01-08 16:00:00 +08:00
										 |  |  | 	if err := bus.Dispatch(&query); err != nil { | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 		c.JsonApiErr(404, "Dashboard not found", nil) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 	cmd := m.DeleteDashboardCommand{Slug: slug, OrgId: c.OrgId} | 
					
						
							| 
									
										
										
										
											2015-01-08 16:00:00 +08:00
										 |  |  | 	if err := bus.Dispatch(&cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 		c.JsonApiErr(500, "Failed to delete dashboard", err) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-29 20:58:06 +08:00
										 |  |  | 	var resp = map[string]interface{}{"title": query.Result.Title} | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	c.JSON(200, resp) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-16 21:32:18 +08:00
										 |  |  | func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) { | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 	cmd.OrgId = c.OrgId | 
					
						
							| 
									
										
										
										
											2015-12-18 19:38:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if !c.IsSignedIn { | 
					
						
							| 
									
										
										
										
											2016-01-28 13:55:54 +08:00
										 |  |  | 		cmd.UserId = -1 | 
					
						
							| 
									
										
										
										
											2015-12-18 19:38:49 +08:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2016-01-28 14:00:24 +08:00
										 |  |  | 		cmd.UserId = c.UserId | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-20 22:38:25 +08:00
										 |  |  | 	dash := cmd.GetDashboardModel() | 
					
						
							|  |  |  | 	if dash.Id == 0 { | 
					
						
							| 
									
										
										
										
											2015-09-11 23:17:10 +08:00
										 |  |  | 		limitReached, err := middleware.QuotaReached(c, "dashboard") | 
					
						
							| 
									
										
										
										
											2015-07-20 22:38:25 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			c.JsonApiErr(500, "failed to get quota", err) | 
					
						
							| 
									
										
										
										
											2015-07-20 22:42:31 +08:00
										 |  |  | 			return | 
					
						
							| 
									
										
										
										
											2015-07-20 22:38:25 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if limitReached { | 
					
						
							|  |  |  | 			c.JsonApiErr(403, "Quota reached", nil) | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 	err := bus.Dispatch(&cmd) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2015-01-06 00:04:29 +08:00
										 |  |  | 		if err == m.ErrDashboardWithSameNameExists { | 
					
						
							| 
									
										
										
										
											2015-03-03 05:24:01 +08:00
										 |  |  | 			c.JSON(412, util.DynMap{"status": "name-exists", "message": err.Error()}) | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if err == m.ErrDashboardVersionMismatch { | 
					
						
							|  |  |  | 			c.JSON(412, util.DynMap{"status": "version-mismatch", "message": err.Error()}) | 
					
						
							| 
									
										
										
										
											2015-01-06 00:04:29 +08:00
										 |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-05-04 13:46:46 +08:00
										 |  |  | 		if err == m.ErrDashboardNotFound { | 
					
						
							|  |  |  | 			c.JSON(404, util.DynMap{"status": "not-found", "message": err.Error()}) | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 		c.JsonApiErr(500, "Failed to save dashboard", err) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-23 03:14:00 +08:00
										 |  |  | 	metrics.M_Api_Dashboard_Post.Inc(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-03 05:24:01 +08:00
										 |  |  | 	c.JSON(200, util.DynMap{"status": "success", "slug": cmd.Result.Slug, "version": cmd.Result.Version}) | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-02-03 22:04:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-27 01:54:32 +08:00
										 |  |  | func canEditDashboard(role m.RoleType) bool { | 
					
						
							|  |  |  | 	return role == m.ROLE_ADMIN || role == m.ROLE_EDITOR || role == m.ROLE_READ_ONLY_EDITOR | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-03 22:04:35 +08:00
										 |  |  | func GetHomeDashboard(c *middleware.Context) { | 
					
						
							| 
									
										
										
										
											2015-02-04 05:06:07 +08:00
										 |  |  | 	filePath := path.Join(setting.StaticRootPath, "dashboards/home.json") | 
					
						
							| 
									
										
										
										
											2015-02-03 22:04:35 +08:00
										 |  |  | 	file, err := os.Open(filePath) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		c.JsonApiErr(500, "Failed to load home dashboard", err) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-04 14:36:44 +08:00
										 |  |  | 	dash := dtos.DashboardFullWithMeta{} | 
					
						
							| 
									
										
										
										
											2015-02-03 22:04:35 +08:00
										 |  |  | 	dash.Meta.IsHome = true | 
					
						
							| 
									
										
										
										
											2015-10-27 01:54:32 +08:00
										 |  |  | 	dash.Meta.CanEdit = canEditDashboard(c.OrgRole) | 
					
						
							| 
									
										
										
										
											2015-02-03 22:04:35 +08:00
										 |  |  | 	jsonParser := json.NewDecoder(file) | 
					
						
							| 
									
										
										
										
											2015-05-04 14:36:44 +08:00
										 |  |  | 	if err := jsonParser.Decode(&dash.Dashboard); err != nil { | 
					
						
							| 
									
										
										
										
											2015-02-03 22:04:35 +08:00
										 |  |  | 		c.JsonApiErr(500, "Failed to load home dashboard", err) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	c.JSON(200, &dash) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-05-12 20:11:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func GetDashboardFromJsonFile(c *middleware.Context) { | 
					
						
							|  |  |  | 	file := c.Params(":file") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dashboard := search.GetDashboardFromJsonIndex(file) | 
					
						
							|  |  |  | 	if dashboard == nil { | 
					
						
							|  |  |  | 		c.JsonApiErr(404, "Dashboard not found", nil) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dash := dtos.DashboardFullWithMeta{Dashboard: dashboard.Data} | 
					
						
							|  |  |  | 	dash.Meta.Type = m.DashTypeJson | 
					
						
							| 
									
										
										
										
											2015-10-27 01:54:32 +08:00
										 |  |  | 	dash.Meta.CanEdit = canEditDashboard(c.OrgRole) | 
					
						
							| 
									
										
										
										
											2015-05-12 20:11:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	c.JSON(200, &dash) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-05-13 16:45:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func GetDashboardTags(c *middleware.Context) { | 
					
						
							|  |  |  | 	query := m.GetDashboardTagsQuery{OrgId: c.OrgId} | 
					
						
							|  |  |  | 	err := bus.Dispatch(&query) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		c.JsonApiErr(500, "Failed to get tags from database", err) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	c.JSON(200, query.Result) | 
					
						
							|  |  |  | } |