11// Copyright (c) 2024 Veritas Technologies LLC. All rights reserved. IP63-2828-7171-04-15-9
22
3- // Package pm graph is used for generating the graph image.
4- package pm
3+ // Package graph is used for generating the graph image.
4+ package graph
55
66import (
77 "os"
88 "path/filepath"
9- "sort"
109 "strconv"
1110 "strings"
1211 "sync"
1312 "time"
1413
1514 "github.com/VeritasOS/plugin-manager/config"
15+ "github.com/VeritasOS/plugin-manager/types"
16+ "github.com/VeritasOS/plugin-manager/types/status"
1617 logger "github.com/VeritasOS/plugin-manager/utils/log"
1718 osutils "github.com/VeritasOS/plugin-manager/utils/os"
1819)
@@ -32,7 +33,14 @@ type graph struct {
3233var g graph
3334var dotCmdPresent = true
3435
35- func initGraphConfig (imgNamePrefix string ) {
36+ // Plugin is of type types.Plugin
37+ type Plugin = types.Plugin
38+
39+ // Plugins is of type types.Plugins
40+ type Plugins = types.Plugins
41+
42+ // InitGraphConfig initliazes output file names.
43+ func InitGraphConfig (imgNamePrefix string ) {
3644 // Initialization should be done only once.
3745 if g .fileNoExt == "" {
3846 // Remove imgNamePrefix if it's end with ".log"
@@ -41,39 +49,28 @@ func initGraphConfig(imgNamePrefix string) {
4149 }
4250}
4351
44- func getImagePath () string {
52+ // GetImagePath gets the path of the image file.
53+ func GetImagePath () string {
4554 return config .GetPMLogDir () + g .fileNoExt + ".svg"
4655}
4756
48- func getDotFilePath () string {
57+ // GetDotFilePath gets the path of the dot file.
58+ func GetDotFilePath () string {
4959 return config .GetPMLogDir () + g .fileNoExt + ".dot"
5060}
5161
52- // initGraph initliazes the graph data structure and invokes generateGraph.
53- func initGraph (pluginType string , pluginsInfo Plugins ) error {
54- initGraphConfig (config .GetPMLogFile ())
62+ // InitGraph initliazes the graph data structure and invokes generateGraph.
63+ func InitGraph (pluginType string , pluginsInfo Plugins ) error {
64+ InitGraphConfig (config .GetPMLogFile ())
5565
5666 // DOT guide: https://graphviz.gitlab.io/_pages/pdf/dotguide.pdf
5767
58- // INFO: Sort the plugins so that list of dependencies generated
59- // (used by documentation) doesn't change.
60- // NOTE: If not sorted, then even without addition of any new plugin,
61- // the dependency file generated will keep changing and appears in
62- // git staged list.
63- orderedPluginsList := []string {}
64- pluginsIdx := map [string ]int {}
6568 for pIdx , p := range pluginsInfo {
66- orderedPluginsList = append (orderedPluginsList , p .Name )
67- pluginsIdx [p .Name ] = pIdx
68- }
69- sort .Strings (orderedPluginsList )
70- for _ , pName := range orderedPluginsList {
71- pIdx := pluginsIdx [pName ]
72- pFileString := "\" " + pName + "\" "
69+ pFileString := "\" " + p .Name + "\" "
7370 absLogPath , _ := filepath .Abs (config .GetPMLogDir ())
7471 absLibraryPath , _ := filepath .Abs (config .GetPluginsLibrary ())
7572 relPath , _ := filepath .Rel (absLogPath , absLibraryPath )
76- pURL := "\" " + filepath .FromSlash (relPath + string (os .PathSeparator )+ pName ) + "\" "
73+ pURL := "\" " + filepath .FromSlash (relPath + string (os .PathSeparator )+ p . Name ) + "\" "
7774 rows := []string {}
7875 rowsInterface , ok := g .subgraph .Load (pluginType )
7976 if ok {
@@ -82,10 +79,10 @@ func initGraph(pluginType string, pluginsInfo Plugins) error {
8279 rows = append (rows , pFileString + " [label=\" " +
8380 strings .Replace (pluginsInfo [pIdx ].Description , "\" " , `\"` , - 1 )+
8481 "\" ,style=filled,fillcolor=lightgrey,URL=" + pURL + "]" )
85- rows = append (rows , "\" " + pName + "\" " )
82+ rows = append (rows , "\" " + p . Name + "\" " )
8683 rbyLen := len (pluginsInfo [pIdx ].RequiredBy )
8784 if rbyLen != 0 {
88- graphRow := "\" " + pName + "\" -> "
85+ graphRow := "\" " + p . Name + "\" -> "
8986 for rby := range pluginsInfo [pIdx ].RequiredBy {
9087 graphRow += "\" " + pluginsInfo [pIdx ].RequiredBy [rby ] + "\" "
9188 if rby != rbyLen - 1 {
@@ -103,7 +100,7 @@ func initGraph(pluginType string, pluginsInfo Plugins) error {
103100 graphRow += ", "
104101 }
105102 }
106- graphRow += " -> \" " + pName + "\" "
103+ graphRow += " -> \" " + p . Name + "\" "
107104 rows = append (rows , graphRow )
108105 }
109106 g .subgraph .Store (pluginType , rows )
@@ -115,8 +112,8 @@ func initGraph(pluginType string, pluginsInfo Plugins) error {
115112// generateGraph generates an input `.dot` file based on the fileNoExt name,
116113// and then generates an `.svg` image output file as fileNoExt.svg.
117114func generateGraph () error {
118- dotFile := getDotFilePath ()
119- svgFile := getImagePath ()
115+ dotFile := GetDotFilePath ()
116+ svgFile := GetImagePath ()
120117
121118 fhDigraph , openerr := osutils .OsOpenFile (dotFile , os .O_RDWR | os .O_CREATE | os .O_TRUNC , 0666 )
122119 if openerr != nil {
@@ -168,20 +165,21 @@ func generateGraph() error {
168165}
169166
170167// getStatusColor returns the color for a given result status.
171- func getStatusColor (status string ) string {
168+ func getStatusColor (myStatus string ) string {
172169 // Node color
173- ncolor := "blue" // dStatusStart by default
174- if status == dStatusFail {
170+ ncolor := "blue" // status.Start by default
171+ if myStatus == status . Fail {
175172 ncolor = "red"
176- } else if status == dStatusOk {
173+ } else if myStatus == status . Ok {
177174 ncolor = "green"
178- } else if status == dStatusSkip {
175+ } else if myStatus == status . Skip {
179176 ncolor = "yellow"
180177 }
181178 return ncolor
182179}
183180
184- func updateGraph (subgraphName , plugin , status , url string ) error {
181+ // UpdateGraph updates the plugin node with the status and url.
182+ func UpdateGraph (subgraphName , plugin , status , url string ) error {
185183 ncolor := getStatusColor (status )
186184 gContents := []string {}
187185 gContentsInterface , ok := g .subgraph .Load (subgraphName )
0 commit comments