fork/gdu
git clone https://git.julia.blue/fork/gdu
1eaa266
subvolume
1// Package webui implements a web-based frontend for Gdu. It serves an embedded 2// React single-page application and a small, bounded JSON API over the 3// in-memory analysis tree, streaming scan progress via Server-Sent Events. 4package webui 5 6import ( 7"encoding/json" 8"io" 9"sync" 10"time" 11 12 log"github.com/sirupsen/logrus" 13 14"github.com/dundee/gdu/v5/internal/common" 15"github.com/dundee/gdu/v5/pkg/analyze" 16"github.com/dundee/gdu/v5/pkg/device" 17"github.com/dundee/gdu/v5/pkg/fs" 18"github.com/dundee/gdu/v5/report" 19) 20 21// progressPollInterval is how often the scan progress is sampled and broadcast. 22const progressPollInterval = 100 * time .Millisecond 23 24// UI is the web frontend. It embeds the shared *common.UI so that all the 25// generic Set* configuration methods are provided for free; only the 26// frontend-specific methods are implemented in this package. 27type UI struct { 28* common.UI 29 30output io.Writer 31listenAddr string 32openBrowser bool 33browserCmd string 34 35getter device.DevicesInfoGetter 36devices device.Devices 37 38mu sync.RWMutex 39topDir fs.Item 40topDirPath string 41linkedItems fs.HardLinkedItems 42collapsePath bool 43scanning bool 44scanErr error 45progress common.CurrentProgress 46 47hub * hub 48} 49 50// CreateUI creates a new web UI. 51func CreateUI ( 52output io.Writer , 53listenAddr string , 54openBrowser bool , 55browserCmd string , 56useColors bool , 57showApparentSize bool , 58showRelativeSize bool , 59useSIPrefix bool , 60)* UI { 61ui := & UI { 62UI :& common.UI { 63UseColors :useColors , 64ShowApparentSize :showApparentSize , 65ShowRelativeSize :showRelativeSize , 66UseSIPrefix :useSIPrefix , 67Analyzer :analyze .CreateAnalyzer (), 68 }, 69output :output , 70listenAddr :listenAddr , 71openBrowser :openBrowser , 72browserCmd :browserCmd , 73linkedItems :make (fs.HardLinkedItems ), 74hub :newHub (), 75 } 76if listenAddr == "" { 77ui .listenAddr = "localhost:0" 78 } 79if ! useSIPrefix { 80ui .SetBlockSizeFromEnvironment () 81 } 82return ui 83} 84 85// SetCollapsePath sets whether single-child directory chains are collapsed. 86func (ui * UI )SetCollapsePath (value bool ) { 87ui .collapsePath = value 88} 89 90// SetShowSymlinkTarget is a no-op for the web UI (rendering is browser-side). 91func (ui * UI )SetShowSymlinkTarget (value bool ) { 92} 93 94// ListDevices loads mounted devices so they can be served to the browser. 95func (ui * UI )ListDevices (getter device.DevicesInfoGetter )error { 96ui .getter = getter 97devices ,err := getter .GetDevicesInfo () 98if err != nil { 99return err 100 } 101ui .mu .Lock () 102ui .devices = devices 103ui .mu .Unlock () 104return nil 105} 106 107// AnalyzePath analyzes disk usage in the given path in the background while the 108// HTTP server serves progress and partial results. 109func (ui * UI )AnalyzePath (path string ,parentDir fs.Item )error { 110ui .mu .Lock () 111ui .scanning = true 112ui .scanErr = nil 113ui .topDirPath = path 114ui .mu .Unlock () 115 116ui .Analyzer .ResetProgress () 117 118scanDone := make (chan struct {}) 119go ui .pollProgress (scanDone ) 120 121go func () { 122dir := ui .Analyzer .AnalyzeDir (path ,ui .CreateIgnoreFunc (),ui .CreateFileTypeFilter ()) 123 124if parentDir != nil { 125dir .SetParent (parentDir ) 126parentDir .RemoveFileByName (dir .GetName ()) 127parentDir .AddFile (dir ) 128 } 129 130if ui .IsFilteringFiles () { 131dir .UpdateStatsWithFileFiltering (ui .linkedItems ) 132 }else { 133dir .UpdateStats (ui .linkedItems ) 134 } 135 136ui .mu .Lock () 137if parentDir == nil { 138ui .topDir = dir 139ui .topDirPath = dir .GetPath () 140 } 141ui .scanning = false 142ui .mu .Unlock () 143 144close (scanDone ) 145ui .hub .publish (ui .statusJSON ()) 146 }() 147 148return nil 149} 150 151// ReadAnalysis reads an analysis report from a JSON reader and serves it. 152func (ui * UI )ReadAnalysis (input io.Reader )error { 153ui .mu .Lock () 154ui .scanning = true 155ui .mu .Unlock () 156 157dir ,err := report .ReadAnalysis (input ) 158if err != nil { 159ui .mu .Lock () 160ui .scanning = false 161ui .scanErr = err 162ui .mu .Unlock () 163return err 164 } 165dir .UpdateStats (ui .linkedItems ) 166 167ui .mu .Lock () 168ui .topDir = dir 169ui .topDirPath = dir .GetPath () 170ui .scanning = false 171ui .mu .Unlock () 172return nil 173} 174 175// ReadFromStorage reads a previously stored analysis and serves it. 176func (ui * UI )ReadFromStorage (storagePath ,path string )error { 177storage := analyze .NewStorage (storagePath ,path ) 178closeFn := storage .Open () 179defer closeFn () 180 181dir ,err := storage .GetDirForPath (path ) 182if err != nil { 183return err 184 } 185dir .UpdateStats (ui .linkedItems ) 186 187ui .mu .Lock () 188ui .topDir = dir 189ui .topDirPath = dir .GetPath () 190ui .scanning = false 191ui .mu .Unlock () 192return nil 193} 194 195// pollProgress samples the analyzer progress and broadcasts it until the scan 196// goroutine signals completion by closing scanDone. 197func (ui * UI )pollProgress (scanDone <- chan struct {}) { 198ticker := time .NewTicker (progressPollInterval ) 199defer ticker .Stop () 200 201for { 202select { 203case <- scanDone : 204return 205case <- ticker .C : 206p := ui .Analyzer .GetProgress () 207ui .mu .Lock () 208ui .progress = p 209ui .mu .Unlock () 210ui .hub .publish (ui .statusJSON ()) 211 } 212 } 213} 214 215// statusJSON renders the current scan status as a JSON string for SSE. 216func (ui * UI )statusJSON ()string { 217data ,err := json .Marshal (ui .buildStatus ()) 218if err != nil { 219log .Printf ("webui: marshaling status: %s" ,err ) 220return `{"state":"error"}` 221 } 222return string (data ) 223}