diff --git a/board.go b/board.go index fb3f45d8..c25f9967 100644 --- a/board.go +++ b/board.go @@ -40,69 +40,72 @@ const ( type ( // Board represents Grafana dashboard. Board struct { - ID uint `json:"id"` - Slug string `json:"-"` - Title string `json:"title"` - OriginalTitle string `json:"originalTitle"` - Tags []string `json:"tags"` - Style string `json:"style"` - Timezone string `json:"timezone"` - Editable bool `json:"editable"` - HideControls bool `json:"hideControls" graf:"hide-controls"` - SharedCrosshair bool `json:"sharedCrosshair" graf:"shared-crosshair"` - Rows []*Row `json:"rows"` - Templating struct { - List []templateVar `json:"list"` - } `json:"templating"` - Annotations struct { - List []annotation `json:"list"` + ID uint `json:"id,omitempty"` + Slug string `json:"slug"` + Title string `json:"title"` + OriginalTitle string `json:"originalTitle"` + Tags []string `json:"tags"` + Style string `json:"style"` + Timezone string `json:"timezone"` + Editable bool `json:"editable"` + HideControls bool `json:"hideControls" graf:"hide-controls"` + SharedCrosshair bool `json:"sharedCrosshair" graf:"shared-crosshair"` + Rows []*Row `json:"rows"` + Templating Templating `json:"templating"` + Annotations struct { + List []Annotation `json:"list"` } `json:"annotations"` Refresh *BoolString `json:"refresh,omitempty"` SchemaVersion uint `json:"schemaVersion"` Version uint `json:"version"` Links []link `json:"links"` - Time struct { - From string `json:"from"` - To string `json:"to"` - } `json:"time"` - Timepicker struct { - Now *bool `json:"now,omitempty"` - RefreshIntervals []string `json:"refresh_intervals"` - TimeOptions []string `json:"time_options"` - } `json:"timepicker"` - lastPanelID uint - } - templateVar struct { + Time Time `json:"time"` + Timepicker Timepicker `json:"timepicker"` + lastPanelID uint + } + Time struct { + From string `json:"from"` + To string `json:"to"` + } + Timepicker struct { + Now *bool `json:"now,omitempty"` + RefreshIntervals []string `json:"refresh_intervals"` + TimeOptions []string `json:"time_options"` + } + Templating struct { + List []TemplateVar `json:"list"` + } + TemplateVar struct { Name string `json:"name"` Type string `json:"type"` Auto bool `json:"auto,omitempty"` AutoCount *int `json:"auto_count,omitempty"` Datasource *string `json:"datasource"` Refresh BoolInt `json:"refresh"` - Options []option `json:"options"` + Options []Option `json:"options"` IncludeAll bool `json:"includeAll"` AllFormat string `json:"allFormat"` Multi bool `json:"multi"` MultiFormat string `json:"multiFormat"` Query string `json:"query"` Regex string `json:"regex"` - Current current `json:"current"` + Current Current `json:"current"` Label string `json:"label"` Hide uint8 `json:"hide"` } // for templateVar - option struct { + Option struct { Text string `json:"text"` Value string `json:"value"` Selected bool `json:"selected"` } // for templateVar - current struct { + Current struct { Tags []*string `json:"tags,omitempty"` Text string `json:"text"` Value interface{} `json:"value"` // TODO select more precise type } - annotation struct { + Annotation struct { Name string `json:"name"` Datasource *string `json:"datasource"` ShowLine bool `json:"showLine"` @@ -206,7 +209,7 @@ func (b *Board) AddRow(title string) *Row { Collapse: false, Editable: true, Height: "250px", - board: b} + } b.Rows = append(b.Rows, row) return row } diff --git a/panel.go b/panel.go index 8a92debe..8345ed75 100644 --- a/panel.go +++ b/panel.go @@ -95,31 +95,32 @@ type ( Values bool `json:"values"` SideWidth *uint `json:"sideWidth,omitempty"` } `json:"legend,omitempty"` - LeftYAxisLabel *string `json:"leftYAxisLabel,omitempty"` - RightYAxisLabel *string `json:"rightYAxisLabel,omitempty"` - Lines bool `json:"lines"` - Linewidth uint `json:"linewidth"` - NullPointMode string `json:"nullPointMode"` - Percentage bool `json:"percentage"` - Pointradius int `json:"pointradius"` - Points bool `json:"points"` - SeriesOverrides []serieOverride `json:"seriesOverrides"` - Stack bool `json:"stack"` - SteppedLine bool `json:"steppedLine"` - Targets []Target `json:"targets,omitempty"` - TimeFrom *string `json:"timeFrom"` - TimeShift *string `json:"timeShift"` - Tooltip struct { - Shared bool `json:"shared"` - ValueType string `json:"value_type"` - MsResolution bool `json:"msResolution,omitempty"` // was added in Grafana 3.x - } `json:"tooltip"` - XAxis bool `json:"x-axis"` - YAxis bool `json:"y-axis"` - YFormats []string `json:"y_formats"` - Xaxis yaxis `json:"xaxis"` // was added in Grafana 4.x? - Yaxes []yaxis `json:"yaxes"` // was added in Grafana 4.x? - Decimals *uint `json:"decimals,omitempty"` + LeftYAxisLabel *string `json:"leftYAxisLabel,omitempty"` + RightYAxisLabel *string `json:"rightYAxisLabel,omitempty"` + Lines bool `json:"lines"` + Linewidth uint `json:"linewidth"` + NullPointMode string `json:"nullPointMode"` + Percentage bool `json:"percentage"` + Pointradius int `json:"pointradius"` + Points bool `json:"points"` + SeriesOverrides []SeriesOverride `json:"seriesOverrides,omitempty"` + Stack bool `json:"stack"` + SteppedLine bool `json:"steppedLine"` + Targets []Target `json:"targets,omitempty"` + TimeFrom *string `json:"timeFrom,omitempty"` + TimeShift *string `json:"timeShift,omitempty"` + Tooltip Tooltip `json:"tooltip"` + XAxis bool `json:"x-axis,omitempty"` + YAxis bool `json:"y-axis,omitempty"` + YFormats []string `json:"y_formats,omitempty"` + Xaxis Axis `json:"xaxis"` // was added in Grafana 4.x? + Yaxes []Axis `json:"yaxes"` // was added in Grafana 4.x? + Decimals *uint `json:"decimals,omitempty"` + } + Tooltip struct { + Shared bool `json:"shared"` + ValueType string `json:"value_type"` + MsResolution bool `json:"msResolution,omitempty"` // was added in Grafana 3.x } TablePanel struct { Columns []column `json:"columns"` @@ -210,14 +211,14 @@ type ( Show bool `json:"show"` Values *[]string `json:"values,omitempty"` } - yaxis struct { + Axis struct { Format string `json:"format"` LogBase int `json:"logBase"` - Max *IntString `json:"max"` - Min *IntString `json:"min"` + Max *IntString `json:"max,omitempty"` + Min *IntString `json:"min,omitempty"` Show bool `json:"show"` } - serieOverride struct { + SeriesOverride struct { Alias string `json:"alias"` Bars *bool `json:"bars,omitempty"` Color *string `json:"color,omitempty"` @@ -261,14 +262,14 @@ type valueMap struct { // for an any panel type Target struct { RefID string `json:"refId"` - Datasource string `json:"datasource"` + Datasource string `json:"datasource,omitempty"` // For Prometheus - Expr string `json:"expr"` - IntervalFactor int `json:"intervalFactor"` - Interval string `json:"interval"` - Step int `json:"step"` - LegendFormat string `json:"legendFormat"` + Expr string `json:"expr,omitempty"` + IntervalFactor int `json:"intervalFactor,omitempty"` + Interval string `json:"interval,omitempty"` + Step int `json:"step,omitempty"` + LegendFormat string `json:"legendFormat,omitempty"` // For Elasticsearch DsType *string `json:"dsType,omitempty"` @@ -288,6 +289,9 @@ type Target struct { MinDocCount int `json:"min_doc_count"` } `json:"settings"` } `json:"bucketAggs,omitempty"` + + // For Graphite + Target string `json:"target,omitempty"` } // NewDashlist initializes panel with a dashlist panel. diff --git a/rest-request.go b/rest-request.go index 33c75a3d..c8f6e83e 100644 --- a/rest-request.go +++ b/rest-request.go @@ -21,9 +21,11 @@ package sdk import ( "bytes" "fmt" + "io" "io/ioutil" "net/http" "net/url" + "path" ) // DefaultHTTPClient initialized Grafana with appropriate conditions. @@ -71,9 +73,9 @@ func (r *Client) delete(query string) ([]byte, int, error) { return r.doRequest("DELETE", query, nil, nil) } -func (r *Client) doRequest(method, query string, params url.Values, buf *bytes.Buffer) ([]byte, int, error) { +func (r *Client) doRequest(method, query string, params url.Values, buf io.Reader) ([]byte, int, error) { u, _ := url.Parse(r.baseURL) - u.Path = query + u.Path = path.Join(u.Path, query) if params != nil { u.RawQuery = params.Encode() } diff --git a/row.go b/row.go index 18c2c48b..018dcdf6 100644 --- a/row.go +++ b/row.go @@ -26,59 +26,60 @@ type Row struct { Editable bool `json:"editable"` Height height `json:"height"` Panels []Panel `json:"panels"` - board *Board } +var lastPanelID uint + func (r *Row) Add(panel *Panel) { - r.board.lastPanelID++ - panel.ID = r.board.lastPanelID + lastPanelID++ + panel.ID = lastPanelID r.Panels = append(r.Panels, *panel) } func (r *Row) AddDashlist(data *DashlistPanel) { - r.board.lastPanelID++ + lastPanelID++ panel := NewDashlist("") - panel.ID = r.board.lastPanelID + panel.ID = lastPanelID panel.DashlistPanel = data r.Panels = append(r.Panels, *panel) } func (r *Row) AddGraph(data *GraphPanel) { - r.board.lastPanelID++ + lastPanelID++ panel := NewGraph("") - panel.ID = r.board.lastPanelID + panel.ID = lastPanelID panel.GraphPanel = data r.Panels = append(r.Panels, *panel) } func (r *Row) AddTable(data *TablePanel) { - r.board.lastPanelID++ + lastPanelID++ panel := NewTable("") - panel.ID = r.board.lastPanelID + panel.ID = lastPanelID panel.TablePanel = data r.Panels = append(r.Panels, *panel) } func (r *Row) AddText(data *TextPanel) { - r.board.lastPanelID++ + lastPanelID++ panel := NewText("") - panel.ID = r.board.lastPanelID + panel.ID = lastPanelID panel.TextPanel = data r.Panels = append(r.Panels, *panel) } func (r *Row) AddSinglestat(data *SinglestatPanel) { - r.board.lastPanelID++ + lastPanelID++ panel := NewSinglestat("") - panel.ID = r.board.lastPanelID + panel.ID = lastPanelID panel.SinglestatPanel = data r.Panels = append(r.Panels, *panel) } func (r *Row) AddCustom(data *CustomPanel) { - r.board.lastPanelID++ + lastPanelID++ panel := NewCustom("") - panel.ID = r.board.lastPanelID + panel.ID = lastPanelID panel.CustomPanel = data r.Panels = append(r.Panels, *panel) }