models.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package dtos
  2. import (
  3. "crypto/md5"
  4. "fmt"
  5. "strings"
  6. "github.com/torkelo/grafana-pro/pkg/models"
  7. )
  8. type LoginResult struct {
  9. Status string `json:"status"`
  10. User CurrentUser `json:"user"`
  11. }
  12. type CurrentUser struct {
  13. Login string `json:"login"`
  14. Email string `json:"email"`
  15. IsAdmin bool `json:"isAdmin"`
  16. GravatarUrl string `json:"gravatarUrl"`
  17. }
  18. type DataSource struct {
  19. Id int64 `json:"id"`
  20. AccountId int64 `json:"accountId"`
  21. Name string `json:"name"`
  22. Type models.DsType `json:"type"`
  23. Access models.DsAccess `json:"access"`
  24. Url string `json:"url"`
  25. Password string `json:"password"`
  26. User string `json:"user"`
  27. Database string `json:"database"`
  28. BasicAuth bool `json:"basicAuth"`
  29. IsDefault bool `json:"isDefault"`
  30. }
  31. type MetricQueryResultDto struct {
  32. Data []MetricQueryResultDataDto `json:"data"`
  33. }
  34. type MetricQueryResultDataDto struct {
  35. Target string `json:"target"`
  36. DataPoints [][2]float64 `json:"datapoints"`
  37. }
  38. func NewCurrentUser(account *models.Account) *CurrentUser {
  39. model := &CurrentUser{}
  40. if account != nil {
  41. model.Login = account.Login
  42. model.Email = account.Email
  43. model.GravatarUrl = getGravatarUrl(account.Email)
  44. model.IsAdmin = account.IsAdmin
  45. }
  46. return model
  47. }
  48. func getGravatarUrl(text string) string {
  49. hasher := md5.New()
  50. hasher.Write([]byte(strings.ToLower(text)))
  51. return fmt.Sprintf("https://secure.gravatar.com/avatar/%x?s=90&default=mm", hasher.Sum(nil))
  52. }