2022-09-23 04:04:48 +08:00
package navtree
2022-09-28 14:29:35 +08:00
import (
"encoding/json"
"sort"
)
2022-09-23 04:04:48 +08:00
const (
// These weights may be used by an extension to reliably place
// itself in relation to a particular item in the menu. The weights
// are negative to ensure that the default items are placed above
// any items with default weight.
2022-10-03 22:05:19 +08:00
WeightHome = ( iota - 20 ) * 100
2024-07-22 18:43:40 +08:00
WeightBookmarks
2022-10-03 22:05:19 +08:00
WeightSavedItems
2022-09-23 04:04:48 +08:00
WeightDashboard
WeightExplore
WeightAlerting
2022-10-05 17:46:27 +08:00
WeightAlertsAndIncidents
2023-12-12 18:57:52 +08:00
WeightTestingAndSynthetics
2022-10-05 17:46:27 +08:00
WeightMonitoring
2024-05-29 19:28:33 +08:00
WeightCloudServiceProviders
2023-11-30 23:18:05 +08:00
WeightInfrastructure
WeightApplication
WeightFrontend
2024-01-26 23:30:36 +08:00
WeightAsserts
2023-04-14 18:46:18 +08:00
WeightDataConnections
2022-10-05 17:46:27 +08:00
WeightApps
2023-04-17 23:01:32 +08:00
WeightPlugin
WeightConfig
2022-09-23 04:04:48 +08:00
WeightProfile
WeightHelp
)
2022-09-28 14:29:35 +08:00
const (
2023-12-12 18:57:52 +08:00
NavIDRoot = "root"
NavIDDashboards = "dashboards/browse"
NavIDExplore = "explore"
NavIDCfg = "cfg" // NavIDCfg is the id for org configuration navigation node
NavIDAlertsAndIncidents = "alerts-and-incidents"
NavIDTestingAndSynthetics = "testing-and-synthetics"
NavIDAlerting = "alerting"
NavIDMonitoring = "monitoring"
NavIDInfrastructure = "infrastructure"
NavIDFrontend = "frontend"
NavIDReporting = "reports"
NavIDApps = "apps"
NavIDCfgGeneral = "cfg/general"
NavIDCfgPlugins = "cfg/plugins"
NavIDCfgAccess = "cfg/access"
2024-07-23 19:58:50 +08:00
NavIDBookmarks = "bookmarks"
2022-09-28 14:29:35 +08:00
)
2022-09-23 04:04:48 +08:00
type NavLink struct {
2023-04-17 23:01:32 +08:00
Id string ` json:"id,omitempty" `
Text string ` json:"text" `
SubTitle string ` json:"subTitle,omitempty" `
Icon string ` json:"icon,omitempty" ` // Available icons can be browsed in Storybook: https://developers.grafana.com/ui/latest/index.html?path=/story/docs-overview-icon--icons-overview
Img string ` json:"img,omitempty" `
Url string ` json:"url,omitempty" `
Target string ` json:"target,omitempty" `
SortWeight int64 ` json:"sortWeight,omitempty" `
HideFromTabs bool ` json:"hideFromTabs,omitempty" `
RoundIcon bool ` json:"roundIcon,omitempty" `
IsSection bool ` json:"isSection,omitempty" `
Children [ ] * NavLink ` json:"children,omitempty" `
HighlightText string ` json:"highlightText,omitempty" `
HighlightID string ` json:"highlightId,omitempty" `
EmptyMessageId string ` json:"emptyMessageId,omitempty" `
PluginID string ` json:"pluginId,omitempty" ` // (Optional) The ID of the plugin that registered nav link (e.g. as a standalone plugin page)
IsCreateAction bool ` json:"isCreateAction,omitempty" `
2024-01-15 19:30:55 +08:00
Keywords [ ] string ` json:"keywords,omitempty" `
2024-07-23 19:58:50 +08:00
ParentItem * NavLink ` json:"parentItem,omitempty" ` // (Optional) The parent item of the nav link
2022-09-23 04:04:48 +08:00
}
2022-09-28 14:29:35 +08:00
func ( node * NavLink ) Sort ( ) {
Sort ( node . Children )
}
type NavTreeRoot struct {
Children [ ] * NavLink
}
func ( root * NavTreeRoot ) AddSection ( node * NavLink ) {
root . Children = append ( root . Children , node )
}
func ( root * NavTreeRoot ) RemoveSection ( node * NavLink ) {
var result [ ] * NavLink
for _ , child := range root . Children {
if child != node {
result = append ( result , child )
}
}
root . Children = result
}
func ( root * NavTreeRoot ) FindById ( id string ) * NavLink {
return FindById ( root . Children , id )
}
2023-10-18 00:15:51 +08:00
func ( root * NavTreeRoot ) FindByURL ( url string ) * NavLink {
return FindByURL ( root . Children , url )
}
2022-09-28 14:29:35 +08:00
func ( root * NavTreeRoot ) Sort ( ) {
Sort ( root . Children )
}
func ( root * NavTreeRoot ) MarshalJSON ( ) ( [ ] byte , error ) {
return json . Marshal ( root . Children )
}
func Sort ( nodes [ ] * NavLink ) {
sort . SliceStable ( nodes , func ( i , j int ) bool {
iw := nodes [ i ] . SortWeight
if iw == 0 {
iw = int64 ( i ) + 1
}
jw := nodes [ j ] . SortWeight
if jw == 0 {
jw = int64 ( j ) + 1
}
2022-09-23 04:04:48 +08:00
2022-09-28 14:29:35 +08:00
return iw < jw
} )
for _ , child := range nodes {
child . Sort ( )
2022-09-23 04:04:48 +08:00
}
2022-09-28 14:29:35 +08:00
}
2024-03-16 00:39:13 +08:00
func ( root * NavTreeRoot ) ApplyHelpVersion ( version string ) {
helpNode := root . FindById ( "help" )
if helpNode != nil {
helpNode . SubTitle = version
}
}
2024-06-12 23:45:13 +08:00
func ( root * NavTreeRoot ) ApplyCostManagementIA ( ) {
2022-11-18 23:11:59 +08:00
orgAdminNode := root . FindById ( NavIDCfg )
2024-06-12 23:45:13 +08:00
var costManagementApp * NavLink
var adaptiveMetricsApp * NavLink
2024-07-04 00:59:47 +08:00
var adaptiveLogsApp * NavLink
2024-06-12 23:45:13 +08:00
var attributionsApp * NavLink
var logVolumeExplorerApp * NavLink
2022-11-18 23:11:59 +08:00
if orgAdminNode != nil {
2023-01-13 14:32:09 +08:00
adminNodeLinks := [ ] * NavLink { }
2024-06-12 23:45:13 +08:00
for _ , element := range orgAdminNode . Children {
switch navId := element . Id ; navId {
case "plugin-page-grafana-costmanagementui-app" :
costManagementApp = element
case "plugin-page-grafana-adaptive-metrics-app" :
adaptiveMetricsApp = element
2024-07-04 00:59:47 +08:00
case "plugin-page-grafana-adaptivelogs-app" :
adaptiveLogsApp = element
2024-06-12 23:45:13 +08:00
case "plugin-page-grafana-attributions-app" :
attributionsApp = element
case "plugin-page-grafana-logvolumeexplorer-app" :
logVolumeExplorerApp = element
default :
adminNodeLinks = append ( adminNodeLinks , element )
}
2024-05-16 05:26:18 +08:00
}
2024-06-12 23:45:13 +08:00
if costManagementApp != nil {
costManagementMetricsNode := FindByURL ( costManagementApp . Children , "/a/grafana-costmanagementui-app/metrics" )
if costManagementMetricsNode != nil {
if adaptiveMetricsApp != nil {
costManagementMetricsNode . Children = append ( costManagementMetricsNode . Children , adaptiveMetricsApp )
}
if attributionsApp != nil {
costManagementMetricsNode . Children = append ( costManagementMetricsNode . Children , attributionsApp )
}
}
2022-12-09 02:57:33 +08:00
2024-06-12 23:45:13 +08:00
costManagementLogsNode := FindByURL ( costManagementApp . Children , "/a/grafana-costmanagementui-app/logs" )
if costManagementLogsNode != nil {
2024-07-04 00:59:47 +08:00
if adaptiveLogsApp != nil {
costManagementLogsNode . Children = append ( costManagementLogsNode . Children , adaptiveLogsApp )
}
2024-06-12 23:45:13 +08:00
if logVolumeExplorerApp != nil {
costManagementLogsNode . Children = append ( costManagementLogsNode . Children , logVolumeExplorerApp )
}
}
adminNodeLinks = append ( adminNodeLinks , costManagementApp )
2022-11-18 23:11:59 +08:00
}
2024-06-12 23:45:13 +08:00
orgAdminNode . Children = adminNodeLinks
2022-11-18 23:11:59 +08:00
}
}
func AppendIfNotNil ( children [ ] * NavLink , newChild * NavLink ) [ ] * NavLink {
if newChild != nil {
return append ( children , newChild )
}
return children
}
2022-09-28 14:29:35 +08:00
func FindById ( nodes [ ] * NavLink , id string ) * NavLink {
for _ , child := range nodes {
if child . Id == id {
return child
} else if len ( child . Children ) > 0 {
if found := FindById ( child . Children , id ) ; found != nil {
return found
}
}
2022-09-23 04:04:48 +08:00
}
2022-09-28 14:29:35 +08:00
return nil
2022-09-23 04:04:48 +08:00
}
2023-10-18 00:15:51 +08:00
func FindByURL ( nodes [ ] * NavLink , url string ) * NavLink {
for _ , child := range nodes {
if child . Url == url {
return child
} else if len ( child . Children ) > 0 {
if found := FindByURL ( child . Children , url ) ; found != nil {
return found
}
}
}
return nil
}