grpcApiDashboard.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package grpcapi
  2. import (
  3. apiv1 "github.com/OliveTin/OliveTin/gen/grpc/olivetin/api/v1"
  4. config "github.com/OliveTin/OliveTin/internal/config"
  5. log "github.com/sirupsen/logrus"
  6. "golang.org/x/exp/slices"
  7. )
  8. func dashboardCfgToPb(rr *DashboardRenderRequest) []*apiv1.DashboardComponent {
  9. ret := make([]*apiv1.DashboardComponent, 0)
  10. for _, dashboard := range cfg.Dashboards {
  11. pbdb := &apiv1.DashboardComponent{
  12. Type: "dashboard",
  13. Title: dashboard.Title,
  14. Contents: removeNulls(getDashboardComponentContents(dashboard, rr)),
  15. }
  16. if len(pbdb.Contents) == 0 {
  17. log.WithFields(log.Fields{
  18. "dashboard": dashboard.Title,
  19. "username": rr.AuthenticatedUser.Username,
  20. }).Debugf("Dashboard has no readable contents, so it will not be visible in the web ui")
  21. continue
  22. }
  23. ret = append(ret, pbdb)
  24. }
  25. return ret
  26. }
  27. func removeNulls(components []*apiv1.DashboardComponent) []*apiv1.DashboardComponent {
  28. ret := make([]*apiv1.DashboardComponent, 0)
  29. for _, component := range components {
  30. if component == nil {
  31. continue
  32. }
  33. ret = append(ret, component)
  34. }
  35. return ret
  36. }
  37. func getDashboardComponentContents(dashboard *config.DashboardComponent, rr *DashboardRenderRequest) []*apiv1.DashboardComponent {
  38. ret := make([]*apiv1.DashboardComponent, 0)
  39. for _, subitem := range dashboard.Contents {
  40. if subitem.Type == "fieldset" && subitem.Entity != "" {
  41. ret = append(ret, buildEntityFieldsets(subitem.Entity, &subitem, rr)...)
  42. } else {
  43. ret = append(ret, buildDashboardComponentSimple(&subitem, rr))
  44. }
  45. }
  46. return ret
  47. }
  48. func buildDashboardComponentSimple(subitem *config.DashboardComponent, rr *DashboardRenderRequest) *apiv1.DashboardComponent {
  49. if (subitem.Type == "" || subitem.Type == "link") && (len(subitem.Contents) == 0) {
  50. if !slices.Contains(rr.AllowedActionTitles, subitem.Title) {
  51. return nil
  52. }
  53. }
  54. newitem := &apiv1.DashboardComponent{
  55. Title: subitem.Title,
  56. Type: getDashboardComponentType(subitem),
  57. Contents: getDashboardComponentContents(subitem, rr),
  58. Icon: getDashboardComponentIcon(subitem, rr.cfg),
  59. CssClass: subitem.CssClass,
  60. }
  61. return newitem
  62. }
  63. func getDashboardComponentIcon(item *config.DashboardComponent, cfg *config.Config) string {
  64. if item.Icon == "" {
  65. return cfg.DefaultIconForDirectories
  66. }
  67. return item.Icon
  68. }
  69. func getDashboardComponentType(item *config.DashboardComponent) string {
  70. allowedTypes := []string{
  71. "stdout-most-recent-execution",
  72. "display",
  73. }
  74. if len(item.Contents) > 0 {
  75. if item.Type != "fieldset" {
  76. return "directory"
  77. }
  78. return "fieldset"
  79. } else if slices.Contains(allowedTypes, item.Type) {
  80. return item.Type
  81. }
  82. return "link"
  83. }