Public/Measure-PCOciMetric.ps1

function Measure-PCOciMetric {
    <#
    .SYNOPSIS
        Summarises a metrics file: count, mean, minimum, maximum, median and 95th percentile per resource per day and over the window.

    .DESCRIPTION
        The separate summary step of D-014: it reads a file written by Get-PCOciMetric, never changes it, and
        writes a summary that names it. Percentiles are nearest-rank on the values the stream held, so every
        summary value is a value the service returned. A consumer with a question these summaries do not answer
        reads the raw file.

    .PARAMETER Path
        The metrics file.

    .PARAMETER OutputPath
        The summary file. Default: the metrics file's name with .summary.json.

    .EXAMPLE
        Measure-PCOciMetric .\data\metrics\cpu.json
    #>

    [CmdletBinding(SupportsShouldProcess)]
    [OutputType([pscustomobject], [PowerCraft.OCI.Core.Metrics.MetricSummary])]
    param(
        [Parameter(Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)]
        [Alias('MetricsPath', 'FullName')]
        [string]$Path,
        [string]$OutputPath,
        [switch]$Deterministic,
        [switch]$PassThru,
        [switch]$Force
    )

    process {
        if (-not (Test-Path -LiteralPath $Path)) { throw "No metrics file at '$Path'." }
        $resolved = (Resolve-Path -LiteralPath $Path).Path
        $target = if ($OutputPath) { $OutputPath } else { [IO.Path]::ChangeExtension($resolved, '.summary.json') }
        if ((Test-Path -LiteralPath $target) -and -not $Force) { throw "A file already exists at '$target'. Use -Force to overwrite it." }
        if (-not $PSCmdlet.ShouldProcess($target, "Summarise '$resolved'")) { return }

        $series = [PowerCraft.OCI.Core.Metrics.MetricWriter]::Read($resolved)
        $summary = [PowerCraft.OCI.Core.Metrics.MetricSummariser]::Summarise($series, [IO.Path]::GetFileName($resolved))
        [PowerCraft.OCI.Core.Metrics.MetricWriter]::Write($summary, $target, [bool]$Deterministic)

        if ($PassThru) { return $summary }
        [pscustomobject]@{
            SummaryPath = (Resolve-Path -LiteralPath $target).Path
            Source      = $summary.Source
            Name        = $summary.Name
            Query       = $summary.Query
            Start       = $summary.Start
            End         = $summary.End
            Resources   = $summary.Resources.Count
            Points      = $summary.Points
        }
    }
}