4
0

dashboards.go 12 KB

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