Files
SharepointToolbox-Web/Components/Pages/Storage.razor
T

146 lines
6.8 KiB
Plaintext

@page "/storage"
@attribute [Authorize]
@inject IUserSessionService Session
@inject ISessionManager SessionMgr
@inject IElevationCoordinator Elevation
@inject IStorageService StorageSvc
@inject StorageCsvExportService CsvExport
@inject StorageHtmlExportService HtmlExport
@inject WebExportService WebExport
@rendermode InteractiveServer
<h1 class="page-title">Storage Metrics</h1>
@if (!Session.HasProfile) { <NoProfilePrompt /> return; }
<div class="card">
<div class="card-title">Scan Options</div>
<SitePicker Profile="Session.CurrentProfile!" @bind-SelectedSites="_sites" />
<div class="form-row mt-8">
<div class="form-group" style="max-width:220px">
<label class="form-label">Folder scan depth</label>
<input class="form-input" type="number" min="0" max="20" @bind="_folderDepth" />
<small class="text-muted">0 = libraries only. 1+ = drill into subfolders that many levels deep.</small>
</div>
</div>
<div class="form-row">
<div class="form-group" style="display:flex;align-items:center;gap:16px;padding-top:20px">
<label><input type="checkbox" @bind="_includeSubsites" /> Include subsites</label>
<label><input type="checkbox" @bind="_includeHidden" /> Include hidden libs</label>
<label><input type="checkbox" @bind="_includeRecycleBin" /> Include recycle bin</label>
</div>
</div>
<div class="flex-row mt-8">
<button class="btn btn-primary" @onclick="RunScan" disabled="@_running">
@(_running ? "Scanning…" : "Scan Storage")
</button>
@if (_sites.Count > 0) { <span class="text-muted" style="align-self:center">@_sites.Count site(s) selected</span> }
@if (_running) { <button class="btn btn-secondary" @onclick="Cancel">Cancel</button> }
</div>
<ProgressPanel IsRunning="_running" StatusMessage="@_status" Current="_current" Total="_total" />
</div>
@if (!string.IsNullOrEmpty(_error)) { <div class="alert alert-error">@_error</div> }
@if (_results.Count > 0)
{
<div class="card">
<div class="flex-row">
<div class="card-title">Storage Report <span class="count-badge">@_results.Count libraries</span></div>
<div class="spacer"></div>
<MergeModeSelect Value="_mergeMode" ValueChanged="v => _mergeMode = v" Visible="_bySite.Count > 1" />
<button class="btn btn-secondary btn-sm" @onclick="ExportCsv">Export CSV</button>
<button class="btn btn-secondary btn-sm" @onclick="ExportHtml">Export HTML</button>
</div>
<div class="data-table-wrap">
<table class="data-table">
<thead>
<tr>
<th>Library</th>
<th>Site</th>
<th class="num">Files</th>
<th class="num">Total (MB)</th>
<th class="num">Versions (MB)</th>
<th>Last Modified</th>
</tr>
</thead>
<tbody>
@foreach (var n in _results)
{
<tr>
<td style="padding-left:@(n.IndentLevel * 20 + 12)px">@n.Name</td>
<td>@n.SiteTitle</td>
<td class="num">@n.TotalFileCount.ToString("N0")</td>
<td class="num">@((n.TotalSizeBytes / 1048576.0).ToString("F2"))</td>
<td class="num">@((n.VersionSizeBytes / 1048576.0).ToString("F2"))</td>
<td>@(n.LastModified?.ToString("yyyy-MM-dd") ?? "")</td>
</tr>
}
</tbody>
</table>
</div>
</div>
}
@code {
private List<SiteInfo> _sites = new();
private bool _includeSubsites, _includeHidden = true, _includeRecycleBin = true;
private int _folderDepth;
private bool _running; private string _status = string.Empty, _error = string.Empty;
private int _current, _total;
private List<StorageNode> _results = new();
private List<(string Label, IReadOnlyList<StorageNode> Results)> _bySite = new();
private ReportMergeMode _mergeMode = ReportMergeMode.SingleMerged;
private CancellationTokenSource? _cts;
private async Task RunScan()
{
_error = string.Empty; _results = new(); _bySite = new(); _running = true;
_cts = new CancellationTokenSource();
if (_sites.Count == 0) { _error = "Please select at least one site."; _running = false; return; }
var progress = new Progress<OperationProgress>(p => { _status = p.Message; _current = p.Current; _total = p.Total; InvokeAsync(StateHasChanged); });
try
{
var opts = new StorageScanOptions(IncludeSubsites: _includeSubsites, FolderDepth: Math.Clamp(_folderDepth, 0, 20), IncludeHiddenLibraries: _includeHidden, IncludeRecycleBin: _includeRecycleBin);
var bySite = new List<(string, IReadOnlyList<StorageNode>)>();
var flat = new List<StorageNode>();
int i = 0;
foreach (var site in _sites)
{
_cts.Token.ThrowIfCancellationRequested();
_status = $"Scanning {site.Title} ({++i}/{_sites.Count})…";
await InvokeAsync(StateHasChanged);
var nodes = await Elevation.RunAsync(async c =>
{
var ctx = await SessionMgr.GetOrCreateContextAsync(site.Url, Session.CurrentProfile!, c);
return await StorageSvc.CollectStorageAsync(ctx, opts, progress, c);
}, _cts.Token);
bySite.Add((site.Title, nodes));
flat.AddRange(nodes);
}
_bySite = bySite; _results = flat;
_status = $"Complete: {_results.Count} nodes across {_sites.Count} site(s).";
}
catch (OperationCanceledException) { _status = "Cancelled."; }
catch (Exception ex) { _error = ex.Message; }
finally { _running = false; await InvokeAsync(StateHasChanged); }
}
private void Cancel() => _cts?.Cancel();
private async Task ExportCsv()
{
var ts = DateTime.Now.ToString("yyyyMMdd_HHmmss");
var output = ReportMergeHelper.Build(_bySite, _mergeMode, "storage", ts, ReportFormat.Csv,
rs => CsvExport.BuildCsv(rs));
await WebExport.DownloadBytesAsync(output.Bytes, output.FileName, output.Mime);
}
private async Task ExportHtml()
{
var ts = DateTime.Now.ToString("yyyyMMdd_HHmmss");
var output = ReportMergeHelper.Build(_bySite, _mergeMode, "storage", ts, ReportFormat.Html,
rs => HtmlExport.BuildHtml(rs, Session.CurrentBranding));
await WebExport.DownloadBytesAsync(output.Bytes, output.FileName, output.Mime);
}
}