diff --git a/CHANGELOG.md b/CHANGELOG.md index 92f4ddfc586..5baddec644d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,9 @@ * **Influxdb**: Add support for elapsed(), closes [#5827](https://github.com/grafana/grafana/pull/5827) * **OAuth**: Add support for generic oauth, closes [#4718](https://github.com/grafana/grafana/pull/4718) * **Cloudwatch**: Add support to expand multi select template variable, closes [#5003](https://github.com/grafana/grafana/pull/5003) +* **Graph Panel**: Now supports flexible lower/upper bounds on Y-Max and Y-Min, PR [#5720](https://github.com/grafana/grafana/pull/5720) +* **Background Tasks**: Now support automatic purging of old snapshots, closes [#4087](https://github.com/grafana/grafana/issues/4087) +* **Background Tasks**: Now support automatic purging of old rendered images, closes [#2172](https://github.com/grafana/grafana/issues/2172) ### Breaking changes * **SystemD**: Change systemd description, closes [#5971](https://github.com/grafana/grafana/pull/5971) @@ -20,6 +23,11 @@ ### Bugfixes * **Table Panel**: Fixed problem when switching to Mixed datasource in metrics tab, fixes [#5999](https://github.com/grafana/grafana/pull/5999) +* **Playlist**: Fixed problem with play order not matching order defined in playlist, fixes [#5467](https://github.com/grafana/grafana/pull/5467) +* **Graph panel**: Fixed problem with auto decimals on y axis when datamin=datamax, fixes [#6070](https://github.com/grafana/grafana/pull/6070) +* **Snapshot**: Can view embedded panels/png rendered panels in snapshots without login, fixes [#3769](https://github.com/grafana/grafana/pull/3769) +* **Elasticsearch**: Fix for query template variable when looking up terms without query, no longer relies on elasticsearch default field, fixes [#3887](https://github.com/grafana/grafana/pull/3887) +* **PNG Rendering**: Fix for server side rendering when using auth proxy, fixes [#5906](https://github.com/grafana/grafana/pull/5906) # 3.1.2 (unreleased) * **Templating**: Fixed issue when combining row & panel repeats, fixes [#5790](https://github.com/grafana/grafana/issues/5790) diff --git a/build.go b/build.go index f9ef09ff5b9..202caa1837b 100644 --- a/build.go +++ b/build.go @@ -334,9 +334,7 @@ func gruntBuildArg(task string) []string { func setup() { runPrint("go", "get", "-v", "github.com/kardianos/govendor") - runPrint("go", "get", "-v", "github.com/blang/semver") - runPrint("go", "get", "-v", "github.com/mattn/go-sqlite3") - runPrint("go", "install", "-v", "github.com/mattn/go-sqlite3") + runPrint("go", "install", "-v", "./pkg/cmd/grafana-server") } func test(pkg string) { diff --git a/conf/defaults.ini b/conf/defaults.ini index 3596d41bb14..9fbecb6a1ea 100644 --- a/conf/defaults.ini +++ b/conf/defaults.ini @@ -161,7 +161,13 @@ external_enabled = true external_snapshot_url = https://snapshots-origin.raintank.io external_snapshot_name = Publish to snapshot.raintank.io -#################################### Users ############################### +# remove expired snapshot +snapshot_remove_expired = true + +# remove snapshots after 90 days +snapshot_TTL_days = 90 + +#################################### Users #################################### [users] # disable user signup / registration allow_sign_up = true @@ -276,7 +282,7 @@ from_address = admin@grafana.localhost welcome_email_on_sign_up = false templates_pattern = emails/*.html -#################################### Logging ############################# +#################################### Logging ########################## [log] # Either "console", "file", "syslog". Default is console and file # Use space to separate multiple modes, e.g. "console file" diff --git a/conf/sample.ini b/conf/sample.ini index 2c428ea775f..27f64ee8066 100644 --- a/conf/sample.ini +++ b/conf/sample.ini @@ -116,7 +116,7 @@ # in some UI views to notify that grafana or plugin update exists # This option does not cause any auto updates, nor send any information # only a GET request to http://grafana.net to get latest versions -check_for_updates = true +;check_for_updates = true # Google Analytics universal tracking code, only enabled if you specify an id here ;google_analytics_ua_id = @@ -149,6 +149,12 @@ check_for_updates = true ;external_snapshot_url = https://snapshots-origin.raintank.io ;external_snapshot_name = Publish to snapshot.raintank.io +# remove expired snapshot +;snapshot_remove_expired = true + +# remove snapshots after 90 days +;snapshot_TTL_days = 90 + #################################### Users #################################### [users] # disable user signup / registration @@ -218,6 +224,15 @@ check_for_updates = true ;team_ids = ;allowed_organizations = +#################################### Grafana.net Auth #################### +[auth.grafananet] +;enabled = false +;allow_sign_up = false +;client_id = some_id +;client_secret = some_secret +;scopes = user:email +;allowed_organizations = + #################################### Auth Proxy ########################## [auth.proxy] ;enabled = false diff --git a/docs/sources/http_api/admin.md b/docs/sources/http_api/admin.md index ba1a0923861..fb1b0659cd9 100644 --- a/docs/sources/http_api/admin.md +++ b/docs/sources/http_api/admin.md @@ -6,6 +6,10 @@ page_keywords: grafana, admin, http, api, documentation # Admin API +The admin http API does not currently work with an api token. Api Token's are currently only linked to an organization and organization role. They cannot given +the permission of server admin, only user's can be given that permission. So in order to use these API calls you will have to use basic auth and Grafana user +with Grafana admin permission. + ## Settings `GET /api/admin/settings` @@ -15,7 +19,6 @@ page_keywords: grafana, admin, http, api, documentation GET /api/admin/settings Accept: application/json Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk **Example Response**: @@ -171,7 +174,6 @@ page_keywords: grafana, admin, http, api, documentation GET /api/admin/stats Accept: application/json Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk **Example Response**: @@ -201,7 +203,6 @@ Create new user POST /api/admin/users HTTP/1.1 Accept: application/json Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk { "name":"User", @@ -228,7 +229,6 @@ Change password for specific user PUT /api/admin/users/2/password HTTP/1.1 Accept: application/json Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk **Example Response**: @@ -246,7 +246,6 @@ Change password for specific user PUT /api/admin/users/2/permissions HTTP/1.1 Accept: application/json Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk **Example Response**: @@ -264,7 +263,6 @@ Change password for specific user DELETE /api/admin/users/2 HTTP/1.1 Accept: application/json Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk **Example Response**: diff --git a/docs/sources/installation/configuration.md b/docs/sources/installation/configuration.md index 4c7f63d53ae..d8f29dd7029 100644 --- a/docs/sources/installation/configuration.md +++ b/docs/sources/installation/configuration.md @@ -525,3 +525,9 @@ Set root url to a Grafana instance where you want to publish external snapshots ### external_snapshot_name Set name for external snapshot button. Defaults to `Publish to snapshot.raintank.io` + +### remove expired snapshot +Enabled to automatically remove expired snapshots + +### remove snapshots after 90 days +Time to live for snapshots. diff --git a/pkg/api/api.go b/pkg/api/api.go index 71331acda9f..bac3db429d2 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -58,6 +58,7 @@ func Register(r *macaron.Macaron) { r.Get("/plugins/:id/page/:page", reqSignedIn, Index) r.Get("/dashboard/*", reqSignedIn, Index) + r.Get("/dashboard-solo/snapshot/*", Index) r.Get("/dashboard-solo/*", reqSignedIn, Index) r.Get("/import/dashboard", reqSignedIn, Index) r.Get("/dashboards/*", reqSignedIn, Index) @@ -202,9 +203,9 @@ func Register(r *macaron.Macaron) { r.Get("/plugins", wrap(GetPluginList)) r.Get("/plugins/:pluginId/settings", wrap(GetPluginSettingById)) + r.Get("/plugins/:pluginId/readme", wrap(GetPluginReadme)) r.Group("/plugins", func() { - r.Get("/:pluginId/readme", wrap(GetPluginReadme)) r.Get("/:pluginId/dashboards/", wrap(GetPluginDashboards)) r.Post("/:pluginId/settings", bind(m.UpdatePluginSettingCmd{}), wrap(UpdatePluginSetting)) }, reqOrgAdmin) @@ -243,7 +244,8 @@ func Register(r *macaron.Macaron) { r.Get("/search/", Search) // metrics - r.Get("/metrics/test", wrap(GetTestMetrics)) + r.Post("/tsdb/query", bind(dtos.MetricRequest{}), wrap(QueryMetrics)) + r.Get("/tsdb/testdata/scenarios", wrap(GetTestDataScenarios)) // metrics r.Get("/metrics", wrap(GetInternalMetrics)) diff --git a/pkg/api/dtos/models.go b/pkg/api/dtos/models.go index 8bfc9f9138d..170a5a868fc 100644 --- a/pkg/api/dtos/models.go +++ b/pkg/api/dtos/models.go @@ -96,13 +96,10 @@ func (slice DataSourceList) Swap(i, j int) { slice[i], slice[j] = slice[j], slice[i] } -type MetricQueryResultDto struct { - Data []MetricQueryResultDataDto `json:"data"` -} - -type MetricQueryResultDataDto struct { - Target string `json:"target"` - DataPoints [][2]float64 `json:"datapoints"` +type MetricRequest struct { + From string `json:"from"` + To string `json:"to"` + Queries []*simplejson.Json `json:"queries"` } type UserStars struct { diff --git a/pkg/api/dtos/playlist.go b/pkg/api/dtos/playlist.go new file mode 100644 index 00000000000..317ff83339a --- /dev/null +++ b/pkg/api/dtos/playlist.go @@ -0,0 +1,23 @@ +package dtos + +type PlaylistDashboard struct { + Id int64 `json:"id"` + Slug string `json:"slug"` + Title string `json:"title"` + Uri string `json:"uri"` + Order int `json:"order"` +} + +type PlaylistDashboardsSlice []PlaylistDashboard + +func (slice PlaylistDashboardsSlice) Len() int { + return len(slice) +} + +func (slice PlaylistDashboardsSlice) Less(i, j int) bool { + return slice[i].Order < slice[j].Order +} + +func (slice PlaylistDashboardsSlice) Swap(i, j int) { + slice[i], slice[j] = slice[j], slice[i] +} diff --git a/pkg/api/frontendsettings.go b/pkg/api/frontendsettings.go index 3a019e80c49..5a324aa1331 100644 --- a/pkg/api/frontendsettings.go +++ b/pkg/api/frontendsettings.go @@ -38,7 +38,7 @@ func getFrontendSettingsMap(c *middleware.Context) (map[string]interface{}, erro url := ds.Url if ds.Access == m.DS_ACCESS_PROXY { - url = setting.AppSubUrl + "/api/datasources/proxy/" + strconv.FormatInt(ds.Id, 10) + url = "/api/datasources/proxy/" + strconv.FormatInt(ds.Id, 10) } var dsMap = map[string]interface{}{ diff --git a/pkg/api/index.go b/pkg/api/index.go index e9d784cb652..385810b942e 100644 --- a/pkg/api/index.go +++ b/pkg/api/index.go @@ -1,6 +1,7 @@ package api import ( + "fmt" "strings" "github.com/grafana/grafana/pkg/api/dtos" @@ -32,6 +33,16 @@ func setIndexViewData(c *middleware.Context) (*dtos.IndexViewData, error) { locale = parts[0] } + appUrl := setting.AppUrl + appSubUrl := setting.AppSubUrl + + // special case when doing localhost call from phantomjs + if c.IsRenderCall { + appUrl = fmt.Sprintf("%s://localhost:%s", setting.Protocol, setting.HttpPort) + appSubUrl = "" + settings["appSubUrl"] = "" + } + var data = dtos.IndexViewData{ User: &dtos.CurrentUser{ Id: c.UserId, @@ -49,8 +60,8 @@ func setIndexViewData(c *middleware.Context) (*dtos.IndexViewData, error) { Locale: locale, }, Settings: settings, - AppUrl: setting.AppUrl, - AppSubUrl: setting.AppSubUrl, + AppUrl: appUrl, + AppSubUrl: appSubUrl, GoogleAnalyticsId: setting.GoogleAnalyticsId, GoogleTagManagerId: setting.GoogleTagManagerId, BuildVersion: setting.BuildVersion, @@ -154,7 +165,7 @@ func setIndexViewData(c *middleware.Context) (*dtos.IndexViewData, error) { } } - if c.OrgRole == m.ROLE_ADMIN { + if len(appLink.Children) > 0 && c.OrgRole == m.ROLE_ADMIN { appLink.Children = append(appLink.Children, &dtos.NavLink{Divider: true}) appLink.Children = append(appLink.Children, &dtos.NavLink{Text: "Plugin Config", Icon: "fa fa-cog", Url: setting.AppSubUrl + "/plugins/" + plugin.Id + "/edit"}) } diff --git a/pkg/api/metrics.go b/pkg/api/metrics.go index 154f863af53..0fa6003d67a 100644 --- a/pkg/api/metrics.go +++ b/pkg/api/metrics.go @@ -2,39 +2,54 @@ package api import ( "encoding/json" - "math/rand" "net/http" - "strconv" "github.com/grafana/grafana/pkg/api/dtos" "github.com/grafana/grafana/pkg/metrics" "github.com/grafana/grafana/pkg/middleware" + "github.com/grafana/grafana/pkg/tsdb" + "github.com/grafana/grafana/pkg/tsdb/testdata" "github.com/grafana/grafana/pkg/util" ) -func GetTestMetrics(c *middleware.Context) Response { - from := c.QueryInt64("from") - to := c.QueryInt64("to") - maxDataPoints := c.QueryInt64("maxDataPoints") - stepInSeconds := (to - from) / maxDataPoints +// POST /api/tsdb/query +func QueryMetrics(c *middleware.Context, reqDto dtos.MetricRequest) Response { + timeRange := tsdb.NewTimeRange(reqDto.From, reqDto.To) - result := dtos.MetricQueryResultDto{} - result.Data = make([]dtos.MetricQueryResultDataDto, 1) + request := &tsdb.Request{TimeRange: timeRange} - for seriesIndex := range result.Data { - points := make([][2]float64, maxDataPoints) - walker := rand.Float64() * 100 - time := from + for _, query := range reqDto.Queries { + request.Queries = append(request.Queries, &tsdb.Query{ + RefId: query.Get("refId").MustString("A"), + MaxDataPoints: query.Get("maxDataPoints").MustInt64(100), + IntervalMs: query.Get("intervalMs").MustInt64(1000), + Model: query, + DataSource: &tsdb.DataSourceInfo{ + Name: "Grafana TestDataDB", + PluginId: "grafana-testdata-datasource", + }, + }) + } - for i := range points { - points[i][0] = walker - points[i][1] = float64(time) - walker += rand.Float64() - 0.5 - time += stepInSeconds - } + resp, err := tsdb.HandleRequest(request) + if err != nil { + return ApiError(500, "Metric request error", err) + } - result.Data[seriesIndex].Target = "test-series-" + strconv.Itoa(seriesIndex) - result.Data[seriesIndex].DataPoints = points + return Json(200, &resp) +} + +// GET /api/tsdb/testdata/scenarios +func GetTestDataScenarios(c *middleware.Context) Response { + result := make([]interface{}, 0) + + for _, scenario := range testdata.ScenarioRegistry { + result = append(result, map[string]interface{}{ + "id": scenario.Id, + "name": scenario.Name, + "description": scenario.Description, + "stringInput": scenario.StringInput, + }) } return Json(200, &result) diff --git a/pkg/api/playlist_play.go b/pkg/api/playlist_play.go index e4feb3442fb..780767531a8 100644 --- a/pkg/api/playlist_play.go +++ b/pkg/api/playlist_play.go @@ -1,16 +1,18 @@ package api import ( + "sort" "strconv" + "github.com/grafana/grafana/pkg/api/dtos" "github.com/grafana/grafana/pkg/bus" _ "github.com/grafana/grafana/pkg/log" m "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/search" ) -func populateDashboardsById(dashboardByIds []int64) ([]m.PlaylistDashboardDto, error) { - result := make([]m.PlaylistDashboardDto, 0) +func populateDashboardsById(dashboardByIds []int64, dashboardIdOrder map[int64]int) (dtos.PlaylistDashboardsSlice, error) { + result := make(dtos.PlaylistDashboardsSlice, 0) if len(dashboardByIds) > 0 { dashboardQuery := m.GetDashboardsQuery{DashboardIds: dashboardByIds} @@ -19,11 +21,12 @@ func populateDashboardsById(dashboardByIds []int64) ([]m.PlaylistDashboardDto, e } for _, item := range dashboardQuery.Result { - result = append(result, m.PlaylistDashboardDto{ + result = append(result, dtos.PlaylistDashboard{ Id: item.Id, Slug: item.Slug, Title: item.Title, Uri: "db/" + item.Slug, + Order: dashboardIdOrder[item.Id], }) } } @@ -31,8 +34,8 @@ func populateDashboardsById(dashboardByIds []int64) ([]m.PlaylistDashboardDto, e return result, nil } -func populateDashboardsByTag(orgId, userId int64, dashboardByTag []string) []m.PlaylistDashboardDto { - result := make([]m.PlaylistDashboardDto, 0) +func populateDashboardsByTag(orgId, userId int64, dashboardByTag []string, dashboardTagOrder map[string]int) dtos.PlaylistDashboardsSlice { + result := make(dtos.PlaylistDashboardsSlice, 0) if len(dashboardByTag) > 0 { for _, tag := range dashboardByTag { @@ -47,10 +50,11 @@ func populateDashboardsByTag(orgId, userId int64, dashboardByTag []string) []m.P if err := bus.Dispatch(&searchQuery); err == nil { for _, item := range searchQuery.Result { - result = append(result, m.PlaylistDashboardDto{ + result = append(result, dtos.PlaylistDashboard{ Id: item.Id, Title: item.Title, Uri: item.Uri, + Order: dashboardTagOrder[tag], }) } } @@ -60,28 +64,33 @@ func populateDashboardsByTag(orgId, userId int64, dashboardByTag []string) []m.P return result } -func LoadPlaylistDashboards(orgId, userId, playlistId int64) ([]m.PlaylistDashboardDto, error) { +func LoadPlaylistDashboards(orgId, userId, playlistId int64) (dtos.PlaylistDashboardsSlice, error) { playlistItems, _ := LoadPlaylistItems(playlistId) dashboardByIds := make([]int64, 0) dashboardByTag := make([]string, 0) + dashboardIdOrder := make(map[int64]int) + dashboardTagOrder := make(map[string]int) for _, i := range playlistItems { if i.Type == "dashboard_by_id" { dashboardId, _ := strconv.ParseInt(i.Value, 10, 64) dashboardByIds = append(dashboardByIds, dashboardId) + dashboardIdOrder[dashboardId] = i.Order } if i.Type == "dashboard_by_tag" { dashboardByTag = append(dashboardByTag, i.Value) + dashboardTagOrder[i.Value] = i.Order } } - result := make([]m.PlaylistDashboardDto, 0) + result := make(dtos.PlaylistDashboardsSlice, 0) - var k, _ = populateDashboardsById(dashboardByIds) + var k, _ = populateDashboardsById(dashboardByIds, dashboardIdOrder) result = append(result, k...) - result = append(result, populateDashboardsByTag(orgId, userId, dashboardByTag)...) + result = append(result, populateDashboardsByTag(orgId, userId, dashboardByTag, dashboardTagOrder)...) + sort.Sort(sort.Reverse(result)) return result, nil } diff --git a/pkg/api/render.go b/pkg/api/render.go index 65c1499d0c5..6018656badb 100644 --- a/pkg/api/render.go +++ b/pkg/api/render.go @@ -6,35 +6,21 @@ import ( "github.com/grafana/grafana/pkg/components/renderer" "github.com/grafana/grafana/pkg/middleware" - "github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/util" ) func RenderToPng(c *middleware.Context) { queryReader := util.NewUrlQueryReader(c.Req.URL) queryParams := fmt.Sprintf("?%s", c.Req.URL.RawQuery) - sessionId := c.Session.ID() - - // Handle api calls authenticated without session - if sessionId == "" && c.ApiKeyId != 0 { - c.Session.Start(c) - c.Session.Set(middleware.SESS_KEY_APIKEY, c.ApiKeyId) - // release will make sure the new session is persisted before - // we spin up phantomjs - c.Session.Release() - // cleanup session after render is complete - defer func() { c.Session.Destory(c) }() - } renderOpts := &renderer.RenderOpts{ - Url: c.Params("*") + queryParams, - Width: queryReader.Get("width", "800"), - Height: queryReader.Get("height", "400"), - SessionId: c.Session.ID(), - Timeout: queryReader.Get("timeout", "30"), + Path: c.Params("*") + queryParams, + Width: queryReader.Get("width", "800"), + Height: queryReader.Get("height", "400"), + OrgId: c.OrgId, + Timeout: queryReader.Get("timeout", "30"), } - renderOpts.Url = setting.ToAbsUrl(renderOpts.Url) pngPath, err := renderer.RenderToPng(renderOpts) if err != nil { diff --git a/pkg/cmd/grafana-server/main.go b/pkg/cmd/grafana-server/main.go index d3d291a74bf..42c8dfedacf 100644 --- a/pkg/cmd/grafana-server/main.go +++ b/pkg/cmd/grafana-server/main.go @@ -17,6 +17,7 @@ import ( "github.com/grafana/grafana/pkg/metrics" "github.com/grafana/grafana/pkg/plugins" alertingInit "github.com/grafana/grafana/pkg/services/alerting/init" + "github.com/grafana/grafana/pkg/services/backgroundtasks" "github.com/grafana/grafana/pkg/services/eventpublisher" "github.com/grafana/grafana/pkg/services/notifications" "github.com/grafana/grafana/pkg/services/search" @@ -56,19 +57,19 @@ func main() { setting.BuildCommit = commit setting.BuildStamp = buildstampInt64 - go listenToSystemSignels() + go listenToSystemSignals() flag.Parse() writePIDFile() initRuntime() metrics.Init() - search.Init() login.Init() social.NewOAuthService() eventpublisher.Init() plugins.Init() alertingInit.Init() + backgroundtasks.Init() if err := notifications.Init(); err != nil { log.Fatal(3, "Notification service failed to initialize", err) @@ -116,7 +117,7 @@ func writePIDFile() { } } -func listenToSystemSignels() { +func listenToSystemSignals() { signalChan := make(chan os.Signal, 1) code := 0 diff --git a/pkg/components/renderer/renderer.go b/pkg/components/renderer/renderer.go index ad8f76e03aa..a55ba5e0ab5 100644 --- a/pkg/components/renderer/renderer.go +++ b/pkg/components/renderer/renderer.go @@ -12,36 +12,51 @@ import ( "strconv" "github.com/grafana/grafana/pkg/log" + "github.com/grafana/grafana/pkg/middleware" "github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/util" ) type RenderOpts struct { - Url string - Width string - Height string - SessionId string - Timeout string + Path string + Width string + Height string + Timeout string + OrgId int64 } var rendererLog log.Logger = log.New("png-renderer") func RenderToPng(params *RenderOpts) (string, error) { - rendererLog.Info("Rendering", "url", params.Url) + rendererLog.Info("Rendering", "path", params.Path) var executable = "phantomjs" if runtime.GOOS == "windows" { executable = executable + ".exe" } + url := fmt.Sprintf("%s://localhost:%s/%s", setting.Protocol, setting.HttpPort, params.Path) + binPath, _ := filepath.Abs(filepath.Join(setting.PhantomDir, executable)) scriptPath, _ := filepath.Abs(filepath.Join(setting.PhantomDir, "render.js")) pngPath, _ := filepath.Abs(filepath.Join(setting.ImagesDir, util.GetRandomString(20))) pngPath = pngPath + ".png" - cmd := exec.Command(binPath, "--ignore-ssl-errors=true", scriptPath, "url="+params.Url, "width="+params.Width, - "height="+params.Height, "png="+pngPath, "cookiename="+setting.SessionOptions.CookieName, - "domain="+setting.Domain, "sessionid="+params.SessionId) + renderKey := middleware.AddRenderAuthKey(params.OrgId) + defer middleware.RemoveRenderAuthKey(renderKey) + + cmdArgs := []string{ + "--ignore-ssl-errors=true", + scriptPath, + "url=" + url, + "width=" + params.Width, + "height=" + params.Height, + "png=" + pngPath, + "domain=" + setting.Domain, + "renderKey=" + renderKey, + } + + cmd := exec.Command(binPath, cmdArgs...) stdout, err := cmd.StdoutPipe() if err != nil { diff --git a/pkg/log/log.go b/pkg/log/log.go index 34a2aed4762..fd18e9c65bf 100644 --- a/pkg/log/log.go +++ b/pkg/log/log.go @@ -32,11 +32,25 @@ func New(logger string, ctx ...interface{}) Logger { } func Trace(format string, v ...interface{}) { - Root.Debug(fmt.Sprintf(format, v)) + var message string + if len(v) > 0 { + message = fmt.Sprintf(format, v) + } else { + message = format + } + + Root.Debug(message) } func Debug(format string, v ...interface{}) { - Root.Debug(fmt.Sprintf(format, v)) + var message string + if len(v) > 0 { + message = fmt.Sprintf(format, v) + } else { + message = format + } + + Root.Debug(message) } func Debug2(message string, v ...interface{}) { diff --git a/pkg/metrics/gauge.go b/pkg/metrics/gauge.go index 01cd584cb39..59758aa4ecb 100644 --- a/pkg/metrics/gauge.go +++ b/pkg/metrics/gauge.go @@ -24,10 +24,10 @@ func NewGauge(meta *MetricMeta) Gauge { } } -func RegGauge(meta *MetricMeta) Gauge { - g := NewGauge(meta) - MetricStats.Register(g) - return g +func RegGauge(name string, tagStrings ...string) Gauge { + tr := NewGauge(NewMetricMeta(name, tagStrings)) + MetricStats.Register(tr) + return tr } // GaugeSnapshot is a read-only copy of another Gauge. diff --git a/pkg/metrics/graphite.go b/pkg/metrics/graphite.go index e88df2ebb1b..59c992776de 100644 --- a/pkg/metrics/graphite.go +++ b/pkg/metrics/graphite.go @@ -63,6 +63,8 @@ func (this *GraphitePublisher) Publish(metrics []Metric) { switch metric := m.(type) { case Counter: this.addCount(buf, metricName+".count", metric.Count(), now) + case Gauge: + this.addCount(buf, metricName, metric.Value(), now) case Timer: percentiles := metric.Percentiles([]float64{0.25, 0.75, 0.90, 0.99}) this.addCount(buf, metricName+".count", metric.Count(), now) diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index bbe580de218..002f2369c9b 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -49,6 +49,12 @@ var ( // Timers M_DataSource_ProxyReq_Timer Timer M_Alerting_Exeuction_Time Timer + + // StatTotals + M_StatTotal_Dashboards Gauge + M_StatTotal_Users Gauge + M_StatTotal_Orgs Gauge + M_StatTotal_Playlists Gauge ) func initMetricVars(settings *MetricSettings) { @@ -105,4 +111,10 @@ func initMetricVars(settings *MetricSettings) { // Timers M_DataSource_ProxyReq_Timer = RegTimer("api.dataproxy.request.all") M_Alerting_Exeuction_Time = RegTimer("alerting.execution_time") + + // StatTotals + M_StatTotal_Dashboards = RegGauge("stat_totals", "stat", "dashboards") + M_StatTotal_Users = RegGauge("stat_totals", "stat", "users") + M_StatTotal_Orgs = RegGauge("stat_totals", "stat", "orgs") + M_StatTotal_Playlists = RegGauge("stat_totals", "stat", "playlists") } diff --git a/pkg/metrics/publish.go b/pkg/metrics/publish.go index 9c1de6e05d2..4255481b8d1 100644 --- a/pkg/metrics/publish.go +++ b/pkg/metrics/publish.go @@ -15,6 +15,7 @@ import ( ) var metricsLogger log.Logger = log.New("metrics") +var metricPublishCounter int64 = 0 func Init() { settings := readSettings() @@ -45,12 +46,33 @@ func sendMetrics(settings *MetricSettings) { return } + updateTotalStats() + metrics := MetricStats.GetSnapshots() for _, publisher := range settings.Publishers { publisher.Publish(metrics) } } +func updateTotalStats() { + + // every interval also publish totals + metricPublishCounter++ + if metricPublishCounter%10 == 0 { + // get stats + statsQuery := m.GetSystemStatsQuery{} + if err := bus.Dispatch(&statsQuery); err != nil { + metricsLogger.Error("Failed to get system stats", "error", err) + return + } + + M_StatTotal_Dashboards.Update(statsQuery.Result.DashboardCount) + M_StatTotal_Users.Update(statsQuery.Result.UserCount) + M_StatTotal_Playlists.Update(statsQuery.Result.PlaylistCount) + M_StatTotal_Orgs.Update(statsQuery.Result.OrgCount) + } +} + func sendUsageStats() { if !setting.ReportingEnabled { return diff --git a/pkg/middleware/middleware.go b/pkg/middleware/middleware.go index df1768e1c3a..cb3f4480821 100644 --- a/pkg/middleware/middleware.go +++ b/pkg/middleware/middleware.go @@ -22,6 +22,7 @@ type Context struct { Session SessionStore IsSignedIn bool + IsRenderCall bool AllowAnonymous bool Logger log.Logger } @@ -42,11 +43,11 @@ func GetContextHandler() macaron.Handler { // then init session and look for userId in session // then look for api key in session (special case for render calls via api) // then test if anonymous access is enabled - if initContextWithApiKey(ctx) || + if initContextWithRenderAuth(ctx) || + initContextWithApiKey(ctx) || initContextWithBasicAuth(ctx) || initContextWithAuthProxy(ctx) || initContextWithUserSessionCookie(ctx) || - initContextWithApiKeyFromSession(ctx) || initContextWithAnonymousUser(ctx) { } @@ -176,29 +177,6 @@ func initContextWithBasicAuth(ctx *Context) bool { } } -// special case for panel render calls with api key -func initContextWithApiKeyFromSession(ctx *Context) bool { - keyId := ctx.Session.Get(SESS_KEY_APIKEY) - if keyId == nil { - return false - } - - keyQuery := m.GetApiKeyByIdQuery{ApiKeyId: keyId.(int64)} - if err := bus.Dispatch(&keyQuery); err != nil { - ctx.Logger.Error("Failed to get api key by id", "id", keyId, "error", err) - return false - } else { - apikey := keyQuery.Result - - ctx.IsSignedIn = true - ctx.SignedInUser = &m.SignedInUser{} - ctx.OrgRole = apikey.Role - ctx.ApiKeyId = apikey.Id - ctx.OrgId = apikey.OrgId - return true - } -} - // Handle handles and logs error by given status. func (ctx *Context) Handle(status int, title string, err error) { if err != nil { diff --git a/pkg/middleware/render_auth.go b/pkg/middleware/render_auth.go new file mode 100644 index 00000000000..3a57660c9bf --- /dev/null +++ b/pkg/middleware/render_auth.go @@ -0,0 +1,55 @@ +package middleware + +import ( + "sync" + + m "github.com/grafana/grafana/pkg/models" + "github.com/grafana/grafana/pkg/util" +) + +var renderKeysLock sync.Mutex +var renderKeys map[string]*m.SignedInUser = make(map[string]*m.SignedInUser) + +func initContextWithRenderAuth(ctx *Context) bool { + key := ctx.GetCookie("renderKey") + if key == "" { + return false + } + + renderKeysLock.Lock() + defer renderKeysLock.Unlock() + + if renderUser, exists := renderKeys[key]; !exists { + ctx.JsonApiErr(401, "Invalid Render Key", nil) + return true + } else { + + ctx.IsSignedIn = true + ctx.SignedInUser = renderUser + ctx.IsRenderCall = true + return true + } +} + +type renderContextFunc func(key string) (string, error) + +func AddRenderAuthKey(orgId int64) string { + renderKeysLock.Lock() + + key := util.GetRandomString(32) + + renderKeys[key] = &m.SignedInUser{ + OrgId: orgId, + OrgRole: m.ROLE_VIEWER, + } + + renderKeysLock.Unlock() + + return key +} + +func RemoveRenderAuthKey(key string) { + renderKeysLock.Lock() + delete(renderKeys, key) + renderKeysLock.Unlock() +} diff --git a/pkg/middleware/session.go b/pkg/middleware/session.go index 583c57b85a5..ee6462be37a 100644 --- a/pkg/middleware/session.go +++ b/pkg/middleware/session.go @@ -13,7 +13,6 @@ import ( const ( SESS_KEY_USERID = "uid" - SESS_KEY_APIKEY = "apikey_id" // used fror render requests with api keys ) var sessionManager *session.Manager diff --git a/pkg/models/playlist.go b/pkg/models/playlist.go index 4c6eacbb6a6..5c49bb9256c 100644 --- a/pkg/models/playlist.go +++ b/pkg/models/playlist.go @@ -57,17 +57,6 @@ func (this PlaylistDashboard) TableName() string { type Playlists []*Playlist type PlaylistDashboards []*PlaylistDashboard -// -// DTOS -// - -type PlaylistDashboardDto struct { - Id int64 `json:"id"` - Slug string `json:"slug"` - Title string `json:"title"` - Uri string `json:"uri"` -} - // // COMMANDS // diff --git a/pkg/models/stats.go b/pkg/models/stats.go index fa9cfdab6e8..067dec763e5 100644 --- a/pkg/models/stats.go +++ b/pkg/models/stats.go @@ -1,10 +1,10 @@ package models type SystemStats struct { - DashboardCount int - UserCount int - OrgCount int - PlaylistCount int + DashboardCount int64 + UserCount int64 + OrgCount int64 + PlaylistCount int64 } type DataSourceStats struct { diff --git a/pkg/models/timer.go b/pkg/models/timer.go new file mode 100644 index 00000000000..6cbd7ed29d5 --- /dev/null +++ b/pkg/models/timer.go @@ -0,0 +1,7 @@ +package models + +import "time" + +type HourCommand struct { + Time time.Time +} diff --git a/pkg/plugins/datasource_plugin.go b/pkg/plugins/datasource_plugin.go index b8c79f22998..aa092c2bc20 100644 --- a/pkg/plugins/datasource_plugin.go +++ b/pkg/plugins/datasource_plugin.go @@ -6,6 +6,7 @@ type DataSourcePlugin struct { FrontendPluginBase Annotations bool `json:"annotations"` Metrics bool `json:"metrics"` + Alerting bool `json:"alerting"` BuiltIn bool `json:"builtIn"` Mixed bool `json:"mixed"` App string `json:"app"` diff --git a/pkg/plugins/frontend_plugin.go b/pkg/plugins/frontend_plugin.go index 974559001d1..8db480f947d 100644 --- a/pkg/plugins/frontend_plugin.go +++ b/pkg/plugins/frontend_plugin.go @@ -43,7 +43,12 @@ func (fp *FrontendPluginBase) setPathsBasedOnApp(app *AppPlugin) { appSubPath := strings.Replace(fp.PluginDir, app.PluginDir, "", 1) fp.IncludedInAppId = app.Id fp.BaseUrl = app.BaseUrl - fp.Module = util.JoinUrlFragments("plugins/"+app.Id, appSubPath) + "/module" + + if isExternalPlugin(app.PluginDir) { + fp.Module = util.JoinUrlFragments("plugins/"+app.Id, appSubPath) + "/module" + } else { + fp.Module = util.JoinUrlFragments("app/plugins/app/"+app.Id, appSubPath) + "/module" + } } func (fp *FrontendPluginBase) handleModuleDefaults() { diff --git a/pkg/plugins/update_checker.go b/pkg/plugins/update_checker.go index ed43398357e..76c566803ac 100644 --- a/pkg/plugins/update_checker.go +++ b/pkg/plugins/update_checker.go @@ -9,6 +9,11 @@ import ( "github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/setting" + "github.com/hashicorp/go-version" +) + +var ( + httpClient http.Client = http.Client{Timeout: time.Duration(10 * time.Second)} ) type GrafanaNetPlugin struct { @@ -39,26 +44,23 @@ func StartPluginUpdateChecker() { } func getAllExternalPluginSlugs() string { - str := "" - + var result []string for _, plug := range Plugins { if plug.IsCorePlugin { continue } - str += plug.Id + "," + result = append(result, plug.Id) } - return str + return strings.Join(result, ",") } func checkForUpdates() { log.Trace("Checking for updates") - client := http.Client{Timeout: time.Duration(5 * time.Second)} - pluginSlugs := getAllExternalPluginSlugs() - resp, err := client.Get("https://grafana.net/api/plugins/versioncheck?slugIn=" + pluginSlugs + "&grafanaVersion=" + setting.BuildVersion) + resp, err := httpClient.Get("https://grafana.net/api/plugins/versioncheck?slugIn=" + pluginSlugs + "&grafanaVersion=" + setting.BuildVersion) if err != nil { log.Trace("Failed to get plugins repo from grafana.net, %v", err.Error()) @@ -84,12 +86,20 @@ func checkForUpdates() { for _, gplug := range gNetPlugins { if gplug.Slug == plug.Id { plug.GrafanaNetVersion = gplug.Version - plug.GrafanaNetHasUpdate = plug.Info.Version != plug.GrafanaNetVersion + + plugVersion, err1 := version.NewVersion(plug.Info.Version) + gplugVersion, err2 := version.NewVersion(gplug.Version) + + if err1 != nil || err2 != nil { + plug.GrafanaNetHasUpdate = plug.Info.Version != plug.GrafanaNetVersion + } else { + plug.GrafanaNetHasUpdate = plugVersion.LessThan(gplugVersion) + } } } } - resp2, err := client.Get("https://raw.githubusercontent.com/grafana/grafana/master/latest.json") + resp2, err := httpClient.Get("https://raw.githubusercontent.com/grafana/grafana/master/latest.json") if err != nil { log.Trace("Failed to get latest.json repo from github: %v", err.Error()) return @@ -116,4 +126,11 @@ func checkForUpdates() { GrafanaLatestVersion = githubLatest.Stable GrafanaHasUpdate = githubLatest.Stable != setting.BuildVersion } + + currVersion, err1 := version.NewVersion(setting.BuildVersion) + latestVersion, err2 := version.NewVersion(GrafanaLatestVersion) + + if err1 == nil && err2 == nil { + GrafanaHasUpdate = currVersion.LessThan(latestVersion) + } } diff --git a/pkg/services/alerting/conditions/evaluator.go b/pkg/services/alerting/conditions/evaluator.go index 18a2bf35262..1c154e17ec2 100644 --- a/pkg/services/alerting/conditions/evaluator.go +++ b/pkg/services/alerting/conditions/evaluator.go @@ -5,6 +5,7 @@ import ( "github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/services/alerting" + "gopkg.in/guregu/null.v3" ) var ( @@ -13,13 +14,13 @@ var ( ) type AlertEvaluator interface { - Eval(reducedValue *float64) bool + Eval(reducedValue null.Float) bool } type NoDataEvaluator struct{} -func (e *NoDataEvaluator) Eval(reducedValue *float64) bool { - return reducedValue == nil +func (e *NoDataEvaluator) Eval(reducedValue null.Float) bool { + return reducedValue.Valid == false } type ThresholdEvaluator struct { @@ -43,16 +44,16 @@ func newThresholdEvaludator(typ string, model *simplejson.Json) (*ThresholdEvalu return defaultEval, nil } -func (e *ThresholdEvaluator) Eval(reducedValue *float64) bool { - if reducedValue == nil { +func (e *ThresholdEvaluator) Eval(reducedValue null.Float) bool { + if reducedValue.Valid == false { return false } switch e.Type { case "gt": - return *reducedValue > e.Threshold + return reducedValue.Float64 > e.Threshold case "lt": - return *reducedValue < e.Threshold + return reducedValue.Float64 < e.Threshold } return false @@ -86,16 +87,18 @@ func newRangedEvaluator(typ string, model *simplejson.Json) (*RangedEvaluator, e return rangedEval, nil } -func (e *RangedEvaluator) Eval(reducedValue *float64) bool { - if reducedValue == nil { +func (e *RangedEvaluator) Eval(reducedValue null.Float) bool { + if reducedValue.Valid == false { return false } + floatValue := reducedValue.Float64 + switch e.Type { case "within_range": - return (e.Lower < *reducedValue && e.Upper > *reducedValue) || (e.Upper < *reducedValue && e.Lower > *reducedValue) + return (e.Lower < floatValue && e.Upper > floatValue) || (e.Upper < floatValue && e.Lower > floatValue) case "outside_range": - return (e.Upper < *reducedValue && e.Lower < *reducedValue) || (e.Upper > *reducedValue && e.Lower > *reducedValue) + return (e.Upper < floatValue && e.Lower < floatValue) || (e.Upper > floatValue && e.Lower > floatValue) } return false diff --git a/pkg/services/alerting/conditions/evaluator_test.go b/pkg/services/alerting/conditions/evaluator_test.go index d2919f37d9d..24c5cfacea4 100644 --- a/pkg/services/alerting/conditions/evaluator_test.go +++ b/pkg/services/alerting/conditions/evaluator_test.go @@ -3,6 +3,8 @@ package conditions import ( "testing" + "gopkg.in/guregu/null.v3" + "github.com/grafana/grafana/pkg/components/simplejson" . "github.com/smartystreets/goconvey/convey" ) @@ -14,7 +16,7 @@ func evalutorScenario(json string, reducedValue float64, datapoints ...float64) evaluator, err := NewAlertEvaluator(jsonModel) So(err, ShouldBeNil) - return evaluator.Eval(&reducedValue) + return evaluator.Eval(null.FloatFrom(reducedValue)) } func TestEvalutors(t *testing.T) { @@ -51,6 +53,6 @@ func TestEvalutors(t *testing.T) { evaluator, err := NewAlertEvaluator(jsonModel) So(err, ShouldBeNil) - So(evaluator.Eval(nil), ShouldBeTrue) + So(evaluator.Eval(null.FloatFromPtr(nil)), ShouldBeTrue) }) } diff --git a/pkg/services/alerting/conditions/query.go b/pkg/services/alerting/conditions/query.go index f966985f132..b5300a261a3 100644 --- a/pkg/services/alerting/conditions/query.go +++ b/pkg/services/alerting/conditions/query.go @@ -2,6 +2,8 @@ package conditions import ( "fmt" + "strings" + "time" "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/components/simplejson" @@ -32,7 +34,8 @@ type AlertQuery struct { } func (c *QueryCondition) Eval(context *alerting.EvalContext) { - seriesList, err := c.executeQuery(context) + timeRange := tsdb.NewTimeRange(c.Query.From, c.Query.To) + seriesList, err := c.executeQuery(context, timeRange) if err != nil { context.Error = err return @@ -43,21 +46,21 @@ func (c *QueryCondition) Eval(context *alerting.EvalContext) { reducedValue := c.Reducer.Reduce(series) evalMatch := c.Evaluator.Eval(reducedValue) - if reducedValue == nil { + if reducedValue.Valid == false { emptySerieCount++ continue } if context.IsTestRun { context.Logs = append(context.Logs, &alerting.ResultLogEntry{ - Message: fmt.Sprintf("Condition[%d]: Eval: %v, Metric: %s, Value: %1.3f", c.Index, evalMatch, series.Name, *reducedValue), + Message: fmt.Sprintf("Condition[%d]: Eval: %v, Metric: %s, Value: %1.3f", c.Index, evalMatch, series.Name, reducedValue.Float64), }) } if evalMatch { context.EvalMatches = append(context.EvalMatches, &alerting.EvalMatch{ Metric: series.Name, - Value: *reducedValue, + Value: reducedValue.Float64, }) } } @@ -66,7 +69,7 @@ func (c *QueryCondition) Eval(context *alerting.EvalContext) { context.Firing = len(context.EvalMatches) > 0 } -func (c *QueryCondition) executeQuery(context *alerting.EvalContext) (tsdb.TimeSeriesSlice, error) { +func (c *QueryCondition) executeQuery(context *alerting.EvalContext, timeRange *tsdb.TimeRange) (tsdb.TimeSeriesSlice, error) { getDsInfo := &m.GetDataSourceByIdQuery{ Id: c.Query.DatasourceId, OrgId: context.Rule.OrgId, @@ -76,7 +79,7 @@ func (c *QueryCondition) executeQuery(context *alerting.EvalContext) (tsdb.TimeS return nil, fmt.Errorf("Could not find datasource") } - req := c.getRequestForAlertRule(getDsInfo.Result) + req := c.getRequestForAlertRule(getDsInfo.Result, timeRange) result := make(tsdb.TimeSeriesSlice, 0) resp, err := c.HandleRequest(req) @@ -102,16 +105,13 @@ func (c *QueryCondition) executeQuery(context *alerting.EvalContext) (tsdb.TimeS return result, nil } -func (c *QueryCondition) getRequestForAlertRule(datasource *m.DataSource) *tsdb.Request { +func (c *QueryCondition) getRequestForAlertRule(datasource *m.DataSource, timeRange *tsdb.TimeRange) *tsdb.Request { req := &tsdb.Request{ - TimeRange: tsdb.TimeRange{ - From: c.Query.From, - To: c.Query.To, - }, + TimeRange: timeRange, Queries: []*tsdb.Query{ { RefId: "A", - Query: c.Query.Model.Get("target").MustString(), + Model: c.Query.Model, DataSource: &tsdb.DataSourceInfo{ Id: datasource.Id, Name: datasource.Name, @@ -141,6 +141,15 @@ func NewQueryCondition(model *simplejson.Json, index int) (*QueryCondition, erro condition.Query.Model = queryJson.Get("model") condition.Query.From = queryJson.Get("params").MustArray()[1].(string) condition.Query.To = queryJson.Get("params").MustArray()[2].(string) + + if err := validateFromValue(condition.Query.From); err != nil { + return nil, err + } + + if err := validateToValue(condition.Query.To); err != nil { + return nil, err + } + condition.Query.DatasourceId = queryJson.Get("datasourceId").MustInt64() reducerJson := model.Get("reducer") @@ -155,3 +164,26 @@ func NewQueryCondition(model *simplejson.Json, index int) (*QueryCondition, erro condition.Evaluator = evaluator return &condition, nil } + +func validateFromValue(from string) error { + fromRaw := strings.Replace(from, "now-", "", 1) + + _, err := time.ParseDuration("-" + fromRaw) + return err +} + +func validateToValue(to string) error { + if to == "now" { + return nil + } else if strings.HasPrefix(to, "now-") { + withoutNow := strings.Replace(to, "now-", "", 1) + + _, err := time.ParseDuration("-" + withoutNow) + if err == nil { + return nil + } + } + + _, err := time.ParseDuration(to) + return err +} diff --git a/pkg/services/alerting/conditions/query_test.go b/pkg/services/alerting/conditions/query_test.go index 983e75c4c1b..51c4226f81c 100644 --- a/pkg/services/alerting/conditions/query_test.go +++ b/pkg/services/alerting/conditions/query_test.go @@ -3,6 +3,8 @@ package conditions import ( "testing" + null "gopkg.in/guregu/null.v3" + "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/components/simplejson" m "github.com/grafana/grafana/pkg/models" @@ -41,9 +43,8 @@ func TestQueryCondition(t *testing.T) { }) Convey("should fire when avg is above 100", func() { - one := float64(120) - two := float64(0) - ctx.series = tsdb.TimeSeriesSlice{tsdb.NewTimeSeries("test1", [][2]*float64{{&one, &two}})} + points := tsdb.NewTimeSeriesPointsFromArgs(120, 0) + ctx.series = tsdb.TimeSeriesSlice{tsdb.NewTimeSeries("test1", points)} ctx.exec() So(ctx.result.Error, ShouldBeNil) @@ -51,9 +52,8 @@ func TestQueryCondition(t *testing.T) { }) Convey("Should not fire when avg is below 100", func() { - one := float64(90) - two := float64(0) - ctx.series = tsdb.TimeSeriesSlice{tsdb.NewTimeSeries("test1", [][2]*float64{{&one, &two}})} + points := tsdb.NewTimeSeriesPointsFromArgs(90, 0) + ctx.series = tsdb.TimeSeriesSlice{tsdb.NewTimeSeries("test1", points)} ctx.exec() So(ctx.result.Error, ShouldBeNil) @@ -61,11 +61,9 @@ func TestQueryCondition(t *testing.T) { }) Convey("Should fire if only first serie matches", func() { - one := float64(120) - two := float64(0) ctx.series = tsdb.TimeSeriesSlice{ - tsdb.NewTimeSeries("test1", [][2]*float64{{&one, &two}}), - tsdb.NewTimeSeries("test2", [][2]*float64{{&two, &two}}), + tsdb.NewTimeSeries("test1", tsdb.NewTimeSeriesPointsFromArgs(120, 0)), + tsdb.NewTimeSeries("test2", tsdb.NewTimeSeriesPointsFromArgs(0, 0)), } ctx.exec() @@ -76,8 +74,8 @@ func TestQueryCondition(t *testing.T) { Convey("Empty series", func() { Convey("Should set NoDataFound both series are empty", func() { ctx.series = tsdb.TimeSeriesSlice{ - tsdb.NewTimeSeries("test1", [][2]*float64{}), - tsdb.NewTimeSeries("test2", [][2]*float64{}), + tsdb.NewTimeSeries("test1", tsdb.NewTimeSeriesPointsFromArgs()), + tsdb.NewTimeSeries("test2", tsdb.NewTimeSeriesPointsFromArgs()), } ctx.exec() @@ -86,10 +84,9 @@ func TestQueryCondition(t *testing.T) { }) Convey("Should set NoDataFound both series contains null", func() { - one := float64(120) ctx.series = tsdb.TimeSeriesSlice{ - tsdb.NewTimeSeries("test1", [][2]*float64{{nil, &one}}), - tsdb.NewTimeSeries("test2", [][2]*float64{{nil, &one}}), + tsdb.NewTimeSeries("test1", tsdb.TimeSeriesPoints{tsdb.TimePoint{null.FloatFromPtr(nil), null.FloatFrom(0)}}), + tsdb.NewTimeSeries("test2", tsdb.TimeSeriesPoints{tsdb.TimePoint{null.FloatFromPtr(nil), null.FloatFrom(0)}}), } ctx.exec() @@ -98,11 +95,9 @@ func TestQueryCondition(t *testing.T) { }) Convey("Should not set NoDataFound if one serie is empty", func() { - one := float64(120) - two := float64(0) ctx.series = tsdb.TimeSeriesSlice{ - tsdb.NewTimeSeries("test1", [][2]*float64{}), - tsdb.NewTimeSeries("test2", [][2]*float64{{&one, &two}}), + tsdb.NewTimeSeries("test1", tsdb.NewTimeSeriesPointsFromArgs()), + tsdb.NewTimeSeries("test2", tsdb.NewTimeSeriesPointsFromArgs(120, 0)), } ctx.exec() diff --git a/pkg/services/alerting/conditions/reducer.go b/pkg/services/alerting/conditions/reducer.go index 2bb4cec00be..a982fa63d33 100644 --- a/pkg/services/alerting/conditions/reducer.go +++ b/pkg/services/alerting/conditions/reducer.go @@ -4,19 +4,20 @@ import ( "math" "github.com/grafana/grafana/pkg/tsdb" + "gopkg.in/guregu/null.v3" ) type QueryReducer interface { - Reduce(timeSeries *tsdb.TimeSeries) *float64 + Reduce(timeSeries *tsdb.TimeSeries) null.Float } type SimpleReducer struct { Type string } -func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) *float64 { +func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) null.Float { if len(series.Points) == 0 { - return nil + return null.FloatFromPtr(nil) } value := float64(0) @@ -25,36 +26,36 @@ func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) *float64 { switch s.Type { case "avg": for _, point := range series.Points { - if point[0] != nil { - value += *point[0] + if point[0].Valid { + value += point[0].Float64 allNull = false } } value = value / float64(len(series.Points)) case "sum": for _, point := range series.Points { - if point[0] != nil { - value += *point[0] + if point[0].Valid { + value += point[0].Float64 allNull = false } } case "min": value = math.MaxFloat64 for _, point := range series.Points { - if point[0] != nil { + if point[0].Valid { allNull = false - if value > *point[0] { - value = *point[0] + if value > point[0].Float64 { + value = point[0].Float64 } } } case "max": value = -math.MaxFloat64 for _, point := range series.Points { - if point[0] != nil { + if point[0].Valid { allNull = false - if value < *point[0] { - value = *point[0] + if value < point[0].Float64 { + value = point[0].Float64 } } } @@ -64,10 +65,10 @@ func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) *float64 { } if allNull { - return nil + return null.FloatFromPtr(nil) } - return &value + return null.FloatFrom(value) } func NewSimpleReducer(typ string) *SimpleReducer { diff --git a/pkg/services/alerting/conditions/reducer_test.go b/pkg/services/alerting/conditions/reducer_test.go index f60154bc98d..67765f9c310 100644 --- a/pkg/services/alerting/conditions/reducer_test.go +++ b/pkg/services/alerting/conditions/reducer_test.go @@ -10,44 +10,41 @@ import ( func TestSimpleReducer(t *testing.T) { Convey("Test simple reducer by calculating", t, func() { Convey("avg", func() { - result := *testReducer("avg", 1, 2, 3) + result := testReducer("avg", 1, 2, 3) So(result, ShouldEqual, float64(2)) }) Convey("sum", func() { - result := *testReducer("sum", 1, 2, 3) + result := testReducer("sum", 1, 2, 3) So(result, ShouldEqual, float64(6)) }) Convey("min", func() { - result := *testReducer("min", 3, 2, 1) + result := testReducer("min", 3, 2, 1) So(result, ShouldEqual, float64(1)) }) Convey("max", func() { - result := *testReducer("max", 1, 2, 3) + result := testReducer("max", 1, 2, 3) So(result, ShouldEqual, float64(3)) }) Convey("count", func() { - result := *testReducer("count", 1, 2, 3000) + result := testReducer("count", 1, 2, 3000) So(result, ShouldEqual, float64(3)) }) }) } -func testReducer(typ string, datapoints ...float64) *float64 { +func testReducer(typ string, datapoints ...float64) float64 { reducer := NewSimpleReducer(typ) - var timeserie [][2]*float64 - dummieTimestamp := float64(521452145) + series := &tsdb.TimeSeries{ + Name: "test time serie", + } for idx := range datapoints { - timeserie = append(timeserie, [2]*float64{&datapoints[idx], &dummieTimestamp}) + series.Points = append(series.Points, tsdb.NewTimePoint(datapoints[idx], 1234134)) } - tsdb := &tsdb.TimeSeries{ - Name: "test time serie", - Points: timeserie, - } - return reducer.Reduce(tsdb) + return reducer.Reduce(series).Float64 } diff --git a/pkg/services/alerting/engine.go b/pkg/services/alerting/engine.go index 7abfe32425c..19befe87ed8 100644 --- a/pkg/services/alerting/engine.go +++ b/pkg/services/alerting/engine.go @@ -93,14 +93,18 @@ func (e *Engine) executeJob(job *Job) { } func (e *Engine) resultDispatcher() { + for result := range e.resultQueue { + go e.handleResponse(result) + } +} + +func (e *Engine) handleResponse(result *EvalContext) { defer func() { if err := recover(); err != nil { e.log.Error("Panic in resultDispatcher", "error", err, "stack", log.Stack(1)) } }() - for result := range e.resultQueue { - e.log.Debug("Alert Rule Result", "ruleId", result.Rule.Id, "firing", result.Firing) - e.resultHandler.Handle(result) - } + e.log.Debug("Alert Rule Result", "ruleId", result.Rule.Id, "firing", result.Firing) + e.resultHandler.Handle(result) } diff --git a/pkg/services/alerting/eval_context.go b/pkg/services/alerting/eval_context.go index 13067c25f08..a76ed8d519f 100644 --- a/pkg/services/alerting/eval_context.go +++ b/pkg/services/alerting/eval_context.go @@ -71,7 +71,7 @@ func (c *EvalContext) GetNotificationTitle() string { return "[" + c.GetStateModel().Text + "] " + c.Rule.Name } -func (c *EvalContext) getDashboardSlug() (string, error) { +func (c *EvalContext) GetDashboardSlug() (string, error) { if c.dashboardSlug != "" { return c.dashboardSlug, nil } @@ -86,7 +86,7 @@ func (c *EvalContext) getDashboardSlug() (string, error) { } func (c *EvalContext) GetRuleUrl() (string, error) { - if slug, err := c.getDashboardSlug(); err != nil { + if slug, err := c.GetDashboardSlug(); err != nil { return "", err } else { ruleUrl := fmt.Sprintf("%sdashboard/db/%s?fullscreen&edit&tab=alert&panelId=%d", setting.AppUrl, slug, c.Rule.PanelId) @@ -94,15 +94,6 @@ func (c *EvalContext) GetRuleUrl() (string, error) { } } -func (c *EvalContext) GetImageUrl() (string, error) { - if slug, err := c.getDashboardSlug(); err != nil { - return "", err - } else { - ruleUrl := fmt.Sprintf("%sdashboard-solo/db/%s?&panelId=%d", setting.AppUrl, slug, c.Rule.PanelId) - return ruleUrl, nil - } -} - func NewEvalContext(rule *Rule) *EvalContext { return &EvalContext{ StartTime: time.Now(), diff --git a/pkg/services/alerting/eval_handler.go b/pkg/services/alerting/eval_handler.go index ab4c377197b..a5599b96d2c 100644 --- a/pkg/services/alerting/eval_handler.go +++ b/pkg/services/alerting/eval_handler.go @@ -20,7 +20,7 @@ type DefaultEvalHandler struct { func NewEvalHandler() *DefaultEvalHandler { return &DefaultEvalHandler{ log: log.New("alerting.evalHandler"), - alertJobTimeout: time.Second * 10, + alertJobTimeout: time.Second * 15, } } diff --git a/pkg/services/alerting/init/init.go b/pkg/services/alerting/init/init.go index b6627a359e6..94f97a41905 100644 --- a/pkg/services/alerting/init/init.go +++ b/pkg/services/alerting/init/init.go @@ -6,6 +6,8 @@ import ( _ "github.com/grafana/grafana/pkg/services/alerting/notifiers" "github.com/grafana/grafana/pkg/setting" _ "github.com/grafana/grafana/pkg/tsdb/graphite" + _ "github.com/grafana/grafana/pkg/tsdb/prometheus" + _ "github.com/grafana/grafana/pkg/tsdb/testdata" ) var engine *alerting.Engine diff --git a/pkg/services/alerting/notifier.go b/pkg/services/alerting/notifier.go index bb48f71cdcd..52d4075ae6e 100644 --- a/pkg/services/alerting/notifier.go +++ b/pkg/services/alerting/notifier.go @@ -2,6 +2,7 @@ package alerting import ( "errors" + "fmt" "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/components/imguploader" @@ -60,20 +61,20 @@ func (n *RootNotifier) sendNotifications(notifiers []Notifier, context *EvalCont } } -func (n *RootNotifier) uploadImage(context *EvalContext) error { +func (n *RootNotifier) uploadImage(context *EvalContext) (err error) { uploader, _ := imguploader.NewImageUploader() - imageUrl, err := context.GetImageUrl() - if err != nil { - return err + renderOpts := &renderer.RenderOpts{ + Width: "800", + Height: "400", + Timeout: "30", + OrgId: context.Rule.OrgId, } - renderOpts := &renderer.RenderOpts{ - Url: imageUrl, - Width: "800", - Height: "400", - SessionId: "123", - Timeout: "10", + if slug, err := context.GetDashboardSlug(); err != nil { + return err + } else { + renderOpts.Path = fmt.Sprintf("dashboard-solo/db/%s?&panelId=%d", slug, context.Rule.PanelId) } if imagePath, err := renderer.RenderToPng(renderOpts); err != nil { diff --git a/pkg/services/alerting/notifiers/webhook.go b/pkg/services/alerting/notifiers/webhook.go index 7e28b35cd0a..320f273eddc 100644 --- a/pkg/services/alerting/notifiers/webhook.go +++ b/pkg/services/alerting/notifiers/webhook.go @@ -52,9 +52,8 @@ func (this *WebhookNotifier) Notify(context *alerting.EvalContext) { bodyJSON.Set("rule_url", ruleUrl) } - imageUrl, err := context.GetImageUrl() - if err == nil { - bodyJSON.Set("image_url", imageUrl) + if context.ImagePublicUrl != "" { + bodyJSON.Set("image_url", context.ImagePublicUrl) } body, _ := bodyJSON.MarshalJSON() diff --git a/pkg/services/backgroundtasks/background_tasks.go b/pkg/services/backgroundtasks/background_tasks.go new file mode 100644 index 00000000000..5c4a7d197a8 --- /dev/null +++ b/pkg/services/backgroundtasks/background_tasks.go @@ -0,0 +1,39 @@ +//"I want to be a cleaner, just like you," said Mathilda +//"Okay," replied Leon + +package backgroundtasks + +import ( + "time" + + "github.com/grafana/grafana/pkg/bus" + "github.com/grafana/grafana/pkg/log" + "github.com/grafana/grafana/pkg/models" +) + +var ( + tlog log.Logger = log.New("ticker") +) + +func Init() { + go start() +} + +func start() { + go cleanup(time.Now()) + + ticker := time.NewTicker(time.Hour * 1) + for { + select { + case tick := <-ticker.C: + go cleanup(tick) + } + } +} + +func cleanup(now time.Time) { + err := bus.Publish(&models.HourCommand{Time: now}) + if err != nil { + tlog.Error("Cleanup job failed", "error", err) + } +} diff --git a/pkg/services/backgroundtasks/remove_tmp_images.go b/pkg/services/backgroundtasks/remove_tmp_images.go new file mode 100644 index 00000000000..d6048f09523 --- /dev/null +++ b/pkg/services/backgroundtasks/remove_tmp_images.go @@ -0,0 +1,38 @@ +package backgroundtasks + +import ( + "io/ioutil" + "os" + "path" + + "github.com/grafana/grafana/pkg/bus" + "github.com/grafana/grafana/pkg/models" + "github.com/grafana/grafana/pkg/setting" +) + +func init() { + bus.AddEventListener(CleanTmpFiles) +} + +func CleanTmpFiles(cmd *models.HourCommand) error { + files, err := ioutil.ReadDir(setting.ImagesDir) + + var toDelete []os.FileInfo + for _, file := range files { + if file.ModTime().AddDate(0, 0, setting.RenderedImageTTLDays).Before(cmd.Time) { + toDelete = append(toDelete, file) + } + } + + for _, file := range toDelete { + fullPath := path.Join(setting.ImagesDir, file.Name()) + err := os.Remove(fullPath) + if err != nil { + return err + } + } + + tlog.Debug("Found old rendered image to delete", "deleted", len(toDelete), "keept", len(files)) + + return err +} diff --git a/pkg/services/notifications/mailer.go b/pkg/services/notifications/mailer.go index 309436cb7d9..91c75a1889e 100644 --- a/pkg/services/notifications/mailer.go +++ b/pkg/services/notifications/mailer.go @@ -12,6 +12,7 @@ import ( "net/smtp" "os" "strings" + "time" "github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/setting" @@ -66,7 +67,7 @@ func sendToSmtpServer(recipients []string, msgContent []byte) error { tlsconfig.Certificates = []tls.Certificate{cert} } - conn, err := net.Dial("tcp", net.JoinHostPort(host, port)) + conn, err := net.DialTimeout("tcp", net.JoinHostPort(host, port), time.Second*10) if err != nil { return err } diff --git a/pkg/services/notifications/webhook.go b/pkg/services/notifications/webhook.go index 31f00baebd3..67ffa43900a 100644 --- a/pkg/services/notifications/webhook.go +++ b/pkg/services/notifications/webhook.go @@ -44,7 +44,7 @@ func sendWebRequest(webhook *Webhook) error { webhookLog.Debug("Sending webhook", "url", webhook.Url) client := http.Client{ - Timeout: time.Duration(3 * time.Second), + Timeout: time.Duration(10 * time.Second), } request, err := http.NewRequest("POST", webhook.Url, bytes.NewReader([]byte(webhook.Body))) diff --git a/pkg/services/sqlstore/alert.go b/pkg/services/sqlstore/alert.go index 5a430238823..64a4a6b3d8a 100644 --- a/pkg/services/sqlstore/alert.go +++ b/pkg/services/sqlstore/alert.go @@ -92,7 +92,7 @@ func HandleAlertsQuery(query *m.GetAlertsQuery) error { params = append(params, query.Limit) } - sql.WriteString("ORDER BY name ASC") + sql.WriteString(" ORDER BY name ASC") alerts := make([]*m.Alert, 0) if err := x.Sql(sql.String(), params...).Find(&alerts); err != nil { diff --git a/pkg/services/sqlstore/alert_notification.go b/pkg/services/sqlstore/alert_notification.go index 5105ca39eff..5acb53c3c09 100644 --- a/pkg/services/sqlstore/alert_notification.go +++ b/pkg/services/sqlstore/alert_notification.go @@ -66,7 +66,8 @@ func GetAlertNotificationsToSend(query *m.GetAlertNotificationsToSendQuery) erro sql.WriteString(` WHERE alert_notification.org_id = ?`) params = append(params, query.OrgId) - sql.WriteString(` AND ((alert_notification.is_default = 1)`) + sql.WriteString(` AND ((alert_notification.is_default = ?)`) + params = append(params, dialect.BooleanStr(true)) if len(query.Ids) > 0 { sql.WriteString(` OR alert_notification.id IN (?` + strings.Repeat(",?", len(query.Ids)-1) + ")") for _, v := range query.Ids { diff --git a/pkg/services/sqlstore/annotation.go b/pkg/services/sqlstore/annotation.go index 1b0f02fce09..3ea8647d3fa 100644 --- a/pkg/services/sqlstore/annotation.go +++ b/pkg/services/sqlstore/annotation.go @@ -75,7 +75,7 @@ func (r *SqlAnnotationRepo) Find(query *annotations.ItemQuery) ([]*annotations.I query.Limit = 10 } - sql.WriteString(fmt.Sprintf("ORDER BY epoch DESC LIMIT %v", query.Limit)) + sql.WriteString(fmt.Sprintf(" ORDER BY epoch DESC LIMIT %v", query.Limit)) items := make([]*annotations.Item, 0) if err := x.Sql(sql.String(), params...).Find(&items); err != nil { diff --git a/pkg/services/sqlstore/dashboard_snapshot.go b/pkg/services/sqlstore/dashboard_snapshot.go index fc94a91cce5..50a7ece05f3 100644 --- a/pkg/services/sqlstore/dashboard_snapshot.go +++ b/pkg/services/sqlstore/dashboard_snapshot.go @@ -5,7 +5,9 @@ import ( "github.com/go-xorm/xorm" "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" ) func init() { @@ -13,6 +15,31 @@ func init() { bus.AddHandler("sql", GetDashboardSnapshot) bus.AddHandler("sql", DeleteDashboardSnapshot) bus.AddHandler("sql", SearchDashboardSnapshots) + bus.AddEventListener(DeleteExpiredSnapshots) +} + +func DeleteExpiredSnapshots(cmd *m.HourCommand) error { + return inTransaction(func(sess *xorm.Session) error { + var expiredCount int64 = 0 + var oldCount int64 = 0 + + if setting.SnapShotRemoveExpired { + deleteExpiredSql := "DELETE FROM dashboard_snapshot WHERE expires < ?" + expiredResponse, err := x.Exec(deleteExpiredSql, cmd.Time) + if err != nil { + return err + } + expiredCount, _ = expiredResponse.RowsAffected() + } + + oldSnapshotsSql := "DELETE FROM dashboard_snapshot WHERE created < ?" + oldResponse, err := x.Exec(oldSnapshotsSql, cmd.Time.AddDate(0, 0, setting.SnapShotTTLDays*-1)) + oldCount, _ = oldResponse.RowsAffected() + + log.Debug2("Deleted old/expired snaphots", "to old", oldCount, "expired", expiredCount) + + return err + }) } func CreateDashboardSnapshot(cmd *m.CreateDashboardSnapshotCommand) error { diff --git a/pkg/services/sqlstore/migrations/dashboard_mig.go b/pkg/services/sqlstore/migrations/dashboard_mig.go index 4f286dce68a..283501d366f 100644 --- a/pkg/services/sqlstore/migrations/dashboard_mig.go +++ b/pkg/services/sqlstore/migrations/dashboard_mig.go @@ -120,4 +120,9 @@ func addDashboardMigration(mg *Migrator) { mg.AddMigration("Add index for plugin_id in dashboard", NewAddIndexMigration(dashboardV2, &Index{ Cols: []string{"org_id", "plugin_id"}, Type: IndexType, })) + + // dashboard_id index for dashboard_tag table + mg.AddMigration("Add index for dashboard_id in dashboard_tag", NewAddIndexMigration(dashboardTagV1, &Index{ + Cols: []string{"dashboard_id"}, Type: IndexType, + })) } diff --git a/pkg/services/sqlstore/migrator/dialect.go b/pkg/services/sqlstore/migrator/dialect.go index 0c94eb82234..4473b560428 100644 --- a/pkg/services/sqlstore/migrator/dialect.go +++ b/pkg/services/sqlstore/migrator/dialect.go @@ -18,6 +18,7 @@ type Dialect interface { SupportEngine() bool LikeStr() string Default(col *Column) string + BooleanStr(bool) string CreateIndexSql(tableName string, index *Index) string CreateTableSql(table *Table) string diff --git a/pkg/services/sqlstore/migrator/mysql_dialect.go b/pkg/services/sqlstore/migrator/mysql_dialect.go index 195d52d1934..fc64842bd07 100644 --- a/pkg/services/sqlstore/migrator/mysql_dialect.go +++ b/pkg/services/sqlstore/migrator/mysql_dialect.go @@ -29,6 +29,10 @@ func (db *Mysql) AutoIncrStr() string { return "AUTO_INCREMENT" } +func (db *Mysql) BooleanStr(value bool) string { + return strconv.FormatBool(value) +} + func (db *Mysql) SqlType(c *Column) string { var res string switch c.Type { diff --git a/pkg/services/sqlstore/migrator/postgres_dialect.go b/pkg/services/sqlstore/migrator/postgres_dialect.go index 826a00a1410..5500b9f1684 100644 --- a/pkg/services/sqlstore/migrator/postgres_dialect.go +++ b/pkg/services/sqlstore/migrator/postgres_dialect.go @@ -36,6 +36,10 @@ func (db *Postgres) AutoIncrStr() string { return "" } +func (db *Postgres) BooleanStr(value bool) string { + return strconv.FormatBool(value) +} + func (b *Postgres) Default(col *Column) string { if col.Type == DB_Bool { if col.Default == "0" { diff --git a/pkg/services/sqlstore/migrator/sqlite_dialect.go b/pkg/services/sqlstore/migrator/sqlite_dialect.go index 8555754ab92..fe1e781c8df 100644 --- a/pkg/services/sqlstore/migrator/sqlite_dialect.go +++ b/pkg/services/sqlstore/migrator/sqlite_dialect.go @@ -29,6 +29,13 @@ func (db *Sqlite3) AutoIncrStr() string { return "AUTOINCREMENT" } +func (db *Sqlite3) BooleanStr(value bool) string { + if value { + return "1" + } + return "0" +} + func (db *Sqlite3) SqlType(c *Column) string { switch c.Type { case DB_Date, DB_DateTime, DB_TimeStamp, DB_Time: diff --git a/pkg/setting/setting.go b/pkg/setting/setting.go index 33b026713a1..79e61dd0114 100644 --- a/pkg/setting/setting.go +++ b/pkg/setting/setting.go @@ -78,9 +78,11 @@ var ( DataProxyWhiteList map[string]bool // Snapshots - ExternalSnapshotUrl string - ExternalSnapshotName string - ExternalEnabled bool + ExternalSnapshotUrl string + ExternalSnapshotName string + ExternalEnabled bool + SnapShotTTLDays int + SnapShotRemoveExpired bool // User settings AllowUserSignUp bool @@ -118,8 +120,9 @@ var ( IsWindows bool // PhantomJs Rendering - ImagesDir string - PhantomDir string + ImagesDir string + PhantomDir string + RenderedImageTTLDays int // for logging purposes configFiles []string @@ -495,6 +498,8 @@ func NewConfigContext(args *CommandLineArgs) error { ExternalSnapshotUrl = snapshots.Key("external_snapshot_url").String() ExternalSnapshotName = snapshots.Key("external_snapshot_name").String() ExternalEnabled = snapshots.Key("external_enabled").MustBool(true) + SnapShotRemoveExpired = snapshots.Key("snapshot_remove_expired").MustBool(true) + SnapShotTTLDays = snapshots.Key("snapshot_TTL_days").MustInt(90) // read data source proxy white list DataProxyWhiteList = make(map[string]bool) @@ -535,6 +540,9 @@ func NewConfigContext(args *CommandLineArgs) error { ImagesDir = filepath.Join(DataPath, "png") PhantomDir = filepath.Join(HomePath, "vendor/phantomjs") + tmpFilesSection := Cfg.Section("tmp.files") + RenderedImageTTLDays = tmpFilesSection.Key("rendered_image_ttl_days").MustInt(14) + analytics := Cfg.Section("analytics") ReportingEnabled = analytics.Key("reporting_enabled").MustBool(true) CheckForUpdates = analytics.Key("check_for_updates").MustBool(true) diff --git a/pkg/setting/setting_oauth.go b/pkg/setting/setting_oauth.go index 540b32ad83e..63d0da928e3 100644 --- a/pkg/setting/setting_oauth.go +++ b/pkg/setting/setting_oauth.go @@ -12,8 +12,8 @@ type OAuthInfo struct { type OAuther struct { GitHub, Google, Twitter, Generic, GrafanaNet bool - OAuthInfos map[string]*OAuthInfo - OAuthProviderName string + OAuthInfos map[string]*OAuthInfo + OAuthProviderName string } var OAuthService *OAuther diff --git a/pkg/tsdb/batch.go b/pkg/tsdb/batch.go index bc16ed1e75a..4dee7b31c86 100644 --- a/pkg/tsdb/batch.go +++ b/pkg/tsdb/batch.go @@ -26,7 +26,7 @@ func (bg *Batch) process(context *QueryContext) { if executor == nil { bg.Done = true result := &BatchResult{ - Error: errors.New("Could not find executor for data source type " + bg.Queries[0].DataSource.PluginId), + Error: errors.New("Could not find executor for data source type: " + bg.Queries[0].DataSource.PluginId), QueryResults: make(map[string]*QueryResult), } for _, query := range bg.Queries { diff --git a/pkg/tsdb/graphite/graphite.go b/pkg/tsdb/graphite/graphite.go index 4042702378c..78685d52371 100644 --- a/pkg/tsdb/graphite/graphite.go +++ b/pkg/tsdb/graphite/graphite.go @@ -38,7 +38,7 @@ func init() { } HttpClient = http.Client{ - Timeout: time.Duration(10 * time.Second), + Timeout: time.Duration(15 * time.Second), Transport: tr, } } @@ -54,7 +54,7 @@ func (e *GraphiteExecutor) Execute(queries tsdb.QuerySlice, context *tsdb.QueryC } for _, query := range queries { - formData["target"] = []string{query.Query} + formData["target"] = []string{query.Model.Get("target").MustString()} } if setting.Env == setting.DEV { @@ -79,7 +79,8 @@ func (e *GraphiteExecutor) Execute(queries tsdb.QuerySlice, context *tsdb.QueryC } result.QueryResults = make(map[string]*tsdb.QueryResult) - queryRes := &tsdb.QueryResult{} + queryRes := tsdb.NewQueryResult() + for _, series := range data { queryRes.Series = append(queryRes.Series, &tsdb.TimeSeries{ Name: series.Target, @@ -102,9 +103,9 @@ func (e *GraphiteExecutor) parseResponse(res *http.Response) ([]TargetResponseDT return nil, err } - if res.StatusCode == http.StatusUnauthorized { - glog.Info("Request is Unauthorized", "status", res.Status, "body", string(body)) - return nil, fmt.Errorf("Request is Unauthorized status: %v body: %s", res.Status, string(body)) + if res.StatusCode/100 != 2 { + glog.Info("Request failed", "status", res.Status, "body", string(body)) + return nil, fmt.Errorf("Request failed status: %v", res.Status) } var data []TargetResponseDTO diff --git a/pkg/tsdb/graphite/graphite_test.go b/pkg/tsdb/graphite/graphite_test.go index 59007b43ed5..7a3bba9035b 100644 --- a/pkg/tsdb/graphite/graphite_test.go +++ b/pkg/tsdb/graphite/graphite_test.go @@ -1,23 +1 @@ package graphite - -// func TestGraphite(t *testing.T) { -// -// Convey("When executing graphite query", t, func() { -// executor := NewGraphiteExecutor(&tsdb.DataSourceInfo{ -// Url: "http://localhost:8080", -// }) -// -// queries := tsdb.QuerySlice{ -// &tsdb.Query{Query: "{\"target\": \"apps.backend.*.counters.requests.count\"}"}, -// } -// -// context := tsdb.NewQueryContext(queries, tsdb.TimeRange{}) -// result := executor.Execute(queries, context) -// So(result.Error, ShouldBeNil) -// -// Convey("Should return series", func() { -// So(result.QueryResults, ShouldNotBeEmpty) -// }) -// }) -// -// } diff --git a/pkg/tsdb/graphite/types.go b/pkg/tsdb/graphite/types.go index 085b1fb2b94..8bd13aec4f2 100644 --- a/pkg/tsdb/graphite/types.go +++ b/pkg/tsdb/graphite/types.go @@ -1,6 +1,8 @@ package graphite +import "github.com/grafana/grafana/pkg/tsdb" + type TargetResponseDTO struct { - Target string `json:"target"` - DataPoints [][2]*float64 `json:"datapoints"` + Target string `json:"target"` + DataPoints tsdb.TimeSeriesPoints `json:"datapoints"` } diff --git a/pkg/tsdb/models.go b/pkg/tsdb/models.go index 05a8b13ef84..bbf7bba7ac7 100644 --- a/pkg/tsdb/models.go +++ b/pkg/tsdb/models.go @@ -1,19 +1,31 @@ package tsdb -type TimeRange struct { - From string - To string +import ( + "github.com/grafana/grafana/pkg/components/simplejson" + "gopkg.in/guregu/null.v3" +) + +type Query struct { + RefId string + Model *simplejson.Json + Depends []string + DataSource *DataSourceInfo + Results []*TimeSeries + Exclude bool + MaxDataPoints int64 + IntervalMs int64 } +type QuerySlice []*Query + type Request struct { - TimeRange TimeRange - MaxDataPoints int - Queries QuerySlice + TimeRange *TimeRange + Queries QuerySlice } type Response struct { - BatchTimings []*BatchTiming - Results map[string]*QueryResult + BatchTimings []*BatchTiming `json:"timings"` + Results map[string]*QueryResult `json:"results"` } type DataSourceInfo struct { @@ -40,19 +52,41 @@ type BatchResult struct { } type QueryResult struct { - Error error - RefId string - Series TimeSeriesSlice + Error error `json:"error"` + RefId string `json:"refId"` + Series TimeSeriesSlice `json:"series"` } type TimeSeries struct { - Name string `json:"name"` - Points [][2]*float64 `json:"points"` + Name string `json:"name"` + Points TimeSeriesPoints `json:"points"` } +type TimePoint [2]null.Float +type TimeSeriesPoints []TimePoint type TimeSeriesSlice []*TimeSeries -func NewTimeSeries(name string, points [][2]*float64) *TimeSeries { +func NewQueryResult() *QueryResult { + return &QueryResult{ + Series: make(TimeSeriesSlice, 0), + } +} + +func NewTimePoint(value float64, timestamp float64) TimePoint { + return TimePoint{null.FloatFrom(value), null.FloatFrom(timestamp)} +} + +func NewTimeSeriesPointsFromArgs(values ...float64) TimeSeriesPoints { + points := make(TimeSeriesPoints, 0) + + for i := 0; i < len(values); i += 2 { + points = append(points, NewTimePoint(values[i], values[i+1])) + } + + return points +} + +func NewTimeSeries(name string, points TimeSeriesPoints) *TimeSeries { return &TimeSeries{ Name: name, Points: points, diff --git a/pkg/tsdb/prometheus/prometheus.go b/pkg/tsdb/prometheus/prometheus.go new file mode 100644 index 00000000000..f7e68662efa --- /dev/null +++ b/pkg/tsdb/prometheus/prometheus.go @@ -0,0 +1,161 @@ +package prometheus + +import ( + "fmt" + "net/http" + "regexp" + "strings" + "time" + + "github.com/grafana/grafana/pkg/log" + "github.com/grafana/grafana/pkg/tsdb" + "github.com/prometheus/client_golang/api/prometheus" + pmodel "github.com/prometheus/common/model" + "golang.org/x/net/context" +) + +type PrometheusExecutor struct { + *tsdb.DataSourceInfo +} + +func NewPrometheusExecutor(dsInfo *tsdb.DataSourceInfo) tsdb.Executor { + return &PrometheusExecutor{dsInfo} +} + +var ( + plog log.Logger + HttpClient http.Client +) + +func init() { + plog = log.New("tsdb.prometheus") + tsdb.RegisterExecutor("prometheus", NewPrometheusExecutor) +} + +func (e *PrometheusExecutor) getClient() (prometheus.QueryAPI, error) { + cfg := prometheus.Config{ + Address: e.DataSourceInfo.Url, + } + + client, err := prometheus.New(cfg) + if err != nil { + return nil, err + } + + return prometheus.NewQueryAPI(client), nil +} + +func (e *PrometheusExecutor) Execute(queries tsdb.QuerySlice, queryContext *tsdb.QueryContext) *tsdb.BatchResult { + result := &tsdb.BatchResult{} + + client, err := e.getClient() + if err != nil { + return resultWithError(result, err) + } + + query, err := parseQuery(queries, queryContext) + if err != nil { + return resultWithError(result, err) + } + + timeRange := prometheus.Range{ + Start: query.Start, + End: query.End, + Step: query.Step, + } + + value, err := client.QueryRange(context.Background(), query.Expr, timeRange) + + if err != nil { + return resultWithError(result, err) + } + + queryResult, err := parseResponse(value, query) + if err != nil { + return resultWithError(result, err) + } + result.QueryResults = queryResult + return result +} + +func formatLegend(metric pmodel.Metric, query *PrometheusQuery) string { + reg, _ := regexp.Compile(`\{\{\s*(.+?)\s*\}\}`) + + result := reg.ReplaceAllFunc([]byte(query.LegendFormat), func(in []byte) []byte { + ind := strings.Replace(strings.Replace(string(in), "{{", "", 1), "}}", "", 1) + if val, exists := metric[pmodel.LabelName(ind)]; exists { + return []byte(val) + } + + return in + }) + + return string(result) +} + +func parseQuery(queries tsdb.QuerySlice, queryContext *tsdb.QueryContext) (*PrometheusQuery, error) { + queryModel := queries[0] + + expr, err := queryModel.Model.Get("expr").String() + if err != nil { + return nil, err + } + + step, err := queryModel.Model.Get("step").Int64() + if err != nil { + return nil, err + } + + format, err := queryModel.Model.Get("legendFormat").String() + if err != nil { + return nil, err + } + + start, err := queryContext.TimeRange.ParseFrom() + if err != nil { + return nil, err + } + + end, err := queryContext.TimeRange.ParseTo() + if err != nil { + return nil, err + } + + return &PrometheusQuery{ + Expr: expr, + Step: time.Second * time.Duration(step), + LegendFormat: format, + Start: start, + End: end, + }, nil +} + +func parseResponse(value pmodel.Value, query *PrometheusQuery) (map[string]*tsdb.QueryResult, error) { + queryResults := make(map[string]*tsdb.QueryResult) + queryRes := tsdb.NewQueryResult() + + data, ok := value.(pmodel.Matrix) + if !ok { + return queryResults, fmt.Errorf("Unsupported result format: %s", value.Type().String()) + } + + for _, v := range data { + series := tsdb.TimeSeries{ + Name: formatLegend(v.Metric, query), + } + + for _, k := range v.Values { + series.Points = append(series.Points, tsdb.NewTimePoint(float64(k.Value), float64(k.Timestamp.Unix()*1000))) + } + + queryRes.Series = append(queryRes.Series, &series) + } + + queryResults["A"] = queryRes + return queryResults, nil +} + +func resultWithError(result *tsdb.BatchResult, err error) *tsdb.BatchResult { + result.Error = err + return result +} diff --git a/pkg/tsdb/prometheus/prometheus_test.go b/pkg/tsdb/prometheus/prometheus_test.go new file mode 100644 index 00000000000..f7489ae9afc --- /dev/null +++ b/pkg/tsdb/prometheus/prometheus_test.go @@ -0,0 +1,26 @@ +package prometheus + +import ( + "testing" + + p "github.com/prometheus/common/model" + . "github.com/smartystreets/goconvey/convey" +) + +func TestPrometheus(t *testing.T) { + Convey("Prometheus", t, func() { + + Convey("converting metric name", func() { + metric := map[p.LabelName]p.LabelValue{ + p.LabelName("app"): p.LabelValue("backend"), + p.LabelName("device"): p.LabelValue("mobile"), + } + + query := &PrometheusQuery{ + LegendFormat: "legend {{app}} {{device}} {{broken}}", + } + + So(formatLegend(metric, query), ShouldEqual, "legend backend mobile {{broken}}") + }) + }) +} diff --git a/pkg/tsdb/prometheus/types.go b/pkg/tsdb/prometheus/types.go new file mode 100644 index 00000000000..8ed665d0123 --- /dev/null +++ b/pkg/tsdb/prometheus/types.go @@ -0,0 +1,11 @@ +package prometheus + +import "time" + +type PrometheusQuery struct { + Expr string + Step time.Duration + LegendFormat string + Start time.Time + End time.Time +} diff --git a/pkg/tsdb/query.go b/pkg/tsdb/query.go deleted file mode 100644 index bcead660450..00000000000 --- a/pkg/tsdb/query.go +++ /dev/null @@ -1,12 +0,0 @@ -package tsdb - -type Query struct { - RefId string - Query string - Depends []string - DataSource *DataSourceInfo - Results []*TimeSeries - Exclude bool -} - -type QuerySlice []*Query diff --git a/pkg/tsdb/query_context.go b/pkg/tsdb/query_context.go index a1fc4c9bcb5..db40ba6253c 100644 --- a/pkg/tsdb/query_context.go +++ b/pkg/tsdb/query_context.go @@ -3,7 +3,7 @@ package tsdb import "sync" type QueryContext struct { - TimeRange TimeRange + TimeRange *TimeRange Queries QuerySlice Results map[string]*QueryResult ResultsChan chan *BatchResult @@ -11,7 +11,7 @@ type QueryContext struct { BatchWaits sync.WaitGroup } -func NewQueryContext(queries QuerySlice, timeRange TimeRange) *QueryContext { +func NewQueryContext(queries QuerySlice, timeRange *TimeRange) *QueryContext { return &QueryContext{ TimeRange: timeRange, Queries: queries, diff --git a/pkg/tsdb/testdata/scenarios.go b/pkg/tsdb/testdata/scenarios.go new file mode 100644 index 00000000000..e90b0d4df79 --- /dev/null +++ b/pkg/tsdb/testdata/scenarios.go @@ -0,0 +1,130 @@ +package testdata + +import ( + "math/rand" + "strconv" + "strings" + "time" + + "github.com/grafana/grafana/pkg/log" + "github.com/grafana/grafana/pkg/tsdb" +) + +type ScenarioHandler func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult + +type Scenario struct { + Id string `json:"id"` + Name string `json:"name"` + StringInput string `json:"stringOption"` + Description string `json:"description"` + Handler ScenarioHandler `json:"-"` +} + +var ScenarioRegistry map[string]*Scenario + +func init() { + ScenarioRegistry = make(map[string]*Scenario) + logger := log.New("tsdb.testdata") + + logger.Debug("Initializing TestData Scenario") + + registerScenario(&Scenario{ + Id: "random_walk", + Name: "Random Walk", + + Handler: func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult { + timeWalkerMs := context.TimeRange.GetFromAsMsEpoch() + to := context.TimeRange.GetToAsMsEpoch() + + series := newSeriesForQuery(query) + + points := make(tsdb.TimeSeriesPoints, 0) + walker := rand.Float64() * 100 + + for i := int64(0); i < 10000 && timeWalkerMs < to; i++ { + points = append(points, tsdb.NewTimePoint(walker, float64(timeWalkerMs))) + + walker += rand.Float64() - 0.5 + timeWalkerMs += query.IntervalMs + } + + series.Points = points + + queryRes := tsdb.NewQueryResult() + queryRes.Series = append(queryRes.Series, series) + return queryRes + }, + }) + + registerScenario(&Scenario{ + Id: "no_data_points", + Name: "No Data Points", + Handler: func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult { + return tsdb.NewQueryResult() + }, + }) + + registerScenario(&Scenario{ + Id: "datapoints_outside_range", + Name: "Datapoints Outside Range", + Handler: func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult { + queryRes := tsdb.NewQueryResult() + + series := newSeriesForQuery(query) + outsideTime := context.TimeRange.MustGetFrom().Add(-1*time.Hour).Unix() * 1000 + + series.Points = append(series.Points, tsdb.NewTimePoint(10, float64(outsideTime))) + queryRes.Series = append(queryRes.Series, series) + + return queryRes + }, + }) + + registerScenario(&Scenario{ + Id: "csv_metric_values", + Name: "CSV Metric Values", + StringInput: "1,20,90,30,5,0", + Handler: func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult { + queryRes := tsdb.NewQueryResult() + + stringInput := query.Model.Get("stringInput").MustString() + values := []float64{} + for _, strVal := range strings.Split(stringInput, ",") { + if val, err := strconv.ParseFloat(strVal, 64); err == nil { + values = append(values, val) + } + } + + if len(values) == 0 { + return queryRes + } + + series := newSeriesForQuery(query) + startTime := context.TimeRange.GetFromAsMsEpoch() + endTime := context.TimeRange.GetToAsMsEpoch() + step := (endTime - startTime) / int64(len(values)-1) + + for _, val := range values { + series.Points = append(series.Points, tsdb.NewTimePoint(val, float64(startTime))) + startTime += step + } + + queryRes.Series = append(queryRes.Series, series) + + return queryRes + }, + }) +} + +func registerScenario(scenario *Scenario) { + ScenarioRegistry[scenario.Id] = scenario +} + +func newSeriesForQuery(query *tsdb.Query) *tsdb.TimeSeries { + alias := query.Model.Get("alias").MustString("") + if alias == "" { + alias = query.RefId + "-series" + } + + return &tsdb.TimeSeries{Name: alias} +} diff --git a/pkg/tsdb/testdata/testdata.go b/pkg/tsdb/testdata/testdata.go new file mode 100644 index 00000000000..5b40bb6de5a --- /dev/null +++ b/pkg/tsdb/testdata/testdata.go @@ -0,0 +1,39 @@ +package testdata + +import ( + "github.com/grafana/grafana/pkg/log" + "github.com/grafana/grafana/pkg/tsdb" +) + +type TestDataExecutor struct { + *tsdb.DataSourceInfo + log log.Logger +} + +func NewTestDataExecutor(dsInfo *tsdb.DataSourceInfo) tsdb.Executor { + return &TestDataExecutor{ + DataSourceInfo: dsInfo, + log: log.New("tsdb.testdata"), + } +} + +func init() { + tsdb.RegisterExecutor("grafana-testdata-datasource", NewTestDataExecutor) +} + +func (e *TestDataExecutor) Execute(queries tsdb.QuerySlice, context *tsdb.QueryContext) *tsdb.BatchResult { + result := &tsdb.BatchResult{} + result.QueryResults = make(map[string]*tsdb.QueryResult) + + for _, query := range queries { + scenarioId := query.Model.Get("scenarioId").MustString("random_walk") + if scenario, exist := ScenarioRegistry[scenarioId]; exist { + result.QueryResults[query.RefId] = scenario.Handler(query, context) + result.QueryResults[query.RefId].RefId = query.RefId + } else { + e.log.Error("Scenario not found", "scenarioId", scenarioId) + } + } + + return result +} diff --git a/pkg/tsdb/time_range.go b/pkg/tsdb/time_range.go new file mode 100644 index 00000000000..cf6bc6a5048 --- /dev/null +++ b/pkg/tsdb/time_range.go @@ -0,0 +1,90 @@ +package tsdb + +import ( + "fmt" + "strconv" + "strings" + "time" +) + +func NewTimeRange(from, to string) *TimeRange { + return &TimeRange{ + From: from, + To: to, + Now: time.Now(), + } +} + +type TimeRange struct { + From string + To string + Now time.Time +} + +func (tr *TimeRange) GetFromAsMsEpoch() int64 { + return tr.MustGetFrom().UnixNano() / int64(time.Millisecond) +} + +func (tr *TimeRange) GetToAsMsEpoch() int64 { + return tr.MustGetTo().UnixNano() / int64(time.Millisecond) +} + +func (tr *TimeRange) MustGetFrom() time.Time { + if res, err := tr.ParseFrom(); err != nil { + return time.Unix(0, 0) + } else { + return res + } +} + +func (tr *TimeRange) MustGetTo() time.Time { + if res, err := tr.ParseTo(); err != nil { + return time.Unix(0, 0) + } else { + return res + } +} + +func tryParseUnixMsEpoch(val string) (time.Time, bool) { + if val, err := strconv.ParseInt(val, 10, 64); err == nil { + seconds := val / 1000 + nano := (val - seconds*1000) * 1000000 + return time.Unix(seconds, nano), true + } + return time.Time{}, false +} + +func (tr *TimeRange) ParseFrom() (time.Time, error) { + if res, ok := tryParseUnixMsEpoch(tr.From); ok { + return res, nil + } + + fromRaw := strings.Replace(tr.From, "now-", "", 1) + diff, err := time.ParseDuration("-" + fromRaw) + if err != nil { + return time.Time{}, err + } + + return tr.Now.Add(diff), nil +} + +func (tr *TimeRange) ParseTo() (time.Time, error) { + if tr.To == "now" { + return tr.Now, nil + } else if strings.HasPrefix(tr.To, "now-") { + withoutNow := strings.Replace(tr.To, "now-", "", 1) + + diff, err := time.ParseDuration("-" + withoutNow) + if err != nil { + return time.Time{}, nil + } + + return tr.Now.Add(diff), nil + } + + if res, ok := tryParseUnixMsEpoch(tr.To); ok { + return res, nil + } + + return time.Time{}, fmt.Errorf("cannot parse to value %s", tr.To) +} diff --git a/pkg/tsdb/time_range_test.go b/pkg/tsdb/time_range_test.go new file mode 100644 index 00000000000..5412d0d05f3 --- /dev/null +++ b/pkg/tsdb/time_range_test.go @@ -0,0 +1,95 @@ +package tsdb + +import ( + "testing" + "time" + + . "github.com/smartystreets/goconvey/convey" +) + +func TestTimeRange(t *testing.T) { + Convey("Time range", t, func() { + + now := time.Now() + + Convey("Can parse 5m, now", func() { + tr := TimeRange{ + From: "5m", + To: "now", + Now: now, + } + + Convey("5m ago ", func() { + fiveMinAgo, _ := time.ParseDuration("-5m") + expected := now.Add(fiveMinAgo) + + res, err := tr.ParseFrom() + So(err, ShouldBeNil) + So(res.Unix(), ShouldEqual, expected.Unix()) + }) + + Convey("now ", func() { + res, err := tr.ParseTo() + So(err, ShouldBeNil) + So(res.Unix(), ShouldEqual, now.Unix()) + }) + }) + + Convey("Can parse 5h, now-10m", func() { + tr := TimeRange{ + From: "5h", + To: "now-10m", + Now: now, + } + + Convey("5h ago ", func() { + fiveHourAgo, _ := time.ParseDuration("-5h") + expected := now.Add(fiveHourAgo) + + res, err := tr.ParseFrom() + So(err, ShouldBeNil) + So(res.Unix(), ShouldEqual, expected.Unix()) + }) + + Convey("now-10m ", func() { + fiveMinAgo, _ := time.ParseDuration("-10m") + expected := now.Add(fiveMinAgo) + res, err := tr.ParseTo() + So(err, ShouldBeNil) + So(res.Unix(), ShouldEqual, expected.Unix()) + }) + }) + + Convey("can parse unix epocs", func() { + var err error + tr := TimeRange{ + From: "1474973725473", + To: "1474975757930", + Now: now, + } + + res, err := tr.ParseFrom() + So(err, ShouldBeNil) + So(res.UnixNano()/int64(time.Millisecond), ShouldEqual, 1474973725473) + + res, err = tr.ParseTo() + So(err, ShouldBeNil) + So(res.UnixNano()/int64(time.Millisecond), ShouldEqual, 1474975757930) + }) + + Convey("Cannot parse asdf", func() { + var err error + tr := TimeRange{ + From: "asdf", + To: "asdf", + Now: now, + } + + _, err = tr.ParseFrom() + So(err, ShouldNotBeNil) + + _, err = tr.ParseTo() + So(err, ShouldNotBeNil) + }) + }) +} diff --git a/pkg/tsdb/tsdb_test.go b/pkg/tsdb/tsdb_test.go index 24d84a27c74..429dd01d6ba 100644 --- a/pkg/tsdb/tsdb_test.go +++ b/pkg/tsdb/tsdb_test.go @@ -14,9 +14,9 @@ func TestMetricQuery(t *testing.T) { Convey("Given 3 queries for 2 data sources", func() { request := &Request{ Queries: QuerySlice{ - {RefId: "A", Query: "asd", DataSource: &DataSourceInfo{Id: 1}}, - {RefId: "B", Query: "asd", DataSource: &DataSourceInfo{Id: 1}}, - {RefId: "C", Query: "asd", DataSource: &DataSourceInfo{Id: 2}}, + {RefId: "A", DataSource: &DataSourceInfo{Id: 1}}, + {RefId: "B", DataSource: &DataSourceInfo{Id: 1}}, + {RefId: "C", DataSource: &DataSourceInfo{Id: 2}}, }, } @@ -31,9 +31,9 @@ func TestMetricQuery(t *testing.T) { Convey("Given query 2 depends on query 1", func() { request := &Request{ Queries: QuerySlice{ - {RefId: "A", Query: "asd", DataSource: &DataSourceInfo{Id: 1}}, - {RefId: "B", Query: "asd", DataSource: &DataSourceInfo{Id: 2}}, - {RefId: "C", Query: "#A / #B", DataSource: &DataSourceInfo{Id: 3}, Depends: []string{"A", "B"}}, + {RefId: "A", DataSource: &DataSourceInfo{Id: 1}}, + {RefId: "B", DataSource: &DataSourceInfo{Id: 2}}, + {RefId: "C", DataSource: &DataSourceInfo{Id: 3}, Depends: []string{"A", "B"}}, }, } @@ -55,7 +55,7 @@ func TestMetricQuery(t *testing.T) { Convey("When executing request with one query", t, func() { req := &Request{ Queries: QuerySlice{ - {RefId: "A", Query: "asd", DataSource: &DataSourceInfo{Id: 1, PluginId: "test"}}, + {RefId: "A", DataSource: &DataSourceInfo{Id: 1, PluginId: "test"}}, }, } @@ -74,8 +74,8 @@ func TestMetricQuery(t *testing.T) { Convey("When executing one request with two queries from same data source", t, func() { req := &Request{ Queries: QuerySlice{ - {RefId: "A", Query: "asd", DataSource: &DataSourceInfo{Id: 1, PluginId: "test"}}, - {RefId: "B", Query: "asd", DataSource: &DataSourceInfo{Id: 1, PluginId: "test"}}, + {RefId: "A", DataSource: &DataSourceInfo{Id: 1, PluginId: "test"}}, + {RefId: "B", DataSource: &DataSourceInfo{Id: 1, PluginId: "test"}}, }, } @@ -100,9 +100,9 @@ func TestMetricQuery(t *testing.T) { Convey("When executing one request with three queries from different datasources", t, func() { req := &Request{ Queries: QuerySlice{ - {RefId: "A", Query: "asd", DataSource: &DataSourceInfo{Id: 1, PluginId: "test"}}, - {RefId: "B", Query: "asd", DataSource: &DataSourceInfo{Id: 1, PluginId: "test"}}, - {RefId: "C", Query: "asd", DataSource: &DataSourceInfo{Id: 2, PluginId: "test"}}, + {RefId: "A", DataSource: &DataSourceInfo{Id: 1, PluginId: "test"}}, + {RefId: "B", DataSource: &DataSourceInfo{Id: 1, PluginId: "test"}}, + {RefId: "C", DataSource: &DataSourceInfo{Id: 2, PluginId: "test"}}, }, } @@ -117,7 +117,7 @@ func TestMetricQuery(t *testing.T) { Convey("When query uses data source of unknown type", t, func() { req := &Request{ Queries: QuerySlice{ - {RefId: "A", Query: "asd", DataSource: &DataSourceInfo{Id: 1, PluginId: "asdasdas"}}, + {RefId: "A", DataSource: &DataSourceInfo{Id: 1, PluginId: "asdasdas"}}, }, } @@ -129,10 +129,10 @@ func TestMetricQuery(t *testing.T) { req := &Request{ Queries: QuerySlice{ { - RefId: "A", Query: "asd", DataSource: &DataSourceInfo{Id: 1, PluginId: "test"}, + RefId: "A", DataSource: &DataSourceInfo{Id: 1, PluginId: "test"}, }, { - RefId: "B", Query: "#A / 2", DataSource: &DataSourceInfo{Id: 2, PluginId: "test"}, Depends: []string{"A"}, + RefId: "B", DataSource: &DataSourceInfo{Id: 2, PluginId: "test"}, Depends: []string{"A"}, }, }, } diff --git a/public/app/core/core.ts b/public/app/core/core.ts index 1174e267f4e..d44cbf4dbfb 100644 --- a/public/app/core/core.ts +++ b/public/app/core/core.ts @@ -41,6 +41,7 @@ import 'app/core/routes/routes'; import './filters/filters'; import coreModule from './core_module'; import appEvents from './app_events'; +import colors from './utils/colors'; export { @@ -60,4 +61,5 @@ export { dashboardSelector, queryPartEditorDirective, WizardFlow, + colors, }; diff --git a/public/app/core/directives/metric_segment.js b/public/app/core/directives/metric_segment.js index 9257f3c6a79..381f9110c65 100644 --- a/public/app/core/directives/metric_segment.js +++ b/public/app/core/directives/metric_segment.js @@ -23,10 +23,10 @@ function (_, $, coreModule) { getOptions: "&", onChange: "&", }, - link: function($scope, elem, attrs) { + link: function($scope, elem) { var $input = $(inputTemplate); - var $button = $(attrs.styleMode === 'select' ? selectTemplate : linkTemplate); var segment = $scope.segment; + var $button = $(segment.selectMode ? selectTemplate : linkTemplate); var options = null; var cancelBlur = null; var linkMode = true; @@ -170,6 +170,7 @@ function (_, $, coreModule) { }, link: { pre: function postLink($scope, elem, attrs) { + var cachedOptions; $scope.valueToSegment = function(value) { var option = _.find($scope.options, {value: value}); @@ -177,7 +178,9 @@ function (_, $, coreModule) { cssClass: attrs.cssClass, custom: attrs.custom, value: option ? option.text : value, + selectMode: attrs.selectMode, }; + return uiSegmentSrv.newSegment(segment); }; @@ -188,13 +191,20 @@ function (_, $, coreModule) { }); return $q.when(optionSegments); } else { - return $scope.getOptions(); + return $scope.getOptions().then(function(options) { + cachedOptions = options; + return _.map(options, function(option) { + return uiSegmentSrv.newSegment({value: option.text}); + }); + }); } }; $scope.onSegmentChange = function() { - if ($scope.options) { - var option = _.find($scope.options, {text: $scope.segment.value}); + var options = $scope.options || cachedOptions; + + if (options) { + var option = _.find(options, {text: $scope.segment.value}); if (option && option.value !== $scope.property) { $scope.property = option.value; } else if (attrs.custom !== 'false') { diff --git a/public/app/core/services/backend_srv.ts b/public/app/core/services/backend_srv.ts index fdc2b6cb974..1e620e88216 100644 --- a/public/app/core/services/backend_srv.ts +++ b/public/app/core/services/backend_srv.ts @@ -114,6 +114,10 @@ export class BackendSrv { var requestIsLocal = options.url.indexOf('/') === 0; var firstAttempt = options.retry === 0; + if (requestIsLocal && !options.hasSubUrl && options.retry === 0) { + options.url = config.appSubUrl + options.url; + } + if (requestIsLocal && options.headers && options.headers.Authorization) { options.headers['X-DS-Authorization'] = options.headers.Authorization; delete options.headers.Authorization; diff --git a/public/app/core/services/segment_srv.js b/public/app/core/services/segment_srv.js index d05a2bb011f..9d13e8e27e3 100644 --- a/public/app/core/services/segment_srv.js +++ b/public/app/core/services/segment_srv.js @@ -28,6 +28,7 @@ function (angular, _, coreModule) { this.type = options.type; this.fake = options.fake; this.value = options.value; + this.selectMode = options.selectMode; this.type = options.type; this.expandable = options.expandable; this.html = options.html || $sce.trustAsHtml(templateSrv.highlightVariablesAsHtml(this.value)); diff --git a/public/app/core/time_series2.ts b/public/app/core/time_series2.ts index dfae26fb48b..d672e0dd0dc 100644 --- a/public/app/core/time_series2.ts +++ b/public/app/core/time_series2.ts @@ -31,6 +31,8 @@ export default class TimeSeries { allIsZero: boolean; decimals: number; scaledDecimals: number; + hasMsResolution: boolean; + isOutsideRange: boolean; lines: any; bars: any; @@ -54,6 +56,7 @@ export default class TimeSeries { this.stats = {}; this.legend = true; this.unit = opts.unit; + this.hasMsResolution = this.isMsResolutionNeeded(); } applySeriesOverrides(overrides) { diff --git a/public/app/core/utils/colors.ts b/public/app/core/utils/colors.ts new file mode 100644 index 00000000000..bd774ea02ea --- /dev/null +++ b/public/app/core/utils/colors.ts @@ -0,0 +1,12 @@ + + +export default [ + "#7EB26D","#EAB839","#6ED0E0","#EF843C","#E24D42","#1F78C1","#BA43A9","#705DA0", + "#508642","#CCA300","#447EBC","#C15C17","#890F02","#0A437C","#6D1F62","#584477", + "#B7DBAB","#F4D598","#70DBED","#F9BA8F","#F29191","#82B5D8","#E5A8E2","#AEA2E0", + "#629E51","#E5AC0E","#64B0C8","#E0752D","#BF1B00","#0A50A1","#962D82","#614D93", + "#9AC48A","#F2C96D","#65C5DB","#F9934E","#EA6460","#5195CE","#D683CE","#806EB7", + "#3F6833","#967302","#2F575E","#99440A","#58140C","#052B51","#511749","#3F2B5B", + "#E0F9D7","#FCEACA","#CFFAFF","#F9E2D2","#FCE2DE","#BADFF4","#F9D9F9","#DEDAF7" +]; + diff --git a/public/app/core/utils/kbn.js b/public/app/core/utils/kbn.js index cf80d671d71..a807a249235 100644 --- a/public/app/core/utils/kbn.js +++ b/public/app/core/utils/kbn.js @@ -174,7 +174,10 @@ function($, _, moment) { lowLimitMs = kbn.interval_to_ms(lowLimitInterval); } else { - return userInterval; + return { + intervalMs: kbn.interval_to_ms(userInterval), + interval: userInterval, + }; } } @@ -183,7 +186,10 @@ function($, _, moment) { intervalMs = lowLimitMs; } - return kbn.secondsToHms(intervalMs / 1000); + return { + intervalMs: intervalMs, + interval: kbn.secondsToHms(intervalMs / 1000), + }; }; kbn.describe_interval = function (string) { diff --git a/public/app/features/alerting/alert_tab_ctrl.ts b/public/app/features/alerting/alert_tab_ctrl.ts index 01cacaf9740..ec0386ba5a9 100644 --- a/public/app/features/alerting/alert_tab_ctrl.ts +++ b/public/app/features/alerting/alert_tab_ctrl.ts @@ -227,8 +227,8 @@ export class AlertTabCtrl { var datasourceName = foundTarget.datasource || this.panel.datasource; this.datasourceSrv.get(datasourceName).then(ds => { - if (ds.meta.id !== 'graphite') { - this.error = 'Currently the alerting backend only supports Graphite queries'; + if (!ds.meta.alerting) { + this.error = 'The datasource does not support alerting queries'; } else if (this.templateSrv.variableExists(foundTarget.target)) { this.error = 'Template variables are not supported in alert queries'; } else { diff --git a/public/app/features/dashboard/dashboard_srv.ts b/public/app/features/dashboard/dashboard_srv.ts index 289e3a841f5..92cfa4dc925 100644 --- a/public/app/features/dashboard/dashboard_srv.ts +++ b/public/app/features/dashboard/dashboard_srv.ts @@ -30,6 +30,7 @@ export class DashboardModel { snapshot: any; schemaVersion: number; version: number; + revision: number; links: any; gnetId: any; meta: any; @@ -42,6 +43,7 @@ export class DashboardModel { this.events = new Emitter(); this.id = data.id || null; + this.revision = data.revision; this.title = data.title || 'No Title'; this.autoUpdate = data.autoUpdate; this.description = data.description; diff --git a/public/app/features/dashboard/shareModalCtrl.js b/public/app/features/dashboard/shareModalCtrl.js index 36949f51f5b..d0de3dbb4a9 100644 --- a/public/app/features/dashboard/shareModalCtrl.js +++ b/public/app/features/dashboard/shareModalCtrl.js @@ -8,7 +8,7 @@ function (angular, _, require, config) { var module = angular.module('grafana.controllers'); - module.controller('ShareModalCtrl', function($scope, $rootScope, $location, $timeout, timeSrv, $element, templateSrv, linkSrv) { + module.controller('ShareModalCtrl', function($scope, $rootScope, $location, $timeout, timeSrv, templateSrv, linkSrv) { $scope.options = { forCurrent: true, includeTemplateVars: true, theme: 'current' }; $scope.editor = { index: $scope.tabIndex || 0}; diff --git a/public/app/features/dashboard/submenu/submenu.html b/public/app/features/dashboard/submenu/submenu.html index 2b2f7af6fbe..04bbba2f59d 100644 --- a/public/app/features/dashboard/submenu/submenu.html +++ b/public/app/features/dashboard/submenu/submenu.html @@ -2,7 +2,7 @@ @@ -67,7 +67,7 @@ export class MetricsDsSelectorCtrl { this.current = {name: dsValue + ' not found', value: null}; } - this.dsSegment = uiSegmentSrv.newSegment(this.current.name); + this.dsSegment = uiSegmentSrv.newSegment({value: this.current.name, selectMode: true}); } getOptions() { diff --git a/public/app/features/panel/metrics_panel_ctrl.ts b/public/app/features/panel/metrics_panel_ctrl.ts index 62cece44acf..f6f2d730cd3 100644 --- a/public/app/features/panel/metrics_panel_ctrl.ts +++ b/public/app/features/panel/metrics_panel_ctrl.ts @@ -25,6 +25,7 @@ class MetricsPanelCtrl extends PanelCtrl { range: any; rangeRaw: any; interval: any; + intervalMs: any; resolution: any; timeInfo: any; skipDataOnInit: boolean; @@ -123,11 +124,22 @@ class MetricsPanelCtrl extends PanelCtrl { this.resolution = Math.ceil($(window).width() * (this.panel.span / 12)); } - var panelInterval = this.panel.interval; - var datasourceInterval = (this.datasource || {}).interval; - this.interval = kbn.calculateInterval(this.range, this.resolution, panelInterval || datasourceInterval); + this.calculateInterval(); }; + calculateInterval() { + var intervalOverride = this.panel.interval; + + // if no panel interval check datasource + if (!intervalOverride && this.datasource && this.datasource.interval) { + intervalOverride = this.datasource.interval; + } + + var res = kbn.calculateInterval(this.range, this.resolution, intervalOverride); + this.interval = res.interval; + this.intervalMs = res.intervalMs; + } + applyPanelTimeOverrides() { this.timeInfo = ''; @@ -183,6 +195,7 @@ class MetricsPanelCtrl extends PanelCtrl { range: this.range, rangeRaw: this.rangeRaw, interval: this.interval, + intervalMs: this.intervalMs, targets: this.panel.targets, format: this.panel.renderer === 'png' ? 'png' : 'json', maxDataPoints: this.resolution, diff --git a/public/app/features/playlist/partials/playlist.html b/public/app/features/playlist/partials/playlist.html index 08c31522b7f..6dfb8b2f8e0 100644 --- a/public/app/features/playlist/partials/playlist.html +++ b/public/app/features/playlist/partials/playlist.html @@ -25,7 +25,7 @@
-
+
Available
@@ -72,7 +72,7 @@
-
+
Selected
diff --git a/public/app/features/playlist/playlist_search.ts b/public/app/features/playlist/playlist_search.ts index e00c2cb3a36..b0ccd58eaeb 100644 --- a/public/app/features/playlist/playlist_search.ts +++ b/public/app/features/playlist/playlist_search.ts @@ -14,7 +14,7 @@ export class PlaylistSearchCtrl { /** @ngInject */ constructor(private $scope, private $location, private $timeout, private backendSrv, private contextSrv) { - this.query = { query: '', tag: [], starred: false }; + this.query = {query: '', tag: [], starred: false, limit: 30}; $timeout(() => { this.query.query = ''; diff --git a/public/app/features/plugins/partials/plugin_list.html b/public/app/features/plugins/partials/plugin_list.html index 0870b8727ec..7cfb14d238d 100644 --- a/public/app/features/plugins/partials/plugin_list.html +++ b/public/app/features/plugins/partials/plugin_list.html @@ -3,7 +3,9 @@
diff --git a/public/app/features/templating/constant_variable.ts b/public/app/features/templating/constant_variable.ts index 9fe2acfdfb2..bf31dba96f9 100644 --- a/public/app/features/templating/constant_variable.ts +++ b/public/app/features/templating/constant_variable.ts @@ -18,7 +18,7 @@ export class ConstantVariable implements Variable { current: {}, }; - /** @ngInject */ + /** @ngInject **/ constructor(private model, private variableSrv) { assignModelProperties(this, model, this.defaults); } diff --git a/public/app/features/templating/datasource_variable.ts b/public/app/features/templating/datasource_variable.ts index 96776c31163..d43c0dd486d 100644 --- a/public/app/features/templating/datasource_variable.ts +++ b/public/app/features/templating/datasource_variable.ts @@ -10,6 +10,7 @@ export class DatasourceVariable implements Variable { query: string; options: any; current: any; + refresh: any; defaults = { type: 'datasource', @@ -20,11 +21,13 @@ export class DatasourceVariable implements Variable { regex: '', options: [], query: '', + refresh: 1, }; - /** @ngInject */ + /** @ngInject **/ constructor(private model, private datasourceSrv, private variableSrv) { assignModelProperties(this, model, this.defaults); + this.refresh = 1; } getModel() { diff --git a/public/app/features/templating/editor_ctrl.ts b/public/app/features/templating/editor_ctrl.ts index 3c0410e1316..489625c16f8 100644 --- a/public/app/features/templating/editor_ctrl.ts +++ b/public/app/features/templating/editor_ctrl.ts @@ -6,7 +6,7 @@ import {variableTypes} from './variable'; export class VariableEditorCtrl { - /** @ngInject */ + /** @ngInject **/ constructor(private $scope, private datasourceSrv, private variableSrv, templateSrv) { $scope.variableTypes = variableTypes; $scope.ctrl = {}; diff --git a/public/app/features/templating/interval_variable.ts b/public/app/features/templating/interval_variable.ts index d53e44ae533..a1cfbf324c0 100644 --- a/public/app/features/templating/interval_variable.ts +++ b/public/app/features/templating/interval_variable.ts @@ -28,7 +28,7 @@ export class IntervalVariable implements Variable { auto_count: 30, }; - /** @ngInject */ + /** @ngInject **/ constructor(private model, private timeSrv, private templateSrv, private variableSrv) { assignModelProperties(this, model, this.defaults); this.refresh = 2; @@ -54,8 +54,8 @@ export class IntervalVariable implements Variable { this.options.unshift({ text: 'auto', value: '$__auto_interval' }); } - var interval = kbn.calculateInterval(this.timeSrv.timeRange(), this.auto_count, (this.auto_min ? ">"+this.auto_min : null)); - this.templateSrv.setGrafanaVariable('$__auto_interval', interval); + var res = kbn.calculateInterval(this.timeSrv.timeRange(), this.auto_count, (this.auto_min ? ">"+this.auto_min : null)); + this.templateSrv.setGrafanaVariable('$__auto_interval', res.interval); } updateOptions() { diff --git a/public/app/features/templating/query_variable.ts b/public/app/features/templating/query_variable.ts index 96766d1bbfb..5ee9f0609bc 100644 --- a/public/app/features/templating/query_variable.ts +++ b/public/app/features/templating/query_variable.ts @@ -40,6 +40,7 @@ export class QueryVariable implements Variable { tagValuesQuery: null, }; + /** @ngInject **/ constructor(private model, private datasourceSrv, private templateSrv, private variableSrv, private $q) { // copy model properties to this instance assignModelProperties(this, model, this.defaults); diff --git a/public/app/features/templating/specs/variable_srv_init_specs.ts b/public/app/features/templating/specs/variable_srv_init_specs.ts index 8cac63135ca..533c70dfc25 100644 --- a/public/app/features/templating/specs/variable_srv_init_specs.ts +++ b/public/app/features/templating/specs/variable_srv_init_specs.ts @@ -62,6 +62,7 @@ describe('VariableSrv init', function() { options: [{text: "test", value: "test"}] }]; scenario.urlParams["var-apps"] = "new"; + scenario.metricSources = []; }); it('should update current value', () => { @@ -110,6 +111,30 @@ describe('VariableSrv init', function() { }); }); + describeInitScenario('when datasource variable is initialized', scenario => { + scenario.setup(() => { + scenario.variables = [{ + type: 'datasource', + query: 'graphite', + name: 'test', + current: {value: 'backend4_pee', text: 'backend4_pee'}, + regex: '/pee$/' + } + ]; + scenario.metricSources = [ + {name: 'backend1', meta: {id: 'influx'}}, + {name: 'backend2_pee', meta: {id: 'graphite'}}, + {name: 'backend3', meta: {id: 'graphite'}}, + {name: 'backend4_pee', meta: {id: 'graphite'}}, + ]; + }); + + it('should update current value', function() { + var variable = ctx.variableSrv.variables[0]; + expect(variable.options.length).to.be(2); + }); + }); + describeInitScenario('when template variable is present in url multiple times', scenario => { scenario.setup(() => { scenario.variables = [{ diff --git a/public/app/features/templating/templateSrv.js b/public/app/features/templating/templateSrv.js index f7784e2cb50..dadb8f23a89 100644 --- a/public/app/features/templating/templateSrv.js +++ b/public/app/features/templating/templateSrv.js @@ -43,6 +43,10 @@ function (angular, _, kbn) { } }; + this.variableInitialized = function(variable) { + this._index[variable.name] = variable; + }; + this.getAdhocFilters = function(datasourceName) { var variable = this._adhocVariables[datasourceName]; if (variable) { diff --git a/public/app/features/templating/templateValuesSrv.js b/public/app/features/templating/templateValuesSrv.js deleted file mode 100644 index a3db47fe3a9..00000000000 --- a/public/app/features/templating/templateValuesSrv.js +++ /dev/null @@ -1,417 +0,0 @@ -define([ - 'angular', - 'lodash', - 'jquery', - 'app/core/utils/kbn', -], -function (angular, _, $, kbn) { - 'use strict'; - - var module = angular.module('grafana.services'); - - module.service('templateValuesSrv', function($q, $rootScope, datasourceSrv, $location, templateSrv, timeSrv) { - var self = this; - this.variableLock = {}; - - function getNoneOption() { return { text: 'None', value: '', isNone: true }; } - - // update time variant variables - $rootScope.onAppEvent('refresh', function() { - - // look for interval variables - var intervalVariable = _.find(self.variables, { type: 'interval' }); - if (intervalVariable) { - self.updateAutoInterval(intervalVariable); - } - - // update variables with refresh === 2 - var promises = self.variables - .filter(function(variable) { - return variable.refresh === 2; - }).map(function(variable) { - var previousOptions = variable.options.slice(); - - return self.updateOptions(variable).then(function () { - return self.variableUpdated(variable).then(function () { - // check if current options changed due to refresh - if (angular.toJson(previousOptions) !== angular.toJson(variable.options)) { - $rootScope.appEvent('template-variable-value-updated'); - } - }); - }); - }); - - return $q.all(promises); - - }, $rootScope); - - this.init = function(dashboard) { - this.dashboard = dashboard; - this.variables = dashboard.templating.list; - templateSrv.init(this.variables); - - var queryParams = $location.search(); - var promises = []; - - // use promises to delay processing variables that - // depend on other variables. - this.variableLock = {}; - _.forEach(this.variables, function(variable) { - self.variableLock[variable.name] = $q.defer(); - }); - - for (var i = 0; i < this.variables.length; i++) { - var variable = this.variables[i]; - promises.push(this.processVariable(variable, queryParams)); - } - - return $q.all(promises); - }; - - this.processVariable = function(variable, queryParams) { - var dependencies = []; - var lock = self.variableLock[variable.name]; - - // determine our dependencies. - if (variable.type === "query") { - _.forEach(this.variables, function(v) { - // both query and datasource can contain variable - if (templateSrv.containsVariable(variable.query, v.name) || - templateSrv.containsVariable(variable.datasource, v.name)) { - dependencies.push(self.variableLock[v.name].promise); - } - }); - } - - return $q.all(dependencies).then(function() { - var urlValue = queryParams['var-' + variable.name]; - if (urlValue !== void 0) { - return self.setVariableFromUrl(variable, urlValue).then(lock.resolve); - } - else if (variable.refresh === 1 || variable.refresh === 2) { - return self.updateOptions(variable).then(function() { - if (_.isEmpty(variable.current) && variable.options.length) { - self.setVariableValue(variable, variable.options[0]); - } - lock.resolve(); - }); - } - else if (variable.type === 'interval') { - self.updateAutoInterval(variable); - lock.resolve(); - } else { - lock.resolve(); - } - }).finally(function() { - delete self.variableLock[variable.name]; - }); - }; - - this.setVariableFromUrl = function(variable, urlValue) { - var promise = $q.when(true); - - if (variable.refresh) { - promise = this.updateOptions(variable); - } - - return promise.then(function() { - var option = _.find(variable.options, function(op) { - return op.text === urlValue || op.value === urlValue; - }); - - option = option || { text: urlValue, value: urlValue }; - - self.updateAutoInterval(variable); - return self.setVariableValue(variable, option, true); - }); - }; - - this.updateAutoInterval = function(variable) { - if (!variable.auto) { return; } - - // add auto option if missing - if (variable.options.length && variable.options[0].text !== 'auto') { - variable.options.unshift({ text: 'auto', value: '$__auto_interval' }); - } - - var interval = kbn.calculateInterval(timeSrv.timeRange(), variable.auto_count, (variable.auto_min ? ">"+variable.auto_min : null)); - templateSrv.setGrafanaVariable('$__auto_interval', interval); - }; - - this.setVariableValue = function(variable, option) { - variable.current = angular.copy(option); - - if (_.isArray(variable.current.text)) { - variable.current.text = variable.current.text.join(' + '); - } - - self.selectOptionsForCurrentValue(variable); - templateSrv.updateTemplateData(); - - return this.updateOptionsInChildVariables(variable); - }; - - this.variableUpdated = function(variable) { - templateSrv.updateTemplateData(); - return self.updateOptionsInChildVariables(variable); - }; - - this.updateOptionsInChildVariables = function(updatedVariable) { - // if there is a variable lock ignore cascading update because we are in a boot up scenario - if (self.variableLock[updatedVariable.name]) { - return $q.when(); - } - - var promises = _.map(self.variables, function(otherVariable) { - if (otherVariable === updatedVariable) { - return; - } - if (templateSrv.containsVariable(otherVariable.regex, updatedVariable.name) || - templateSrv.containsVariable(otherVariable.query, updatedVariable.name) || - templateSrv.containsVariable(otherVariable.datasource, updatedVariable.name)) { - return self.updateOptions(otherVariable); - } - }); - - return $q.all(promises); - }; - - this._updateNonQueryVariable = function(variable) { - if (variable.type === 'datasource') { - self.updateDataSourceVariable(variable); - return; - } - - if (variable.type === 'constant') { - variable.options = [{text: variable.query, value: variable.query}]; - return; - } - - if (variable.type === 'adhoc') { - variable.current = {}; - variable.options = []; - return; - } - - // extract options in comma separated string - variable.options = _.map(variable.query.split(/[,]+/), function(text) { - return { text: text.trim(), value: text.trim() }; - }); - - if (variable.type === 'interval') { - self.updateAutoInterval(variable); - return; - } - - if (variable.type === 'custom' && variable.includeAll) { - self.addAllOption(variable); - } - }; - - this.updateDataSourceVariable = function(variable) { - var options = []; - var sources = datasourceSrv.getMetricSources({skipVariables: true}); - var regex; - - if (variable.regex) { - regex = kbn.stringToJsRegex(templateSrv.replace(variable.regex)); - } - - for (var i = 0; i < sources.length; i++) { - var source = sources[i]; - // must match on type - if (source.meta.id !== variable.query) { - continue; - } - - if (regex && !regex.exec(source.name)) { - continue; - } - - options.push({text: source.name, value: source.name}); - } - - if (options.length === 0) { - options.push({text: 'No data sources found', value: ''}); - } - - variable.options = options; - }; - - this.updateOptions = function(variable) { - if (variable.type !== 'query') { - self._updateNonQueryVariable(variable); - return self.validateVariableSelectionState(variable); - } - - return datasourceSrv.get(variable.datasource) - .then(_.partial(this.updateOptionsFromMetricFindQuery, variable)) - .then(_.partial(this.updateTags, variable)) - .then(_.partial(this.validateVariableSelectionState, variable)); - }; - - this.selectOptionsForCurrentValue = function(variable) { - var i, y, value, option; - var selected = []; - - for (i = 0; i < variable.options.length; i++) { - option = variable.options[i]; - option.selected = false; - if (_.isArray(variable.current.value)) { - for (y = 0; y < variable.current.value.length; y++) { - value = variable.current.value[y]; - if (option.value === value) { - option.selected = true; - selected.push(option); - } - } - } else if (option.value === variable.current.value) { - option.selected = true; - selected.push(option); - } - } - - return selected; - }; - - this.validateVariableSelectionState = function(variable) { - if (!variable.current) { - if (!variable.options.length) { return $q.when(); } - return self.setVariableValue(variable, variable.options[0], false); - } - - if (_.isArray(variable.current.value)) { - var selected = self.selectOptionsForCurrentValue(variable); - - // if none pick first - if (selected.length === 0) { - selected = variable.options[0]; - } else { - selected = { - value: _.map(selected, function(val) {return val.value;}), - text: _.map(selected, function(val) {return val.text;}).join(' + '), - }; - } - - return self.setVariableValue(variable, selected, false); - } else { - var currentOption = _.find(variable.options, {text: variable.current.text}); - if (currentOption) { - return self.setVariableValue(variable, currentOption, false); - } else { - if (!variable.options.length) { return $q.when(null); } - return self.setVariableValue(variable, variable.options[0]); - } - } - }; - - this.updateTags = function(variable, datasource) { - if (variable.useTags) { - return datasource.metricFindQuery(variable.tagsQuery).then(function (results) { - variable.tags = []; - for (var i = 0; i < results.length; i++) { - variable.tags.push(results[i].text); - } - return datasource; - }); - } else { - delete variable.tags; - } - - return datasource; - }; - - this.updateOptionsFromMetricFindQuery = function(variable, datasource) { - return datasource.metricFindQuery(variable.query).then(function (results) { - variable.options = self.metricNamesToVariableValues(variable, results); - if (variable.includeAll) { - self.addAllOption(variable); - } - if (!variable.options.length) { - variable.options.push(getNoneOption()); - } - return datasource; - }); - }; - - this.getValuesForTag = function(variable, tagKey) { - return datasourceSrv.get(variable.datasource).then(function(datasource) { - var query = variable.tagValuesQuery.replace('$tag', tagKey); - return datasource.metricFindQuery(query).then(function (results) { - return _.map(results, function(value) { - return value.text; - }); - }); - }); - }; - - this.metricNamesToVariableValues = function(variable, metricNames) { - var regex, options, i, matches; - options = []; - - if (variable.regex) { - regex = kbn.stringToJsRegex(templateSrv.replace(variable.regex)); - } - - for (i = 0; i < metricNames.length; i++) { - var item = metricNames[i]; - var value = item.value || item.text; - var text = item.text || item.value; - - if (_.isNumber(value)) { - value = value.toString(); - } - - if (_.isNumber(text)) { - text = text.toString(); - } - - if (regex) { - matches = regex.exec(value); - if (!matches) { continue; } - if (matches.length > 1) { - value = matches[1]; - text = value; - } - } - - options.push({text: text, value: value}); - } - - options = _.uniq(options, 'value'); - return this.sortVariableValues(options, variable.sort); - }; - - this.addAllOption = function(variable) { - variable.options.unshift({text: 'All', value: "$__all"}); - }; - - this.sortVariableValues = function(options, sortOrder) { - if (sortOrder === 0) { - return options; - } - - var sortType = Math.ceil(sortOrder / 2); - var reverseSort = (sortOrder % 2 === 0); - if (sortType === 1) { - options = _.sortBy(options, 'text'); - } else if (sortType === 2) { - options = _.sortBy(options, function(opt) { - var matches = opt.text.match(/.*?(\d+).*/); - if (!matches) { - return 0; - } else { - return parseInt(matches[1], 10); - } - }); - } - if (reverseSort) { - options = options.reverse(); - } - - return options; - }; - - }); - -}); diff --git a/public/app/features/templating/variable_srv.ts b/public/app/features/templating/variable_srv.ts index b7013d517f4..bb6f4f7cde3 100644 --- a/public/app/features/templating/variable_srv.ts +++ b/public/app/features/templating/variable_srv.ts @@ -8,7 +8,6 @@ import {Variable, variableTypes} from './variable'; export class VariableSrv { dashboard: any; variables: any; - variableLock: any; /** @ngInject */ constructor(private $rootScope, private $q, private $location, private $injector, private templateSrv) { @@ -18,7 +17,6 @@ export class VariableSrv { } init(dashboard) { - this.variableLock = {}; this.dashboard = dashboard; // create working class models representing variables @@ -30,13 +28,15 @@ export class VariableSrv { // init variables for (let variable of this.variables) { - this.variableLock[variable.name] = this.$q.defer(); + variable.initLock = this.$q.defer(); } var queryParams = this.$location.search(); return this.$q.all(this.variables.map(variable => { return this.processVariable(variable, queryParams); - })); + })).then(() => { + this.templateSrv.updateTemplateData(); + }); } onDashboardRefresh() { @@ -59,27 +59,27 @@ export class VariableSrv { processVariable(variable, queryParams) { var dependencies = []; - var lock = this.variableLock[variable.name]; for (let otherVariable of this.variables) { if (variable.dependsOn(otherVariable)) { - dependencies.push(this.variableLock[otherVariable.name].promise); + dependencies.push(otherVariable.initLock.promise); } } return this.$q.all(dependencies).then(() => { var urlValue = queryParams['var-' + variable.name]; if (urlValue !== void 0) { - return variable.setValueFromUrl(urlValue).then(lock.resolve); + return variable.setValueFromUrl(urlValue).then(variable.initLock.resolve); } if (variable.refresh === 1 || variable.refresh === 2) { - return variable.updateOptions().then(lock.resolve); + return variable.updateOptions().then(variable.initLock.resolve); } - lock.resolve(); + variable.initLock.resolve(); }).finally(() => { - delete this.variableLock[variable.name]; + this.templateSrv.variableInitialized(variable); + delete variable.initLock; }); } @@ -111,7 +111,7 @@ export class VariableSrv { variableUpdated(variable) { // if there is a variable lock ignore cascading update because we are in a boot up scenario - if (this.variableLock[variable.name]) { + if (variable.initLock) { return this.$q.when(); } @@ -155,8 +155,7 @@ export class VariableSrv { validateVariableSelectionState(variable) { if (!variable.current) { - if (!variable.options.length) { return this.$q.when(); } - return variable.setValue(variable.options[0]); + variable.current = {}; } if (_.isArray(variable.current.value)) { diff --git a/public/app/plugins/app/testdata/dashboards/alerts.json b/public/app/plugins/app/testdata/dashboards/alerts.json new file mode 100644 index 00000000000..159df0f458b --- /dev/null +++ b/public/app/plugins/app/testdata/dashboards/alerts.json @@ -0,0 +1,287 @@ +{ + "revision": 2, + "title": "TestData - Alerts", + "tags": [ + "grafana-test" + ], + "style": "dark", + "timezone": "browser", + "editable": true, + "hideControls": false, + "sharedCrosshair": false, + "rows": [ + { + "collapse": false, + "editable": true, + "height": 255.625, + "panels": [ + { + "alert": { + "conditions": [ + { + "evaluator": { + "params": [ + 60 + ], + "type": "gt" + }, + "query": { + "params": [ + "A", + "5m", + "now" + ] + }, + "reducer": { + "params": [], + "type": "avg" + }, + "type": "query" + } + ], + "enabled": true, + "frequency": "60s", + "handler": 1, + "name": "TestData - Always OK", + "noDataState": "no_data", + "notifications": [] + }, + "aliasColors": {}, + "bars": false, + "datasource": "Grafana TestData", + "editable": true, + "error": false, + "fill": 1, + "id": 3, + "isNew": true, + "legend": { + "avg": false, + "current": false, + "max": false, + "min": false, + "show": true, + "total": false, + "values": false + }, + "lines": true, + "linewidth": 2, + "links": [], + "nullPointMode": "connected", + "percentage": false, + "pointradius": 5, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "span": 6, + "stack": false, + "steppedLine": false, + "targets": [ + { + "refId": "A", + "scenario": "random_walk", + "scenarioId": "csv_metric_values", + "stringInput": "1,20,90,30,5,0", + "target": "" + } + ], + "thresholds": [ + { + "value": 60, + "op": "gt", + "fill": true, + "line": true, + "colorMode": "critical" + } + ], + "timeFrom": null, + "timeShift": null, + "title": "Always OK", + "tooltip": { + "msResolution": false, + "shared": true, + "sort": 0, + "value_type": "cumulative" + }, + "type": "graph", + "xaxis": { + "mode": "time", + "name": null, + "show": true, + "values": [] + }, + "yaxes": [ + { + "format": "short", + "label": "", + "logBase": 1, + "max": "125", + "min": "0", + "show": true + }, + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + } + ] + }, + { + "alert": { + "conditions": [ + { + "evaluator": { + "params": [ + 177 + ], + "type": "gt" + }, + "query": { + "params": [ + "A", + "5m", + "now" + ] + }, + "reducer": { + "params": [], + "type": "avg" + }, + "type": "query" + } + ], + "enabled": true, + "frequency": "60s", + "handler": 1, + "name": "TestData - Always Alerting", + "noDataState": "no_data", + "notifications": [] + }, + "aliasColors": {}, + "bars": false, + "datasource": "Grafana TestData", + "editable": true, + "error": false, + "fill": 1, + "id": 4, + "isNew": true, + "legend": { + "avg": false, + "current": false, + "max": false, + "min": false, + "show": true, + "total": false, + "values": false + }, + "lines": true, + "linewidth": 2, + "links": [], + "nullPointMode": "connected", + "percentage": false, + "pointradius": 5, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "span": 6, + "stack": false, + "steppedLine": false, + "targets": [ + { + "refId": "A", + "scenario": "random_walk", + "scenarioId": "csv_metric_values", + "stringInput": "200,445,100,150,200,220,190", + "target": "" + } + ], + "thresholds": [ + { + "colorMode": "critical", + "fill": true, + "line": true, + "op": "gt", + "value": 177 + } + ], + "timeFrom": null, + "timeShift": null, + "title": "Always Alerting", + "tooltip": { + "msResolution": false, + "shared": true, + "sort": 0, + "value_type": "cumulative" + }, + "type": "graph", + "xaxis": { + "mode": "time", + "name": null, + "show": true, + "values": [] + }, + "yaxes": [ + { + "format": "short", + "label": "", + "logBase": 1, + "max": null, + "min": "0", + "show": true + }, + { + "format": "short", + "label": "", + "logBase": 1, + "max": null, + "min": null, + "show": true + } + ] + } + ], + "title": "New row" + } + ], + "time": { + "from": "now-6h", + "to": "now" + }, + "timepicker": { + "refresh_intervals": [ + "5s", + "10s", + "30s", + "1m", + "5m", + "15m", + "30m", + "1h", + "2h", + "1d" + ], + "time_options": [ + "5m", + "15m", + "1h", + "6h", + "12h", + "24h", + "2d", + "7d", + "30d" + ] + }, + "templating": { + "list": [] + }, + "annotations": { + "list": [] + }, + "schemaVersion": 13, + "version": 4, + "links": [], + "gnetId": null +} diff --git a/public/app/plugins/app/testdata/dashboards/graph_last_1h.json b/public/app/plugins/app/testdata/dashboards/graph_last_1h.json new file mode 100644 index 00000000000..757dd48e50f --- /dev/null +++ b/public/app/plugins/app/testdata/dashboards/graph_last_1h.json @@ -0,0 +1,483 @@ +{ + "revision": 4, + "title": "TestData - Graph Panel Last 1h", + "tags": [ + "grafana-test" + ], + "style": "dark", + "timezone": "browser", + "editable": true, + "hideControls": false, + "sharedCrosshair": false, + "rows": [ + { + "collapse": false, + "editable": true, + "height": "250px", + "panels": [ + { + "aliasColors": {}, + "bars": false, + "datasource": "Grafana TestData", + "editable": true, + "error": false, + "fill": 1, + "id": 1, + "isNew": true, + "legend": { + "avg": false, + "current": false, + "max": false, + "min": false, + "show": true, + "total": false, + "values": false + }, + "lines": true, + "linewidth": 2, + "links": [], + "nullPointMode": "connected", + "percentage": false, + "pointradius": 5, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "span": 4, + "stack": false, + "steppedLine": false, + "targets": [ + { + "refId": "A", + "scenario": "random_walk", + "scenarioId": "no_data_points", + "target": "" + } + ], + "thresholds": [], + "timeFrom": null, + "timeShift": null, + "title": "No Data Points Warning", + "tooltip": { + "msResolution": false, + "shared": true, + "sort": 0, + "value_type": "cumulative" + }, + "type": "graph", + "xaxis": { + "mode": "time", + "name": null, + "show": true, + "values": [] + }, + "yaxes": [ + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + }, + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + } + ] + }, + { + "aliasColors": {}, + "bars": false, + "datasource": "Grafana TestData", + "editable": true, + "error": false, + "fill": 1, + "id": 2, + "isNew": true, + "legend": { + "avg": false, + "current": false, + "max": false, + "min": false, + "show": true, + "total": false, + "values": false + }, + "lines": true, + "linewidth": 2, + "links": [], + "nullPointMode": "connected", + "percentage": false, + "pointradius": 5, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "span": 4, + "stack": false, + "steppedLine": false, + "targets": [ + { + "refId": "A", + "scenario": "random_walk", + "scenarioId": "datapoints_outside_range", + "target": "" + } + ], + "thresholds": [], + "timeFrom": null, + "timeShift": null, + "title": "Datapoints Outside Range Warning", + "tooltip": { + "msResolution": false, + "shared": true, + "sort": 0, + "value_type": "cumulative" + }, + "type": "graph", + "xaxis": { + "mode": "time", + "name": null, + "show": true, + "values": [] + }, + "yaxes": [ + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + }, + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + } + ] + }, + { + "aliasColors": {}, + "bars": false, + "datasource": "Grafana TestData", + "editable": true, + "error": false, + "fill": 1, + "id": 3, + "isNew": true, + "legend": { + "avg": false, + "current": false, + "max": false, + "min": false, + "show": true, + "total": false, + "values": false + }, + "lines": true, + "linewidth": 2, + "links": [], + "nullPointMode": "connected", + "percentage": false, + "pointradius": 5, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "span": 4, + "stack": false, + "steppedLine": false, + "targets": [ + { + "refId": "A", + "scenario": "random_walk", + "scenarioId": "random_walk", + "target": "" + } + ], + "thresholds": [], + "timeFrom": null, + "timeShift": null, + "title": "Random walk series", + "tooltip": { + "msResolution": false, + "shared": true, + "sort": 0, + "value_type": "cumulative" + }, + "type": "graph", + "xaxis": { + "mode": "time", + "name": null, + "show": true, + "values": [] + }, + "yaxes": [ + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + }, + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + } + ] + } + ], + "title": "New row" + }, + { + "collapse": false, + "editable": true, + "height": "250px", + "panels": [ + { + "aliasColors": {}, + "bars": false, + "datasource": "Grafana TestData", + "editable": true, + "error": false, + "fill": 1, + "id": 4, + "isNew": true, + "legend": { + "avg": false, + "current": false, + "max": false, + "min": false, + "show": true, + "total": false, + "values": false + }, + "lines": true, + "linewidth": 2, + "links": [], + "nullPointMode": "connected", + "percentage": false, + "pointradius": 5, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "span": 8, + "stack": false, + "steppedLine": false, + "targets": [ + { + "refId": "A", + "scenario": "random_walk", + "scenarioId": "random_walk", + "target": "" + } + ], + "thresholds": [], + "timeFrom": "2s", + "timeShift": null, + "title": "Millisecond res x-axis and tooltip", + "tooltip": { + "msResolution": false, + "shared": true, + "sort": 0, + "value_type": "cumulative" + }, + "type": "graph", + "xaxis": { + "mode": "time", + "name": null, + "show": true, + "values": [] + }, + "yaxes": [ + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + }, + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + } + ] + }, + { + "title": "", + "error": false, + "span": 4, + "editable": true, + "type": "text", + "isNew": true, + "id": 6, + "mode": "markdown", + "content": "Just verify that the tooltip time has millisecond resolution ", + "links": [] + } + ], + "title": "New row" + }, + { + "title": "New row", + "height": 336, + "editable": true, + "collapse": false, + "panels": [ + { + "title": "2 yaxis and axis lables", + "error": false, + "span": 7.99561403508772, + "editable": true, + "type": "graph", + "isNew": true, + "id": 5, + "targets": [ + { + "target": "", + "refId": "A", + "scenarioId": "csv_metric_values", + "stringInput": "1,20,90,30,5,0" + }, + { + "target": "", + "refId": "B", + "scenarioId": "csv_metric_values", + "stringInput": "2000,3000,4000,1000,3000,10000" + } + ], + "datasource": "Grafana TestData", + "renderer": "flot", + "yaxes": [ + { + "label": "Perecent", + "show": true, + "logBase": 1, + "min": null, + "max": null, + "format": "percent" + }, + { + "label": "Pressure", + "show": true, + "logBase": 1, + "min": null, + "max": null, + "format": "short" + } + ], + "xaxis": { + "show": true, + "mode": "time", + "name": null, + "values": [] + }, + "lines": true, + "fill": 1, + "linewidth": 2, + "points": false, + "pointradius": 5, + "bars": false, + "stack": false, + "percentage": false, + "legend": { + "show": true, + "values": false, + "min": false, + "max": false, + "current": false, + "total": false, + "avg": false + }, + "nullPointMode": "connected", + "steppedLine": false, + "tooltip": { + "value_type": "cumulative", + "shared": true, + "sort": 0, + "msResolution": false + }, + "timeFrom": null, + "timeShift": null, + "aliasColors": {}, + "seriesOverrides": [ + { + "alias": "B-series", + "yaxis": 2 + } + ], + "thresholds": [], + "links": [] + }, + { + "title": "", + "error": false, + "span": 4.00438596491228, + "editable": true, + "type": "text", + "isNew": true, + "id": 7, + "mode": "markdown", + "content": "Verify that axis labels look ok", + "links": [] + } + ] + } + ], + "time": { + "from": "now-1h", + "to": "now" + }, + "timepicker": { + "refresh_intervals": [ + "5s", + "10s", + "30s", + "1m", + "5m", + "15m", + "30m", + "1h", + "2h", + "1d" + ], + "time_options": [ + "5m", + "15m", + "1h", + "6h", + "12h", + "24h", + "2d", + "7d", + "30d" + ] + }, + "templating": { + "list": [] + }, + "annotations": { + "list": [] + }, + "refresh": false, + "schemaVersion": 13, + "version": 3, + "links": [], + "gnetId": null +} diff --git a/public/app/plugins/app/testdata/datasource/datasource.ts b/public/app/plugins/app/testdata/datasource/datasource.ts new file mode 100644 index 00000000000..e0846d99ab6 --- /dev/null +++ b/public/app/plugins/app/testdata/datasource/datasource.ts @@ -0,0 +1,62 @@ +/// + +import _ from 'lodash'; +import angular from 'angular'; + +class TestDataDatasource { + + /** @ngInject */ + constructor(private backendSrv, private $q) {} + + query(options) { + var queries = _.filter(options.targets, item => { + return item.hide !== true; + }).map(item => { + return { + refId: item.refId, + scenarioId: item.scenarioId, + intervalMs: options.intervalMs, + maxDataPoints: options.maxDataPoints, + stringInput: item.stringInput, + jsonInput: angular.fromJson(item.jsonInput), + }; + }); + + if (queries.length === 0) { + return this.$q.when({data: []}); + } + + return this.backendSrv.post('/api/tsdb/query', { + from: options.range.from.valueOf().toString(), + to: options.range.to.valueOf().toString(), + queries: queries, + }).then(res => { + var data = []; + + if (res.results) { + _.forEach(res.results, queryRes => { + for (let series of queryRes.series) { + data.push({ + target: series.name, + datapoints: series.points + }); + } + }); + } + + return {data: data}; + }); + } + + annotationQuery(options) { + return this.backendSrv.get('/api/annotations', { + from: options.range.from.valueOf(), + to: options.range.to.valueOf(), + limit: options.limit, + type: options.type, + }); + } + +} + +export {TestDataDatasource}; diff --git a/public/app/plugins/app/testdata/datasource/module.ts b/public/app/plugins/app/testdata/datasource/module.ts new file mode 100644 index 00000000000..309b7443836 --- /dev/null +++ b/public/app/plugins/app/testdata/datasource/module.ts @@ -0,0 +1,22 @@ +/// + +import {TestDataDatasource} from './datasource'; +import {TestDataQueryCtrl} from './query_ctrl'; + +class TestDataAnnotationsQueryCtrl { + annotation: any; + + constructor() { + } + + static template = '

test data

'; +} + + +export { + TestDataDatasource, + TestDataDatasource as Datasource, + TestDataQueryCtrl as QueryCtrl, + TestDataAnnotationsQueryCtrl as AnnotationsQueryCtrl, +}; + diff --git a/public/app/plugins/app/testdata/datasource/plugin.json b/public/app/plugins/app/testdata/datasource/plugin.json new file mode 100644 index 00000000000..4d66253d78e --- /dev/null +++ b/public/app/plugins/app/testdata/datasource/plugin.json @@ -0,0 +1,20 @@ +{ + "type": "datasource", + "name": "Grafana TestDataDB", + "id": "grafana-testdata-datasource", + + "metrics": true, + "alerting": true, + "annotations": true, + + "info": { + "author": { + "name": "Grafana Project", + "url": "http://grafana.org" + }, + "logos": { + "small": "", + "large": "" + } + } +} diff --git a/public/app/plugins/app/testdata/datasource/query_ctrl.ts b/public/app/plugins/app/testdata/datasource/query_ctrl.ts new file mode 100644 index 00000000000..6b0ad93f26c --- /dev/null +++ b/public/app/plugins/app/testdata/datasource/query_ctrl.ts @@ -0,0 +1,35 @@ +/// + +import _ from 'lodash'; + +import {TestDataDatasource} from './datasource'; +import {QueryCtrl} from 'app/plugins/sdk'; + +export class TestDataQueryCtrl extends QueryCtrl { + static templateUrl = 'partials/query.editor.html'; + + scenarioList: any; + scenario: any; + + /** @ngInject **/ + constructor($scope, $injector, private backendSrv) { + super($scope, $injector); + + this.target.scenarioId = this.target.scenarioId || 'random_walk'; + this.scenarioList = []; + } + + $onInit() { + return this.backendSrv.get('/api/tsdb/testdata/scenarios').then(res => { + this.scenarioList = res; + this.scenario = _.find(this.scenarioList, {id: this.target.scenarioId}); + }); + } + + scenarioChanged() { + this.scenario = _.find(this.scenarioList, {id: this.target.scenarioId}); + this.target.stringInput = this.scenario.stringInput; + this.refresh(); + } +} + diff --git a/public/app/plugins/app/testdata/module.ts b/public/app/plugins/app/testdata/module.ts new file mode 100644 index 00000000000..dee1679637a --- /dev/null +++ b/public/app/plugins/app/testdata/module.ts @@ -0,0 +1,36 @@ +/// + +export class ConfigCtrl { + static template = ''; + + appEditCtrl: any; + + constructor(private backendSrv) { + this.appEditCtrl.setPreUpdateHook(this.initDatasource.bind(this)); + } + + initDatasource() { + return this.backendSrv.get('/api/datasources').then(res => { + var found = false; + for (let ds of res) { + if (ds.type === "grafana-testdata-datasource") { + found = true; + } + } + + if (!found) { + var dsInstance = { + name: 'Grafana TestData', + type: 'grafana-testdata-datasource', + access: 'direct', + jsonData: {} + }; + + return this.backendSrv.post('/api/datasources', dsInstance); + } + + return Promise.resolve(); + }); + } +} + diff --git a/public/app/plugins/app/testdata/partials/query.editor.html b/public/app/plugins/app/testdata/partials/query.editor.html new file mode 100644 index 00000000000..a39582d5397 --- /dev/null +++ b/public/app/plugins/app/testdata/partials/query.editor.html @@ -0,0 +1,22 @@ + +
+
+ +
+ +
+
+
+ + +
+
+ + +
+
+
+
+
+
+ diff --git a/public/app/plugins/app/testdata/plugin.json b/public/app/plugins/app/testdata/plugin.json new file mode 100644 index 00000000000..6742ad04ecb --- /dev/null +++ b/public/app/plugins/app/testdata/plugin.json @@ -0,0 +1,32 @@ +{ + "type": "app", + "name": "Grafana TestData", + "id": "testdata", + + "info": { + "description": "Grafana test data app", + "author": { + "name": "Grafana Project", + "url": "http://grafana.org" + }, + "version": "1.0.13", + "updated": "2016-09-26" + }, + + "includes": [ + { + "type": "dashboard", + "name": "TestData - Graph Last 1h", + "path": "dashboards/graph_last_1h.json" + }, + { + "type": "dashboard", + "name": "TestData - Alerts", + "path": "dashboards/alerts.json" + } + ], + + "dependencies": { + "grafanaVersion": "4.x.x" + } +} diff --git a/public/app/plugins/datasource/elasticsearch/datasource.js b/public/app/plugins/datasource/elasticsearch/datasource.js index 0889c078082..9f98a794da4 100644 --- a/public/app/plugins/datasource/elasticsearch/datasource.js +++ b/public/app/plugins/datasource/elasticsearch/datasource.js @@ -216,11 +216,6 @@ function (angular, _, moment, kbn, ElasticQueryBuilder, IndexPattern, ElasticRes }); }; - function escapeForJson(value) { - var luceneQuery = JSON.stringify(value); - return luceneQuery.substr(1, luceneQuery.length - 2); - } - this.getFields = function(query) { return this._get('/_mapping').then(function(result) { var typeMap = { @@ -285,7 +280,6 @@ function (angular, _, moment, kbn, ElasticQueryBuilder, IndexPattern, ElasticRes var header = this.getQueryHeader('count', range.from, range.to); var esQuery = angular.toJson(this.queryBuilder.getTermsQuery(queryDef)); - esQuery = esQuery.replace("$lucene_query", escapeForJson(queryDef.query)); esQuery = esQuery.replace(/\$timeFrom/g, range.from.valueOf()); esQuery = esQuery.replace(/\$timeTo/g, range.to.valueOf()); esQuery = header + '\n' + esQuery + '\n'; diff --git a/public/app/plugins/datasource/elasticsearch/query_builder.js b/public/app/plugins/datasource/elasticsearch/query_builder.js index d256c6d1438..4be5404a950 100644 --- a/public/app/plugins/datasource/elasticsearch/query_builder.js +++ b/public/app/plugins/datasource/elasticsearch/query_builder.js @@ -221,12 +221,6 @@ function (queryDef) { "size": 0, "query": { "filtered": { - "query": { - "query_string": { - "analyze_wildcard": true, - "query": '$lucene_query', - } - }, "filter": { "bool": { "must": [{"range": this.getRangeFilter()}] @@ -235,6 +229,16 @@ function (queryDef) { } } }; + + if (queryDef.query) { + query.query.filtered.query = { + "query_string": { + "analyze_wildcard": true, + "query": queryDef.query, + } + }; + } + query.aggs = { "1": { "terms": { diff --git a/public/app/plugins/datasource/grafana/datasource.ts b/public/app/plugins/datasource/grafana/datasource.ts index 58799778acd..3ae030e4423 100644 --- a/public/app/plugins/datasource/grafana/datasource.ts +++ b/public/app/plugins/datasource/grafana/datasource.ts @@ -9,6 +9,8 @@ class GrafanaDatasource { return this.backendSrv.get('/api/metrics/test', { from: options.range.from.valueOf(), to: options.range.to.valueOf(), + scenario: 'random_walk', + interval: options.intervalMs, maxDataPoints: options.maxDataPoints }); } diff --git a/public/app/plugins/datasource/graphite/lexer.ts b/public/app/plugins/datasource/graphite/lexer.ts index 1835921d40b..760f0fe2573 100644 --- a/public/app/plugins/datasource/graphite/lexer.ts +++ b/public/app/plugins/datasource/graphite/lexer.ts @@ -134,13 +134,7 @@ for (var i = 0; i < 128; i++) { i >= 97 && i <= 122; // a-z } -var identifierPartTable = []; - -for (var i2 = 0; i2 < 128; i2++) { - identifierPartTable[i2] = - identifierStartTable[i2] || // $, _, A-Z, a-z - i2 >= 48 && i2 <= 57; // 0-9 -} +var identifierPartTable = identifierStartTable; export function Lexer(expression) { this.input = expression; @@ -423,256 +417,260 @@ Lexer.prototype = { if (char === '-') { value += char; index += 1; - char = this.peek(index); - } + char = this.peek(index); + } - // Numbers must start either with a decimal digit or a point. - if (char !== "." && !isDecimalDigit(char)) { - return null; - } + // Numbers must start either with a decimal digit or a point. + if (char !== "." && !isDecimalDigit(char)) { + return null; + } - if (char !== ".") { - value += this.peek(index); - index += 1; - char = this.peek(index); + if (char !== ".") { + value += this.peek(index); + index += 1; + char = this.peek(index); - if (value === "0") { - // Base-16 numbers. - if (char === "x" || char === "X") { - index += 1; - value += char; - - while (index < length) { - char = this.peek(index); - if (!isHexDigit(char)) { - break; - } - value += char; - index += 1; - } - - if (value.length <= 2) { // 0x - return { - type: 'number', - value: value, - isMalformed: true, - pos: this.char - }; - } - - if (index < length) { - char = this.peek(index); - if (isIdentifierStart(char)) { - return null; - } - } - - return { - type: 'number', - value: value, - base: 16, - isMalformed: false, - pos: this.char - }; - } - - // Base-8 numbers. - if (isOctalDigit(char)) { - index += 1; - value += char; - bad = false; - - while (index < length) { - char = this.peek(index); - - // Numbers like '019' (note the 9) are not valid octals - // but we still parse them and mark as malformed. - - if (isDecimalDigit(char)) { - bad = true; - } else if (!isOctalDigit(char)) { - break; - } - value += char; - index += 1; - } - - if (index < length) { - char = this.peek(index); - if (isIdentifierStart(char)) { - return null; - } - } - - return { - type: 'number', - value: value, - base: 8, - isMalformed: false - }; - } - - // Decimal numbers that start with '0' such as '09' are illegal - // but we still parse them and return as malformed. - - if (isDecimalDigit(char)) { - index += 1; - value += char; - } - } - - while (index < length) { - char = this.peek(index); - if (!isDecimalDigit(char)) { - break; - } + if (value === "0") { + // Base-16 numbers. + if (char === "x" || char === "X") { + index += 1; value += char; - index += 1; - } - } - - // Decimal digits. - - if (char === ".") { - value += char; - index += 1; - - while (index < length) { - char = this.peek(index); - if (!isDecimalDigit(char)) { - break; - } - value += char; - index += 1; - } - } - - // Exponent part. - - if (char === "e" || char === "E") { - value += char; - index += 1; - char = this.peek(index); - - if (char === "+" || char === "-") { - value += this.peek(index); - index += 1; - } - - char = this.peek(index); - if (isDecimalDigit(char)) { - value += char; - index += 1; while (index < length) { char = this.peek(index); - if (!isDecimalDigit(char)) { + if (!isHexDigit(char)) { break; } value += char; index += 1; } - } else { - return null; - } - } - if (index < length) { - char = this.peek(index); - if (!this.isPunctuator(char)) { - return null; - } - } + if (value.length <= 2) { // 0x + return { + type: 'number', + value: value, + isMalformed: true, + pos: this.char + }; + } - return { - type: 'number', - value: value, - base: 10, - pos: this.char, - isMalformed: !isFinite(+value) - }; - }, + if (index < length) { + char = this.peek(index); + if (isIdentifierStart(char)) { + return null; + } + } - isPunctuator: function (ch1) { - switch (ch1) { - case ".": - case "(": - case ")": - case ",": - case "{": - case "}": - return true; - } - - return false; - }, - - scanPunctuator: function () { - var ch1 = this.peek(); - - if (this.isPunctuator(ch1)) { - return { - type: ch1, - value: ch1, - pos: this.char - }; - } - - return null; - }, - - /* - * Extract a string out of the next sequence of characters and/or - * lines or return 'null' if its not possible. Since strings can - * span across multiple lines this method has to move the char - * pointer. - * - * This method recognizes pseudo-multiline JavaScript strings: - * - * var str = "hello\ - * world"; - */ - scanStringLiteral: function () { - /*jshint loopfunc:true */ - var quote = this.peek(); - - // String must start with a quote. - if (quote !== "\"" && quote !== "'") { - return null; - } - - var value = ""; - - this.skip(); - - while (this.peek() !== quote) { - if (this.peek() === "") { // End Of Line return { - type: 'string', + type: 'number', value: value, - isUnclosed: true, - quote: quote, + base: 16, + isMalformed: false, pos: this.char }; } - var char = this.peek(); - var jump = 1; // A length of a jump, after we're done - // parsing this character. + // Base-8 numbers. + if (isOctalDigit(char)) { + index += 1; + value += char; + bad = false; - value += char; - this.skip(jump); + while (index < length) { + char = this.peek(index); + + // Numbers like '019' (note the 9) are not valid octals + // but we still parse them and mark as malformed. + + if (isDecimalDigit(char)) { + bad = true; + } if (!isOctalDigit(char)) { + // if the char is a non punctuator then its not a valid number + if (!this.isPunctuator(char)) { + return null; + } + break; + } + value += char; + index += 1; + } + + if (index < length) { + char = this.peek(index); + if (isIdentifierStart(char)) { + return null; + } + } + + return { + type: 'number', + value: value, + base: 8, + isMalformed: bad + }; + } + + // Decimal numbers that start with '0' such as '09' are illegal + // but we still parse them and return as malformed. + + if (isDecimalDigit(char)) { + index += 1; + value += char; + } } - this.skip(); + while (index < length) { + char = this.peek(index); + if (!isDecimalDigit(char)) { + break; + } + value += char; + index += 1; + } + } + + // Decimal digits. + + if (char === ".") { + value += char; + index += 1; + + while (index < length) { + char = this.peek(index); + if (!isDecimalDigit(char)) { + break; + } + value += char; + index += 1; + } + } + + // Exponent part. + + if (char === "e" || char === "E") { + value += char; + index += 1; + char = this.peek(index); + + if (char === "+" || char === "-") { + value += this.peek(index); + index += 1; + } + + char = this.peek(index); + if (isDecimalDigit(char)) { + value += char; + index += 1; + + while (index < length) { + char = this.peek(index); + if (!isDecimalDigit(char)) { + break; + } + value += char; + index += 1; + } + } else { + return null; + } + } + + if (index < length) { + char = this.peek(index); + if (!this.isPunctuator(char)) { + return null; + } + } + + return { + type: 'number', + value: value, + base: 10, + pos: this.char, + isMalformed: !isFinite(+value) + }; + }, + + isPunctuator: function (ch1) { + switch (ch1) { + case ".": + case "(": + case ")": + case ",": + case "{": + case "}": + return true; + } + + return false; + }, + + scanPunctuator: function () { + var ch1 = this.peek(); + + if (this.isPunctuator(ch1)) { return { - type: 'string', - value: value, - isUnclosed: false, - quote: quote, + type: ch1, + value: ch1, pos: this.char }; - }, + } - }; + return null; + }, + + /* + * Extract a string out of the next sequence of characters and/or + * lines or return 'null' if its not possible. Since strings can + * span across multiple lines this method has to move the char + * pointer. + * + * This method recognizes pseudo-multiline JavaScript strings: + * + * var str = "hello\ + * world"; + */ + scanStringLiteral: function () { + /*jshint loopfunc:true */ + var quote = this.peek(); + + // String must start with a quote. + if (quote !== "\"" && quote !== "'") { + return null; + } + + var value = ""; + + this.skip(); + + while (this.peek() !== quote) { + if (this.peek() === "") { // End Of Line + return { + type: 'string', + value: value, + isUnclosed: true, + quote: quote, + pos: this.char + }; + } + + var char = this.peek(); + var jump = 1; // A length of a jump, after we're done + // parsing this character. + + value += char; + this.skip(jump); + } + + this.skip(); + return { + type: 'string', + value: value, + isUnclosed: false, + quote: quote, + pos: this.char + }; + }, + +}; diff --git a/public/app/plugins/datasource/graphite/parser.ts b/public/app/plugins/datasource/graphite/parser.ts index bfafdda7815..5c5c618d227 100644 --- a/public/app/plugins/datasource/graphite/parser.ts +++ b/public/app/plugins/datasource/graphite/parser.ts @@ -100,10 +100,7 @@ Parser.prototype = { }, metricExpression: function() { - if (!this.match('templateStart') && - !this.match('identifier') && - !this.match('number') && - !this.match('{')) { + if (!this.match('templateStart') && !this.match('identifier') && !this.match('number') && !this.match('{')) { return null; } diff --git a/public/app/plugins/datasource/graphite/plugin.json b/public/app/plugins/datasource/graphite/plugin.json index c47c49e05fa..76242fd883c 100644 --- a/public/app/plugins/datasource/graphite/plugin.json +++ b/public/app/plugins/datasource/graphite/plugin.json @@ -8,6 +8,7 @@ ], "metrics": true, + "alerting": true, "annotations": true, "info": { @@ -20,4 +21,4 @@ "large": "img/graphite_logo.png" } } -} \ No newline at end of file +} diff --git a/public/app/plugins/datasource/graphite/specs/lexer_specs.ts b/public/app/plugins/datasource/graphite/specs/lexer_specs.ts index 1f4be736536..e68c17099fe 100644 --- a/public/app/plugins/datasource/graphite/specs/lexer_specs.ts +++ b/public/app/plugins/datasource/graphite/specs/lexer_specs.ts @@ -62,6 +62,14 @@ describe('when lexing graphite expression', function() { expect(tokens[4].type).to.be('identifier'); }); + it('should tokenize metric expression with segment that start with number', function() { + var lexer = new Lexer("metric.001-server"); + var tokens = lexer.tokenize(); + expect(tokens[0].type).to.be('identifier'); + expect(tokens[2].type).to.be('identifier'); + expect(tokens.length).to.be(3); + }); + it('should tokenize func call with numbered metric and number arg', function() { var lexer = new Lexer("scale(metric.10, 15)"); var tokens = lexer.tokenize(); diff --git a/public/app/plugins/datasource/prometheus/datasource.ts b/public/app/plugins/datasource/prometheus/datasource.ts index eedf9c1badd..941591791ef 100644 --- a/public/app/plugins/datasource/prometheus/datasource.ts +++ b/public/app/plugins/datasource/prometheus/datasource.ts @@ -113,7 +113,6 @@ export function PrometheusDatasource(instanceSettings, $q, backendSrv, templateS throw response.error; } delete self.lastErrors.query; - _.each(response.data.data.result, function(metricData) { result.push(self.transformMetricData(metricData, activeTargets[index], start, end)); }); @@ -124,6 +123,10 @@ export function PrometheusDatasource(instanceSettings, $q, backendSrv, templateS }; this.performTimeSeriesQuery = function(query, start, end) { + if (start > end) { + throw { message: 'Invalid time range' }; + } + var url = '/api/v1/query_range?query=' + encodeURIComponent(query.expr) + '&start=' + start + '&end=' + end + '&step=' + query.step; return this._request('GET', url, query.requestId); }; diff --git a/public/app/plugins/datasource/prometheus/plugin.json b/public/app/plugins/datasource/prometheus/plugin.json index f39f8691661..54fd1129b8b 100644 --- a/public/app/plugins/datasource/prometheus/plugin.json +++ b/public/app/plugins/datasource/prometheus/plugin.json @@ -8,6 +8,7 @@ ], "metrics": true, + "alerting": true, "annotations": true, "info": { diff --git a/public/app/plugins/panel/graph/tab_axes.html b/public/app/plugins/panel/graph/axes_editor.html similarity index 58% rename from public/app/plugins/panel/graph/tab_axes.html rename to public/app/plugins/panel/graph/axes_editor.html index e85e266dd7d..4c89b5b8b73 100644 --- a/public/app/plugins/panel/graph/tab_axes.html +++ b/public/app/plugins/panel/graph/axes_editor.html @@ -40,6 +40,32 @@
X-Axis
+ +
+ +
+ +
+
+ + +
+ + +
+ + +
+ + +
+ + +
+ + +
+
diff --git a/public/app/plugins/panel/graph/axes_editor.ts b/public/app/plugins/panel/graph/axes_editor.ts new file mode 100644 index 00000000000..502af07ff79 --- /dev/null +++ b/public/app/plugins/panel/graph/axes_editor.ts @@ -0,0 +1,85 @@ +/// + +import kbn from 'app/core/utils/kbn'; + +export class AxesEditorCtrl { + panel: any; + panelCtrl: any; + unitFormats: any; + logScales: any; + xAxisModes: any; + xAxisStatOptions: any; + xNameSegment: any; + + /** @ngInject **/ + constructor(private $scope, private $q) { + this.panelCtrl = $scope.ctrl; + this.panel = this.panelCtrl.panel; + $scope.ctrl = this; + + this.unitFormats = kbn.getUnitFormats(); + + this.logScales = { + 'linear': 1, + 'log (base 2)': 2, + 'log (base 10)': 10, + 'log (base 32)': 32, + 'log (base 1024)': 1024 + }; + + this.xAxisModes = { + 'Time': 'time', + 'Series': 'series', + // 'Data field': 'field', + }; + + this.xAxisStatOptions = [ + {text: 'Avg', value: 'avg'}, + {text: 'Min', value: 'min'}, + {text: 'Max', value: 'min'}, + {text: 'Total', value: 'total'}, + {text: 'Count', value: 'count'}, + ]; + + if (this.panel.xaxis.mode === 'custom') { + if (!this.panel.xaxis.name) { + this.panel.xaxis.name = 'specify field'; + } + } + } + + setUnitFormat(axis, subItem) { + axis.format = subItem.value; + this.panelCtrl.render(); + } + + render() { + this.panelCtrl.render(); + } + + xAxisOptionChanged() { + this.panelCtrl.processor.setPanelDefaultsForNewXAxisMode(); + this.panelCtrl.onDataReceived(this.panelCtrl.dataList); + } + + getDataFieldNames(onlyNumbers) { + var props = this.panelCtrl.processor.getDataFieldNames(this.panelCtrl.dataList, onlyNumbers); + var items = props.map(prop => { + return {text: prop, value: prop}; + }); + + return this.$q.when(items); + } + +} + +/** @ngInject **/ +export function axesEditorComponent() { + 'use strict'; + return { + restrict: 'E', + scope: true, + templateUrl: 'public/app/plugins/panel/graph/axes_editor.html', + controller: AxesEditorCtrl, + }; +} diff --git a/public/app/plugins/panel/graph/data_processor.ts b/public/app/plugins/panel/graph/data_processor.ts new file mode 100644 index 00000000000..6233ac345c9 --- /dev/null +++ b/public/app/plugins/panel/graph/data_processor.ts @@ -0,0 +1,192 @@ +/// + +import kbn from 'app/core/utils/kbn'; +import _ from 'lodash'; +import moment from 'moment'; +import TimeSeries from 'app/core/time_series2'; +import {colors} from 'app/core/core'; + +export class DataProcessor { + + constructor(private panel) { + } + + getSeriesList(options) { + if (!options.dataList || options.dataList.length === 0) { + return []; + } + + // auto detect xaxis mode + var firstItem; + if (options.dataList && options.dataList.length > 0) { + firstItem = options.dataList[0]; + let autoDetectMode = this.getAutoDetectXAxisMode(firstItem); + if (this.panel.xaxis.mode !== autoDetectMode) { + this.panel.xaxis.mode = autoDetectMode; + this.setPanelDefaultsForNewXAxisMode(); + } + } + + switch (this.panel.xaxis.mode) { + case 'series': + case 'time': { + return options.dataList.map((item, index) => { + return this.timeSeriesHandler(item, index, options); + }); + } + case 'field': { + return this.customHandler(firstItem); + } + } + } + + getAutoDetectXAxisMode(firstItem) { + switch (firstItem.type) { + case 'docs': return 'field'; + case 'table': return 'field'; + default: { + if (this.panel.xaxis.mode === 'series') { + return 'series'; + } + return 'time'; + } + } + } + + setPanelDefaultsForNewXAxisMode() { + switch (this.panel.xaxis.mode) { + case 'time': { + this.panel.bars = false; + this.panel.lines = true; + this.panel.points = false; + this.panel.legend.show = true; + this.panel.tooltip.shared = true; + this.panel.xaxis.values = []; + break; + } + case 'series': { + this.panel.bars = true; + this.panel.lines = false; + this.panel.points = false; + this.panel.stack = false; + this.panel.legend.show = false; + this.panel.tooltip.shared = false; + this.panel.xaxis.values = ['total']; + break; + } + } + } + + timeSeriesHandler(seriesData, index, options) { + var datapoints = seriesData.datapoints || []; + var alias = seriesData.target; + + var colorIndex = index % colors.length; + var color = this.panel.aliasColors[alias] || colors[colorIndex]; + + var series = new TimeSeries({datapoints: datapoints, alias: alias, color: color, unit: seriesData.unit}); + + if (datapoints && datapoints.length > 0) { + var last = datapoints[datapoints.length - 1][1]; + var from = options.range.from; + if (last - from < -10000) { + series.isOutsideRange = true; + } + } + + return series; + } + + customHandler(dataItem) { + let nameField = this.panel.xaxis.name; + if (!nameField) { + throw {message: 'No field name specified to use for x-axis, check your axes settings'}; + } + return []; + } + + validateXAxisSeriesValue() { + switch (this.panel.xaxis.mode) { + case 'series': { + if (this.panel.xaxis.values.length === 0) { + this.panel.xaxis.values = ['total']; + return; + } + + var validOptions = this.getXAxisValueOptions({}); + var found = _.find(validOptions, {value: this.panel.xaxis.values[0]}); + if (!found) { + this.panel.xaxis.values = ['total']; + } + return; + } + } + } + + getDataFieldNames(dataList, onlyNumbers) { + if (dataList.length === 0) { + return []; + } + + let fields = []; + var firstItem = dataList[0]; + if (firstItem.type === 'docs'){ + if (firstItem.datapoints.length === 0) { + return []; + } + + let fieldParts = []; + + function getPropertiesRecursive(obj) { + _.forEach(obj, (value, key) => { + if (_.isObject(value)) { + fieldParts.push(key); + getPropertiesRecursive(value); + } else { + if (!onlyNumbers || _.isNumber(value)) { + let field = fieldParts.concat(key).join('.'); + fields.push(field); + } + } + }); + fieldParts.pop(); + } + + getPropertiesRecursive(firstItem.datapoints[0]); + return fields; + } + } + + getXAxisValueOptions(options) { + switch (this.panel.xaxis.mode) { + case 'time': { + return []; + } + case 'series': { + return [ + {text: 'Avg', value: 'avg'}, + {text: 'Min', value: 'min'}, + {text: 'Max', value: 'min'}, + {text: 'Total', value: 'total'}, + {text: 'Count', value: 'count'}, + ]; + } + } + } + + pluckDeep(obj: any, property: string) { + let propertyParts = property.split('.'); + let value = obj; + for (let i = 0; i < propertyParts.length; ++i) { + if (value[propertyParts[i]]) { + value = value[propertyParts[i]]; + } else { + return undefined; + } + } + return value; + } + +} + + diff --git a/public/app/plugins/panel/graph/graph.js b/public/app/plugins/panel/graph/graph.js deleted file mode 100755 index 2e7353b6048..00000000000 --- a/public/app/plugins/panel/graph/graph.js +++ /dev/null @@ -1,533 +0,0 @@ -define([ - 'angular', - 'jquery', - 'moment', - 'lodash', - 'app/core/utils/kbn', - './graph_tooltip', - './threshold_manager', - 'jquery.flot', - 'jquery.flot.selection', - 'jquery.flot.time', - 'jquery.flot.stack', - 'jquery.flot.stackpercent', - 'jquery.flot.fillbelow', - 'jquery.flot.crosshair', - './jquery.flot.events', -], -function (angular, $, moment, _, kbn, GraphTooltip, thresholdManExports) { - 'use strict'; - - var module = angular.module('grafana.directives'); - var labelWidthCache = {}; - - module.directive('grafanaGraph', function($rootScope, timeSrv) { - return { - restrict: 'A', - template: '
', - link: function(scope, elem) { - var ctrl = scope.ctrl; - var dashboard = ctrl.dashboard; - var panel = ctrl.panel; - var data, annotations; - var sortedSeries; - var legendSideLastValue = null; - var rootScope = scope.$root; - var panelWidth = 0; - var thresholdManager = new thresholdManExports.ThresholdManager(ctrl); - - rootScope.onAppEvent('setCrosshair', function(event, info) { - // do not need to to this if event is from this panel - if (info.scope === scope) { - return; - } - - if(dashboard.sharedCrosshair) { - var plot = elem.data().plot; - if (plot) { - plot.setCrosshair({ x: info.pos.x, y: info.pos.y }); - } - } - }, scope); - - rootScope.onAppEvent('clearCrosshair', function() { - var plot = elem.data().plot; - if (plot) { - plot.clearCrosshair(); - } - }, scope); - - // Receive render events - ctrl.events.on('render', function(renderData) { - data = renderData || data; - if (!data) { - return; - } - annotations = data.annotations || annotations; - render_panel(); - }); - - function getLegendHeight(panelHeight) { - if (!panel.legend.show || panel.legend.rightSide) { - return 0; - } - - if (panel.legend.alignAsTable) { - var legendSeries = _.filter(data, function(series) { - return series.hideFromLegend(panel.legend) === false; - }); - var total = 23 + (21 * legendSeries.length); - return Math.min(total, Math.floor(panelHeight/2)); - } else { - return 26; - } - } - - function setElementHeight() { - try { - var height = ctrl.height - getLegendHeight(ctrl.height); - elem.css('height', height + 'px'); - - return true; - } catch(e) { // IE throws errors sometimes - console.log(e); - return false; - } - } - - function shouldAbortRender() { - if (!data) { - return true; - } - - if (!setElementHeight()) { return true; } - - if (panelWidth === 0) { - return true; - } - } - - function getLabelWidth(text, elem) { - var labelWidth = labelWidthCache[text]; - - if (!labelWidth) { - labelWidth = labelWidthCache[text] = elem.width(); - } - - return labelWidth; - } - - function drawHook(plot) { - // Update legend values - var yaxis = plot.getYAxes(); - for (var i = 0; i < data.length; i++) { - var series = data[i]; - var axis = yaxis[series.yaxis - 1]; - var formater = kbn.valueFormats[panel.yaxes[series.yaxis - 1].format]; - - // decimal override - if (_.isNumber(panel.decimals)) { - series.updateLegendValues(formater, panel.decimals, null); - } else { - // auto decimals - // legend and tooltip gets one more decimal precision - // than graph legend ticks - var tickDecimals = (axis.tickDecimals || -1) + 1; - series.updateLegendValues(formater, tickDecimals, axis.scaledDecimals + 2); - } - - if(!rootScope.$$phase) { scope.$digest(); } - } - - // add left axis labels - if (panel.yaxes[0].label) { - var yaxisLabel = $("
") - .text(panel.yaxes[0].label) - .appendTo(elem); - - yaxisLabel[0].style.marginTop = (getLabelWidth(panel.yaxes[0].label, yaxisLabel) / 2) + 'px'; - } - - // add right axis labels - if (panel.yaxes[1].label) { - var rightLabel = $("
") - .text(panel.yaxes[1].label) - .appendTo(elem); - - rightLabel[0].style.marginTop = (getLabelWidth(panel.yaxes[1].label, rightLabel) / 2) + 'px'; - } - - thresholdManager.draw(plot); - } - - function processOffsetHook(plot, gridMargin) { - var left = panel.yaxes[0]; - var right = panel.yaxes[1]; - if (left.show && left.label) { gridMargin.left = 20; } - if (right.show && right.label) { gridMargin.right = 20; } - } - - // Function for rendering panel - function render_panel() { - panelWidth = elem.width(); - - if (shouldAbortRender()) { - return; - } - - // give space to alert editing - thresholdManager.prepare(elem, data); - - var stack = panel.stack ? true : null; - - // Populate element - var options = { - hooks: { - draw: [drawHook], - processOffset: [processOffsetHook], - }, - legend: { show: false }, - series: { - stackpercent: panel.stack ? panel.percentage : false, - stack: panel.percentage ? null : stack, - lines: { - show: panel.lines, - zero: false, - fill: translateFillOption(panel.fill), - lineWidth: panel.linewidth, - steps: panel.steppedLine - }, - bars: { - show: panel.bars, - fill: 1, - barWidth: 1, - zero: false, - lineWidth: 0 - }, - points: { - show: panel.points, - fill: 1, - fillColor: false, - radius: panel.points ? panel.pointradius : 2 - }, - shadowSize: 0 - }, - yaxes: [], - xaxis: {}, - grid: { - minBorderMargin: 0, - markings: [], - backgroundColor: null, - borderWidth: 0, - hoverable: true, - color: '#c8c8c8', - margin: { left: 0, right: 0 }, - }, - selection: { - mode: "x", - color: '#666' - }, - crosshair: { - mode: panel.tooltip.shared || dashboard.sharedCrosshair ? "x" : null - } - }; - - for (var i = 0; i < data.length; i++) { - var series = data[i]; - series.data = series.getFlotPairs(series.nullPointMode || panel.nullPointMode); - - // if hidden remove points and disable stack - if (ctrl.hiddenSeries[series.alias]) { - series.data = []; - series.stack = false; - } - } - - if (data.length && data[0].stats.timeStep) { - options.series.bars.barWidth = data[0].stats.timeStep / 1.5; - } - - addTimeAxis(options); - thresholdManager.addPlotOptions(options, panel); - addAnnotations(options); - configureAxisOptions(data, options); - - sortedSeries = _.sortBy(data, function(series) { return series.zindex; }); - - function callPlot(incrementRenderCounter) { - try { - $.plot(elem, sortedSeries, options); - if (ctrl.renderError) { - delete ctrl.error; - delete ctrl.inspector; - } - } catch (e) { - console.log('flotcharts error', e); - ctrl.error = e.message || "Render Error"; - ctrl.renderError = true; - ctrl.inspector = {error: e}; - } - - if (incrementRenderCounter) { - ctrl.renderingCompleted(); - } - } - - if (shouldDelayDraw(panel)) { - // temp fix for legends on the side, need to render twice to get dimensions right - callPlot(false); - setTimeout(function() { callPlot(true); }, 50); - legendSideLastValue = panel.legend.rightSide; - } - else { - callPlot(true); - } - } - - function translateFillOption(fill) { - return fill === 0 ? 0.001 : fill/10; - } - - function shouldDelayDraw(panel) { - if (panel.legend.rightSide) { - return true; - } - if (legendSideLastValue !== null && panel.legend.rightSide !== legendSideLastValue) { - return true; - } - } - - function addTimeAxis(options) { - var ticks = panelWidth / 100; - var min = _.isUndefined(ctrl.range.from) ? null : ctrl.range.from.valueOf(); - var max = _.isUndefined(ctrl.range.to) ? null : ctrl.range.to.valueOf(); - - options.xaxis = { - timezone: dashboard.getTimezone(), - show: panel.xaxis.show, - mode: "time", - min: min, - max: max, - label: "Datetime", - ticks: ticks, - timeformat: time_format(ticks, min, max), - }; - } - - function addAnnotations(options) { - if(!annotations || annotations.length === 0) { - return; - } - - var types = {}; - for (var i = 0; i < annotations.length; i++) { - var item = annotations[i]; - - if (!types[item.source.name]) { - types[item.source.name] = { - color: item.source.iconColor, - position: 'BOTTOM', - markerSize: 5, - }; - } - } - - options.events = { - levels: _.keys(types).length + 1, - data: annotations, - types: types, - }; - } - - //Override min/max to provide more flexible autoscaling - function autoscaleSpanOverride(yaxis, data, options) { - var expr; - if (yaxis.min != null && data != null) { - expr = parseThresholdExpr(yaxis.min); - options.min = autoscaleYAxisMin(expr, data.stats); - } - if (yaxis.max != null && data != null) { - expr = parseThresholdExpr(yaxis.max); - options.max = autoscaleYAxisMax(expr, data.stats); - } - } - - function parseThresholdExpr(expr) { - var match, operator, value, precision; - expr = String(expr); - match = expr.match(/\s*([<=>~]*)\s*(\-?\d+(\.\d+)?)/); - if (match) { - operator = match[1]; - value = parseFloat(match[2]); - //Precision based on input - precision = match[3] ? match[3].length - 1 : 0; - return { - operator: operator, - value: value, - precision: precision - }; - } else { - return undefined; - } - } - - function autoscaleYAxisMax(expr, dataStats) { - var operator = expr.operator, - value = expr.value, - precision = expr.precision; - if (operator === ">") { - return dataStats.max < value ? value : null; - } else if (operator === "<") { - return dataStats.max > value ? value : null; - } else if (operator === "~") { - return kbn.roundValue(dataStats.avg + value, precision); - } else if (operator === "=") { - return kbn.roundValue(dataStats.current + value, precision); - } else if (!operator && !isNaN(value)) { - return kbn.roundValue(value, precision); - } else { - return null; - } - } - - function autoscaleYAxisMin(expr, dataStats) { - var operator = expr.operator, - value = expr.value, - precision = expr.precision; - if (operator === ">") { - return dataStats.min < value ? value : null; - } else if (operator === "<") { - return dataStats.min > value ? value : null; - } else if (operator === "~") { - return kbn.roundValue(dataStats.avg - value, precision); - } else if (operator === "=") { - return kbn.roundValue(dataStats.current - value, precision); - } else if (!operator && !isNaN(value)) { - return kbn.roundValue(value, precision); - } else { - return null; - } - } - - function configureAxisOptions(data, options) { - var defaults = { - position: 'left', - show: panel.yaxes[0].show, - min: panel.yaxes[0].min, - index: 1, - logBase: panel.yaxes[0].logBase || 1, - max: panel.percentage && panel.stack ? 100 : panel.yaxes[0].max, - }; - - autoscaleSpanOverride(panel.yaxes[0], data[0], defaults); - options.yaxes.push(defaults); - - if (_.find(data, {yaxis: 2})) { - var secondY = _.clone(defaults); - secondY.index = 2, - secondY.show = panel.yaxes[1].show; - secondY.logBase = panel.yaxes[1].logBase || 1, - secondY.position = 'right'; - secondY.min = panel.yaxes[1].min; - secondY.max = panel.percentage && panel.stack ? 100 : panel.yaxes[1].max; - autoscaleSpanOverride(panel.yaxes[1], data[1], secondY); - options.yaxes.push(secondY); - - applyLogScale(options.yaxes[1], data); - configureAxisMode(options.yaxes[1], panel.percentage && panel.stack ? "percent" : panel.yaxes[1].format); - } - - applyLogScale(options.yaxes[0], data); - configureAxisMode(options.yaxes[0], panel.percentage && panel.stack ? "percent" : panel.yaxes[0].format); - } - - function applyLogScale(axis, data) { - if (axis.logBase === 1) { - return; - } - - var series, i; - var max = axis.max; - - if (max === null) { - for (i = 0; i < data.length; i++) { - series = data[i]; - if (series.yaxis === axis.index) { - if (max < series.stats.max) { - max = series.stats.max; - } - } - } - if (max === void 0) { - max = Number.MAX_VALUE; - } - } - - axis.min = axis.min !== null ? axis.min : 0; - axis.ticks = [0, 1]; - var nextTick = 1; - - while (true) { - nextTick = nextTick * axis.logBase; - axis.ticks.push(nextTick); - if (nextTick > max) { - break; - } - } - - if (axis.logBase === 10) { - axis.transform = function(v) { return Math.log(v+0.1); }; - axis.inverseTransform = function (v) { return Math.pow(10,v); }; - } else { - axis.transform = function(v) { return Math.log(v+0.1) / Math.log(axis.logBase); }; - axis.inverseTransform = function (v) { return Math.pow(axis.logBase,v); }; - } - } - - function configureAxisMode(axis, format) { - axis.tickFormatter = function(val, axis) { - return kbn.valueFormats[format](val, axis.tickDecimals, axis.scaledDecimals); - }; - } - - function time_format(ticks, min, max) { - if (min && max && ticks) { - var range = max - min; - var secPerTick = (range/ticks) / 1000; - var oneDay = 86400000; - var oneYear = 31536000000; - - if (secPerTick <= 45) { - return "%H:%M:%S"; - } - if (secPerTick <= 7200 || range <= oneDay) { - return "%H:%M"; - } - if (secPerTick <= 80000) { - return "%m/%d %H:%M"; - } - if (secPerTick <= 2419200 || range <= oneYear) { - return "%m/%d"; - } - return "%Y-%m"; - } - - return "%H:%M"; - } - - new GraphTooltip(elem, dashboard, scope, function() { - return sortedSeries; - }); - - elem.bind("plotselected", function (event, ranges) { - scope.$apply(function() { - timeSrv.setTime({ - from : moment.utc(ranges.xaxis.from), - to : moment.utc(ranges.xaxis.to), - }); - }); - }); - } - }; - }); -}); diff --git a/public/app/plugins/panel/graph/graph.ts b/public/app/plugins/panel/graph/graph.ts new file mode 100755 index 00000000000..36c50f4e703 --- /dev/null +++ b/public/app/plugins/panel/graph/graph.ts @@ -0,0 +1,521 @@ +/// + +import 'jquery.flot'; +import 'jquery.flot.selection'; +import 'jquery.flot.time'; +import 'jquery.flot.stack'; +import 'jquery.flot.stackpercent'; +import 'jquery.flot.fillbelow'; +import 'jquery.flot.crosshair'; +import './jquery.flot.events'; + +import angular from 'angular'; +import $ from 'jquery'; +import moment from 'moment'; +import _ from 'lodash'; +import kbn from 'app/core/utils/kbn'; +import GraphTooltip from './graph_tooltip'; +import {ThresholdManager} from './threshold_manager'; + +var module = angular.module('grafana.directives'); +var labelWidthCache = {}; + +module.directive('grafanaGraph', function($rootScope, timeSrv) { + return { + restrict: 'A', + template: '', + link: function(scope, elem) { + var ctrl = scope.ctrl; + var dashboard = ctrl.dashboard; + var panel = ctrl.panel; + var data, annotations; + var sortedSeries; + var legendSideLastValue = null; + var rootScope = scope.$root; + var panelWidth = 0; + var thresholdManager = new ThresholdManager(ctrl); + + rootScope.onAppEvent('setCrosshair', function(event, info) { + // do not need to to this if event is from this panel + if (info.scope === scope) { + return; + } + + if (dashboard.sharedCrosshair) { + var plot = elem.data().plot; + if (plot) { + plot.setCrosshair({ x: info.pos.x, y: info.pos.y }); + } + } + }, scope); + + rootScope.onAppEvent('clearCrosshair', function() { + var plot = elem.data().plot; + if (plot) { + plot.clearCrosshair(); + } + }, scope); + + // Receive render events + ctrl.events.on('render', function(renderData) { + data = renderData || data; + if (!data) { + return; + } + annotations = data.annotations || annotations; + render_panel(); + }); + + function getLegendHeight(panelHeight) { + if (!panel.legend.show || panel.legend.rightSide) { + return 0; + } + + if (panel.legend.alignAsTable) { + var legendSeries = _.filter(data, function(series) { + return series.hideFromLegend(panel.legend) === false; + }); + var total = 23 + (21 * legendSeries.length); + return Math.min(total, Math.floor(panelHeight/2)); + } else { + return 26; + } + } + + function setElementHeight() { + try { + var height = ctrl.height - getLegendHeight(ctrl.height); + elem.css('height', height + 'px'); + + return true; + } catch (e) { // IE throws errors sometimes + console.log(e); + return false; + } + } + + function shouldAbortRender() { + if (!data) { + return true; + } + + if (!setElementHeight()) { return true; } + + if (panelWidth === 0) { + return true; + } + } + + function getLabelWidth(text, elem) { + var labelWidth = labelWidthCache[text]; + + if (!labelWidth) { + labelWidth = labelWidthCache[text] = elem.width(); + } + + return labelWidth; + } + + function drawHook(plot) { + // Update legend values + var yaxis = plot.getYAxes(); + for (var i = 0; i < data.length; i++) { + var series = data[i]; + var axis = yaxis[series.yaxis - 1]; + var formater = kbn.valueFormats[panel.yaxes[series.yaxis - 1].format]; + + // decimal override + if (_.isNumber(panel.decimals)) { + series.updateLegendValues(formater, panel.decimals, null); + } else { + // auto decimals + // legend and tooltip gets one more decimal precision + // than graph legend ticks + var tickDecimals = (axis.tickDecimals || -1) + 1; + series.updateLegendValues(formater, tickDecimals, axis.scaledDecimals + 2); + } + + if (!rootScope.$$phase) { scope.$digest(); } + } + + // add left axis labels + if (panel.yaxes[0].label) { + var yaxisLabel = $("
") + .text(panel.yaxes[0].label) + .appendTo(elem); + + yaxisLabel[0].style.marginTop = (getLabelWidth(panel.yaxes[0].label, yaxisLabel) / 2) + 'px'; + } + + // add right axis labels + if (panel.yaxes[1].label) { + var rightLabel = $("
") + .text(panel.yaxes[1].label) + .appendTo(elem); + + rightLabel[0].style.marginTop = (getLabelWidth(panel.yaxes[1].label, rightLabel) / 2) + 'px'; + } + + thresholdManager.draw(plot); + } + + function processOffsetHook(plot, gridMargin) { + var left = panel.yaxes[0]; + var right = panel.yaxes[1]; + if (left.show && left.label) { gridMargin.left = 20; } + if (right.show && right.label) { gridMargin.right = 20; } + + // apply y-axis min/max options + var yaxis = plot.getYAxes(); + for (var i = 0; i < yaxis.length; i++) { + var axis = yaxis[i]; + var panelOptions = panel.yaxes[i]; + axis.options.max = panelOptions.max; + axis.options.min = panelOptions.min; + } + } + + // Function for rendering panel + function render_panel() { + panelWidth = elem.width(); + + if (shouldAbortRender()) { + return; + } + + // give space to alert editing + thresholdManager.prepare(elem, data); + + var stack = panel.stack ? true : null; + + // Populate element + var options: any = { + hooks: { + draw: [drawHook], + processOffset: [processOffsetHook], + }, + legend: { show: false }, + series: { + stackpercent: panel.stack ? panel.percentage : false, + stack: panel.percentage ? null : stack, + lines: { + show: panel.lines, + zero: false, + fill: translateFillOption(panel.fill), + lineWidth: panel.linewidth, + steps: panel.steppedLine + }, + bars: { + show: panel.bars, + fill: 1, + barWidth: 1, + zero: false, + lineWidth: 0 + }, + points: { + show: panel.points, + fill: 1, + fillColor: false, + radius: panel.points ? panel.pointradius : 2 + }, + shadowSize: 0 + }, + yaxes: [], + xaxis: {}, + grid: { + minBorderMargin: 0, + markings: [], + backgroundColor: null, + borderWidth: 0, + hoverable: true, + color: '#c8c8c8', + margin: { left: 0, right: 0 }, + }, + selection: { + mode: "x", + color: '#666' + }, + crosshair: { + mode: panel.tooltip.shared || dashboard.sharedCrosshair ? "x" : null + } + }; + + for (let i = 0; i < data.length; i++) { + var series = data[i]; + series.data = series.getFlotPairs(series.nullPointMode || panel.nullPointMode); + + // if hidden remove points and disable stack + if (ctrl.hiddenSeries[series.alias]) { + series.data = []; + series.stack = false; + } + } + + switch (panel.xaxis.mode) { + case 'series': { + options.series.bars.barWidth = 0.7; + options.series.bars.align = 'center'; + + for (let i = 0; i < data.length; i++) { + var series = data[i]; + series.data = [[i + 1, series.stats[panel.xaxis.values[0]]]]; + } + + addXSeriesAxis(options); + break; + } + case 'table': { + options.series.bars.barWidth = 0.7; + options.series.bars.align = 'center'; + addXTableAxis(options); + break; + } + default: { + if (data.length && data[0].stats.timeStep) { + options.series.bars.barWidth = data[0].stats.timeStep / 1.5; + } + addTimeAxis(options); + break; + } + } + + thresholdManager.addPlotOptions(options, panel); + addAnnotations(options); + configureAxisOptions(data, options); + + sortedSeries = _.sortBy(data, function(series) { return series.zindex; }); + + function callPlot(incrementRenderCounter) { + try { + $.plot(elem, sortedSeries, options); + if (ctrl.renderError) { + delete ctrl.error; + delete ctrl.inspector; + } + } catch (e) { + console.log('flotcharts error', e); + ctrl.error = e.message || "Render Error"; + ctrl.renderError = true; + ctrl.inspector = {error: e}; + } + + if (incrementRenderCounter) { + ctrl.renderingCompleted(); + } + } + + if (shouldDelayDraw(panel)) { + // temp fix for legends on the side, need to render twice to get dimensions right + callPlot(false); + setTimeout(function() { callPlot(true); }, 50); + legendSideLastValue = panel.legend.rightSide; + } else { + callPlot(true); + } + } + + function translateFillOption(fill) { + return fill === 0 ? 0.001 : fill/10; + } + + function shouldDelayDraw(panel) { + if (panel.legend.rightSide) { + return true; + } + if (legendSideLastValue !== null && panel.legend.rightSide !== legendSideLastValue) { + return true; + } + } + + function addTimeAxis(options) { + var ticks = panelWidth / 100; + var min = _.isUndefined(ctrl.range.from) ? null : ctrl.range.from.valueOf(); + var max = _.isUndefined(ctrl.range.to) ? null : ctrl.range.to.valueOf(); + + options.xaxis = { + timezone: dashboard.getTimezone(), + show: panel.xaxis.show, + mode: "time", + min: min, + max: max, + label: "Datetime", + ticks: ticks, + timeformat: time_format(ticks, min, max), + }; + } + + function addXSeriesAxis(options) { + var ticks = _.map(data, function(series, index) { + return [index + 1, series.alias]; + }); + + options.xaxis = { + timezone: dashboard.getTimezone(), + show: panel.xaxis.show, + mode: null, + min: 0, + max: ticks.length + 1, + label: "Datetime", + ticks: ticks + }; + } + + function addXTableAxis(options) { + var ticks = _.map(data, function(series, seriesIndex) { + return _.map(series.datapoints, function(point, pointIndex) { + var tickIndex = seriesIndex * series.datapoints.length + pointIndex; + return [tickIndex + 1, point[1]]; + }); + }); + ticks = _.flatten(ticks, true); + + options.xaxis = { + timezone: dashboard.getTimezone(), + show: panel.xaxis.show, + mode: null, + min: 0, + max: ticks.length + 1, + label: "Datetime", + ticks: ticks + }; + } + + function addAnnotations(options) { + if (!annotations || annotations.length === 0) { + return; + } + + var types = {}; + for (var i = 0; i < annotations.length; i++) { + var item = annotations[i]; + + if (!types[item.source.name]) { + types[item.source.name] = { + color: item.source.iconColor, + position: 'BOTTOM', + markerSize: 5, + }; + } + } + + options.events = { + levels: _.keys(types).length + 1, + data: annotations, + types: types, + }; + } + + function configureAxisOptions(data, options) { + var defaults = { + position: 'left', + show: panel.yaxes[0].show, + index: 1, + logBase: panel.yaxes[0].logBase || 1, + max: 100, // correct later + }; + + options.yaxes.push(defaults); + + if (_.find(data, {yaxis: 2})) { + var secondY = _.clone(defaults); + secondY.index = 2; + secondY.show = panel.yaxes[1].show; + secondY.logBase = panel.yaxes[1].logBase || 1; + secondY.position = 'right'; + options.yaxes.push(secondY); + configureAxisMode(options.yaxes[1], panel.percentage && panel.stack ? "percent" : panel.yaxes[1].format); + } + + applyLogScale(options.yaxes[0], data); + configureAxisMode(options.yaxes[0], panel.percentage && panel.stack ? "percent" : panel.yaxes[0].format); + } + + function applyLogScale(axis, data) { + if (axis.logBase === 1) { + return; + } + + var series, i; + var max = axis.max; + + if (max === null) { + for (i = 0; i < data.length; i++) { + series = data[i]; + if (series.yaxis === axis.index) { + if (max < series.stats.max) { + max = series.stats.max; + } + } + } + if (max === void 0) { + max = Number.MAX_VALUE; + } + } + + axis.min = axis.min !== null ? axis.min : 0; + axis.ticks = [0, 1]; + var nextTick = 1; + + while (true) { + nextTick = nextTick * axis.logBase; + axis.ticks.push(nextTick); + if (nextTick > max) { + break; + } + } + + if (axis.logBase === 10) { + axis.transform = function(v) { return Math.log(v+0.1); }; + axis.inverseTransform = function (v) { return Math.pow(10,v); }; + } else { + axis.transform = function(v) { return Math.log(v+0.1) / Math.log(axis.logBase); }; + axis.inverseTransform = function (v) { return Math.pow(axis.logBase,v); }; + } + } + + function configureAxisMode(axis, format) { + axis.tickFormatter = function(val, axis) { + return kbn.valueFormats[format](val, axis.tickDecimals, axis.scaledDecimals); + }; + } + + function time_format(ticks, min, max) { + if (min && max && ticks) { + var range = max - min; + var secPerTick = (range/ticks) / 1000; + var oneDay = 86400000; + var oneYear = 31536000000; + + if (secPerTick <= 45) { + return "%H:%M:%S"; + } + if (secPerTick <= 7200 || range <= oneDay) { + return "%H:%M"; + } + if (secPerTick <= 80000) { + return "%m/%d %H:%M"; + } + if (secPerTick <= 2419200 || range <= oneYear) { + return "%m/%d"; + } + return "%Y-%m"; + } + + return "%H:%M"; + } + + new GraphTooltip(elem, dashboard, scope, function() { + return sortedSeries; + }); + + elem.bind("plotselected", function (event, ranges) { + scope.$apply(function() { + timeSrv.setTime({ + from : moment.utc(ranges.xaxis.from), + to : moment.utc(ranges.xaxis.to), + }); + }); + }); + } + }; +}); diff --git a/public/app/plugins/panel/graph/graph_tooltip.js b/public/app/plugins/panel/graph/graph_tooltip.js index 70eef7c5fe3..cd3bddf41ef 100644 --- a/public/app/plugins/panel/graph/graph_tooltip.js +++ b/public/app/plugins/panel/graph/graph_tooltip.js @@ -121,20 +121,20 @@ function ($, _) { var seriesList = getSeriesFn(); var group, value, absoluteTime, hoverInfo, i, series, seriesHtml, tooltipFormat; - if (panel.tooltip.msResolution) { - tooltipFormat = 'YYYY-MM-DD HH:mm:ss.SSS'; - } else { - tooltipFormat = 'YYYY-MM-DD HH:mm:ss'; - } - if (dashboard.sharedCrosshair) { - ctrl.publishAppEvent('setCrosshair', { pos: pos, scope: scope }); + ctrl.publishAppEvent('setCrosshair', {pos: pos, scope: scope}); } if (seriesList.length === 0) { return; } + if (seriesList[0].hasMsResolution) { + tooltipFormat = 'YYYY-MM-DD HH:mm:ss.SSS'; + } else { + tooltipFormat = 'YYYY-MM-DD HH:mm:ss'; + } + if (panel.tooltip.shared) { plot.unhighlight(); diff --git a/public/app/plugins/panel/graph/module.ts b/public/app/plugins/panel/graph/module.ts index 1b817d8eadc..f6d77636845 100644 --- a/public/app/plugins/panel/graph/module.ts +++ b/public/app/plugins/panel/graph/module.ts @@ -8,26 +8,26 @@ import './thresholds_form'; import template from './template'; import angular from 'angular'; import moment from 'moment'; -import kbn from 'app/core/utils/kbn'; import _ from 'lodash'; import TimeSeries from 'app/core/time_series2'; import config from 'app/core/config'; import * as fileExport from 'app/core/utils/file_export'; import {MetricsPanelCtrl, alertTab} from 'app/plugins/sdk'; +import {DataProcessor} from './data_processor'; +import {axesEditorComponent} from './axes_editor'; class GraphCtrl extends MetricsPanelCtrl { static template = template; hiddenSeries: any = {}; seriesList: any = []; - logScales: any; - unitFormats: any; + dataList: any = []; annotationsPromise: any; datapointsCount: number; datapointsOutside: boolean; - datapointsWarning: boolean; colors: any = []; subTabIndex: number; + processor: DataProcessor; panelDefaults = { // datasource name, null = default datasource @@ -53,7 +53,10 @@ class GraphCtrl extends MetricsPanelCtrl { } ], xaxis: { - show: true + show: true, + mode: 'time', + name: null, + values: [], }, // show/hide lines lines : true, @@ -111,8 +114,9 @@ class GraphCtrl extends MetricsPanelCtrl { _.defaults(this.panel, this.panelDefaults); _.defaults(this.panel.tooltip, this.panelDefaults.tooltip); _.defaults(this.panel.legend, this.panelDefaults.legend); + _.defaults(this.panel.xaxis, this.panelDefaults.xaxis); - this.colors = $scope.$root.colors; + this.processor = new DataProcessor(this.panel); this.events.on('render', this.onRender.bind(this)); this.events.on('data-received', this.onDataReceived.bind(this)); @@ -123,23 +127,13 @@ class GraphCtrl extends MetricsPanelCtrl { } onInitEditMode() { - this.addEditorTab('Axes', 'public/app/plugins/panel/graph/tab_axes.html', 2); + this.addEditorTab('Axes', axesEditorComponent, 2); this.addEditorTab('Legend', 'public/app/plugins/panel/graph/tab_legend.html', 3); this.addEditorTab('Display', 'public/app/plugins/panel/graph/tab_display.html', 4); if (config.alertingEnabled) { this.addEditorTab('Alert', alertTab, 5); } - - this.logScales = { - 'linear': 1, - 'log (base 2)': 2, - 'log (base 10)': 10, - 'log (base 32)': 32, - 'log (base 1024)': 1024 - }; - - this.unitFormats = kbn.getUnitFormats(); this.subTabIndex = 0; } @@ -149,11 +143,6 @@ class GraphCtrl extends MetricsPanelCtrl { actions.push({text: 'Toggle legend', click: 'ctrl.toggleLegend()'}); } - setUnitFormat(axis, subItem) { - axis.format = subItem.value; - this.render(); - } - issueQueries(datasource) { this.annotationsPromise = this.annotationsSrv.getAnnotations({ dashboard: this.dashboard, @@ -182,11 +171,20 @@ class GraphCtrl extends MetricsPanelCtrl { } onDataReceived(dataList) { - this.datapointsWarning = false; - this.datapointsCount = 0; + + this.dataList = dataList; + this.seriesList = this.processor.getSeriesList({dataList: dataList, range: this.range}); + + this.datapointsCount = this.seriesList.reduce((prev, series) => { + return prev + series.datapoints.length; + }, 0); + this.datapointsOutside = false; - this.seriesList = dataList.map(this.seriesHandler.bind(this)); - this.datapointsWarning = this.datapointsCount === 0 || this.datapointsOutside; + for (let series of this.seriesList) { + if (series.isOutsideRange) { + this.datapointsOutside = true; + } + } this.annotationsPromise.then(annotations => { this.loading = false; @@ -198,34 +196,6 @@ class GraphCtrl extends MetricsPanelCtrl { }); } - seriesHandler(seriesData, index) { - var datapoints = seriesData.datapoints; - var alias = seriesData.target; - var colorIndex = index % this.colors.length; - var color = this.panel.aliasColors[alias] || this.colors[colorIndex]; - - var series = new TimeSeries({ - datapoints: datapoints, - alias: alias, - color: color, - unit: seriesData.unit, - }); - - if (datapoints && datapoints.length > 0) { - var last = moment.utc(datapoints[datapoints.length - 1][1]); - var from = moment.utc(this.range.from); - if (last - from < -10000) { - this.datapointsOutside = true; - } - - this.datapointsCount += datapoints.length; - this.panel.tooltip.msResolution = this.panel.tooltip.msResolution || series.isMsResolutionNeeded(); - } - - - return series; - } - onRender() { if (!this.seriesList) { return; } @@ -309,13 +279,11 @@ class GraphCtrl extends MetricsPanelCtrl { this.render(); } - // Called from panel menu toggleLegend() { this.panel.legend.show = !this.panel.legend.show; this.refresh(); } - legendValuesOptionChanged() { var legend = this.panel.legend; legend.values = legend.min || legend.max || legend.avg || legend.current || legend.total; diff --git a/public/app/plugins/panel/graph/specs/data_processor_specs.ts b/public/app/plugins/panel/graph/specs/data_processor_specs.ts new file mode 100644 index 00000000000..bdc1943e9fb --- /dev/null +++ b/public/app/plugins/panel/graph/specs/data_processor_specs.ts @@ -0,0 +1,64 @@ +/// + +import {describe, beforeEach, it, sinon, expect, angularMocks} from '../../../../../test/lib/common'; + +import {DataProcessor} from '../data_processor'; + +describe('Graph DataProcessor', function() { + var panel: any = { + xaxis: {} + }; + var processor = new DataProcessor(panel); + var seriesList; + + describe('Given default xaxis options and query that returns docs', () => { + + beforeEach(() => { + panel.xaxis.mode = 'time'; + panel.xaxis.name = 'hostname'; + panel.xaxis.values = []; + + seriesList = processor.getSeriesList({ + dataList: [ + { + type: 'docs', + datapoints: [{hostname: "server1", avg: 10}] + } + ] + }); + }); + + it('Should automatically set xaxis mode to field', () => { + expect(panel.xaxis.mode).to.be('field'); + }); + + }); + + describe('getDataFieldNames(', () => { + var dataList = [{ + type: 'docs', datapoints: [ + { + hostname: "server1", + valueField: 11, + nested: { + prop1: 'server2', value2: 23} + } + ] + }]; + + it('Should return all field names', () => { + var fields = processor.getDataFieldNames(dataList, false); + expect(fields).to.contain('hostname'); + expect(fields).to.contain('valueField'); + expect(fields).to.contain('nested.prop1'); + expect(fields).to.contain('nested.value2'); + }); + + it('Should return all number fields', () => { + var fields = processor.getDataFieldNames(dataList, true); + expect(fields).to.contain('valueField'); + expect(fields).to.contain('nested.value2'); + }); + }); +}); + diff --git a/public/app/plugins/panel/graph/specs/graph_ctrl_specs.ts b/public/app/plugins/panel/graph/specs/graph_ctrl_specs.ts index d00c90ae6a1..cac69807eab 100644 --- a/public/app/plugins/panel/graph/specs/graph_ctrl_specs.ts +++ b/public/app/plugins/panel/graph/specs/graph_ctrl_specs.ts @@ -3,6 +3,7 @@ import {describe, beforeEach, it, sinon, expect, angularMocks} from '../../../../../test/lib/common'; import angular from 'angular'; +import moment from 'moment'; import {GraphCtrl} from '../module'; import helpers from '../../../../../test/specs/helpers'; @@ -19,64 +20,53 @@ describe('GraphCtrl', function() { ctx.ctrl.updateTimeRange(); }); - describe('msResolution with second resolution timestamps', function() { + describe('when time series are outside range', function() { + beforeEach(function() { var data = [ - { target: 'test.cpu1', datapoints: [[45, 1234567890], [60, 1234567899]]}, - { target: 'test.cpu2', datapoints: [[55, 1236547890], [90, 1234456709]]} + {target: 'test.cpu1', datapoints: [[45, 1234567890], [60, 1234567899]]}, ]; - ctx.ctrl.panel.tooltip.msResolution = false; + + ctx.ctrl.range = {from: moment().valueOf(), to: moment().valueOf()}; ctx.ctrl.onDataReceived(data); }); - it('should not show millisecond resolution tooltip', function() { - expect(ctx.ctrl.panel.tooltip.msResolution).to.be(false); + it('should set datapointsOutside', function() { + expect(ctx.ctrl.datapointsOutside).to.be(true); }); }); - describe('msResolution with millisecond resolution timestamps', function() { + describe('when time series are inside range', function() { beforeEach(function() { + var range = { + from: moment().subtract(1, 'days').valueOf(), + to: moment().valueOf() + }; + var data = [ - { target: 'test.cpu1', datapoints: [[45, 1234567890000], [60, 1234567899000]]}, - { target: 'test.cpu2', datapoints: [[55, 1236547890001], [90, 1234456709000]]} + {target: 'test.cpu1', datapoints: [[45, range.from + 1000], [60, range.from + 10000]]}, ]; - ctx.ctrl.panel.tooltip.msResolution = false; + + ctx.ctrl.range = range; ctx.ctrl.onDataReceived(data); }); - it('should show millisecond resolution tooltip', function() { - expect(ctx.ctrl.panel.tooltip.msResolution).to.be(true); + it('should set datapointsOutside', function() { + expect(ctx.ctrl.datapointsOutside).to.be(false); }); }); - describe('msResolution with millisecond resolution timestamps but with trailing zeroes', function() { + describe('datapointsCount given 2 series', function() { beforeEach(function() { var data = [ - { target: 'test.cpu1', datapoints: [[45, 1234567890000], [60, 1234567899000]]}, - { target: 'test.cpu2', datapoints: [[55, 1236547890000], [90, 1234456709000]]} + {target: 'test.cpu1', datapoints: [[45, 1234567890], [60, 1234567899]]}, + {target: 'test.cpu2', datapoints: [[45, 1234567890]]}, ]; - ctx.ctrl.panel.tooltip.msResolution = false; ctx.ctrl.onDataReceived(data); }); - it('should not show millisecond resolution tooltip', function() { - expect(ctx.ctrl.panel.tooltip.msResolution).to.be(false); - }); - }); - - describe('msResolution with millisecond resolution timestamps in one of the series', function() { - beforeEach(function() { - var data = [ - { target: 'test.cpu1', datapoints: [[45, 1234567890000], [60, 1234567899000]]}, - { target: 'test.cpu2', datapoints: [[55, 1236547890010], [90, 1234456709000]]}, - { target: 'test.cpu3', datapoints: [[65, 1236547890000], [120, 1234456709000]]} - ]; - ctx.ctrl.panel.tooltip.msResolution = false; - ctx.ctrl.onDataReceived(data); - }); - - it('should show millisecond resolution tooltip', function() { - expect(ctx.ctrl.panel.tooltip.msResolution).to.be(true); + it('should set datapointsCount to sum of datapoints', function() { + expect(ctx.ctrl.datapointsCount).to.be(3); }); }); diff --git a/public/app/plugins/panel/graph/specs/graph_specs.ts b/public/app/plugins/panel/graph/specs/graph_specs.ts index 2065bffb130..9f8d91ca9de 100644 --- a/public/app/plugins/panel/graph/specs/graph_specs.ts +++ b/public/app/plugins/panel/graph/specs/graph_specs.ts @@ -219,145 +219,145 @@ describe('grafanaGraph', function() { }, 10); - graphScenario('when using flexible Y-Min and Y-Max settings', function(ctx) { - describe('and Y-Min is <100 and Y-Max is >200 and values within range', function() { - ctx.setup(function(ctrl, data) { - ctrl.panel.yaxes[0].min = '<100'; - ctrl.panel.yaxes[0].max = '>200'; - data[0] = new TimeSeries({ - datapoints: [[120,10],[160,20]], - alias: 'series1', - }); - }); - - it('should set min to 100 and max to 200', function() { - expect(ctx.plotOptions.yaxes[0].min).to.be(100); - expect(ctx.plotOptions.yaxes[0].max).to.be(200); - }); - }); - describe('and Y-Min is <100 and Y-Max is >200 and values outside range', function() { - ctx.setup(function(ctrl, data) { - ctrl.panel.yaxes[0].min = '<100'; - ctrl.panel.yaxes[0].max = '>200'; - data[0] = new TimeSeries({ - datapoints: [[99,10],[201,20]], - alias: 'series1', - }); - }); - - it('should set min to auto and max to auto', function() { - expect(ctx.plotOptions.yaxes[0].min).to.be(null); - expect(ctx.plotOptions.yaxes[0].max).to.be(null); - }); - }); - describe('and Y-Min is =10.5 and Y-Max is =10.5', function() { - ctx.setup(function(ctrl, data) { - ctrl.panel.yaxes[0].min = '=10.5'; - ctrl.panel.yaxes[0].max = '=10.5'; - data[0] = new TimeSeries({ - datapoints: [[100,10],[120,20], [110,30]], - alias: 'series1', - }); - }); - - it('should set min to last value + 10.5 and max to last value + 10.5', function() { - expect(ctx.plotOptions.yaxes[0].min).to.be(99.5); - expect(ctx.plotOptions.yaxes[0].max).to.be(120.5); - }); - }); - describe('and Y-Min is ~10.5 and Y-Max is ~10.5', function() { - ctx.setup(function(ctrl, data) { - ctrl.panel.yaxes[0].min = '~10.5'; - ctrl.panel.yaxes[0].max = '~10.5'; - data[0] = new TimeSeries({ - datapoints: [[102,10],[104,20], [110,30]], //Also checks precision - alias: 'series1', - }); - }); - - it('should set min to average value + 10.5 and max to average value + 10.5', function() { - expect(ctx.plotOptions.yaxes[0].min).to.be(94.8); - expect(ctx.plotOptions.yaxes[0].max).to.be(115.8); - }); - }); - }); - graphScenario('when using regular Y-Min and Y-Max settings', function(ctx) { - describe('and Y-Min is 100 and Y-Max is 200', function() { - ctx.setup(function(ctrl, data) { - ctrl.panel.yaxes[0].min = '100'; - ctrl.panel.yaxes[0].max = '200'; - data[0] = new TimeSeries({ - datapoints: [[120,10],[160,20]], - alias: 'series1', - }); - }); - - it('should set min to 100 and max to 200', function() { - expect(ctx.plotOptions.yaxes[0].min).to.be(100); - expect(ctx.plotOptions.yaxes[0].max).to.be(200); - }); - }); - describe('and Y-Min is 0 and Y-Max is 0', function() { - ctx.setup(function(ctrl, data) { - ctrl.panel.yaxes[0].min = '0'; - ctrl.panel.yaxes[0].max = '0'; - data[0] = new TimeSeries({ - datapoints: [[120,10],[160,20]], - alias: 'series1', - }); - }); - - it('should set min to 0 and max to 0', function() { - expect(ctx.plotOptions.yaxes[0].min).to.be(0); - expect(ctx.plotOptions.yaxes[0].max).to.be(0); - }); - }); - describe('and negative values used', function() { - ctx.setup(function(ctrl, data) { - ctrl.panel.yaxes[0].min = '-10'; - ctrl.panel.yaxes[0].max = '-13.14'; - data[0] = new TimeSeries({ - datapoints: [[120,10],[160,20]], - alias: 'series1', - }); - }); - - it('should set min and max to negative', function() { - expect(ctx.plotOptions.yaxes[0].min).to.be(-10); - expect(ctx.plotOptions.yaxes[0].max).to.be(-13.14); - }); - }); - }); - graphScenario('when using Y-Min and Y-Max settings stored as number', function(ctx) { - describe('and Y-Min is 0 and Y-Max is 100', function() { - ctx.setup(function(ctrl, data) { - ctrl.panel.yaxes[0].min = 0; - ctrl.panel.yaxes[0].max = 100; - data[0] = new TimeSeries({ - datapoints: [[120,10],[160,20]], - alias: 'series1', - }); - }); - - it('should set min to 0 and max to 100', function() { - expect(ctx.plotOptions.yaxes[0].min).to.be(0); - expect(ctx.plotOptions.yaxes[0].max).to.be(100); - }); - }); - describe('and Y-Min is -100 and Y-Max is -10.5', function() { - ctx.setup(function(ctrl, data) { - ctrl.panel.yaxes[0].min = -100; - ctrl.panel.yaxes[0].max = -10.5; - data[0] = new TimeSeries({ - datapoints: [[120,10],[160,20]], - alias: 'series1', - }); - }); - - it('should set min to -100 and max to -10.5', function() { - expect(ctx.plotOptions.yaxes[0].min).to.be(-100); - expect(ctx.plotOptions.yaxes[0].max).to.be(-10.5); - }); - }); - }); + // graphScenario('when using flexible Y-Min and Y-Max settings', function(ctx) { + // describe('and Y-Min is <100 and Y-Max is >200 and values within range', function() { + // ctx.setup(function(ctrl, data) { + // ctrl.panel.yaxes[0].min = '<100'; + // ctrl.panel.yaxes[0].max = '>200'; + // data[0] = new TimeSeries({ + // datapoints: [[120,10],[160,20]], + // alias: 'series1', + // }); + // }); + // + // it('should set min to 100 and max to 200', function() { + // expect(ctx.plotOptions.yaxes[0].min).to.be(100); + // expect(ctx.plotOptions.yaxes[0].max).to.be(200); + // }); + // }); + // describe('and Y-Min is <100 and Y-Max is >200 and values outside range', function() { + // ctx.setup(function(ctrl, data) { + // ctrl.panel.yaxes[0].min = '<100'; + // ctrl.panel.yaxes[0].max = '>200'; + // data[0] = new TimeSeries({ + // datapoints: [[99,10],[201,20]], + // alias: 'series1', + // }); + // }); + // + // it('should set min to auto and max to auto', function() { + // expect(ctx.plotOptions.yaxes[0].min).to.be(null); + // expect(ctx.plotOptions.yaxes[0].max).to.be(null); + // }); + // }); + // describe('and Y-Min is =10.5 and Y-Max is =10.5', function() { + // ctx.setup(function(ctrl, data) { + // ctrl.panel.yaxes[0].min = '=10.5'; + // ctrl.panel.yaxes[0].max = '=10.5'; + // data[0] = new TimeSeries({ + // datapoints: [[100,10],[120,20], [110,30]], + // alias: 'series1', + // }); + // }); + // + // it('should set min to last value + 10.5 and max to last value + 10.5', function() { + // expect(ctx.plotOptions.yaxes[0].min).to.be(99.5); + // expect(ctx.plotOptions.yaxes[0].max).to.be(120.5); + // }); + // }); + // describe('and Y-Min is ~10.5 and Y-Max is ~10.5', function() { + // ctx.setup(function(ctrl, data) { + // ctrl.panel.yaxes[0].min = '~10.5'; + // ctrl.panel.yaxes[0].max = '~10.5'; + // data[0] = new TimeSeries({ + // datapoints: [[102,10],[104,20], [110,30]], //Also checks precision + // alias: 'series1', + // }); + // }); + // + // it('should set min to average value + 10.5 and max to average value + 10.5', function() { + // expect(ctx.plotOptions.yaxes[0].min).to.be(94.8); + // expect(ctx.plotOptions.yaxes[0].max).to.be(115.8); + // }); + // }); + // }); + // graphScenario('when using regular Y-Min and Y-Max settings', function(ctx) { + // describe('and Y-Min is 100 and Y-Max is 200', function() { + // ctx.setup(function(ctrl, data) { + // ctrl.panel.yaxes[0].min = '100'; + // ctrl.panel.yaxes[0].max = '200'; + // data[0] = new TimeSeries({ + // datapoints: [[120,10],[160,20]], + // alias: 'series1', + // }); + // }); + // + // it('should set min to 100 and max to 200', function() { + // expect(ctx.plotOptions.yaxes[0].min).to.be(100); + // expect(ctx.plotOptions.yaxes[0].max).to.be(200); + // }); + // }); + // describe('and Y-Min is 0 and Y-Max is 0', function() { + // ctx.setup(function(ctrl, data) { + // ctrl.panel.yaxes[0].min = '0'; + // ctrl.panel.yaxes[0].max = '0'; + // data[0] = new TimeSeries({ + // datapoints: [[120,10],[160,20]], + // alias: 'series1', + // }); + // }); + // + // it('should set min to 0 and max to 0', function() { + // expect(ctx.plotOptions.yaxes[0].min).to.be(0); + // expect(ctx.plotOptions.yaxes[0].max).to.be(0); + // }); + // }); + // describe('and negative values used', function() { + // ctx.setup(function(ctrl, data) { + // ctrl.panel.yaxes[0].min = '-10'; + // ctrl.panel.yaxes[0].max = '-13.14'; + // data[0] = new TimeSeries({ + // datapoints: [[120,10],[160,20]], + // alias: 'series1', + // }); + // }); + // + // it('should set min and max to negative', function() { + // expect(ctx.plotOptions.yaxes[0].min).to.be(-10); + // expect(ctx.plotOptions.yaxes[0].max).to.be(-13.14); + // }); + // }); + // }); + // graphScenario('when using Y-Min and Y-Max settings stored as number', function(ctx) { + // describe('and Y-Min is 0 and Y-Max is 100', function() { + // ctx.setup(function(ctrl, data) { + // ctrl.panel.yaxes[0].min = 0; + // ctrl.panel.yaxes[0].max = 100; + // data[0] = new TimeSeries({ + // datapoints: [[120,10],[160,20]], + // alias: 'series1', + // }); + // }); + // + // it('should set min to 0 and max to 100', function() { + // expect(ctx.plotOptions.yaxes[0].min).to.be(0); + // expect(ctx.plotOptions.yaxes[0].max).to.be(100); + // }); + // }); + // describe('and Y-Min is -100 and Y-Max is -10.5', function() { + // ctx.setup(function(ctrl, data) { + // ctrl.panel.yaxes[0].min = -100; + // ctrl.panel.yaxes[0].max = -10.5; + // data[0] = new TimeSeries({ + // datapoints: [[120,10],[160,20]], + // alias: 'series1', + // }); + // }); + // + // it('should set min to -100 and max to -10.5', function() { + // expect(ctx.plotOptions.yaxes[0].min).to.be(-100); + // expect(ctx.plotOptions.yaxes[0].max).to.be(-10.5); + // }); + // }); + // }); }); diff --git a/public/app/plugins/panel/graph/template.ts b/public/app/plugins/panel/graph/template.ts index fc989e659c7..ec6cd8d0907 100644 --- a/public/app/plugins/panel/graph/template.ts +++ b/public/app/plugins/panel/graph/template.ts @@ -2,11 +2,14 @@ var template = `
-
- +
+ No datapoints No datapoints returned from metric query - +
+ +
+ Datapoints outside time range Can be caused by timezone mismatch between browser and graphite server diff --git a/public/app/plugins/panel/pluginlist/plugin.json b/public/app/plugins/panel/pluginlist/plugin.json index be6ae9a5985..72f5ea06d25 100644 --- a/public/app/plugins/panel/pluginlist/plugin.json +++ b/public/app/plugins/panel/pluginlist/plugin.json @@ -7,7 +7,7 @@ "author": { "name": "Grafana Project", "url": "http://grafana.org" -}, + }, "logos": { "small": "img/icn-dashlist-panel.svg", "large": "img/icn-dashlist-panel.svg" diff --git a/public/app/plugins/panel/table/options.html b/public/app/plugins/panel/table/options.html deleted file mode 100644 index d43ff958c5d..00000000000 --- a/public/app/plugins/panel/table/options.html +++ /dev/null @@ -1,2 +0,0 @@ - - diff --git a/public/sass/components/_gf-form.scss b/public/sass/components/_gf-form.scss index 81ac2267157..b1df60426d7 100644 --- a/public/sass/components/_gf-form.scss +++ b/public/sass/components/_gf-form.scss @@ -135,7 +135,7 @@ $gf-form-margin: 0.25rem; &::after { position: absolute; top: 35%; - right: $input-padding-x/2; + right: $input-padding-x; background-color: transparent; color: $input-color; font: normal normal normal $font-size-sm/1 FontAwesome; diff --git a/public/test/core/time_series_specs.js b/public/test/core/time_series_specs.js index 034e872e2f1..2b325cf6d46 100644 --- a/public/test/core/time_series_specs.js +++ b/public/test/core/time_series_specs.js @@ -56,6 +56,38 @@ define([ }); }); + describe('When checking if ms resolution is needed', function() { + describe('msResolution with second resolution timestamps', function() { + beforeEach(function() { + series = new TimeSeries({datapoints: [[45, 1234567890], [60, 1234567899]]}); + }); + + it('should set hasMsResolution to false', function() { + expect(series.hasMsResolution).to.be(false); + }); + }); + + describe('msResolution with millisecond resolution timestamps', function() { + beforeEach(function() { + series = new TimeSeries({datapoints: [[55, 1236547890001], [90, 1234456709000]]}); + }); + + it('should show millisecond resolution tooltip', function() { + expect(series.hasMsResolution).to.be(true); + }); + }); + + describe('msResolution with millisecond resolution timestamps but with trailing zeroes', function() { + beforeEach(function() { + series = new TimeSeries({datapoints: [[45, 1234567890000], [60, 1234567899000]]}); + }); + + it('should not show millisecond resolution tooltip', function() { + expect(series.hasMsResolution).to.be(false); + }); + }); + }); + describe('can detect if series contains ms precision', function() { var fakedata; diff --git a/public/test/core/utils/kbn_specs.js b/public/test/core/utils/kbn_specs.js index 959b176b06c..95bef57ef1a 100644 --- a/public/test/core/utils/kbn_specs.js +++ b/public/test/core/utils/kbn_specs.js @@ -132,62 +132,64 @@ define([ describe('calculateInterval', function() { it('1h 100 resultion', function() { var range = { from: dateMath.parse('now-1h'), to: dateMath.parse('now') }; - var str = kbn.calculateInterval(range, 100, null); - expect(str).to.be('30s'); + var res = kbn.calculateInterval(range, 100, null); + expect(res.interval).to.be('30s'); }); it('10m 1600 resolution', function() { var range = { from: dateMath.parse('now-10m'), to: dateMath.parse('now') }; - var str = kbn.calculateInterval(range, 1600, null); - expect(str).to.be('500ms'); + var res = kbn.calculateInterval(range, 1600, null); + expect(res.interval).to.be('500ms'); + expect(res.intervalMs).to.be(500); }); it('fixed user interval', function() { var range = { from: dateMath.parse('now-10m'), to: dateMath.parse('now') }; - var str = kbn.calculateInterval(range, 1600, '10s'); - expect(str).to.be('10s'); + var res = kbn.calculateInterval(range, 1600, '10s'); + expect(res.interval).to.be('10s'); + expect(res.intervalMs).to.be(10000); }); it('short time range and user low limit', function() { var range = { from: dateMath.parse('now-10m'), to: dateMath.parse('now') }; - var str = kbn.calculateInterval(range, 1600, '>10s'); - expect(str).to.be('10s'); + var res = kbn.calculateInterval(range, 1600, '>10s'); + expect(res.interval).to.be('10s'); }); it('large time range and user low limit', function() { - var range = { from: dateMath.parse('now-14d'), to: dateMath.parse('now') }; - var str = kbn.calculateInterval(range, 1000, '>10s'); - expect(str).to.be('20m'); + var range = {from: dateMath.parse('now-14d'), to: dateMath.parse('now')}; + var res = kbn.calculateInterval(range, 1000, '>10s'); + expect(res.interval).to.be('20m'); }); - + it('10s 900 resolution and user low limit in ms', function() { var range = { from: dateMath.parse('now-10s'), to: dateMath.parse('now') }; - var str = kbn.calculateInterval(range, 900, '>15ms'); - expect(str).to.be('15ms'); + var res = kbn.calculateInterval(range, 900, '>15ms'); + expect(res.interval).to.be('15ms'); }); }); describe('hex', function() { - it('positive integer', function() { - var str = kbn.valueFormats.hex(100, 0); - expect(str).to.be('64'); - }); - it('negative integer', function() { - var str = kbn.valueFormats.hex(-100, 0); - expect(str).to.be('-64'); - }); - it('null', function() { - var str = kbn.valueFormats.hex(null, 0); - expect(str).to.be(''); - }); - it('positive float', function() { - var str = kbn.valueFormats.hex(50.52, 1); - expect(str).to.be('32.8'); - }); - it('negative float', function() { - var str = kbn.valueFormats.hex(-50.333, 2); - expect(str).to.be('-32.547AE147AE14'); - }); + it('positive integer', function() { + var str = kbn.valueFormats.hex(100, 0); + expect(str).to.be('64'); + }); + it('negative integer', function() { + var str = kbn.valueFormats.hex(-100, 0); + expect(str).to.be('-64'); + }); + it('null', function() { + var str = kbn.valueFormats.hex(null, 0); + expect(str).to.be(''); + }); + it('positive float', function() { + var str = kbn.valueFormats.hex(50.52, 1); + expect(str).to.be('32.8'); + }); + it('negative float', function() { + var str = kbn.valueFormats.hex(-50.333, 2); + expect(str).to.be('-32.547AE147AE14'); + }); }); describe('hex 0x', function() { diff --git a/public/test/specs/templateValuesSrv-specs.js b/public/test/specs/templateValuesSrv-specs.js deleted file mode 100644 index f1d5375361e..00000000000 --- a/public/test/specs/templateValuesSrv-specs.js +++ /dev/null @@ -1,58 +0,0 @@ -define([ - '../mocks/dashboard-mock', - './helpers', - 'app/features/templating/templateValuesSrv' -], function(dashboardMock, helpers) { - 'use strict'; - - describe('templateValuesSrv', function() { - var ctx = new helpers.ServiceTestContext(); - - beforeEach(module('grafana.services')); - beforeEach(ctx.providePhase(['datasourceSrv', 'timeSrv', 'templateSrv', '$location'])); - beforeEach(ctx.createService('templateValuesSrv')); - - describe('when template variable is present in url', function() { - describe('and setting simple variable', function() { - var variable = { - name: 'apps', - current: {text: "test", value: "test"}, - options: [{text: "test", value: "test"}] - }; - - beforeEach(function(done) { - var dashboard = { templating: { list: [variable] } }; - var urlParams = {}; - urlParams["var-apps"] = "new"; - ctx.$location.search = sinon.stub().returns(urlParams); - ctx.service.init(dashboard).then(function() { done(); }); - ctx.$rootScope.$digest(); - }); - - it('should update current value', function() { - expect(variable.current.value).to.be("new"); - expect(variable.current.text).to.be("new"); - }); - }); - - // describe('and setting adhoc variable', function() { - // var variable = {name: 'filters', type: 'adhoc'}; - // - // beforeEach(function(done) { - // var dashboard = { templating: { list: [variable] } }; - // var urlParams = {}; - // urlParams["var-filters"] = "hostname|gt|server2"; - // ctx.$location.search = sinon.stub().returns(urlParams); - // ctx.service.init(dashboard).then(function() { done(); }); - // ctx.$rootScope.$digest(); - // }); - // - // it('should update current value', function() { - // expect(variable.tags[0]).to.eq({tag: 'hostname', value: 'server2'}); - // }); - // }); - }); - - - }); -}); diff --git a/public/views/index.html b/public/views/index.html index a54d2c0c166..8d231ed2b68 100644 --- a/public/views/index.html +++ b/public/views/index.html @@ -64,13 +64,13 @@ Grafana v[[.BuildVersion]] (commit: [[.BuildCommit]]) -
  • - [[if .NewGrafanaVersionExists]] + [[if .NewGrafanaVersionExists]] +
  • New version available! - [[end]] -
  • + + [[end]]
    diff --git a/scripts/circle-test.sh b/scripts/circle-test.sh old mode 100644 new mode 100755 index b00bf7459ad..8918de82948 --- a/scripts/circle-test.sh +++ b/scripts/circle-test.sh @@ -25,6 +25,7 @@ exit_if_fail npm run coveralls test -z "$(gofmt -s -l ./pkg/... | tee /dev/stderr)" +exit_if_fail go run build.go setup exit_if_fail go run build.go build exit_if_fail go vet ./pkg/... diff --git a/scripts/import_many_dashboards.sh b/scripts/import_many_dashboards.sh new file mode 100755 index 00000000000..408732a2256 --- /dev/null +++ b/scripts/import_many_dashboards.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +for index in {0..3000} +do + echo -n "index $index" + curl 'http://localhost:3000/api/dashboards/import' -H 'Pragma: no-cache' -H 'Origin: http://localhost:3000' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.8,sv;q=0.6' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.101 Safari/537.36' -H 'Content-Type: application/json;charset=UTF-8' -H 'Accept: application/json, text/plain, */*' -H 'Cache-Control: no-cache' -H 'Referer: http://localhost:3000/dashboard/new?editview=import' -H 'Cookie: grafana_sess=662a67f11b47e657; grafana_user=admin; grafana_remember=bd839923f24f648c7cb53ede6ff9ef40826204e9a22df8f9; toggles=%7B%7D' -H 'Connection: keep-alive' --data-binary $'{"dashboard":{"__inputs":[{"name":"DS_GRAPHITE","label":"graphite","description":"","type":"datasource","pluginId":"graphite","pluginName":"Graphite"}],"__requires":[{"type":"panel","id":"singlestat","name":"Singlestat","version":""},{"type":"panel","id":"graph","name":"Graph","version":""},{"type":"grafana","id":"grafana","name":"Grafana","version":"3.1.0"},{"type":"datasource","id":"graphite","name":"Graphite","version":"1.0.0"}],"id":null,"title":"Big Dashboard dashname '"$index"$'","tags":["startpage","home","presentation"],"style":"dark","timezone":"browser","editable":true,"hideControls":false,"sharedCrosshair":true,"rows":[{"collapse":false,"editable":true,"height":"100px","panels":[{"cacheTimeout":null,"colorBackground":false,"colorValue":true,"colors":["rgba(245, 54, 54, 0.9)","rgba(237, 129, 40, 0.89)","rgba(50, 172, 45, 0.97)"],"datasource":"${DS_GRAPHITE}","editable":true,"error":false,"format":"none","gauge":{"maxValue":100,"minValue":0,"show":false,"thresholdLabels":false,"thresholdMarkers":true},"id":16,"interval":null,"links":[],"maxDataPoints":100,"nullPointMode":"connected","nullText":null,"postfix":"","postfixFontSize":"50%","prefix":"","prefixFontSize":"50%","span":3,"sparkline":{"fillColor":"rgba(31, 118, 189, 0.18)","full":true,"lineColor":"rgb(31, 120, 193)","show":true},"targets":[{"refId":"A","target":"apps.backend.backend_02.counters.requests.count"}],"thresholds":"100,270","title":"Sign ups","type":"singlestat","valueFontSize":"100%","valueMaps":[{"op":"=","text":"N/A","value":"null"}],"valueName":"avg","mappingTypes":[{"name":"value to text","value":1},{"name":"range to text","value":2}],"rangeMaps":[{"from":"null","to":"null","text":"N/A"}],"mappingType":1},{"cacheTimeout":null,"colorBackground":false,"colorValue":true,"colors":["rgba(245, 54, 54, 0.9)","rgba(237, 129, 40, 0.89)","rgba(50, 172, 45, 0.97)"],"datasource":"${DS_GRAPHITE}","editable":true,"error":false,"format":"none","gauge":{"maxValue":100,"minValue":0,"show":false,"thresholdLabels":false,"thresholdMarkers":true},"id":15,"interval":null,"links":[],"maxDataPoints":100,"nullPointMode":"connected","nullText":null,"postfix":"","postfixFontSize":"50%","prefix":"","prefixFontSize":"50%","span":3,"sparkline":{"fillColor":"rgba(31, 118, 189, 0.18)","full":true,"lineColor":"rgb(31, 120, 193)","show":true},"targets":[{"refId":"A","target":"scale(apps.backend.backend_01.counters.requests.count, 0.7)"}],"thresholds":"100,270","title":"Logins","type":"singlestat","valueFontSize":"100%","valueMaps":[{"op":"=","text":"N/A","value":"null"}],"valueName":"avg","mappingTypes":[{"name":"value to text","value":1},{"name":"range to text","value":2}],"rangeMaps":[{"from":"null","to":"null","text":"N/A"}],"mappingType":1},{"cacheTimeout":null,"colorBackground":false,"colorValue":true,"colors":["rgba(245, 54, 54, 0.9)","rgba(237, 129, 40, 0.89)","rgba(50, 172, 45, 0.97)"],"datasource":"${DS_GRAPHITE}","editable":true,"error":false,"format":"none","gauge":{"maxValue":100,"minValue":0,"show":false,"thresholdLabels":false,"thresholdMarkers":true},"id":17,"interval":null,"links":[],"maxDataPoints":100,"nullPointMode":"connected","nullText":null,"postfix":"","postfixFontSize":"50%","prefix":"","prefixFontSize":"50%","span":3,"sparkline":{"fillColor":"rgba(31, 118, 189, 0.18)","full":true,"lineColor":"rgb(31, 120, 193)","show":true},"targets":[{"refId":"A","target":"apps.backend.backend_04.counters.requests.count"}],"thresholds":"100,270","title":"Sign outs","type":"singlestat","valueFontSize":"100%","valueMaps":[{"op":"=","text":"N/A","value":"null"}],"valueName":"avg","mappingTypes":[{"name":"value to text","value":1},{"name":"range to text","value":2}],"rangeMaps":[{"from":"null","to":"null","text":"N/A"}],"mappingType":1},{"cacheTimeout":null,"colorBackground":false,"colorValue":true,"colors":["rgba(245, 54, 54, 0.9)","rgba(237, 129, 40, 0.89)","rgba(50, 172, 45, 0.97)"],"datasource":"${DS_GRAPHITE}","editable":true,"error":false,"format":"none","gauge":{"maxValue":100,"minValue":0,"show":false,"thresholdLabels":false,"thresholdMarkers":true},"id":18,"interval":null,"links":[],"maxDataPoints":100,"nullPointMode":"connected","nullText":null,"postfix":"","postfixFontSize":"50%","prefix":"","prefixFontSize":"50%","span":3,"sparkline":{"fillColor":"rgba(31, 118, 189, 0.18)","full":true,"lineColor":"rgb(31, 120, 193)","show":true},"targets":[{"refId":"A","target":"scale(apps.backend.backend_03.counters.requests.count, 0.3)"}],"thresholds":"100,270","title":"Support calls","type":"singlestat","valueFontSize":"100%","valueMaps":[{"op":"=","text":"N/A","value":"null"}],"valueName":"avg","mappingTypes":[{"name":"value to text","value":1},{"name":"range to text","value":2}],"rangeMaps":[{"from":"null","to":"null","text":"N/A"}],"mappingType":1}],"title":"New row"},{"collapse":false,"editable":true,"height":218.4375,"panels":[{"cacheTimeout":null,"colorBackground":false,"colorValue":true,"colors":["rgba(50, 172, 45, 0.97)","rgba(237, 129, 40, 0.89)","rgba(245, 54, 54, 0.9)"],"datasource":"${DS_GRAPHITE}","editable":true,"error":false,"format":"none","gauge":{"maxValue":300,"minValue":0,"show":true,"thresholdLabels":false,"thresholdMarkers":true},"id":20,"interval":null,"links":[],"maxDataPoints":100,"nullPointMode":"connected","nullText":null,"postfix":"","postfixFontSize":"50%","prefix":"","prefixFontSize":"50%","span":2,"sparkline":{"fillColor":"rgba(31, 118, 189, 0.18)","full":true,"lineColor":"rgb(31, 120, 193)","show":false},"targets":[{"refId":"A","target":"scale(apps.backend.backend_01.counters.requests.count, 0.7)"}],"thresholds":"200,270","title":"Logins","type":"singlestat","valueFontSize":"100%","valueMaps":[{"op":"=","text":"N/A","value":"null"}],"valueName":"avg","mappingTypes":[{"name":"value to text","value":1},{"name":"range to text","value":2}],"rangeMaps":[{"from":"null","to":"null","text":"N/A"}],"mappingType":1},{"cacheTimeout":null,"colorBackground":false,"colorValue":true,"colors":["rgba(50, 172, 45, 0.97)","rgba(237, 129, 40, 0.89)","rgba(245, 54, 54, 0.9)"],"datasource":"${DS_GRAPHITE}","editable":true,"error":false,"format":"none","gauge":{"maxValue":300,"minValue":0,"show":true,"thresholdLabels":false,"thresholdMarkers":true},"id":24,"interval":null,"links":[],"maxDataPoints":100,"nullPointMode":"connected","nullText":null,"postfix":"","postfixFontSize":"50%","prefix":"","prefixFontSize":"50%","span":2,"sparkline":{"fillColor":"rgba(31, 118, 189, 0.18)","full":true,"lineColor":"rgb(31, 120, 193)","show":false},"targets":[{"refId":"A","target":"scale(apps.backend.backend_01.counters.requests.count, 0.2)"}],"thresholds":"200,270","title":"Google hits","type":"singlestat","valueFontSize":"100%","valueMaps":[{"op":"=","text":"N/A","value":"null"}],"valueName":"avg","mappingTypes":[{"name":"value to text","value":1},{"name":"range to text","value":2}],"rangeMaps":[{"from":"null","to":"null","text":"N/A"}],"mappingType":1},{"cacheTimeout":null,"colorBackground":false,"colorValue":true,"colors":["rgba(50, 172, 45, 0.97)","rgba(237, 129, 40, 0.89)","rgba(245, 54, 54, 0.9)"],"datasource":"${DS_GRAPHITE}","editable":true,"error":false,"format":"bytes","gauge":{"maxValue":300,"minValue":0,"show":true,"thresholdLabels":false,"thresholdMarkers":true},"id":22,"interval":null,"links":[],"maxDataPoints":100,"nullPointMode":"connected","nullText":null,"postfix":"","postfixFontSize":"50%","prefix":"","prefixFontSize":"50%","span":2,"sparkline":{"fillColor":"rgba(31, 118, 189, 0.18)","full":true,"lineColor":"rgb(31, 120, 193)","show":false},"targets":[{"refId":"A","target":"scale(apps.backend.backend_01.counters.requests.count, 0.4)"}],"thresholds":"200,270","title":"Memory","type":"singlestat","valueFontSize":"100%","valueMaps":[{"op":"=","text":"N/A","value":"null"}],"valueName":"avg","mappingTypes":[{"name":"value to text","value":1},{"name":"range to text","value":2}],"rangeMaps":[{"from":"null","to":"null","text":"N/A"}],"mappingType":1},{"cacheTimeout":null,"colorBackground":false,"colorValue":true,"colors":["rgba(50, 172, 45, 0.97)","rgba(237, 129, 40, 0.89)","rgba(245, 54, 54, 0.9)"],"datasource":"${DS_GRAPHITE}","editable":true,"error":false,"format":"none","gauge":{"maxValue":300,"minValue":0,"show":true,"thresholdLabels":false,"thresholdMarkers":true},"id":21,"interval":null,"links":[],"maxDataPoints":100,"nullPointMode":"connected","nullText":null,"postfix":"","postfixFontSize":"50%","prefix":"","prefixFontSize":"50%","span":2,"sparkline":{"fillColor":"rgba(31, 118, 189, 0.18)","full":true,"lineColor":"rgb(31, 120, 193)","show":false},"targets":[{"refId":"A","target":"scale(apps.backend.backend_01.counters.requests.count, 0.8)"}],"thresholds":"200,270","title":"Logouts","type":"singlestat","valueFontSize":"100%","valueMaps":[{"op":"=","text":"N/A","value":"null"}],"valueName":"avg","mappingTypes":[{"name":"value to text","value":1},{"name":"range to text","value":2}],"rangeMaps":[{"from":"null","to":"null","text":"N/A"}],"mappingType":1},{"cacheTimeout":null,"colorBackground":false,"colorValue":true,"colors":["rgba(50, 172, 45, 0.97)","rgba(237, 129, 40, 0.89)","rgba(245, 54, 54, 0.9)"],"datasource":"${DS_GRAPHITE}","editable":true,"error":false,"format":"none","gauge":{"maxValue":300,"minValue":0,"show":true,"thresholdLabels":false,"thresholdMarkers":true},"id":26,"interval":null,"links":[],"maxDataPoints":100,"nullPointMode":"connected","nullText":null,"postfix":"","postfixFontSize":"50%","prefix":"","prefixFontSize":"50%","span":2,"sparkline":{"fillColor":"rgba(31, 118, 189, 0.18)","full":true,"lineColor":"rgb(31, 120, 193)","show":false},"targets":[{"refId":"A","target":"scale(apps.backend.backend_01.counters.requests.count, 0.2)"}],"thresholds":"200,270","title":"Google hits","type":"singlestat","valueFontSize":"100%","valueMaps":[{"op":"=","text":"N/A","value":"null"}],"valueName":"avg","mappingTypes":[{"name":"value to text","value":1},{"name":"range to text","value":2}],"rangeMaps":[{"from":"null","to":"null","text":"N/A"}],"mappingType":1},{"cacheTimeout":null,"colorBackground":false,"colorValue":true,"colors":["rgba(50, 172, 45, 0.97)","rgba(237, 129, 40, 0.89)","rgba(245, 54, 54, 0.9)"],"datasource":"${DS_GRAPHITE}","editable":true,"error":false,"format":"none","gauge":{"maxValue":300,"minValue":0,"show":true,"thresholdLabels":false,"thresholdMarkers":true},"id":25,"interval":null,"links":[],"maxDataPoints":100,"nullPointMode":"connected","nullText":null,"postfix":"","postfixFontSize":"50%","prefix":"","prefixFontSize":"50%","span":2,"sparkline":{"fillColor":"rgba(31, 118, 189, 0.18)","full":true,"lineColor":"rgb(31, 120, 193)","show":false},"targets":[{"refId":"A","target":"scale(apps.backend.backend_01.counters.requests.count, 0.8)"}],"thresholds":"200,270","title":"Logouts","type":"singlestat","valueFontSize":"100%","valueMaps":[{"op":"=","text":"N/A","value":"null"}],"valueName":"avg","mappingTypes":[{"name":"value to text","value":1},{"name":"range to text","value":2}],"rangeMaps":[{"from":"null","to":"null","text":"N/A"}],"mappingType":1}],"title":"New row"},{"collapsable":true,"collapse":false,"editable":true,"height":"250px","notice":false,"panels":[{"aliasColors":{"cpu":"#E24D42","memory":"#6ED0E0","statsd.fakesite.counters.session_start.desktop.count":"#6ED0E0"},"annotate":{"enable":false},"bars":false,"datasource":"${DS_GRAPHITE}","editable":true,"fill":3,"grid":{"max":null,"min":0,"threshold1":null,"threshold1Color":"rgba(216, 200, 27, 0.27)","threshold2":null,"threshold2Color":"rgba(234, 112, 112, 0.22)"},"id":4,"interactive":true,"legend":{"avg":false,"current":true,"max":false,"min":true,"show":true,"total":false,"values":false},"legend_counts":true,"lines":true,"linewidth":2,"nullPointMode":"connected","options":false,"percentage":false,"pointradius":5,"points":false,"renderer":"flot","resolution":100,"scale":1,"seriesOverrides":[{"alias":"cpu","fill":0,"lines":true,"yaxis":2,"zindex":2},{"alias":"memory","pointradius":2,"points":true}],"span":4,"spyable":true,"stack":false,"steppedLine":false,"targets":[{"hide":false,"refId":"A","target":"alias(movingAverage(scaleToSeconds(apps.fakesite.web_server_01.counters.request_status.code_302.count, 10), 20), \'cpu\')"},{"refId":"B","target":"alias(statsd.fakesite.counters.session_start.desktop.count, \'memory\')"}],"timeFrom":null,"timeShift":null,"timezone":"browser","title":"Memory / CPU","tooltip":{"msResolution":false,"query_as_alias":true,"shared":false,"value_type":"cumulative","sort":0},"type":"graph","xaxis":{"show":true},"yaxes":[{"format":"bytes","logBase":1,"max":null,"min":null,"show":true},{"format":"percent","logBase":1,"max":null,"min":0,"show":true}],"zerofill":true},{"aliasColors":{"logins":"#7EB26D","logins (-1 day)":"#447EBC"},"annotate":{"enable":false},"bars":false,"datasource":"${DS_GRAPHITE}","editable":true,"fill":1,"grid":{"max":null,"min":0,"threshold1":null,"threshold1Color":"rgba(216, 200, 27, 0.27)","threshold2":null,"threshold2Color":"rgba(234, 112, 112, 0.22)"},"id":3,"interactive":true,"legend":{"alignAsTable":false,"avg":false,"current":true,"max":true,"min":true,"rightSide":false,"show":true,"total":false,"values":false},"legend_counts":true,"lines":true,"linewidth":1,"nullPointMode":"connected","options":false,"percentage":false,"pointradius":5,"points":false,"renderer":"flot","resolution":100,"scale":1,"seriesOverrides":[],"span":4,"spyable":true,"stack":true,"steppedLine":false,"targets":[{"refId":"A","target":"alias(movingAverage(scaleToSeconds(apps.fakesite.web_server_01.counters.requests.count, 1), 2), \'logins\')"},{"refId":"B","target":"alias(movingAverage(timeShift(scaleToSeconds(apps.fakesite.web_server_01.counters.requests.count, 1), \'1h\'), 2), \'logins (-1 hour)\')"}],"timeFrom":null,"timeShift":"1h","timezone":"browser","title":"logins","tooltip":{"msResolution":false,"query_as_alias":true,"shared":false,"value_type":"cumulative","sort":0},"type":"graph","xaxis":{"show":true},"yaxes":[{"format":"short","logBase":1,"max":null,"min":null,"show":true},{"format":"short","logBase":1,"max":null,"min":null,"show":true}],"zerofill":true},{"aliasColors":{"cpu":"#E24D42","memory":"#6ED0E0","statsd.fakesite.counters.session_start.desktop.count":"#6ED0E0"},"annotate":{"enable":false},"bars":false,"datasource":"${DS_GRAPHITE}","editable":true,"fill":3,"grid":{"max":null,"min":0,"threshold1":null,"threshold1Color":"rgba(216, 200, 27, 0.27)","threshold2":null,"threshold2Color":"rgba(234, 112, 112, 0.22)"},"id":19,"interactive":true,"legend":{"avg":false,"current":true,"max":false,"min":true,"show":true,"total":false,"values":false},"legend_counts":true,"lines":true,"linewidth":2,"nullPointMode":"connected","options":false,"percentage":false,"pointradius":5,"points":false,"renderer":"flot","resolution":100,"scale":1,"seriesOverrides":[{"alias":"cpu","fill":0,"lines":true,"yaxis":2,"zindex":2},{"alias":"memory","pointradius":2,"points":true}],"span":4,"spyable":true,"stack":false,"steppedLine":false,"targets":[{"hide":false,"refId":"A","target":"alias(movingAverage(scaleToSeconds(apps.fakesite.web_server_01.counters.request_status.code_302.count, 10), 20), \'cpu\')"},{"refId":"B","target":"alias(statsd.fakesite.counters.session_start.desktop.count, \'memory\')"}],"timeFrom":null,"timeShift":"1h","timezone":"browser","title":"Memory / CPU","tooltip":{"msResolution":false,"query_as_alias":true,"shared":false,"value_type":"cumulative","sort":0},"type":"graph","xaxis":{"show":true},"yaxes":[{"format":"bytes","logBase":1,"max":null,"min":null,"show":true},{"format":"percent","logBase":1,"max":null,"min":0,"show":true}],"zerofill":true}],"title":"test"},{"collapsable":true,"collapse":false,"editable":true,"height":"300px","notice":false,"panels":[{"aliasColors":{"web_server_01":"#B7DBAB","web_server_02":"#7EB26D","web_server_03":"#508642","web_server_04":"#3F6833"},"annotate":{"enable":false},"bars":false,"datasource":"${DS_GRAPHITE}","editable":true,"fill":8,"grid":{"max":null,"min":0,"threshold1":null,"threshold1Color":"rgba(216, 200, 27, 0.27)","threshold2":null,"threshold2Color":"rgba(234, 112, 112, 0.22)"},"id":2,"interactive":true,"legend":{"alignAsTable":false,"avg":false,"current":false,"max":false,"min":false,"rightSide":false,"show":true,"total":false,"values":false},"legend_counts":true,"lines":true,"linewidth":2,"nullPointMode":"connected","options":false,"percentage":false,"pointradius":5,"points":false,"renderer":"flot","resolution":100,"scale":1,"seriesOverrides":[],"span":4,"spyable":true,"stack":true,"steppedLine":false,"targets":[{"refId":"A","target":"aliasByNode(movingAverage(scaleToSeconds(apps.fakesite.*.counters.requests.count, 1), 2), 2)"}],"timeFrom":null,"timeShift":null,"timezone":"browser","title":"server requests","tooltip":{"msResolution":false,"query_as_alias":true,"shared":true,"value_type":"cumulative","sort":0},"type":"graph","xaxis":{"show":true},"yaxes":[{"format":"short","logBase":1,"max":null,"min":null,"show":true},{"format":"short","logBase":1,"max":null,"min":null,"show":true}],"zerofill":true},{"aliasColors":{"upper_25":"#F9E2D2","upper_50":"#F2C96D","upper_75":"#EAB839"},"annotate":{"enable":false},"bars":true,"datasource":"${DS_GRAPHITE}","editable":true,"fill":1,"grid":{"max":null,"min":0,"threshold1":null,"threshold1Color":"rgba(216, 200, 27, 0.27)","threshold2":null,"threshold2Color":"rgba(234, 112, 112, 0.22)"},"id":5,"interactive":true,"legend":{"alignAsTable":true,"avg":true,"current":false,"max":false,"min":false,"rightSide":true,"show":true,"total":false,"values":true},"legend_counts":true,"lines":false,"linewidth":2,"nullPointMode":"connected","options":false,"percentage":false,"pointradius":5,"points":false,"renderer":"flot","resolution":100,"scale":1,"seriesOverrides":[],"span":4,"spyable":true,"stack":true,"steppedLine":false,"targets":[{"refId":"A","target":"aliasByNode(summarize(statsd.fakesite.timers.ads_timer.*, \'4min\', \'avg\'), 4)"}],"timeFrom":null,"timeShift":null,"timezone":"browser","title":"client side full page load","tooltip":{"msResolution":false,"query_as_alias":true,"shared":false,"value_type":"cumulative","sort":0},"type":"graph","xaxis":{"show":true},"yaxes":[{"format":"ms","logBase":1,"max":null,"min":null,"show":true},{"format":"short","logBase":1,"max":null,"min":null,"show":true}],"zerofill":true},{"aliasColors":{"web_server_01":"#B7DBAB","web_server_02":"#7EB26D","web_server_03":"#508642","web_server_04":"#3F6833"},"annotate":{"enable":false},"bars":false,"datasource":"${DS_GRAPHITE}","editable":true,"fill":8,"grid":{"max":null,"min":0,"threshold1":null,"threshold1Color":"rgba(216, 200, 27, 0.27)","threshold2":null,"threshold2Color":"rgba(234, 112, 112, 0.22)"},"id":14,"interactive":true,"legend":{"alignAsTable":false,"avg":false,"current":false,"max":false,"min":false,"rightSide":false,"show":true,"total":false,"values":false},"legend_counts":true,"lines":true,"linewidth":2,"nullPointMode":"connected","options":false,"percentage":false,"pointradius":5,"points":false,"renderer":"flot","resolution":100,"scale":1,"seriesOverrides":[],"span":4,"spyable":true,"stack":true,"steppedLine":false,"targets":[{"refId":"A","target":"aliasByNode(movingAverage(scaleToSeconds(apps.fakesite.*.counters.requests.count, 1), 2), 2)"}],"timeFrom":null,"timeShift":null,"timezone":"browser","title":"server requests","tooltip":{"msResolution":false,"query_as_alias":true,"shared":true,"value_type":"cumulative","sort":0},"type":"graph","xaxis":{"show":true},"yaxes":[{"format":"short","logBase":1,"max":null,"min":null,"show":true},{"format":"short","logBase":1,"max":null,"min":null,"show":true}],"zerofill":true}],"title":""},{"collapsable":true,"collapse":false,"editable":true,"height":"200px","notice":false,"panels":[{"aliasColors":{"cpu1":"#EF843C","cpu2":"#EAB839","upper_25":"#B7DBAB","upper_50":"#7EB26D","upper_75":"#629E51","upper_90":"#629E51","upper_95":"#508642"},"annotate":{"enable":false},"bars":false,"datasource":"${DS_GRAPHITE}","editable":true,"fill":3,"grid":{"max":null,"min":null,"threshold1":null,"threshold1Color":"rgba(216, 200, 27, 0.27)","threshold2":null,"threshold2Color":"rgba(234, 112, 112, 0.22)"},"id":6,"interactive":true,"legend":{"alignAsTable":true,"avg":true,"current":true,"legendSideLastValue":true,"max":false,"min":false,"rightSide":true,"show":false,"total":false,"values":true},"legend_counts":true,"lines":true,"linewidth":2,"links":[],"nullPointMode":"connected","options":false,"percentage":false,"pointradius":1,"points":false,"renderer":"flot","resolution":100,"scale":1,"seriesOverrides":[{"alias":"this is test of brekaing","yaxis":1}],"span":12,"spyable":true,"stack":false,"steppedLine":false,"targets":[{"refId":"A","target":"aliasByNode(statsd.fakesite.timers.ads_timer.*,4)"},{"refId":"B","target":"alias(scale(statsd.fakesite.timers.ads_timer.upper_95,-1),\'cpu1\')"},{"refId":"C","target":"alias(scale(statsd.fakesite.timers.ads_timer.upper_75,-1),\'cpu2\')"}],"timeFrom":null,"timeShift":null,"timezone":"browser","title":"","tooltip":{"msResolution":false,"query_as_alias":true,"shared":false,"value_type":"cumulative","sort":0},"transparent":true,"type":"graph","xaxis":{"show":false},"yaxes":[{"format":"ms","logBase":1,"max":null,"min":null,"show":false},{"format":"short","logBase":1,"max":null,"min":null,"show":false}],"zerofill":true}],"title":"test"}],"time":{"from":"now-30m","to":"now"},"timepicker":{"collapse":false,"enable":true,"notice":false,"now":true,"refresh_intervals":["5s","10s","30s","1m","5m","15m","30m","1h","2h","1d"],"status":"Stable","time_options":["5m","15m","1h","2h"," 6h","12h","24h","2d","7d","30d"],"type":"timepicker"},"templating":{"enable":false,"list":[]},"annotations":{"enable":false,"list":[]},"refresh":false,"schemaVersion":12,"version":5,"links":[],"gnetId":null},"overwrite":true,"inputs":[{"name":"DS_GRAPHITE","type":"datasource","pluginId":"graphite","value":"graphite"}]}' --compressed +done + diff --git a/vendor/github.com/prometheus/client_golang/LICENSE b/vendor/github.com/prometheus/client_golang/LICENSE new file mode 100644 index 00000000000..261eeb9e9f8 --- /dev/null +++ b/vendor/github.com/prometheus/client_golang/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/vendor/github.com/prometheus/client_golang/NOTICE b/vendor/github.com/prometheus/client_golang/NOTICE new file mode 100644 index 00000000000..dd878a30ee9 --- /dev/null +++ b/vendor/github.com/prometheus/client_golang/NOTICE @@ -0,0 +1,23 @@ +Prometheus instrumentation library for Go applications +Copyright 2012-2015 The Prometheus Authors + +This product includes software developed at +SoundCloud Ltd. (http://soundcloud.com/). + + +The following components are included in this product: + +perks - a fork of https://github.com/bmizerany/perks +https://github.com/beorn7/perks +Copyright 2013-2015 Blake Mizerany, Björn Rabenstein +See https://github.com/beorn7/perks/blob/master/README.md for license details. + +Go support for Protocol Buffers - Google's data interchange format +http://github.com/golang/protobuf/ +Copyright 2010 The Go Authors +See source code for license details. + +Support for streaming Protocol Buffer messages for the Go language (golang). +https://github.com/matttproud/golang_protobuf_extensions +Copyright 2013 Matt T. Proud +Licensed under the Apache License, Version 2.0 diff --git a/vendor/github.com/prometheus/client_golang/api/prometheus/api.go b/vendor/github.com/prometheus/client_golang/api/prometheus/api.go new file mode 100644 index 00000000000..cc5cbc364d3 --- /dev/null +++ b/vendor/github.com/prometheus/client_golang/api/prometheus/api.go @@ -0,0 +1,348 @@ +// Copyright 2015 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Package prometheus provides bindings to the Prometheus HTTP API: +// http://prometheus.io/docs/querying/api/ +package prometheus + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net" + "net/http" + "net/url" + "path" + "strconv" + "strings" + "time" + + "github.com/prometheus/common/model" + "golang.org/x/net/context" + "golang.org/x/net/context/ctxhttp" +) + +const ( + statusAPIError = 422 + apiPrefix = "/api/v1" + + epQuery = "/query" + epQueryRange = "/query_range" + epLabelValues = "/label/:name/values" + epSeries = "/series" +) + +// ErrorType models the different API error types. +type ErrorType string + +// Possible values for ErrorType. +const ( + ErrBadData ErrorType = "bad_data" + ErrTimeout = "timeout" + ErrCanceled = "canceled" + ErrExec = "execution" + ErrBadResponse = "bad_response" +) + +// Error is an error returned by the API. +type Error struct { + Type ErrorType + Msg string +} + +func (e *Error) Error() string { + return fmt.Sprintf("%s: %s", e.Type, e.Msg) +} + +// CancelableTransport is like net.Transport but provides +// per-request cancelation functionality. +type CancelableTransport interface { + http.RoundTripper + CancelRequest(req *http.Request) +} + +// DefaultTransport is used if no Transport is set in Config. +var DefaultTransport CancelableTransport = &http.Transport{ + Proxy: http.ProxyFromEnvironment, + Dial: (&net.Dialer{ + Timeout: 30 * time.Second, + KeepAlive: 30 * time.Second, + }).Dial, + TLSHandshakeTimeout: 10 * time.Second, +} + +// Config defines configuration parameters for a new client. +type Config struct { + // The address of the Prometheus to connect to. + Address string + + // Transport is used by the Client to drive HTTP requests. If not + // provided, DefaultTransport will be used. + Transport CancelableTransport +} + +func (cfg *Config) transport() CancelableTransport { + if cfg.Transport == nil { + return DefaultTransport + } + return cfg.Transport +} + +// Client is the interface for an API client. +type Client interface { + url(ep string, args map[string]string) *url.URL + do(context.Context, *http.Request) (*http.Response, []byte, error) +} + +// New returns a new Client. +// +// It is safe to use the returned Client from multiple goroutines. +func New(cfg Config) (Client, error) { + u, err := url.Parse(cfg.Address) + if err != nil { + return nil, err + } + u.Path = strings.TrimRight(u.Path, "/") + apiPrefix + + return &httpClient{ + endpoint: u, + transport: cfg.transport(), + }, nil +} + +type httpClient struct { + endpoint *url.URL + transport CancelableTransport +} + +func (c *httpClient) url(ep string, args map[string]string) *url.URL { + p := path.Join(c.endpoint.Path, ep) + + for arg, val := range args { + arg = ":" + arg + p = strings.Replace(p, arg, val, -1) + } + + u := *c.endpoint + u.Path = p + + return &u +} + +func (c *httpClient) do(ctx context.Context, req *http.Request) (*http.Response, []byte, error) { + resp, err := ctxhttp.Do(ctx, &http.Client{Transport: c.transport}, req) + + defer func() { + if resp != nil { + resp.Body.Close() + } + }() + + if err != nil { + return nil, nil, err + } + + var body []byte + done := make(chan struct{}) + go func() { + body, err = ioutil.ReadAll(resp.Body) + close(done) + }() + + select { + case <-ctx.Done(): + err = resp.Body.Close() + <-done + if err == nil { + err = ctx.Err() + } + case <-done: + } + + return resp, body, err +} + +// apiClient wraps a regular client and processes successful API responses. +// Successful also includes responses that errored at the API level. +type apiClient struct { + Client +} + +type apiResponse struct { + Status string `json:"status"` + Data json.RawMessage `json:"data"` + ErrorType ErrorType `json:"errorType"` + Error string `json:"error"` +} + +func (c apiClient) do(ctx context.Context, req *http.Request) (*http.Response, []byte, error) { + resp, body, err := c.Client.do(ctx, req) + if err != nil { + return resp, body, err + } + + code := resp.StatusCode + + if code/100 != 2 && code != statusAPIError { + return resp, body, &Error{ + Type: ErrBadResponse, + Msg: fmt.Sprintf("bad response code %d", resp.StatusCode), + } + } + + var result apiResponse + + if err = json.Unmarshal(body, &result); err != nil { + return resp, body, &Error{ + Type: ErrBadResponse, + Msg: err.Error(), + } + } + + if (code == statusAPIError) != (result.Status == "error") { + err = &Error{ + Type: ErrBadResponse, + Msg: "inconsistent body for response code", + } + } + + if code == statusAPIError && result.Status == "error" { + err = &Error{ + Type: result.ErrorType, + Msg: result.Error, + } + } + + return resp, []byte(result.Data), err +} + +// Range represents a sliced time range. +type Range struct { + // The boundaries of the time range. + Start, End time.Time + // The maximum time between two slices within the boundaries. + Step time.Duration +} + +// queryResult contains result data for a query. +type queryResult struct { + Type model.ValueType `json:"resultType"` + Result interface{} `json:"result"` + + // The decoded value. + v model.Value +} + +func (qr *queryResult) UnmarshalJSON(b []byte) error { + v := struct { + Type model.ValueType `json:"resultType"` + Result json.RawMessage `json:"result"` + }{} + + err := json.Unmarshal(b, &v) + if err != nil { + return err + } + + switch v.Type { + case model.ValScalar: + var sv model.Scalar + err = json.Unmarshal(v.Result, &sv) + qr.v = &sv + + case model.ValVector: + var vv model.Vector + err = json.Unmarshal(v.Result, &vv) + qr.v = vv + + case model.ValMatrix: + var mv model.Matrix + err = json.Unmarshal(v.Result, &mv) + qr.v = mv + + default: + err = fmt.Errorf("unexpected value type %q", v.Type) + } + return err +} + +// QueryAPI provides bindings the Prometheus's query API. +type QueryAPI interface { + // Query performs a query for the given time. + Query(ctx context.Context, query string, ts time.Time) (model.Value, error) + // Query performs a query for the given range. + QueryRange(ctx context.Context, query string, r Range) (model.Value, error) +} + +// NewQueryAPI returns a new QueryAPI for the client. +// +// It is safe to use the returned QueryAPI from multiple goroutines. +func NewQueryAPI(c Client) QueryAPI { + return &httpQueryAPI{client: apiClient{c}} +} + +type httpQueryAPI struct { + client Client +} + +func (h *httpQueryAPI) Query(ctx context.Context, query string, ts time.Time) (model.Value, error) { + u := h.client.url(epQuery, nil) + q := u.Query() + + q.Set("query", query) + q.Set("time", ts.Format(time.RFC3339Nano)) + + u.RawQuery = q.Encode() + + req, _ := http.NewRequest("GET", u.String(), nil) + + _, body, err := h.client.do(ctx, req) + if err != nil { + return nil, err + } + + var qres queryResult + err = json.Unmarshal(body, &qres) + + return model.Value(qres.v), err +} + +func (h *httpQueryAPI) QueryRange(ctx context.Context, query string, r Range) (model.Value, error) { + u := h.client.url(epQueryRange, nil) + q := u.Query() + + var ( + start = r.Start.Format(time.RFC3339Nano) + end = r.End.Format(time.RFC3339Nano) + step = strconv.FormatFloat(r.Step.Seconds(), 'f', 3, 64) + ) + + q.Set("query", query) + q.Set("start", start) + q.Set("end", end) + q.Set("step", step) + + u.RawQuery = q.Encode() + + req, _ := http.NewRequest("GET", u.String(), nil) + + _, body, err := h.client.do(ctx, req) + if err != nil { + return nil, err + } + + var qres queryResult + err = json.Unmarshal(body, &qres) + + return model.Value(qres.v), err +} diff --git a/vendor/github.com/prometheus/common/LICENSE b/vendor/github.com/prometheus/common/LICENSE new file mode 100644 index 00000000000..261eeb9e9f8 --- /dev/null +++ b/vendor/github.com/prometheus/common/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/vendor/github.com/prometheus/common/NOTICE b/vendor/github.com/prometheus/common/NOTICE new file mode 100644 index 00000000000..636a2c1a5e8 --- /dev/null +++ b/vendor/github.com/prometheus/common/NOTICE @@ -0,0 +1,5 @@ +Common libraries shared by Prometheus Go components. +Copyright 2015 The Prometheus Authors + +This product includes software developed at +SoundCloud Ltd. (http://soundcloud.com/). diff --git a/vendor/github.com/prometheus/common/model/alert.go b/vendor/github.com/prometheus/common/model/alert.go new file mode 100644 index 00000000000..35e739c7ad2 --- /dev/null +++ b/vendor/github.com/prometheus/common/model/alert.go @@ -0,0 +1,136 @@ +// Copyright 2013 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package model + +import ( + "fmt" + "time" +) + +type AlertStatus string + +const ( + AlertFiring AlertStatus = "firing" + AlertResolved AlertStatus = "resolved" +) + +// Alert is a generic representation of an alert in the Prometheus eco-system. +type Alert struct { + // Label value pairs for purpose of aggregation, matching, and disposition + // dispatching. This must minimally include an "alertname" label. + Labels LabelSet `json:"labels"` + + // Extra key/value information which does not define alert identity. + Annotations LabelSet `json:"annotations"` + + // The known time range for this alert. Both ends are optional. + StartsAt time.Time `json:"startsAt,omitempty"` + EndsAt time.Time `json:"endsAt,omitempty"` + GeneratorURL string `json:"generatorURL"` +} + +// Name returns the name of the alert. It is equivalent to the "alertname" label. +func (a *Alert) Name() string { + return string(a.Labels[AlertNameLabel]) +} + +// Fingerprint returns a unique hash for the alert. It is equivalent to +// the fingerprint of the alert's label set. +func (a *Alert) Fingerprint() Fingerprint { + return a.Labels.Fingerprint() +} + +func (a *Alert) String() string { + s := fmt.Sprintf("%s[%s]", a.Name(), a.Fingerprint().String()[:7]) + if a.Resolved() { + return s + "[resolved]" + } + return s + "[active]" +} + +// Resolved returns true iff the activity interval ended in the past. +func (a *Alert) Resolved() bool { + return a.ResolvedAt(time.Now()) +} + +// ResolvedAt returns true off the activity interval ended before +// the given timestamp. +func (a *Alert) ResolvedAt(ts time.Time) bool { + if a.EndsAt.IsZero() { + return false + } + return !a.EndsAt.After(ts) +} + +// Status returns the status of the alert. +func (a *Alert) Status() AlertStatus { + if a.Resolved() { + return AlertResolved + } + return AlertFiring +} + +// Validate checks whether the alert data is inconsistent. +func (a *Alert) Validate() error { + if a.StartsAt.IsZero() { + return fmt.Errorf("start time missing") + } + if !a.EndsAt.IsZero() && a.EndsAt.Before(a.StartsAt) { + return fmt.Errorf("start time must be before end time") + } + if err := a.Labels.Validate(); err != nil { + return fmt.Errorf("invalid label set: %s", err) + } + if len(a.Labels) == 0 { + return fmt.Errorf("at least one label pair required") + } + if err := a.Annotations.Validate(); err != nil { + return fmt.Errorf("invalid annotations: %s", err) + } + return nil +} + +// Alert is a list of alerts that can be sorted in chronological order. +type Alerts []*Alert + +func (as Alerts) Len() int { return len(as) } +func (as Alerts) Swap(i, j int) { as[i], as[j] = as[j], as[i] } + +func (as Alerts) Less(i, j int) bool { + if as[i].StartsAt.Before(as[j].StartsAt) { + return true + } + if as[i].EndsAt.Before(as[j].EndsAt) { + return true + } + return as[i].Fingerprint() < as[j].Fingerprint() +} + +// HasFiring returns true iff one of the alerts is not resolved. +func (as Alerts) HasFiring() bool { + for _, a := range as { + if !a.Resolved() { + return true + } + } + return false +} + +// Status returns StatusFiring iff at least one of the alerts is firing. +func (as Alerts) Status() AlertStatus { + if as.HasFiring() { + return AlertFiring + } + return AlertResolved +} diff --git a/vendor/github.com/prometheus/common/model/fingerprinting.go b/vendor/github.com/prometheus/common/model/fingerprinting.go new file mode 100644 index 00000000000..fc4de4106e8 --- /dev/null +++ b/vendor/github.com/prometheus/common/model/fingerprinting.go @@ -0,0 +1,105 @@ +// Copyright 2013 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package model + +import ( + "fmt" + "strconv" +) + +// Fingerprint provides a hash-capable representation of a Metric. +// For our purposes, FNV-1A 64-bit is used. +type Fingerprint uint64 + +// FingerprintFromString transforms a string representation into a Fingerprint. +func FingerprintFromString(s string) (Fingerprint, error) { + num, err := strconv.ParseUint(s, 16, 64) + return Fingerprint(num), err +} + +// ParseFingerprint parses the input string into a fingerprint. +func ParseFingerprint(s string) (Fingerprint, error) { + num, err := strconv.ParseUint(s, 16, 64) + if err != nil { + return 0, err + } + return Fingerprint(num), nil +} + +func (f Fingerprint) String() string { + return fmt.Sprintf("%016x", uint64(f)) +} + +// Fingerprints represents a collection of Fingerprint subject to a given +// natural sorting scheme. It implements sort.Interface. +type Fingerprints []Fingerprint + +// Len implements sort.Interface. +func (f Fingerprints) Len() int { + return len(f) +} + +// Less implements sort.Interface. +func (f Fingerprints) Less(i, j int) bool { + return f[i] < f[j] +} + +// Swap implements sort.Interface. +func (f Fingerprints) Swap(i, j int) { + f[i], f[j] = f[j], f[i] +} + +// FingerprintSet is a set of Fingerprints. +type FingerprintSet map[Fingerprint]struct{} + +// Equal returns true if both sets contain the same elements (and not more). +func (s FingerprintSet) Equal(o FingerprintSet) bool { + if len(s) != len(o) { + return false + } + + for k := range s { + if _, ok := o[k]; !ok { + return false + } + } + + return true +} + +// Intersection returns the elements contained in both sets. +func (s FingerprintSet) Intersection(o FingerprintSet) FingerprintSet { + myLength, otherLength := len(s), len(o) + if myLength == 0 || otherLength == 0 { + return FingerprintSet{} + } + + subSet := s + superSet := o + + if otherLength < myLength { + subSet = o + superSet = s + } + + out := FingerprintSet{} + + for k := range subSet { + if _, ok := superSet[k]; ok { + out[k] = struct{}{} + } + } + + return out +} diff --git a/vendor/github.com/prometheus/common/model/fnv.go b/vendor/github.com/prometheus/common/model/fnv.go new file mode 100644 index 00000000000..038fc1c9003 --- /dev/null +++ b/vendor/github.com/prometheus/common/model/fnv.go @@ -0,0 +1,42 @@ +// Copyright 2015 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package model + +// Inline and byte-free variant of hash/fnv's fnv64a. + +const ( + offset64 = 14695981039346656037 + prime64 = 1099511628211 +) + +// hashNew initializies a new fnv64a hash value. +func hashNew() uint64 { + return offset64 +} + +// hashAdd adds a string to a fnv64a hash value, returning the updated hash. +func hashAdd(h uint64, s string) uint64 { + for i := 0; i < len(s); i++ { + h ^= uint64(s[i]) + h *= prime64 + } + return h +} + +// hashAddByte adds a byte to a fnv64a hash value, returning the updated hash. +func hashAddByte(h uint64, b byte) uint64 { + h ^= uint64(b) + h *= prime64 + return h +} diff --git a/vendor/github.com/prometheus/common/model/labels.go b/vendor/github.com/prometheus/common/model/labels.go new file mode 100644 index 00000000000..3b72e7ff8f6 --- /dev/null +++ b/vendor/github.com/prometheus/common/model/labels.go @@ -0,0 +1,206 @@ +// Copyright 2013 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package model + +import ( + "encoding/json" + "fmt" + "regexp" + "strings" + "unicode/utf8" +) + +const ( + // AlertNameLabel is the name of the label containing the an alert's name. + AlertNameLabel = "alertname" + + // ExportedLabelPrefix is the prefix to prepend to the label names present in + // exported metrics if a label of the same name is added by the server. + ExportedLabelPrefix = "exported_" + + // MetricNameLabel is the label name indicating the metric name of a + // timeseries. + MetricNameLabel = "__name__" + + // SchemeLabel is the name of the label that holds the scheme on which to + // scrape a target. + SchemeLabel = "__scheme__" + + // AddressLabel is the name of the label that holds the address of + // a scrape target. + AddressLabel = "__address__" + + // MetricsPathLabel is the name of the label that holds the path on which to + // scrape a target. + MetricsPathLabel = "__metrics_path__" + + // ReservedLabelPrefix is a prefix which is not legal in user-supplied + // label names. + ReservedLabelPrefix = "__" + + // MetaLabelPrefix is a prefix for labels that provide meta information. + // Labels with this prefix are used for intermediate label processing and + // will not be attached to time series. + MetaLabelPrefix = "__meta_" + + // TmpLabelPrefix is a prefix for temporary labels as part of relabelling. + // Labels with this prefix are used for intermediate label processing and + // will not be attached to time series. This is reserved for use in + // Prometheus configuration files by users. + TmpLabelPrefix = "__tmp_" + + // ParamLabelPrefix is a prefix for labels that provide URL parameters + // used to scrape a target. + ParamLabelPrefix = "__param_" + + // JobLabel is the label name indicating the job from which a timeseries + // was scraped. + JobLabel = "job" + + // InstanceLabel is the label name used for the instance label. + InstanceLabel = "instance" + + // BucketLabel is used for the label that defines the upper bound of a + // bucket of a histogram ("le" -> "less or equal"). + BucketLabel = "le" + + // QuantileLabel is used for the label that defines the quantile in a + // summary. + QuantileLabel = "quantile" +) + +// LabelNameRE is a regular expression matching valid label names. +var LabelNameRE = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$") + +// A LabelName is a key for a LabelSet or Metric. It has a value associated +// therewith. +type LabelName string + +// IsValid is true iff the label name matches the pattern of LabelNameRE. +func (ln LabelName) IsValid() bool { + if len(ln) == 0 { + return false + } + for i, b := range ln { + if !((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || b == '_' || (b >= '0' && b <= '9' && i > 0)) { + return false + } + } + return true +} + +// UnmarshalYAML implements the yaml.Unmarshaler interface. +func (ln *LabelName) UnmarshalYAML(unmarshal func(interface{}) error) error { + var s string + if err := unmarshal(&s); err != nil { + return err + } + if !LabelNameRE.MatchString(s) { + return fmt.Errorf("%q is not a valid label name", s) + } + *ln = LabelName(s) + return nil +} + +// UnmarshalJSON implements the json.Unmarshaler interface. +func (ln *LabelName) UnmarshalJSON(b []byte) error { + var s string + if err := json.Unmarshal(b, &s); err != nil { + return err + } + if !LabelNameRE.MatchString(s) { + return fmt.Errorf("%q is not a valid label name", s) + } + *ln = LabelName(s) + return nil +} + +// LabelNames is a sortable LabelName slice. In implements sort.Interface. +type LabelNames []LabelName + +func (l LabelNames) Len() int { + return len(l) +} + +func (l LabelNames) Less(i, j int) bool { + return l[i] < l[j] +} + +func (l LabelNames) Swap(i, j int) { + l[i], l[j] = l[j], l[i] +} + +func (l LabelNames) String() string { + labelStrings := make([]string, 0, len(l)) + for _, label := range l { + labelStrings = append(labelStrings, string(label)) + } + return strings.Join(labelStrings, ", ") +} + +// A LabelValue is an associated value for a LabelName. +type LabelValue string + +// IsValid returns true iff the string is a valid UTF8. +func (lv LabelValue) IsValid() bool { + return utf8.ValidString(string(lv)) +} + +// LabelValues is a sortable LabelValue slice. It implements sort.Interface. +type LabelValues []LabelValue + +func (l LabelValues) Len() int { + return len(l) +} + +func (l LabelValues) Less(i, j int) bool { + return string(l[i]) < string(l[j]) +} + +func (l LabelValues) Swap(i, j int) { + l[i], l[j] = l[j], l[i] +} + +// LabelPair pairs a name with a value. +type LabelPair struct { + Name LabelName + Value LabelValue +} + +// LabelPairs is a sortable slice of LabelPair pointers. It implements +// sort.Interface. +type LabelPairs []*LabelPair + +func (l LabelPairs) Len() int { + return len(l) +} + +func (l LabelPairs) Less(i, j int) bool { + switch { + case l[i].Name > l[j].Name: + return false + case l[i].Name < l[j].Name: + return true + case l[i].Value > l[j].Value: + return false + case l[i].Value < l[j].Value: + return true + default: + return false + } +} + +func (l LabelPairs) Swap(i, j int) { + l[i], l[j] = l[j], l[i] +} diff --git a/vendor/github.com/prometheus/common/model/labelset.go b/vendor/github.com/prometheus/common/model/labelset.go new file mode 100644 index 00000000000..5f931cdb9b3 --- /dev/null +++ b/vendor/github.com/prometheus/common/model/labelset.go @@ -0,0 +1,169 @@ +// Copyright 2013 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package model + +import ( + "encoding/json" + "fmt" + "sort" + "strings" +) + +// A LabelSet is a collection of LabelName and LabelValue pairs. The LabelSet +// may be fully-qualified down to the point where it may resolve to a single +// Metric in the data store or not. All operations that occur within the realm +// of a LabelSet can emit a vector of Metric entities to which the LabelSet may +// match. +type LabelSet map[LabelName]LabelValue + +// Validate checks whether all names and values in the label set +// are valid. +func (ls LabelSet) Validate() error { + for ln, lv := range ls { + if !ln.IsValid() { + return fmt.Errorf("invalid name %q", ln) + } + if !lv.IsValid() { + return fmt.Errorf("invalid value %q", lv) + } + } + return nil +} + +// Equal returns true iff both label sets have exactly the same key/value pairs. +func (ls LabelSet) Equal(o LabelSet) bool { + if len(ls) != len(o) { + return false + } + for ln, lv := range ls { + olv, ok := o[ln] + if !ok { + return false + } + if olv != lv { + return false + } + } + return true +} + +// Before compares the metrics, using the following criteria: +// +// If m has fewer labels than o, it is before o. If it has more, it is not. +// +// If the number of labels is the same, the superset of all label names is +// sorted alphanumerically. The first differing label pair found in that order +// determines the outcome: If the label does not exist at all in m, then m is +// before o, and vice versa. Otherwise the label value is compared +// alphanumerically. +// +// If m and o are equal, the method returns false. +func (ls LabelSet) Before(o LabelSet) bool { + if len(ls) < len(o) { + return true + } + if len(ls) > len(o) { + return false + } + + lns := make(LabelNames, 0, len(ls)+len(o)) + for ln := range ls { + lns = append(lns, ln) + } + for ln := range o { + lns = append(lns, ln) + } + // It's probably not worth it to de-dup lns. + sort.Sort(lns) + for _, ln := range lns { + mlv, ok := ls[ln] + if !ok { + return true + } + olv, ok := o[ln] + if !ok { + return false + } + if mlv < olv { + return true + } + if mlv > olv { + return false + } + } + return false +} + +// Clone returns a copy of the label set. +func (ls LabelSet) Clone() LabelSet { + lsn := make(LabelSet, len(ls)) + for ln, lv := range ls { + lsn[ln] = lv + } + return lsn +} + +// Merge is a helper function to non-destructively merge two label sets. +func (l LabelSet) Merge(other LabelSet) LabelSet { + result := make(LabelSet, len(l)) + + for k, v := range l { + result[k] = v + } + + for k, v := range other { + result[k] = v + } + + return result +} + +func (l LabelSet) String() string { + lstrs := make([]string, 0, len(l)) + for l, v := range l { + lstrs = append(lstrs, fmt.Sprintf("%s=%q", l, v)) + } + + sort.Strings(lstrs) + return fmt.Sprintf("{%s}", strings.Join(lstrs, ", ")) +} + +// Fingerprint returns the LabelSet's fingerprint. +func (ls LabelSet) Fingerprint() Fingerprint { + return labelSetToFingerprint(ls) +} + +// FastFingerprint returns the LabelSet's Fingerprint calculated by a faster hashing +// algorithm, which is, however, more susceptible to hash collisions. +func (ls LabelSet) FastFingerprint() Fingerprint { + return labelSetToFastFingerprint(ls) +} + +// UnmarshalJSON implements the json.Unmarshaler interface. +func (l *LabelSet) UnmarshalJSON(b []byte) error { + var m map[LabelName]LabelValue + if err := json.Unmarshal(b, &m); err != nil { + return err + } + // encoding/json only unmarshals maps of the form map[string]T. It treats + // LabelName as a string and does not call its UnmarshalJSON method. + // Thus, we have to replicate the behavior here. + for ln := range m { + if !LabelNameRE.MatchString(string(ln)) { + return fmt.Errorf("%q is not a valid label name", ln) + } + } + *l = LabelSet(m) + return nil +} diff --git a/vendor/github.com/prometheus/common/model/metric.go b/vendor/github.com/prometheus/common/model/metric.go new file mode 100644 index 00000000000..a5da59a5055 --- /dev/null +++ b/vendor/github.com/prometheus/common/model/metric.go @@ -0,0 +1,98 @@ +// Copyright 2013 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package model + +import ( + "fmt" + "regexp" + "sort" + "strings" +) + +var ( + separator = []byte{0} + MetricNameRE = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_:]*$`) +) + +// A Metric is similar to a LabelSet, but the key difference is that a Metric is +// a singleton and refers to one and only one stream of samples. +type Metric LabelSet + +// Equal compares the metrics. +func (m Metric) Equal(o Metric) bool { + return LabelSet(m).Equal(LabelSet(o)) +} + +// Before compares the metrics' underlying label sets. +func (m Metric) Before(o Metric) bool { + return LabelSet(m).Before(LabelSet(o)) +} + +// Clone returns a copy of the Metric. +func (m Metric) Clone() Metric { + clone := Metric{} + for k, v := range m { + clone[k] = v + } + return clone +} + +func (m Metric) String() string { + metricName, hasName := m[MetricNameLabel] + numLabels := len(m) - 1 + if !hasName { + numLabels = len(m) + } + labelStrings := make([]string, 0, numLabels) + for label, value := range m { + if label != MetricNameLabel { + labelStrings = append(labelStrings, fmt.Sprintf("%s=%q", label, value)) + } + } + + switch numLabels { + case 0: + if hasName { + return string(metricName) + } + return "{}" + default: + sort.Strings(labelStrings) + return fmt.Sprintf("%s{%s}", metricName, strings.Join(labelStrings, ", ")) + } +} + +// Fingerprint returns a Metric's Fingerprint. +func (m Metric) Fingerprint() Fingerprint { + return LabelSet(m).Fingerprint() +} + +// FastFingerprint returns a Metric's Fingerprint calculated by a faster hashing +// algorithm, which is, however, more susceptible to hash collisions. +func (m Metric) FastFingerprint() Fingerprint { + return LabelSet(m).FastFingerprint() +} + +// IsValidMetricName returns true iff name matches the pattern of MetricNameRE. +func IsValidMetricName(n LabelValue) bool { + if len(n) == 0 { + return false + } + for i, b := range n { + if !((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || b == '_' || b == ':' || (b >= '0' && b <= '9' && i > 0)) { + return false + } + } + return true +} diff --git a/vendor/github.com/prometheus/common/model/model.go b/vendor/github.com/prometheus/common/model/model.go new file mode 100644 index 00000000000..a7b9691707e --- /dev/null +++ b/vendor/github.com/prometheus/common/model/model.go @@ -0,0 +1,16 @@ +// Copyright 2013 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Package model contains common data structures that are shared across +// Prometheus components and libraries. +package model diff --git a/vendor/github.com/prometheus/common/model/signature.go b/vendor/github.com/prometheus/common/model/signature.go new file mode 100644 index 00000000000..8762b13c63d --- /dev/null +++ b/vendor/github.com/prometheus/common/model/signature.go @@ -0,0 +1,144 @@ +// Copyright 2014 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package model + +import ( + "sort" +) + +// SeparatorByte is a byte that cannot occur in valid UTF-8 sequences and is +// used to separate label names, label values, and other strings from each other +// when calculating their combined hash value (aka signature aka fingerprint). +const SeparatorByte byte = 255 + +var ( + // cache the signature of an empty label set. + emptyLabelSignature = hashNew() +) + +// LabelsToSignature returns a quasi-unique signature (i.e., fingerprint) for a +// given label set. (Collisions are possible but unlikely if the number of label +// sets the function is applied to is small.) +func LabelsToSignature(labels map[string]string) uint64 { + if len(labels) == 0 { + return emptyLabelSignature + } + + labelNames := make([]string, 0, len(labels)) + for labelName := range labels { + labelNames = append(labelNames, labelName) + } + sort.Strings(labelNames) + + sum := hashNew() + for _, labelName := range labelNames { + sum = hashAdd(sum, labelName) + sum = hashAddByte(sum, SeparatorByte) + sum = hashAdd(sum, labels[labelName]) + sum = hashAddByte(sum, SeparatorByte) + } + return sum +} + +// labelSetToFingerprint works exactly as LabelsToSignature but takes a LabelSet as +// parameter (rather than a label map) and returns a Fingerprint. +func labelSetToFingerprint(ls LabelSet) Fingerprint { + if len(ls) == 0 { + return Fingerprint(emptyLabelSignature) + } + + labelNames := make(LabelNames, 0, len(ls)) + for labelName := range ls { + labelNames = append(labelNames, labelName) + } + sort.Sort(labelNames) + + sum := hashNew() + for _, labelName := range labelNames { + sum = hashAdd(sum, string(labelName)) + sum = hashAddByte(sum, SeparatorByte) + sum = hashAdd(sum, string(ls[labelName])) + sum = hashAddByte(sum, SeparatorByte) + } + return Fingerprint(sum) +} + +// labelSetToFastFingerprint works similar to labelSetToFingerprint but uses a +// faster and less allocation-heavy hash function, which is more susceptible to +// create hash collisions. Therefore, collision detection should be applied. +func labelSetToFastFingerprint(ls LabelSet) Fingerprint { + if len(ls) == 0 { + return Fingerprint(emptyLabelSignature) + } + + var result uint64 + for labelName, labelValue := range ls { + sum := hashNew() + sum = hashAdd(sum, string(labelName)) + sum = hashAddByte(sum, SeparatorByte) + sum = hashAdd(sum, string(labelValue)) + result ^= sum + } + return Fingerprint(result) +} + +// SignatureForLabels works like LabelsToSignature but takes a Metric as +// parameter (rather than a label map) and only includes the labels with the +// specified LabelNames into the signature calculation. The labels passed in +// will be sorted by this function. +func SignatureForLabels(m Metric, labels ...LabelName) uint64 { + if len(labels) == 0 { + return emptyLabelSignature + } + + sort.Sort(LabelNames(labels)) + + sum := hashNew() + for _, label := range labels { + sum = hashAdd(sum, string(label)) + sum = hashAddByte(sum, SeparatorByte) + sum = hashAdd(sum, string(m[label])) + sum = hashAddByte(sum, SeparatorByte) + } + return sum +} + +// SignatureWithoutLabels works like LabelsToSignature but takes a Metric as +// parameter (rather than a label map) and excludes the labels with any of the +// specified LabelNames from the signature calculation. +func SignatureWithoutLabels(m Metric, labels map[LabelName]struct{}) uint64 { + if len(m) == 0 { + return emptyLabelSignature + } + + labelNames := make(LabelNames, 0, len(m)) + for labelName := range m { + if _, exclude := labels[labelName]; !exclude { + labelNames = append(labelNames, labelName) + } + } + if len(labelNames) == 0 { + return emptyLabelSignature + } + sort.Sort(labelNames) + + sum := hashNew() + for _, labelName := range labelNames { + sum = hashAdd(sum, string(labelName)) + sum = hashAddByte(sum, SeparatorByte) + sum = hashAdd(sum, string(m[labelName])) + sum = hashAddByte(sum, SeparatorByte) + } + return sum +} diff --git a/vendor/github.com/prometheus/common/model/silence.go b/vendor/github.com/prometheus/common/model/silence.go new file mode 100644 index 00000000000..7538e299774 --- /dev/null +++ b/vendor/github.com/prometheus/common/model/silence.go @@ -0,0 +1,106 @@ +// Copyright 2015 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package model + +import ( + "encoding/json" + "fmt" + "regexp" + "time" +) + +// Matcher describes a matches the value of a given label. +type Matcher struct { + Name LabelName `json:"name"` + Value string `json:"value"` + IsRegex bool `json:"isRegex"` +} + +func (m *Matcher) UnmarshalJSON(b []byte) error { + type plain Matcher + if err := json.Unmarshal(b, (*plain)(m)); err != nil { + return err + } + + if len(m.Name) == 0 { + return fmt.Errorf("label name in matcher must not be empty") + } + if m.IsRegex { + if _, err := regexp.Compile(m.Value); err != nil { + return err + } + } + return nil +} + +// Validate returns true iff all fields of the matcher have valid values. +func (m *Matcher) Validate() error { + if !m.Name.IsValid() { + return fmt.Errorf("invalid name %q", m.Name) + } + if m.IsRegex { + if _, err := regexp.Compile(m.Value); err != nil { + return fmt.Errorf("invalid regular expression %q", m.Value) + } + } else if !LabelValue(m.Value).IsValid() || len(m.Value) == 0 { + return fmt.Errorf("invalid value %q", m.Value) + } + return nil +} + +// Silence defines the representation of a silence definiton +// in the Prometheus eco-system. +type Silence struct { + ID uint64 `json:"id,omitempty"` + + Matchers []*Matcher `json:"matchers"` + + StartsAt time.Time `json:"startsAt"` + EndsAt time.Time `json:"endsAt"` + + CreatedAt time.Time `json:"createdAt,omitempty"` + CreatedBy string `json:"createdBy"` + Comment string `json:"comment,omitempty"` +} + +// Validate returns true iff all fields of the silence have valid values. +func (s *Silence) Validate() error { + if len(s.Matchers) == 0 { + return fmt.Errorf("at least one matcher required") + } + for _, m := range s.Matchers { + if err := m.Validate(); err != nil { + return fmt.Errorf("invalid matcher: %s", err) + } + } + if s.StartsAt.IsZero() { + return fmt.Errorf("start time missing") + } + if s.EndsAt.IsZero() { + return fmt.Errorf("end time missing") + } + if s.EndsAt.Before(s.StartsAt) { + return fmt.Errorf("start time must be before end time") + } + if s.CreatedBy == "" { + return fmt.Errorf("creator information missing") + } + if s.Comment == "" { + return fmt.Errorf("comment missing") + } + if s.CreatedAt.IsZero() { + return fmt.Errorf("creation timestamp missing") + } + return nil +} diff --git a/vendor/github.com/prometheus/common/model/time.go b/vendor/github.com/prometheus/common/model/time.go new file mode 100644 index 00000000000..548968aebe6 --- /dev/null +++ b/vendor/github.com/prometheus/common/model/time.go @@ -0,0 +1,249 @@ +// Copyright 2013 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package model + +import ( + "fmt" + "math" + "regexp" + "strconv" + "strings" + "time" +) + +const ( + // MinimumTick is the minimum supported time resolution. This has to be + // at least time.Second in order for the code below to work. + minimumTick = time.Millisecond + // second is the Time duration equivalent to one second. + second = int64(time.Second / minimumTick) + // The number of nanoseconds per minimum tick. + nanosPerTick = int64(minimumTick / time.Nanosecond) + + // Earliest is the earliest Time representable. Handy for + // initializing a high watermark. + Earliest = Time(math.MinInt64) + // Latest is the latest Time representable. Handy for initializing + // a low watermark. + Latest = Time(math.MaxInt64) +) + +// Time is the number of milliseconds since the epoch +// (1970-01-01 00:00 UTC) excluding leap seconds. +type Time int64 + +// Interval describes and interval between two timestamps. +type Interval struct { + Start, End Time +} + +// Now returns the current time as a Time. +func Now() Time { + return TimeFromUnixNano(time.Now().UnixNano()) +} + +// TimeFromUnix returns the Time equivalent to the Unix Time t +// provided in seconds. +func TimeFromUnix(t int64) Time { + return Time(t * second) +} + +// TimeFromUnixNano returns the Time equivalent to the Unix Time +// t provided in nanoseconds. +func TimeFromUnixNano(t int64) Time { + return Time(t / nanosPerTick) +} + +// Equal reports whether two Times represent the same instant. +func (t Time) Equal(o Time) bool { + return t == o +} + +// Before reports whether the Time t is before o. +func (t Time) Before(o Time) bool { + return t < o +} + +// After reports whether the Time t is after o. +func (t Time) After(o Time) bool { + return t > o +} + +// Add returns the Time t + d. +func (t Time) Add(d time.Duration) Time { + return t + Time(d/minimumTick) +} + +// Sub returns the Duration t - o. +func (t Time) Sub(o Time) time.Duration { + return time.Duration(t-o) * minimumTick +} + +// Time returns the time.Time representation of t. +func (t Time) Time() time.Time { + return time.Unix(int64(t)/second, (int64(t)%second)*nanosPerTick) +} + +// Unix returns t as a Unix time, the number of seconds elapsed +// since January 1, 1970 UTC. +func (t Time) Unix() int64 { + return int64(t) / second +} + +// UnixNano returns t as a Unix time, the number of nanoseconds elapsed +// since January 1, 1970 UTC. +func (t Time) UnixNano() int64 { + return int64(t) * nanosPerTick +} + +// The number of digits after the dot. +var dotPrecision = int(math.Log10(float64(second))) + +// String returns a string representation of the Time. +func (t Time) String() string { + return strconv.FormatFloat(float64(t)/float64(second), 'f', -1, 64) +} + +// MarshalJSON implements the json.Marshaler interface. +func (t Time) MarshalJSON() ([]byte, error) { + return []byte(t.String()), nil +} + +// UnmarshalJSON implements the json.Unmarshaler interface. +func (t *Time) UnmarshalJSON(b []byte) error { + p := strings.Split(string(b), ".") + switch len(p) { + case 1: + v, err := strconv.ParseInt(string(p[0]), 10, 64) + if err != nil { + return err + } + *t = Time(v * second) + + case 2: + v, err := strconv.ParseInt(string(p[0]), 10, 64) + if err != nil { + return err + } + v *= second + + prec := dotPrecision - len(p[1]) + if prec < 0 { + p[1] = p[1][:dotPrecision] + } else if prec > 0 { + p[1] = p[1] + strings.Repeat("0", prec) + } + + va, err := strconv.ParseInt(p[1], 10, 32) + if err != nil { + return err + } + + *t = Time(v + va) + + default: + return fmt.Errorf("invalid time %q", string(b)) + } + return nil +} + +// Duration wraps time.Duration. It is used to parse the custom duration format +// from YAML. +// This type should not propagate beyond the scope of input/output processing. +type Duration time.Duration + +var durationRE = regexp.MustCompile("^([0-9]+)(y|w|d|h|m|s|ms)$") + +// StringToDuration parses a string into a time.Duration, assuming that a year +// always has 365d, a week always has 7d, and a day always has 24h. +func ParseDuration(durationStr string) (Duration, error) { + matches := durationRE.FindStringSubmatch(durationStr) + if len(matches) != 3 { + return 0, fmt.Errorf("not a valid duration string: %q", durationStr) + } + var ( + n, _ = strconv.Atoi(matches[1]) + dur = time.Duration(n) * time.Millisecond + ) + switch unit := matches[2]; unit { + case "y": + dur *= 1000 * 60 * 60 * 24 * 365 + case "w": + dur *= 1000 * 60 * 60 * 24 * 7 + case "d": + dur *= 1000 * 60 * 60 * 24 + case "h": + dur *= 1000 * 60 * 60 + case "m": + dur *= 1000 * 60 + case "s": + dur *= 1000 + case "ms": + // Value already correct + default: + return 0, fmt.Errorf("invalid time unit in duration string: %q", unit) + } + return Duration(dur), nil +} + +func (d Duration) String() string { + var ( + ms = int64(time.Duration(d) / time.Millisecond) + unit = "ms" + ) + factors := map[string]int64{ + "y": 1000 * 60 * 60 * 24 * 365, + "w": 1000 * 60 * 60 * 24 * 7, + "d": 1000 * 60 * 60 * 24, + "h": 1000 * 60 * 60, + "m": 1000 * 60, + "s": 1000, + "ms": 1, + } + + switch int64(0) { + case ms % factors["y"]: + unit = "y" + case ms % factors["w"]: + unit = "w" + case ms % factors["d"]: + unit = "d" + case ms % factors["h"]: + unit = "h" + case ms % factors["m"]: + unit = "m" + case ms % factors["s"]: + unit = "s" + } + return fmt.Sprintf("%v%v", ms/factors[unit], unit) +} + +// MarshalYAML implements the yaml.Marshaler interface. +func (d Duration) MarshalYAML() (interface{}, error) { + return d.String(), nil +} + +// UnmarshalYAML implements the yaml.Unmarshaler interface. +func (d *Duration) UnmarshalYAML(unmarshal func(interface{}) error) error { + var s string + if err := unmarshal(&s); err != nil { + return err + } + dur, err := ParseDuration(s) + if err != nil { + return err + } + *d = dur + return nil +} diff --git a/vendor/github.com/prometheus/common/model/value.go b/vendor/github.com/prometheus/common/model/value.go new file mode 100644 index 00000000000..dbf5d10e431 --- /dev/null +++ b/vendor/github.com/prometheus/common/model/value.go @@ -0,0 +1,403 @@ +// Copyright 2013 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package model + +import ( + "encoding/json" + "fmt" + "math" + "sort" + "strconv" + "strings" +) + +// A SampleValue is a representation of a value for a given sample at a given +// time. +type SampleValue float64 + +// MarshalJSON implements json.Marshaler. +func (v SampleValue) MarshalJSON() ([]byte, error) { + return json.Marshal(v.String()) +} + +// UnmarshalJSON implements json.Unmarshaler. +func (v *SampleValue) UnmarshalJSON(b []byte) error { + if len(b) < 2 || b[0] != '"' || b[len(b)-1] != '"' { + return fmt.Errorf("sample value must be a quoted string") + } + f, err := strconv.ParseFloat(string(b[1:len(b)-1]), 64) + if err != nil { + return err + } + *v = SampleValue(f) + return nil +} + +// Equal returns true if the value of v and o is equal or if both are NaN. Note +// that v==o is false if both are NaN. If you want the conventional float +// behavior, use == to compare two SampleValues. +func (v SampleValue) Equal(o SampleValue) bool { + if v == o { + return true + } + return math.IsNaN(float64(v)) && math.IsNaN(float64(o)) +} + +func (v SampleValue) String() string { + return strconv.FormatFloat(float64(v), 'f', -1, 64) +} + +// SamplePair pairs a SampleValue with a Timestamp. +type SamplePair struct { + Timestamp Time + Value SampleValue +} + +// MarshalJSON implements json.Marshaler. +func (s SamplePair) MarshalJSON() ([]byte, error) { + t, err := json.Marshal(s.Timestamp) + if err != nil { + return nil, err + } + v, err := json.Marshal(s.Value) + if err != nil { + return nil, err + } + return []byte(fmt.Sprintf("[%s,%s]", t, v)), nil +} + +// UnmarshalJSON implements json.Unmarshaler. +func (s *SamplePair) UnmarshalJSON(b []byte) error { + v := [...]json.Unmarshaler{&s.Timestamp, &s.Value} + return json.Unmarshal(b, &v) +} + +// Equal returns true if this SamplePair and o have equal Values and equal +// Timestamps. The sematics of Value equality is defined by SampleValue.Equal. +func (s *SamplePair) Equal(o *SamplePair) bool { + return s == o || (s.Value.Equal(o.Value) && s.Timestamp.Equal(o.Timestamp)) +} + +func (s SamplePair) String() string { + return fmt.Sprintf("%s @[%s]", s.Value, s.Timestamp) +} + +// Sample is a sample pair associated with a metric. +type Sample struct { + Metric Metric `json:"metric"` + Value SampleValue `json:"value"` + Timestamp Time `json:"timestamp"` +} + +// Equal compares first the metrics, then the timestamp, then the value. The +// sematics of value equality is defined by SampleValue.Equal. +func (s *Sample) Equal(o *Sample) bool { + if s == o { + return true + } + + if !s.Metric.Equal(o.Metric) { + return false + } + if !s.Timestamp.Equal(o.Timestamp) { + return false + } + if s.Value.Equal(o.Value) { + return false + } + + return true +} + +func (s Sample) String() string { + return fmt.Sprintf("%s => %s", s.Metric, SamplePair{ + Timestamp: s.Timestamp, + Value: s.Value, + }) +} + +// MarshalJSON implements json.Marshaler. +func (s Sample) MarshalJSON() ([]byte, error) { + v := struct { + Metric Metric `json:"metric"` + Value SamplePair `json:"value"` + }{ + Metric: s.Metric, + Value: SamplePair{ + Timestamp: s.Timestamp, + Value: s.Value, + }, + } + + return json.Marshal(&v) +} + +// UnmarshalJSON implements json.Unmarshaler. +func (s *Sample) UnmarshalJSON(b []byte) error { + v := struct { + Metric Metric `json:"metric"` + Value SamplePair `json:"value"` + }{ + Metric: s.Metric, + Value: SamplePair{ + Timestamp: s.Timestamp, + Value: s.Value, + }, + } + + if err := json.Unmarshal(b, &v); err != nil { + return err + } + + s.Metric = v.Metric + s.Timestamp = v.Value.Timestamp + s.Value = v.Value.Value + + return nil +} + +// Samples is a sortable Sample slice. It implements sort.Interface. +type Samples []*Sample + +func (s Samples) Len() int { + return len(s) +} + +// Less compares first the metrics, then the timestamp. +func (s Samples) Less(i, j int) bool { + switch { + case s[i].Metric.Before(s[j].Metric): + return true + case s[j].Metric.Before(s[i].Metric): + return false + case s[i].Timestamp.Before(s[j].Timestamp): + return true + default: + return false + } +} + +func (s Samples) Swap(i, j int) { + s[i], s[j] = s[j], s[i] +} + +// Equal compares two sets of samples and returns true if they are equal. +func (s Samples) Equal(o Samples) bool { + if len(s) != len(o) { + return false + } + + for i, sample := range s { + if !sample.Equal(o[i]) { + return false + } + } + return true +} + +// SampleStream is a stream of Values belonging to an attached COWMetric. +type SampleStream struct { + Metric Metric `json:"metric"` + Values []SamplePair `json:"values"` +} + +func (ss SampleStream) String() string { + vals := make([]string, len(ss.Values)) + for i, v := range ss.Values { + vals[i] = v.String() + } + return fmt.Sprintf("%s =>\n%s", ss.Metric, strings.Join(vals, "\n")) +} + +// Value is a generic interface for values resulting from a query evaluation. +type Value interface { + Type() ValueType + String() string +} + +func (Matrix) Type() ValueType { return ValMatrix } +func (Vector) Type() ValueType { return ValVector } +func (*Scalar) Type() ValueType { return ValScalar } +func (*String) Type() ValueType { return ValString } + +type ValueType int + +const ( + ValNone ValueType = iota + ValScalar + ValVector + ValMatrix + ValString +) + +// MarshalJSON implements json.Marshaler. +func (et ValueType) MarshalJSON() ([]byte, error) { + return json.Marshal(et.String()) +} + +func (et *ValueType) UnmarshalJSON(b []byte) error { + var s string + if err := json.Unmarshal(b, &s); err != nil { + return err + } + switch s { + case "": + *et = ValNone + case "scalar": + *et = ValScalar + case "vector": + *et = ValVector + case "matrix": + *et = ValMatrix + case "string": + *et = ValString + default: + return fmt.Errorf("unknown value type %q", s) + } + return nil +} + +func (e ValueType) String() string { + switch e { + case ValNone: + return "" + case ValScalar: + return "scalar" + case ValVector: + return "vector" + case ValMatrix: + return "matrix" + case ValString: + return "string" + } + panic("ValueType.String: unhandled value type") +} + +// Scalar is a scalar value evaluated at the set timestamp. +type Scalar struct { + Value SampleValue `json:"value"` + Timestamp Time `json:"timestamp"` +} + +func (s Scalar) String() string { + return fmt.Sprintf("scalar: %v @[%v]", s.Value, s.Timestamp) +} + +// MarshalJSON implements json.Marshaler. +func (s Scalar) MarshalJSON() ([]byte, error) { + v := strconv.FormatFloat(float64(s.Value), 'f', -1, 64) + return json.Marshal([...]interface{}{s.Timestamp, string(v)}) +} + +// UnmarshalJSON implements json.Unmarshaler. +func (s *Scalar) UnmarshalJSON(b []byte) error { + var f string + v := [...]interface{}{&s.Timestamp, &f} + + if err := json.Unmarshal(b, &v); err != nil { + return err + } + + value, err := strconv.ParseFloat(f, 64) + if err != nil { + return fmt.Errorf("error parsing sample value: %s", err) + } + s.Value = SampleValue(value) + return nil +} + +// String is a string value evaluated at the set timestamp. +type String struct { + Value string `json:"value"` + Timestamp Time `json:"timestamp"` +} + +func (s *String) String() string { + return s.Value +} + +// MarshalJSON implements json.Marshaler. +func (s String) MarshalJSON() ([]byte, error) { + return json.Marshal([]interface{}{s.Timestamp, s.Value}) +} + +// UnmarshalJSON implements json.Unmarshaler. +func (s *String) UnmarshalJSON(b []byte) error { + v := [...]interface{}{&s.Timestamp, &s.Value} + return json.Unmarshal(b, &v) +} + +// Vector is basically only an alias for Samples, but the +// contract is that in a Vector, all Samples have the same timestamp. +type Vector []*Sample + +func (vec Vector) String() string { + entries := make([]string, len(vec)) + for i, s := range vec { + entries[i] = s.String() + } + return strings.Join(entries, "\n") +} + +func (vec Vector) Len() int { return len(vec) } +func (vec Vector) Swap(i, j int) { vec[i], vec[j] = vec[j], vec[i] } + +// Less compares first the metrics, then the timestamp. +func (vec Vector) Less(i, j int) bool { + switch { + case vec[i].Metric.Before(vec[j].Metric): + return true + case vec[j].Metric.Before(vec[i].Metric): + return false + case vec[i].Timestamp.Before(vec[j].Timestamp): + return true + default: + return false + } +} + +// Equal compares two sets of samples and returns true if they are equal. +func (vec Vector) Equal(o Vector) bool { + if len(vec) != len(o) { + return false + } + + for i, sample := range vec { + if !sample.Equal(o[i]) { + return false + } + } + return true +} + +// Matrix is a list of time series. +type Matrix []*SampleStream + +func (m Matrix) Len() int { return len(m) } +func (m Matrix) Less(i, j int) bool { return m[i].Metric.Before(m[j].Metric) } +func (m Matrix) Swap(i, j int) { m[i], m[j] = m[j], m[i] } + +func (mat Matrix) String() string { + matCp := make(Matrix, len(mat)) + copy(matCp, mat) + sort.Sort(matCp) + + strs := make([]string, len(matCp)) + + for i, ss := range matCp { + strs[i] = ss.String() + } + + return strings.Join(strs, "\n") +} diff --git a/vendor/golang.org/x/net/LICENSE b/vendor/golang.org/x/net/LICENSE new file mode 100644 index 00000000000..6a66aea5eaf --- /dev/null +++ b/vendor/golang.org/x/net/LICENSE @@ -0,0 +1,27 @@ +Copyright (c) 2009 The Go Authors. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google Inc. nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/vendor/golang.org/x/net/PATENTS b/vendor/golang.org/x/net/PATENTS new file mode 100644 index 00000000000..733099041f8 --- /dev/null +++ b/vendor/golang.org/x/net/PATENTS @@ -0,0 +1,22 @@ +Additional IP Rights Grant (Patents) + +"This implementation" means the copyrightable works distributed by +Google as part of the Go project. + +Google hereby grants to You a perpetual, worldwide, non-exclusive, +no-charge, royalty-free, irrevocable (except as stated in this section) +patent license to make, have made, use, offer to sell, sell, import, +transfer and otherwise run, modify and propagate the contents of this +implementation of Go, where such license applies only to those patent +claims, both currently owned or controlled by Google and acquired in +the future, licensable by Google that are necessarily infringed by this +implementation of Go. This grant does not include claims that would be +infringed only as a consequence of further modification of this +implementation. If you or your agent or exclusive licensee institute or +order or agree to the institution of patent litigation against any +entity (including a cross-claim or counterclaim in a lawsuit) alleging +that this implementation of Go or any code incorporated within this +implementation of Go constitutes direct or contributory patent +infringement, or inducement of patent infringement, then any patent +rights granted to you under this License for this implementation of Go +shall terminate as of the date such litigation is filed. diff --git a/vendor/golang.org/x/net/context/ctxhttp/ctxhttp.go b/vendor/golang.org/x/net/context/ctxhttp/ctxhttp.go new file mode 100644 index 00000000000..606cf1f9726 --- /dev/null +++ b/vendor/golang.org/x/net/context/ctxhttp/ctxhttp.go @@ -0,0 +1,74 @@ +// Copyright 2016 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build go1.7 + +// Package ctxhttp provides helper functions for performing context-aware HTTP requests. +package ctxhttp // import "golang.org/x/net/context/ctxhttp" + +import ( + "io" + "net/http" + "net/url" + "strings" + + "golang.org/x/net/context" +) + +// Do sends an HTTP request with the provided http.Client and returns +// an HTTP response. +// +// If the client is nil, http.DefaultClient is used. +// +// The provided ctx must be non-nil. If it is canceled or times out, +// ctx.Err() will be returned. +func Do(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error) { + if client == nil { + client = http.DefaultClient + } + resp, err := client.Do(req.WithContext(ctx)) + // If we got an error, and the context has been canceled, + // the context's error is probably more useful. + if err != nil { + select { + case <-ctx.Done(): + err = ctx.Err() + default: + } + } + return resp, err +} + +// Get issues a GET request via the Do function. +func Get(ctx context.Context, client *http.Client, url string) (*http.Response, error) { + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return nil, err + } + return Do(ctx, client, req) +} + +// Head issues a HEAD request via the Do function. +func Head(ctx context.Context, client *http.Client, url string) (*http.Response, error) { + req, err := http.NewRequest("HEAD", url, nil) + if err != nil { + return nil, err + } + return Do(ctx, client, req) +} + +// Post issues a POST request via the Do function. +func Post(ctx context.Context, client *http.Client, url string, bodyType string, body io.Reader) (*http.Response, error) { + req, err := http.NewRequest("POST", url, body) + if err != nil { + return nil, err + } + req.Header.Set("Content-Type", bodyType) + return Do(ctx, client, req) +} + +// PostForm issues a POST request via the Do function. +func PostForm(ctx context.Context, client *http.Client, url string, data url.Values) (*http.Response, error) { + return Post(ctx, client, url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode())) +} diff --git a/vendor/golang.org/x/net/context/ctxhttp/ctxhttp_pre17.go b/vendor/golang.org/x/net/context/ctxhttp/ctxhttp_pre17.go new file mode 100644 index 00000000000..926870cc23f --- /dev/null +++ b/vendor/golang.org/x/net/context/ctxhttp/ctxhttp_pre17.go @@ -0,0 +1,147 @@ +// Copyright 2015 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build !go1.7 + +package ctxhttp // import "golang.org/x/net/context/ctxhttp" + +import ( + "io" + "net/http" + "net/url" + "strings" + + "golang.org/x/net/context" +) + +func nop() {} + +var ( + testHookContextDoneBeforeHeaders = nop + testHookDoReturned = nop + testHookDidBodyClose = nop +) + +// Do sends an HTTP request with the provided http.Client and returns an HTTP response. +// If the client is nil, http.DefaultClient is used. +// If the context is canceled or times out, ctx.Err() will be returned. +func Do(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error) { + if client == nil { + client = http.DefaultClient + } + + // TODO(djd): Respect any existing value of req.Cancel. + cancel := make(chan struct{}) + req.Cancel = cancel + + type responseAndError struct { + resp *http.Response + err error + } + result := make(chan responseAndError, 1) + + // Make local copies of test hooks closed over by goroutines below. + // Prevents data races in tests. + testHookDoReturned := testHookDoReturned + testHookDidBodyClose := testHookDidBodyClose + + go func() { + resp, err := client.Do(req) + testHookDoReturned() + result <- responseAndError{resp, err} + }() + + var resp *http.Response + + select { + case <-ctx.Done(): + testHookContextDoneBeforeHeaders() + close(cancel) + // Clean up after the goroutine calling client.Do: + go func() { + if r := <-result; r.resp != nil { + testHookDidBodyClose() + r.resp.Body.Close() + } + }() + return nil, ctx.Err() + case r := <-result: + var err error + resp, err = r.resp, r.err + if err != nil { + return resp, err + } + } + + c := make(chan struct{}) + go func() { + select { + case <-ctx.Done(): + close(cancel) + case <-c: + // The response's Body is closed. + } + }() + resp.Body = ¬ifyingReader{resp.Body, c} + + return resp, nil +} + +// Get issues a GET request via the Do function. +func Get(ctx context.Context, client *http.Client, url string) (*http.Response, error) { + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return nil, err + } + return Do(ctx, client, req) +} + +// Head issues a HEAD request via the Do function. +func Head(ctx context.Context, client *http.Client, url string) (*http.Response, error) { + req, err := http.NewRequest("HEAD", url, nil) + if err != nil { + return nil, err + } + return Do(ctx, client, req) +} + +// Post issues a POST request via the Do function. +func Post(ctx context.Context, client *http.Client, url string, bodyType string, body io.Reader) (*http.Response, error) { + req, err := http.NewRequest("POST", url, body) + if err != nil { + return nil, err + } + req.Header.Set("Content-Type", bodyType) + return Do(ctx, client, req) +} + +// PostForm issues a POST request via the Do function. +func PostForm(ctx context.Context, client *http.Client, url string, data url.Values) (*http.Response, error) { + return Post(ctx, client, url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode())) +} + +// notifyingReader is an io.ReadCloser that closes the notify channel after +// Close is called or a Read fails on the underlying ReadCloser. +type notifyingReader struct { + io.ReadCloser + notify chan<- struct{} +} + +func (r *notifyingReader) Read(p []byte) (int, error) { + n, err := r.ReadCloser.Read(p) + if err != nil && r.notify != nil { + close(r.notify) + r.notify = nil + } + return n, err +} + +func (r *notifyingReader) Close() error { + err := r.ReadCloser.Close() + if r.notify != nil { + close(r.notify) + r.notify = nil + } + return err +} diff --git a/vendor/gopkg.in/guregu/null.v3/LICENSE b/vendor/gopkg.in/guregu/null.v3/LICENSE new file mode 100644 index 00000000000..69062b45b16 --- /dev/null +++ b/vendor/gopkg.in/guregu/null.v3/LICENSE @@ -0,0 +1,10 @@ +Copyright (c) 2014, Greg Roseberry +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/vendor/gopkg.in/guregu/null.v3/README.md b/vendor/gopkg.in/guregu/null.v3/README.md new file mode 100644 index 00000000000..62bf48be93e --- /dev/null +++ b/vendor/gopkg.in/guregu/null.v3/README.md @@ -0,0 +1,75 @@ +## null [![GoDoc](https://godoc.org/github.com/guregu/null?status.svg)](https://godoc.org/github.com/guregu/null) [![Coverage](http://gocover.io/_badge/github.com/guregu/null)](http://gocover.io/github.com/guregu/null) +`import "gopkg.in/guregu/null.v3"` + +null is a library with reasonable options for dealing with nullable SQL and JSON values + +There are two packages: `null` and its subpackage `zero`. + +Types in `null` will only be considered null on null input, and will JSON encode to `null`. If you need zero and null be considered separate values, use these. + +Types in `zero` are treated like zero values in Go: blank string input will produce a null `zero.String`, and null Strings will JSON encode to `""`. Zero values of these types will be considered null to SQL. If you need zero and null treated the same, use these. + +All types implement `sql.Scanner` and `driver.Valuer`, so you can use this library in place of `sql.NullXXX`. All types also implement: `encoding.TextMarshaler`, `encoding.TextUnmarshaler`, `json.Marshaler`, and `json.Unmarshaler`. + +### null package + +`import "gopkg.in/guregu/null.v3"` + +#### null.String +Nullable string. + +Marshals to JSON null if SQL source data is null. Zero (blank) input will not produce a null String. Can unmarshal from `sql.NullString` JSON input or string input. + +#### null.Int +Nullable int64. + +Marshals to JSON null if SQL source data is null. Zero input will not produce a null Int. Can unmarshal from `sql.NullInt64` JSON input. + +#### null.Float +Nullable float64. + +Marshals to JSON null if SQL source data is null. Zero input will not produce a null Float. Can unmarshal from `sql.NullFloat64` JSON input. + +#### null.Bool +Nullable bool. + +Marshals to JSON null if SQL source data is null. False input will not produce a null Bool. Can unmarshal from `sql.NullBool` JSON input. + +#### null.Time + +Marshals to JSON null if SQL source data is null. Uses `time.Time`'s marshaler. Can unmarshal from `pq.NullTime` and similar JSON input. + +### zero package + +`import "gopkg.in/guregu/null.v3/zero"` + +#### zero.String +Nullable string. + +Will marshal to a blank string if null. Blank string input produces a null String. Null values and zero values are considered equivalent. Can unmarshal from `sql.NullString` JSON input. + +#### zero.Int +Nullable int64. + +Will marshal to 0 if null. 0 produces a null Int. Null values and zero values are considered equivalent. Can unmarshal from `sql.NullInt64` JSON input. + +#### zero.Float +Nullable float64. + +Will marshal to 0 if null. 0.0 produces a null Float. Null values and zero values are considered equivalent. Can unmarshal from `sql.NullFloat64` JSON input. + +#### zero.Bool +Nullable bool. + +Will marshal to false if null. `false` produces a null Float. Null values and zero values are considered equivalent. Can unmarshal from `sql.NullBool` JSON input. + +#### zero.Time + +Will marshal to the zero time if null. Uses `time.Time`'s marshaler. Can unmarshal from `pq.NullTime` and similar JSON input. + + +### Bugs +`json`'s `",omitempty"` struct tag does not work correctly right now. It will never omit a null or empty String. This might be [fixed eventually](https://github.com/golang/go/issues/4357). + +### License +BSD diff --git a/vendor/gopkg.in/guregu/null.v3/bool.go b/vendor/gopkg.in/guregu/null.v3/bool.go new file mode 100644 index 00000000000..1ec13961bfe --- /dev/null +++ b/vendor/gopkg.in/guregu/null.v3/bool.go @@ -0,0 +1,129 @@ +package null + +import ( + "database/sql" + "encoding/json" + "errors" + "fmt" + "reflect" +) + +// Bool is a nullable bool. +// It does not consider false values to be null. +// It will decode to null, not false, if null. +type Bool struct { + sql.NullBool +} + +// NewBool creates a new Bool +func NewBool(b bool, valid bool) Bool { + return Bool{ + NullBool: sql.NullBool{ + Bool: b, + Valid: valid, + }, + } +} + +// BoolFrom creates a new Bool that will always be valid. +func BoolFrom(b bool) Bool { + return NewBool(b, true) +} + +// BoolFromPtr creates a new Bool that will be null if f is nil. +func BoolFromPtr(b *bool) Bool { + if b == nil { + return NewBool(false, false) + } + return NewBool(*b, true) +} + +// UnmarshalJSON implements json.Unmarshaler. +// It supports number and null input. +// 0 will not be considered a null Bool. +// It also supports unmarshalling a sql.NullBool. +func (b *Bool) UnmarshalJSON(data []byte) error { + var err error + var v interface{} + if err = json.Unmarshal(data, &v); err != nil { + return err + } + switch x := v.(type) { + case bool: + b.Bool = x + case map[string]interface{}: + err = json.Unmarshal(data, &b.NullBool) + case nil: + b.Valid = false + return nil + default: + err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.Bool", reflect.TypeOf(v).Name()) + } + b.Valid = err == nil + return err +} + +// UnmarshalText implements encoding.TextUnmarshaler. +// It will unmarshal to a null Bool if the input is a blank or not an integer. +// It will return an error if the input is not an integer, blank, or "null". +func (b *Bool) UnmarshalText(text []byte) error { + str := string(text) + switch str { + case "", "null": + b.Valid = false + return nil + case "true": + b.Bool = true + case "false": + b.Bool = false + default: + b.Valid = false + return errors.New("invalid input:" + str) + } + b.Valid = true + return nil +} + +// MarshalJSON implements json.Marshaler. +// It will encode null if this Bool is null. +func (b Bool) MarshalJSON() ([]byte, error) { + if !b.Valid { + return []byte("null"), nil + } + if !b.Bool { + return []byte("false"), nil + } + return []byte("true"), nil +} + +// MarshalText implements encoding.TextMarshaler. +// It will encode a blank string if this Bool is null. +func (b Bool) MarshalText() ([]byte, error) { + if !b.Valid { + return []byte{}, nil + } + if !b.Bool { + return []byte("false"), nil + } + return []byte("true"), nil +} + +// SetValid changes this Bool's value and also sets it to be non-null. +func (b *Bool) SetValid(v bool) { + b.Bool = v + b.Valid = true +} + +// Ptr returns a pointer to this Bool's value, or a nil pointer if this Bool is null. +func (b Bool) Ptr() *bool { + if !b.Valid { + return nil + } + return &b.Bool +} + +// IsZero returns true for invalid Bools, for future omitempty support (Go 1.4?) +// A non-null Bool with a 0 value will not be considered zero. +func (b Bool) IsZero() bool { + return !b.Valid +} diff --git a/vendor/gopkg.in/guregu/null.v3/float.go b/vendor/gopkg.in/guregu/null.v3/float.go new file mode 100644 index 00000000000..1f57b959ab7 --- /dev/null +++ b/vendor/gopkg.in/guregu/null.v3/float.go @@ -0,0 +1,117 @@ +package null + +import ( + "database/sql" + "encoding/json" + "fmt" + "reflect" + "strconv" +) + +// Float is a nullable float64. +// It does not consider zero values to be null. +// It will decode to null, not zero, if null. +type Float struct { + sql.NullFloat64 +} + +// NewFloat creates a new Float +func NewFloat(f float64, valid bool) Float { + return Float{ + NullFloat64: sql.NullFloat64{ + Float64: f, + Valid: valid, + }, + } +} + +// FloatFrom creates a new Float that will always be valid. +func FloatFrom(f float64) Float { + return NewFloat(f, true) +} + +// FloatFromPtr creates a new Float that be null if f is nil. +func FloatFromPtr(f *float64) Float { + if f == nil { + return NewFloat(0, false) + } + return NewFloat(*f, true) +} + +// UnmarshalJSON implements json.Unmarshaler. +// It supports number and null input. +// 0 will not be considered a null Float. +// It also supports unmarshalling a sql.NullFloat64. +func (f *Float) UnmarshalJSON(data []byte) error { + var err error + var v interface{} + if err = json.Unmarshal(data, &v); err != nil { + return err + } + switch x := v.(type) { + case float64: + f.Float64 = float64(x) + case map[string]interface{}: + err = json.Unmarshal(data, &f.NullFloat64) + case nil: + f.Valid = false + return nil + default: + err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.Float", reflect.TypeOf(v).Name()) + } + f.Valid = err == nil + return err +} + +// UnmarshalText implements encoding.TextUnmarshaler. +// It will unmarshal to a null Float if the input is a blank or not an integer. +// It will return an error if the input is not an integer, blank, or "null". +func (f *Float) UnmarshalText(text []byte) error { + str := string(text) + if str == "" || str == "null" { + f.Valid = false + return nil + } + var err error + f.Float64, err = strconv.ParseFloat(string(text), 64) + f.Valid = err == nil + return err +} + +// MarshalJSON implements json.Marshaler. +// It will encode null if this Float is null. +func (f Float) MarshalJSON() ([]byte, error) { + if !f.Valid { + return []byte("null"), nil + } + return []byte(strconv.FormatFloat(f.Float64, 'f', -1, 64)), nil +} + +// MarshalText implements encoding.TextMarshaler. +// It will encode a blank string if this Float is null. +func (f Float) MarshalText() ([]byte, error) { + if !f.Valid { + return []byte{}, nil + } + return []byte(strconv.FormatFloat(f.Float64, 'f', -1, 64)), nil +} + +// SetValid changes this Float's value and also sets it to be non-null. +func (f *Float) SetValid(n float64) { + f.Float64 = n + f.Valid = true +} + +// Ptr returns a pointer to this Float's value, or a nil pointer if this Float is null. +func (f Float) Ptr() *float64 { + if !f.Valid { + return nil + } + return &f.Float64 +} + +// IsZero returns true for invalid Floats, for future omitempty support (Go 1.4?) +// A non-null Float with a 0 value will not be considered zero. +func (f Float) IsZero() bool { + return !f.Valid +} diff --git a/vendor/gopkg.in/guregu/null.v3/int.go b/vendor/gopkg.in/guregu/null.v3/int.go new file mode 100644 index 00000000000..981d17b09aa --- /dev/null +++ b/vendor/gopkg.in/guregu/null.v3/int.go @@ -0,0 +1,118 @@ +package null + +import ( + "database/sql" + "encoding/json" + "fmt" + "reflect" + "strconv" +) + +// Int is an nullable int64. +// It does not consider zero values to be null. +// It will decode to null, not zero, if null. +type Int struct { + sql.NullInt64 +} + +// NewInt creates a new Int +func NewInt(i int64, valid bool) Int { + return Int{ + NullInt64: sql.NullInt64{ + Int64: i, + Valid: valid, + }, + } +} + +// IntFrom creates a new Int that will always be valid. +func IntFrom(i int64) Int { + return NewInt(i, true) +} + +// IntFromPtr creates a new Int that be null if i is nil. +func IntFromPtr(i *int64) Int { + if i == nil { + return NewInt(0, false) + } + return NewInt(*i, true) +} + +// UnmarshalJSON implements json.Unmarshaler. +// It supports number and null input. +// 0 will not be considered a null Int. +// It also supports unmarshalling a sql.NullInt64. +func (i *Int) UnmarshalJSON(data []byte) error { + var err error + var v interface{} + if err = json.Unmarshal(data, &v); err != nil { + return err + } + switch v.(type) { + case float64: + // Unmarshal again, directly to int64, to avoid intermediate float64 + err = json.Unmarshal(data, &i.Int64) + case map[string]interface{}: + err = json.Unmarshal(data, &i.NullInt64) + case nil: + i.Valid = false + return nil + default: + err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.Int", reflect.TypeOf(v).Name()) + } + i.Valid = err == nil + return err +} + +// UnmarshalText implements encoding.TextUnmarshaler. +// It will unmarshal to a null Int if the input is a blank or not an integer. +// It will return an error if the input is not an integer, blank, or "null". +func (i *Int) UnmarshalText(text []byte) error { + str := string(text) + if str == "" || str == "null" { + i.Valid = false + return nil + } + var err error + i.Int64, err = strconv.ParseInt(string(text), 10, 64) + i.Valid = err == nil + return err +} + +// MarshalJSON implements json.Marshaler. +// It will encode null if this Int is null. +func (i Int) MarshalJSON() ([]byte, error) { + if !i.Valid { + return []byte("null"), nil + } + return []byte(strconv.FormatInt(i.Int64, 10)), nil +} + +// MarshalText implements encoding.TextMarshaler. +// It will encode a blank string if this Int is null. +func (i Int) MarshalText() ([]byte, error) { + if !i.Valid { + return []byte{}, nil + } + return []byte(strconv.FormatInt(i.Int64, 10)), nil +} + +// SetValid changes this Int's value and also sets it to be non-null. +func (i *Int) SetValid(n int64) { + i.Int64 = n + i.Valid = true +} + +// Ptr returns a pointer to this Int's value, or a nil pointer if this Int is null. +func (i Int) Ptr() *int64 { + if !i.Valid { + return nil + } + return &i.Int64 +} + +// IsZero returns true for invalid Ints, for future omitempty support (Go 1.4?) +// A non-null Int with a 0 value will not be considered zero. +func (i Int) IsZero() bool { + return !i.Valid +} diff --git a/vendor/gopkg.in/guregu/null.v3/string.go b/vendor/gopkg.in/guregu/null.v3/string.go new file mode 100644 index 00000000000..554aac820e3 --- /dev/null +++ b/vendor/gopkg.in/guregu/null.v3/string.go @@ -0,0 +1,110 @@ +// Package null contains SQL types that consider zero input and null input as separate values, +// with convenient support for JSON and text marshaling. +// Types in this package will always encode to their null value if null. +// Use the zero subpackage if you want zero values and null to be treated the same. +package null + +import ( + "database/sql" + "encoding/json" + "fmt" + "reflect" +) + +// String is a nullable string. It supports SQL and JSON serialization. +// It will marshal to null if null. Blank string input will be considered null. +type String struct { + sql.NullString +} + +// StringFrom creates a new String that will never be blank. +func StringFrom(s string) String { + return NewString(s, true) +} + +// StringFromPtr creates a new String that be null if s is nil. +func StringFromPtr(s *string) String { + if s == nil { + return NewString("", false) + } + return NewString(*s, true) +} + +// NewString creates a new String +func NewString(s string, valid bool) String { + return String{ + NullString: sql.NullString{ + String: s, + Valid: valid, + }, + } +} + +// UnmarshalJSON implements json.Unmarshaler. +// It supports string and null input. Blank string input does not produce a null String. +// It also supports unmarshalling a sql.NullString. +func (s *String) UnmarshalJSON(data []byte) error { + var err error + var v interface{} + if err = json.Unmarshal(data, &v); err != nil { + return err + } + switch x := v.(type) { + case string: + s.String = x + case map[string]interface{}: + err = json.Unmarshal(data, &s.NullString) + case nil: + s.Valid = false + return nil + default: + err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.String", reflect.TypeOf(v).Name()) + } + s.Valid = err == nil + return err +} + +// MarshalJSON implements json.Marshaler. +// It will encode null if this String is null. +func (s String) MarshalJSON() ([]byte, error) { + if !s.Valid { + return []byte("null"), nil + } + return json.Marshal(s.String) +} + +// MarshalText implements encoding.TextMarshaler. +// It will encode a blank string when this String is null. +func (s String) MarshalText() ([]byte, error) { + if !s.Valid { + return []byte{}, nil + } + return []byte(s.String), nil +} + +// UnmarshalText implements encoding.TextUnmarshaler. +// It will unmarshal to a null String if the input is a blank string. +func (s *String) UnmarshalText(text []byte) error { + s.String = string(text) + s.Valid = s.String != "" + return nil +} + +// SetValid changes this String's value and also sets it to be non-null. +func (s *String) SetValid(v string) { + s.String = v + s.Valid = true +} + +// Ptr returns a pointer to this String's value, or a nil pointer if this String is null. +func (s String) Ptr() *string { + if !s.Valid { + return nil + } + return &s.String +} + +// IsZero returns true for null strings, for potential future omitempty support. +func (s String) IsZero() bool { + return !s.Valid +} diff --git a/vendor/gopkg.in/guregu/null.v3/time.go b/vendor/gopkg.in/guregu/null.v3/time.go new file mode 100644 index 00000000000..a4d843920b4 --- /dev/null +++ b/vendor/gopkg.in/guregu/null.v3/time.go @@ -0,0 +1,135 @@ +package null + +import ( + "database/sql/driver" + "encoding/json" + "fmt" + "reflect" + "time" +) + +// Time is a nullable time.Time. It supports SQL and JSON serialization. +// It will marshal to null if null. +type Time struct { + Time time.Time + Valid bool +} + +// Scan implements the Scanner interface. +func (t *Time) Scan(value interface{}) error { + var err error + switch x := value.(type) { + case time.Time: + t.Time = x + case nil: + t.Valid = false + return nil + default: + err = fmt.Errorf("null: cannot scan type %T into null.Time: %v", value, value) + } + t.Valid = err == nil + return err +} + +// Value implements the driver Valuer interface. +func (t Time) Value() (driver.Value, error) { + if !t.Valid { + return nil, nil + } + return t.Time, nil +} + +// NewTime creates a new Time. +func NewTime(t time.Time, valid bool) Time { + return Time{ + Time: t, + Valid: valid, + } +} + +// TimeFrom creates a new Time that will always be valid. +func TimeFrom(t time.Time) Time { + return NewTime(t, true) +} + +// TimeFromPtr creates a new Time that will be null if t is nil. +func TimeFromPtr(t *time.Time) Time { + if t == nil { + return NewTime(time.Time{}, false) + } + return NewTime(*t, true) +} + +// MarshalJSON implements json.Marshaler. +// It will encode null if this time is null. +func (t Time) MarshalJSON() ([]byte, error) { + if !t.Valid { + return []byte("null"), nil + } + return t.Time.MarshalJSON() +} + +// UnmarshalJSON implements json.Unmarshaler. +// It supports string, object (e.g. pq.NullTime and friends) +// and null input. +func (t *Time) UnmarshalJSON(data []byte) error { + var err error + var v interface{} + if err = json.Unmarshal(data, &v); err != nil { + return err + } + switch x := v.(type) { + case string: + err = t.Time.UnmarshalJSON(data) + case map[string]interface{}: + ti, tiOK := x["Time"].(string) + valid, validOK := x["Valid"].(bool) + if !tiOK || !validOK { + return fmt.Errorf(`json: unmarshalling object into Go value of type null.Time requires key "Time" to be of type string and key "Valid" to be of type bool; found %T and %T, respectively`, x["Time"], x["Valid"]) + } + err = t.Time.UnmarshalText([]byte(ti)) + t.Valid = valid + return err + case nil: + t.Valid = false + return nil + default: + err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.Time", reflect.TypeOf(v).Name()) + } + t.Valid = err == nil + return err +} + +func (t Time) MarshalText() ([]byte, error) { + if !t.Valid { + return []byte("null"), nil + } + return t.Time.MarshalText() +} + +func (t *Time) UnmarshalText(text []byte) error { + str := string(text) + if str == "" || str == "null" { + t.Valid = false + return nil + } + if err := t.Time.UnmarshalText(text); err != nil { + return err + } + t.Valid = true + return nil +} + +// SetValid changes this Time's value and sets it to be non-null. +func (t *Time) SetValid(v time.Time) { + t.Time = v + t.Valid = true +} + +// Ptr returns a pointer to this Time's value, or a nil pointer if this Time is null. +func (t Time) Ptr() *time.Time { + if !t.Valid { + return nil + } + return &t.Time +} diff --git a/vendor/phantomjs/render.js b/vendor/phantomjs/render.js index 3e10ee852f9..2f62bfce955 100644 --- a/vendor/phantomjs/render.js +++ b/vendor/phantomjs/render.js @@ -12,17 +12,17 @@ params[parts[1]] = parts[2]; }); - var usage = "url= png= width= height= cookiename= sessionid= domain="; + var usage = "url= png= width= height= renderKey="; - if (!params.url || !params.png || !params.cookiename || ! params.sessionid || !params.domain) { + if (!params.url || !params.png || !params.renderKey || !params.domain) { console.log(usage); phantom.exit(); } phantom.addCookie({ - 'name': params.cookiename, - 'value': params.sessionid, - 'domain': params.domain + 'name': 'renderKey', + 'value': params.renderKey, + 'domain': 'localhost', }); page.viewportSize = { diff --git a/vendor/vendor.json b/vendor/vendor.json index aea55c068fb..05396911094 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -2,6 +2,18 @@ "comment": "", "ignore": "test", "package": [ + { + "checksumSHA1": "SMUvX2B8eoFd9wnPofwBKlN6btE=", + "path": "github.com/prometheus/client_golang/api/prometheus", + "revision": "5636dc67ae776adf5590da7349e70fbb9559972d", + "revisionTime": "2016-09-16T18:03:40Z" + }, + { + "checksumSHA1": "Jx0GXl5hGnO25s3ryyvtdWHdCpw=", + "path": "github.com/prometheus/common/model", + "revision": "9a94032291f2192936512bab367bc45e77990d6a", + "revisionTime": "2016-09-17T18:44:01Z" + }, { "checksumSHA1": "6AYg4fjEvFuAVN3wHakGApjhZAM=", "path": "github.com/smartystreets/assertions", @@ -37,6 +49,18 @@ "path": "github.com/smartystreets/goconvey/convey/reporting", "revision": "5db88ed452e937f2fd557de6f4f1af7f2eabed0b", "revisionTime": "2016-08-23T18:01:44Z" + }, + { + "checksumSHA1": "WHc3uByvGaMcnSoI21fhzYgbOgg=", + "path": "golang.org/x/net/context/ctxhttp", + "revision": "71a035914f99bb58fe82eac0f1289f10963d876c", + "revisionTime": "2016-09-12T21:59:12Z" + }, + { + "checksumSHA1": "PoHLopxwkiXxa3uVhezeq/qJ/Vo=", + "path": "gopkg.in/guregu/null.v3", + "revision": "41961cea0328defc5f95c1c473f89ebf0d1813f6", + "revisionTime": "2016-02-28T00:53:16Z" } ], "rootPath": "github.com/grafana/grafana"