dashboards.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. package api
  2. import (
  3. "sort"
  4. "strconv"
  5. apiv1 "github.com/OliveTin/OliveTin/gen/olivetin/api/v1"
  6. acl "github.com/OliveTin/OliveTin/internal/acl"
  7. config "github.com/OliveTin/OliveTin/internal/config"
  8. entities "github.com/OliveTin/OliveTin/internal/entities"
  9. "github.com/OliveTin/OliveTin/internal/tpl"
  10. log "github.com/sirupsen/logrus"
  11. "golang.org/x/exp/slices"
  12. )
  13. func renderDashboard(rr *DashboardRenderRequest, dashboardTitle string) *apiv1.Dashboard {
  14. if dashboardTitle == "default" {
  15. return buildDefaultDashboard(rr)
  16. }
  17. return findAndRenderDashboard(rr, dashboardTitle)
  18. }
  19. func getEntityFromRequest(rr *DashboardRenderRequest) *entities.Entity {
  20. if rr.EntityType == "" || rr.EntityKey == "" {
  21. return nil
  22. }
  23. entityInstances := entities.GetEntityInstances(rr.EntityType)
  24. if entity, ok := entityInstances[rr.EntityKey]; ok {
  25. return entity
  26. }
  27. return nil
  28. }
  29. func findAndRenderDashboard(rr *DashboardRenderRequest, dashboardTitle string) *apiv1.Dashboard {
  30. if dashboard := findDashboardByTitle(rr, dashboardTitle); dashboard != nil {
  31. return renderDashboardIfValid(dashboard, rr)
  32. }
  33. return renderDirectoryDashboard(rr, dashboardTitle)
  34. }
  35. func findDashboardByTitle(rr *DashboardRenderRequest, dashboardTitle string) *config.DashboardComponent {
  36. for _, dashboard := range rr.cfg.Dashboards {
  37. if dashboard.Title == dashboardTitle {
  38. return dashboard
  39. }
  40. }
  41. return nil
  42. }
  43. func renderDashboardIfValid(dashboard *config.DashboardComponent, rr *DashboardRenderRequest) *apiv1.Dashboard {
  44. if len(dashboard.Contents) == 0 {
  45. logEmptyDashboard(dashboard.Title, rr.AuthenticatedUser.Username)
  46. return nil
  47. }
  48. return buildDashboardFromConfig(dashboard, rr)
  49. }
  50. func renderDirectoryDashboard(rr *DashboardRenderRequest, dashboardTitle string) *apiv1.Dashboard {
  51. directoryComponent := findDirectoryComponent(rr, dashboardTitle)
  52. if directoryComponent == nil {
  53. return nil
  54. }
  55. entity := getEntityFromRequest(rr)
  56. return buildDashboardFromConfigWithEntity(directoryComponent, rr, entity)
  57. }
  58. func findDirectoryComponent(rr *DashboardRenderRequest, title string) *config.DashboardComponent {
  59. for _, dashboard := range rr.cfg.Dashboards {
  60. if component := searchDirectoryInComponent(dashboard, title); component != nil {
  61. return component
  62. }
  63. }
  64. return nil
  65. }
  66. func searchDirectoryInComponent(component *config.DashboardComponent, title string) *config.DashboardComponent {
  67. if isMatchingDirectory(component, title) {
  68. return component
  69. }
  70. return searchDirectoryInSubcomponents(component.Contents, title)
  71. }
  72. func isMatchingDirectory(component *config.DashboardComponent, title string) bool {
  73. return component.Title == title && len(component.Contents) > 0 && component.Type != "fieldset"
  74. }
  75. func searchDirectoryInSubcomponents(contents []*config.DashboardComponent, title string) *config.DashboardComponent {
  76. for _, subitem := range contents {
  77. if found := searchDirectoryInComponent(subitem, title); found != nil {
  78. return found
  79. }
  80. }
  81. return nil
  82. }
  83. func logEmptyDashboard(dashboardTitle, username string) {
  84. log.WithFields(log.Fields{
  85. "dashboard": dashboardTitle,
  86. "username": username,
  87. }).Debugf("Dashboard has no readable contents, so it will not be visible in the web ui")
  88. }
  89. func buildDashboardFromConfig(dashboard *config.DashboardComponent, rr *DashboardRenderRequest) *apiv1.Dashboard {
  90. return buildDashboardFromConfigWithEntity(dashboard, rr, nil)
  91. }
  92. func buildDashboardFromConfigWithEntity(dashboard *config.DashboardComponent, rr *DashboardRenderRequest, entity *entities.Entity) *apiv1.Dashboard {
  93. contents, root := getDashboardComponentContentsWithEntity(dashboard, rr, entity)
  94. return &apiv1.Dashboard{
  95. Title: dashboard.Title,
  96. Contents: orderTopLevelDashboardComponents(removeNulls(contents), root),
  97. }
  98. }
  99. //gocyclo:ignore
  100. func buildDefaultDashboard(rr *DashboardRenderRequest) *apiv1.Dashboard {
  101. db := &apiv1.Dashboard{
  102. Title: "Actions",
  103. Contents: make([]*apiv1.DashboardComponent, 0),
  104. }
  105. fieldset := &apiv1.DashboardComponent{
  106. Type: "fieldset",
  107. Title: "Actions",
  108. Contents: make([]*apiv1.DashboardComponent, 0),
  109. }
  110. for _, binding := range rr.ex.MapActionBindings {
  111. if binding == nil || binding.Action == nil || binding.Action.Hidden {
  112. continue
  113. }
  114. if binding.IsOnConfiguredDashboard() {
  115. continue
  116. }
  117. if !acl.IsAllowedView(rr.cfg, rr.AuthenticatedUser, binding.Action) {
  118. continue
  119. }
  120. action := buildAction(binding, rr)
  121. if action == nil {
  122. continue
  123. }
  124. comp := &apiv1.DashboardComponent{
  125. Type: "link",
  126. Title: action.Title,
  127. Icon: action.Icon,
  128. Action: action,
  129. }
  130. if binding.Entity != nil {
  131. comp.EntityKey = binding.Entity.UniqueKey
  132. }
  133. fieldset.Contents = append(fieldset.Contents, comp)
  134. }
  135. if len(fieldset.Contents) > 0 {
  136. fieldset.Contents = sortDashboardComponents(fieldset.Contents)
  137. db.Contents = append(db.Contents, fieldset)
  138. }
  139. return db
  140. }
  141. func entityKeyLess(a, b string) bool {
  142. ai, errA := strconv.ParseInt(a, 10, 64)
  143. bi, errB := strconv.ParseInt(b, 10, 64)
  144. if errA == nil && errB == nil {
  145. return ai < bi
  146. }
  147. return a < b
  148. }
  149. //gocyclo:ignore
  150. func sortDashboardComponents(components []*apiv1.DashboardComponent) []*apiv1.DashboardComponent {
  151. sort.Slice(components, func(i, j int) bool {
  152. if components[i].Action == nil || components[j].Action == nil {
  153. if components[i].EntityKey != "" && components[j].EntityKey != "" &&
  154. components[i].EntityKey != components[j].EntityKey {
  155. return entityKeyLess(components[i].EntityKey, components[j].EntityKey)
  156. }
  157. return components[i].Title < components[j].Title
  158. }
  159. if components[i].Action.Order != components[j].Action.Order {
  160. return components[i].Action.Order < components[j].Action.Order
  161. }
  162. if components[i].EntityKey != components[j].EntityKey {
  163. return entityKeyLess(components[i].EntityKey, components[j].EntityKey)
  164. }
  165. return components[i].Action.Title < components[j].Action.Title
  166. })
  167. return components
  168. }
  169. func removeNulls(components []*apiv1.DashboardComponent) []*apiv1.DashboardComponent {
  170. ret := make([]*apiv1.DashboardComponent, 0)
  171. for _, component := range components {
  172. if component == nil {
  173. continue
  174. }
  175. ret = append(ret, component)
  176. }
  177. return ret
  178. }
  179. func isNonEntityFieldset(component *apiv1.DashboardComponent) bool {
  180. return component != nil && component.Type == "fieldset" && component.EntityType == ""
  181. }
  182. func isRegularFieldset(component *apiv1.DashboardComponent, root *apiv1.DashboardComponent) bool {
  183. if !isNonEntityFieldset(component) {
  184. return false
  185. }
  186. return root == nil || component != root
  187. }
  188. func partitionTopLevelComponents(components []*apiv1.DashboardComponent, root *apiv1.DashboardComponent) (regular, sortables []*apiv1.DashboardComponent, isRegular []bool) {
  189. regular = make([]*apiv1.DashboardComponent, 0)
  190. sortables = make([]*apiv1.DashboardComponent, 0)
  191. isRegular = make([]bool, len(components))
  192. for i, c := range components {
  193. anchor := isRegularFieldset(c, root)
  194. isRegular[i] = anchor
  195. if anchor {
  196. regular = append(regular, c)
  197. } else {
  198. sortables = append(sortables, c)
  199. }
  200. }
  201. return regular, sortables, isRegular
  202. }
  203. func mergeOrderedTopLevelComponents(regular, sortables []*apiv1.DashboardComponent, isRegular []bool) []*apiv1.DashboardComponent {
  204. out := make([]*apiv1.DashboardComponent, 0, len(isRegular))
  205. regIdx, sortIdx := 0, 0
  206. for _, anchor := range isRegular {
  207. if anchor {
  208. out = append(out, regular[regIdx])
  209. regIdx++
  210. } else {
  211. out = append(out, sortables[sortIdx])
  212. sortIdx++
  213. }
  214. }
  215. return out
  216. }
  217. func orderTopLevelDashboardComponents(components []*apiv1.DashboardComponent, root *apiv1.DashboardComponent) []*apiv1.DashboardComponent {
  218. if len(components) == 0 {
  219. return components
  220. }
  221. regular, sortables, isRegular := partitionTopLevelComponents(components, root)
  222. sortDashboardComponents(sortables)
  223. return mergeOrderedTopLevelComponents(regular, sortables, isRegular)
  224. }
  225. func getDashboardComponentContentsWithEntity(dashboard *config.DashboardComponent, rr *DashboardRenderRequest, entity *entities.Entity) ([]*apiv1.DashboardComponent, *apiv1.DashboardComponent) {
  226. ret := make([]*apiv1.DashboardComponent, 0)
  227. rootFieldset := createRootFieldset()
  228. for _, subitem := range dashboard.Contents {
  229. processDashboardSubitemWithEntity(subitem, rr, &ret, rootFieldset, entity)
  230. }
  231. if len(rootFieldset.Contents) > 0 {
  232. ret = append(ret, rootFieldset)
  233. return ret, rootFieldset
  234. }
  235. return ret, nil
  236. }
  237. func createRootFieldset() *apiv1.DashboardComponent {
  238. return &apiv1.DashboardComponent{
  239. Type: "fieldset",
  240. Title: "Actions",
  241. Contents: make([]*apiv1.DashboardComponent, 0),
  242. }
  243. }
  244. func appendComponentIfNotNil(components *[]*apiv1.DashboardComponent, comp *apiv1.DashboardComponent) {
  245. if comp != nil {
  246. *components = append(*components, comp)
  247. }
  248. }
  249. func getDashboardComponentOrNil(subitem *config.DashboardComponent, rr *DashboardRenderRequest, entity *entities.Entity) *apiv1.DashboardComponent {
  250. if len(subitem.Contents) == 0 && rr.findActionForEntity(subitem.Title, entity) == nil {
  251. if !isAllowedType(subitem.Type) {
  252. return nil
  253. }
  254. }
  255. return buildDashboardComponentSimpleWithEntity(subitem, rr, entity)
  256. }
  257. func processDashboardSubitemWithEntity(subitem *config.DashboardComponent, rr *DashboardRenderRequest, ret *[]*apiv1.DashboardComponent, rootFieldset *apiv1.DashboardComponent, entity *entities.Entity) {
  258. if subitem.Type != "fieldset" {
  259. appendComponentIfNotNil(&rootFieldset.Contents, getDashboardComponentOrNil(subitem, rr, entity))
  260. return
  261. }
  262. if subitem.Entity != "" {
  263. *ret = append(*ret, buildEntityFieldsets(subitem.Entity, subitem, rr)...)
  264. } else {
  265. appendComponentIfNotNil(ret, getDashboardComponentOrNil(subitem, rr, entity))
  266. }
  267. }
  268. func buildDashboardComponentSimpleWithEntity(subitem *config.DashboardComponent, rr *DashboardRenderRequest, entity *entities.Entity) *apiv1.DashboardComponent {
  269. var contents []*apiv1.DashboardComponent
  270. if len(subitem.Contents) > 0 {
  271. contents, _ = getDashboardComponentContentsWithEntity(subitem, rr, entity)
  272. }
  273. action := rr.findActionForEntity(subitem.Title, entity)
  274. componentType := getDashboardComponentType(subitem, action)
  275. title := subitem.Title
  276. if entity != nil {
  277. title = tpl.ParseTemplateOfActionBeforeExec(subitem.Title, entity)
  278. }
  279. newitem := &apiv1.DashboardComponent{
  280. Title: title,
  281. Type: componentType,
  282. Contents: contents,
  283. Icon: getDashboardComponentIcon(subitem, rr.cfg),
  284. CssClass: subitem.CssClass,
  285. Action: action,
  286. }
  287. return newitem
  288. }
  289. func getDashboardComponentIcon(item *config.DashboardComponent, cfg *config.Config) string {
  290. if item.Icon == "" {
  291. return cfg.DefaultIconForDirectories
  292. }
  293. return item.Icon
  294. }
  295. func getDashboardComponentType(item *config.DashboardComponent, action *apiv1.Action) string {
  296. if hasContents(item) {
  297. return getTypeForComponentWithContents(item)
  298. }
  299. if isAllowedType(item.Type) {
  300. return item.Type
  301. }
  302. return getDefaultType(action)
  303. }
  304. func hasContents(item *config.DashboardComponent) bool {
  305. return len(item.Contents) > 0
  306. }
  307. func getTypeForComponentWithContents(item *config.DashboardComponent) string {
  308. if item.Type != "fieldset" {
  309. return "directory"
  310. }
  311. return "fieldset"
  312. }
  313. func isAllowedType(itemType string) bool {
  314. allowedTypes := []string{
  315. "stdout-most-recent-execution",
  316. "display",
  317. }
  318. return slices.Contains(allowedTypes, itemType)
  319. }
  320. func getDefaultType(action *apiv1.Action) string {
  321. if action == nil {
  322. return "display"
  323. }
  324. return "link"
  325. }