2022-06-02 18:27:23 -08:00
package models
2022-06-22 13:58:52 -08:00
import (
2022-09-13 13:33:41 -03:00
"encoding/json"
2022-09-07 12:08:52 -06:00
"strconv"
2022-06-22 13:58:52 -08:00
"time"
2022-07-06 15:51:44 -08:00
"github.com/grafana/grafana/pkg/models"
2022-09-07 12:08:52 -06:00
"github.com/grafana/grafana/pkg/tsdb/legacydata"
2022-07-06 15:51:44 -08:00
)
// PublicDashboardErr represents a dashboard error.
type PublicDashboardErr struct {
StatusCode int
Status string
Reason string
}
// Error returns the error message.
func ( e PublicDashboardErr ) Error ( ) string {
if e . Reason != "" {
return e . Reason
}
return "Dashboard Error"
}
var (
ErrPublicDashboardFailedGenerateUniqueUid = PublicDashboardErr {
2022-09-14 13:19:21 -06:00
Reason : "failed to generate unique public dashboard id" ,
2022-07-06 15:51:44 -08:00
StatusCode : 500 ,
}
ErrPublicDashboardFailedGenerateAccesstoken = PublicDashboardErr {
2022-09-14 13:19:21 -06:00
Reason : "failed to public dashboard access token" ,
2022-07-06 15:51:44 -08:00
StatusCode : 500 ,
}
ErrPublicDashboardNotFound = PublicDashboardErr {
2022-09-14 13:19:21 -06:00
Reason : "public dashboard not found" ,
2022-07-06 15:51:44 -08:00
StatusCode : 404 ,
Status : "not-found" ,
}
ErrPublicDashboardPanelNotFound = PublicDashboardErr {
2022-09-14 13:19:21 -06:00
Reason : "panel not found in dashboard" ,
2022-07-06 15:51:44 -08:00
StatusCode : 404 ,
Status : "not-found" ,
}
ErrPublicDashboardIdentifierNotSet = PublicDashboardErr {
2022-09-14 13:19:21 -06:00
Reason : "no Uid for public dashboard specified" ,
2022-07-06 15:51:44 -08:00
StatusCode : 400 ,
}
2022-07-21 13:56:20 -06:00
ErrPublicDashboardHasTemplateVariables = PublicDashboardErr {
2022-09-14 13:19:21 -06:00
Reason : "public dashboard has template variables" ,
2022-07-21 13:56:20 -06:00
StatusCode : 422 ,
}
2022-08-29 18:13:06 -03:00
ErrPublicDashboardBadRequest = PublicDashboardErr {
2022-09-14 13:19:21 -06:00
Reason : "bad Request" ,
2022-08-29 18:13:06 -03:00
StatusCode : 400 ,
}
2022-06-22 13:58:52 -08:00
)
2022-06-02 18:27:23 -08:00
type PublicDashboard struct {
2022-09-13 13:33:41 -03:00
Uid string ` json:"uid" xorm:"pk uid" `
DashboardUid string ` json:"dashboardUid" xorm:"dashboard_uid" `
OrgId int64 ` json:"-" xorm:"org_id" ` // Don't ever marshal orgId to Json
TimeSettings * TimeSettings ` json:"timeSettings" xorm:"time_settings" `
IsEnabled bool ` json:"isEnabled" xorm:"is_enabled" `
AccessToken string ` json:"accessToken" xorm:"access_token" `
2022-06-22 13:58:52 -08:00
CreatedBy int64 ` json:"createdBy" xorm:"created_by" `
UpdatedBy int64 ` json:"updatedBy" xorm:"updated_by" `
CreatedAt time . Time ` json:"createdAt" xorm:"created_at" `
UpdatedAt time . Time ` json:"updatedAt" xorm:"updated_at" `
2022-06-02 18:27:23 -08:00
}
func ( pd PublicDashboard ) TableName ( ) string {
2022-06-22 13:58:52 -08:00
return "dashboard_public"
}
type TimeSettings struct {
2022-09-13 13:33:41 -03:00
From string ` json:"from,omitempty" `
To string ` json:"to,omitempty" `
}
func ( ts * TimeSettings ) FromDB ( data [ ] byte ) error {
return json . Unmarshal ( data , ts )
}
func ( ts * TimeSettings ) ToDB ( ) ( [ ] byte , error ) {
return json . Marshal ( ts )
2022-06-22 13:58:52 -08:00
}
// build time settings object from json on public dashboard. If empty, use
// defaults on the dashboard
2022-09-07 12:08:52 -06:00
func ( pd PublicDashboard ) BuildTimeSettings ( dashboard * models . Dashboard ) TimeSettings {
from := dashboard . Data . GetPath ( "time" , "from" ) . MustString ( )
to := dashboard . Data . GetPath ( "time" , "to" ) . MustString ( )
timeRange := legacydata . NewDataTimeRange ( from , to )
// Were using epoch ms because this is used to build a MetricRequest, which is used by query caching, which expected the time range in epoch milliseconds.
ts := TimeSettings {
From : strconv . FormatInt ( timeRange . GetFromAsMsEpoch ( ) , 10 ) ,
To : strconv . FormatInt ( timeRange . GetToAsMsEpoch ( ) , 10 ) ,
2022-06-22 13:58:52 -08:00
}
if pd . TimeSettings == nil {
return ts
}
return ts
2022-06-02 18:27:23 -08:00
}
2022-07-06 15:51:44 -08:00
// DTO for transforming user input in the api
type SavePublicDashboardConfigDTO struct {
DashboardUid string
OrgId int64
UserId int64
PublicDashboard * PublicDashboard
}
2022-08-29 18:13:06 -03:00
type PublicDashboardQueryDTO struct {
IntervalMs int64
MaxDataPoints int64
}
2022-06-02 18:27:23 -08:00
//
// COMMANDS
//
type SavePublicDashboardConfigCommand struct {
2022-06-22 13:58:52 -08:00
PublicDashboard PublicDashboard
2022-06-02 18:27:23 -08:00
}