examples/Print-History-Management.ps1

<#
.SYNOPSIS
    Print history management examples.

.DESCRIPTION
    Demonstrates how to query, analyze, and manage print history records.
#>


Import-Module KlippyCLI

#region Query Print History

# Get recent print history (default: 50 records)
Get-KlippyPrintHistory | Format-Table FileName, Status, StartTime, PrintDuration

# Get more records
Get-KlippyPrintHistory -Limit 100

# Get prints from the last 7 days
$weekAgo = [DateTimeOffset]::Now.AddDays(-7).ToUnixTimeSeconds()
Get-KlippyPrintHistory -Since $weekAgo | Format-Table FileName, Status, StartTime

# Get oldest prints first
Get-KlippyPrintHistory -Order asc -Limit 10

#endregion

#region Analyze Print Statistics

# Get aggregate totals
$totals = Get-KlippyPrintHistoryTotals
Write-Host "Print Statistics:" -ForegroundColor Cyan
Write-Host " Total Jobs: $($totals.TotalJobs)"
Write-Host " Total Print Time: $($totals.TotalPrintTime.ToString('d\.hh\:mm\:ss'))"
Write-Host " Filament Used: $($totals.TotalFilamentUsed) meters"
Write-Host " Longest Print: $($totals.LongestPrint.ToString('hh\:mm\:ss'))"

# Calculate success rate
$history = Get-KlippyPrintHistory -Limit 1000
$completed = ($history | Where-Object Status -eq 'completed').Count
$cancelled = ($history | Where-Object Status -eq 'cancelled').Count
$failed = ($history | Where-Object Status -eq 'error').Count
$total = $history.Count

Write-Host "`nSuccess Rate:" -ForegroundColor Cyan
Write-Host " Completed: $completed ($([math]::Round($completed/$total*100,1))%)"
Write-Host " Cancelled: $cancelled ($([math]::Round($cancelled/$total*100,1))%)"
Write-Host " Failed: $failed ($([math]::Round($failed/$total*100,1))%)"

#endregion

#region Filter and Export History

# Find all failed prints
$failedPrints = Get-KlippyPrintHistory -Limit 500 | Where-Object Status -eq 'error'
$failedPrints | Export-Csv -Path "failed_prints.csv" -NoTypeInformation

# Find prints by filename pattern
$benchyPrints = Get-KlippyPrintHistory -Limit 500 | Where-Object { $_.FileName -like "*benchy*" }
Write-Host "Found $($benchyPrints.Count) benchy prints"

# Get filament usage by file
Get-KlippyPrintHistory -Limit 100 |
    Where-Object Status -eq 'completed' |
    Group-Object FileName |
    Select-Object Name, Count, @{N='TotalFilament';E={($_.Group | Measure-Object -Property FilamentUsed -Sum).Sum}} |
    Sort-Object TotalFilament -Descending |
    Format-Table

#endregion

#region Clean Up History

# Remove all cancelled prints
Get-KlippyPrintHistory -Limit 500 |
    Where-Object Status -eq 'cancelled' |
    Remove-KlippyPrintHistory -Force

# Remove prints older than 30 days
$thirtyDaysAgo = [DateTimeOffset]::Now.AddDays(-30).ToUnixTimeSeconds()
Get-KlippyPrintHistory -Limit 1000 |
    Where-Object { [DateTimeOffset]::new($_.StartTime).ToUnixTimeSeconds() -lt $thirtyDaysAgo } |
    Remove-KlippyPrintHistory -Force

# Remove a specific job by ID
Remove-KlippyPrintHistory -JobId "000123"

# Clear ALL history (use with caution!)
# Remove-KlippyPrintHistory -All -Force

#endregion

#region Print History Report

function Get-PrintHistoryReport {
    param(
        [int]$Days = 30
    )

    $since = [DateTimeOffset]::Now.AddDays(-$Days).ToUnixTimeSeconds()
    $history = Get-KlippyPrintHistory -Limit 1000 -Since $since
    $totals = Get-KlippyPrintHistoryTotals

    $report = [PSCustomObject]@{
        Period = "Last $Days days"
        TotalPrints = $history.Count
        Completed = ($history | Where-Object Status -eq 'completed').Count
        Cancelled = ($history | Where-Object Status -eq 'cancelled').Count
        Failed = ($history | Where-Object Status -eq 'error').Count
        TotalPrintTime = ($history | Measure-Object -Property { $_.PrintDuration.TotalHours } -Sum).Sum
        FilamentUsedMeters = ($history | Where-Object Status -eq 'completed' | Measure-Object -Property FilamentUsed -Sum).Sum
        AvgPrintTime = ($history | Where-Object Status -eq 'completed' | Measure-Object -Property { $_.PrintDuration.TotalMinutes } -Average).Average
    }

    return $report
}

# Generate report
$report = Get-PrintHistoryReport -Days 30
$report | Format-List

#endregion