2014-11-21 09:43:04 -06:00
|
|
|
|
package dtos
|
2014-10-05 14:13:01 -05:00
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"crypto/md5"
|
|
|
|
|
"fmt"
|
2017-12-20 14:20:12 -06:00
|
|
|
|
"regexp"
|
2014-10-05 14:13:01 -05:00
|
|
|
|
"strings"
|
|
|
|
|
|
2016-03-11 16:28:33 -06:00
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2019-10-08 11:57:53 -05:00
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2022-08-10 04:56:48 -05:00
|
|
|
|
"github.com/grafana/grafana/pkg/services/org"
|
|
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
2016-02-20 16:51:22 -06:00
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2014-10-05 14:13:01 -05:00
|
|
|
|
)
|
|
|
|
|
|
2020-07-16 07:39:01 -05:00
|
|
|
|
var regNonAlphaNumeric = regexp.MustCompile("[^a-zA-Z0-9]+")
|
2021-11-08 10:56:56 -06:00
|
|
|
|
var mlog = log.New("models")
|
2020-07-16 07:39:01 -05:00
|
|
|
|
|
2016-03-10 03:31:10 -06:00
|
|
|
|
type AnyId struct {
|
|
|
|
|
Id int64 `json:"id"`
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-21 02:52:40 -06:00
|
|
|
|
type LoginCommand struct {
|
|
|
|
|
User string `json:"user" binding:"Required"`
|
|
|
|
|
Password string `json:"password" binding:"Required"`
|
|
|
|
|
Remember bool `json:"remember"`
|
2014-10-05 14:13:01 -05:00
|
|
|
|
}
|
|
|
|
|
|
2014-11-21 09:43:04 -06:00
|
|
|
|
type CurrentUser struct {
|
2021-04-16 08:02:16 -05:00
|
|
|
|
IsSignedIn bool `json:"isSignedIn"`
|
|
|
|
|
Id int64 `json:"id"`
|
|
|
|
|
Login string `json:"login"`
|
|
|
|
|
Email string `json:"email"`
|
|
|
|
|
Name string `json:"name"`
|
2023-01-30 03:51:51 -06:00
|
|
|
|
Theme string `json:"theme"`
|
|
|
|
|
LightTheme bool `json:"lightTheme"` // deprecated, use theme instead
|
2021-04-16 08:02:16 -05:00
|
|
|
|
OrgCount int `json:"orgCount"`
|
|
|
|
|
OrgId int64 `json:"orgId"`
|
|
|
|
|
OrgName string `json:"orgName"`
|
2022-08-10 04:56:48 -05:00
|
|
|
|
OrgRole org.RoleType `json:"orgRole"`
|
2021-04-16 08:02:16 -05:00
|
|
|
|
IsGrafanaAdmin bool `json:"isGrafanaAdmin"`
|
|
|
|
|
GravatarUrl string `json:"gravatarUrl"`
|
|
|
|
|
Timezone string `json:"timezone"`
|
2021-10-18 08:27:14 -05:00
|
|
|
|
WeekStart string `json:"weekStart"`
|
2021-04-16 08:02:16 -05:00
|
|
|
|
Locale string `json:"locale"`
|
2022-11-22 06:18:34 -06:00
|
|
|
|
Language string `json:"language"`
|
2022-08-10 04:56:48 -05:00
|
|
|
|
HelpFlags1 user.HelpFlags1 `json:"helpFlags1"`
|
2021-04-16 08:02:16 -05:00
|
|
|
|
HasEditPermissionInFolders bool `json:"hasEditPermissionInFolders"`
|
2023-07-28 08:32:58 -05:00
|
|
|
|
AuthenticatedBy string `json:"authenticatedBy"`
|
2021-04-16 08:02:16 -05:00
|
|
|
|
Permissions UserPermissionsMap `json:"permissions,omitempty"`
|
2023-03-03 08:39:53 -06:00
|
|
|
|
Analytics AnalyticsSettings `json:"analytics"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type AnalyticsSettings struct {
|
|
|
|
|
Identifier string `json:"identifier"`
|
|
|
|
|
IntercomIdentifier string `json:"intercomIdentifier,omitempty"`
|
2014-10-05 14:13:01 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 05:19:41 -05:00
|
|
|
|
type UserPermissionsMap map[string]bool
|
2021-04-16 08:02:16 -05:00
|
|
|
|
|
2022-02-08 06:38:43 -06:00
|
|
|
|
// swagger:model
|
2016-09-27 11:17:39 -05:00
|
|
|
|
type MetricRequest struct {
|
2022-02-08 06:38:43 -06:00
|
|
|
|
// From Start time in epoch timestamps in milliseconds or relative using Grafana time units.
|
|
|
|
|
// required: true
|
|
|
|
|
// example: now-1h
|
|
|
|
|
From string `json:"from"`
|
|
|
|
|
// To End time in epoch timestamps in milliseconds or relative using Grafana time units.
|
|
|
|
|
// required: true
|
|
|
|
|
// example: now
|
|
|
|
|
To string `json:"to"`
|
|
|
|
|
// queries.refId – Specifies an identifier of the query. Is optional and default to “A”.
|
|
|
|
|
// queries.datasourceId – Specifies the data source to be queried. Each query in the request must have an unique datasourceId.
|
|
|
|
|
// queries.maxDataPoints - Species maximum amount of data points that dashboard panel can render. Is optional and default to 100.
|
|
|
|
|
// queries.intervalMs - Specifies the time interval in milliseconds of time series. Is optional and defaults to 1000.
|
|
|
|
|
// required: true
|
2022-03-30 10:46:06 -05:00
|
|
|
|
// example: [ { "refId": "A", "intervalMs": 86400000, "maxDataPoints": 1092, "datasource":{ "uid":"PD8C576611E62080A" }, "rawSql": "SELECT 1 as valueOne, 2 as valueTwo", "format": "table" } ]
|
2016-09-27 11:17:39 -05:00
|
|
|
|
Queries []*simplejson.Json `json:"queries"`
|
2022-02-08 06:38:43 -06:00
|
|
|
|
// required: false
|
|
|
|
|
Debug bool `json:"debug"`
|
2022-05-31 10:02:58 -05:00
|
|
|
|
|
2022-07-06 13:42:39 -05:00
|
|
|
|
PublicDashboardAccessToken string `json:"publicDashboardAccessToken"`
|
2015-01-06 02:11:00 -06:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-14 14:19:21 -05:00
|
|
|
|
func (mr *MetricRequest) GetUniqueDatasourceTypes() []string {
|
|
|
|
|
dsTypes := make(map[string]bool)
|
|
|
|
|
for _, query := range mr.Queries {
|
|
|
|
|
if dsType, ok := query.Get("datasource").CheckGet("type"); ok {
|
|
|
|
|
name := dsType.MustString()
|
|
|
|
|
if _, ok := dsTypes[name]; !ok {
|
|
|
|
|
dsTypes[name] = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-22 06:24:36 -06:00
|
|
|
|
res := make([]string, 0, len(dsTypes))
|
2022-09-14 14:19:21 -05:00
|
|
|
|
for dsType := range dsTypes {
|
|
|
|
|
res = append(res, dsType)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-13 18:23:56 -05:00
|
|
|
|
func (mr *MetricRequest) CloneWithQueries(queries []*simplejson.Json) MetricRequest {
|
|
|
|
|
return MetricRequest{
|
2022-12-01 12:08:36 -06:00
|
|
|
|
From: mr.From,
|
|
|
|
|
To: mr.To,
|
|
|
|
|
Queries: queries,
|
|
|
|
|
Debug: mr.Debug,
|
2022-06-13 18:23:56 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-16 07:32:18 -06:00
|
|
|
|
func GetGravatarUrl(text string) string {
|
2018-03-27 15:52:29 -05:00
|
|
|
|
if setting.DisableGravatar {
|
2018-06-04 09:03:59 -05:00
|
|
|
|
return setting.AppSubUrl + "/public/img/user_profile.png"
|
2018-03-27 15:52:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-16 07:32:18 -06:00
|
|
|
|
if text == "" {
|
|
|
|
|
return ""
|
2014-10-05 14:13:01 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-05 21:56:17 -05:00
|
|
|
|
hash, _ := GetGravatarHash(text)
|
|
|
|
|
return fmt.Sprintf(setting.AppSubUrl+"/avatar/%x", hash)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetGravatarHash(text string) ([]byte, bool) {
|
|
|
|
|
if text == "" {
|
|
|
|
|
return make([]byte, 0), false
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-05 14:13:01 -05:00
|
|
|
|
hasher := md5.New()
|
2019-10-08 11:57:53 -05:00
|
|
|
|
if _, err := hasher.Write([]byte(strings.ToLower(text))); err != nil {
|
2021-11-08 10:56:56 -06:00
|
|
|
|
mlog.Warn("Failed to hash text", "err", err)
|
2019-10-08 11:57:53 -05:00
|
|
|
|
}
|
2022-04-05 21:56:17 -05:00
|
|
|
|
return hasher.Sum(nil), true
|
2014-10-05 14:13:01 -05:00
|
|
|
|
}
|
2017-12-20 14:20:12 -06:00
|
|
|
|
|
|
|
|
|
func GetGravatarUrlWithDefault(text string, defaultText string) string {
|
|
|
|
|
if text != "" {
|
|
|
|
|
return GetGravatarUrl(text)
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 07:39:01 -05:00
|
|
|
|
text = regNonAlphaNumeric.ReplaceAllString(defaultText, "") + "@localhost"
|
2017-12-20 14:20:12 -06:00
|
|
|
|
|
|
|
|
|
return GetGravatarUrl(text)
|
|
|
|
|
}
|
2020-11-24 05:10:32 -06:00
|
|
|
|
|
2022-08-10 04:56:48 -05:00
|
|
|
|
func IsHiddenUser(userLogin string, signedInUser *user.SignedInUser, cfg *setting.Cfg) bool {
|
2021-02-12 09:08:18 -06:00
|
|
|
|
if userLogin == "" || signedInUser.IsGrafanaAdmin || userLogin == signedInUser.Login {
|
2020-11-24 05:10:32 -06:00
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, hidden := cfg.HiddenUsers[userLogin]; hidden {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|