functions/public/Get-KlippyPrintHistoryTotals.ps1
|
function Get-KlippyPrintHistoryTotals { <# .SYNOPSIS Gets print history totals from a Klipper printer. .DESCRIPTION Retrieves aggregate statistics from print history including total print time, total filament used, and job counts. .PARAMETER Id The unique identifier of the printer. .PARAMETER PrinterName The friendly name of the printer. .PARAMETER InputObject A printer object from pipeline input. .EXAMPLE Get-KlippyPrintHistoryTotals Gets print totals from the default printer. .EXAMPLE Get-KlippyPrintHistoryTotals -PrinterName "voronv2" Gets print totals from the specified printer. .OUTPUTS KlippyCLI.PrintHistoryTotals object. #> [CmdletBinding(DefaultParameterSetName = 'Default')] [OutputType([PSCustomObject])] param( [Parameter(ParameterSetName = 'ById')] [ValidateNotNullOrEmpty()] [string]$Id, [Parameter(ParameterSetName = 'ByName', Position = 0)] [ValidateNotNullOrEmpty()] [string]$PrinterName, [Parameter(ParameterSetName = 'ByObject', ValueFromPipeline = $true)] [PSCustomObject]$InputObject ) process { # Resolve printer $resolveParams = @{} switch ($PSCmdlet.ParameterSetName) { 'ById' { $resolveParams['Id'] = $Id } 'ByName' { $resolveParams['PrinterName'] = $PrinterName } 'ByObject' { $resolveParams['InputObject'] = $InputObject } } $printer = Resolve-KlippyPrinterTarget @resolveParams try { $response = Invoke-KlippyJsonRpc -Printer $printer -Method "server/history/totals" -NoNormalize if (-not $response -or -not $response.job_totals) { Write-Warning "No print history totals available." return } $totals = $response.job_totals [PSCustomObject]@{ PSTypeName = 'KlippyCLI.PrintHistoryTotals' PrinterId = $printer.Id PrinterName = $printer.PrinterName TotalJobs = $totals.total_jobs TotalPrintTime = [TimeSpan]::FromSeconds($totals.total_print_time) TotalFilamentUsed = [math]::Round($totals.total_filament_used / 1000, 2) # meters LongestJob = [TimeSpan]::FromSeconds($totals.longest_job) LongestPrint = [TimeSpan]::FromSeconds($totals.longest_print) } } catch { Write-Error "Failed to get print history totals from '$($printer.PrinterName)': $_" } } } |