WebinarAttendance.psm1

Set-StrictMode -Version Latest

function ConvertTo-Pct([double]$num, [double]$den) {
    if ($den -le 0) { return 0.0 }
    [math]::Round($num / $den * 100, 1, [System.MidpointRounding]::AwayFromZero)
}

function New-WebinarRow([string]$Name, [long]$reg, [long]$live, [long]$od) {
    [pscustomobject]@{
        Source          = $Name
        Registered      = $reg
        Live            = $live
        OnDemand        = $od
        LiveRate        = ConvertTo-Pct $live $reg
        OnDemandRate    = ConvertTo-Pct $od $reg
        TotalViewedRate = ConvertTo-Pct ($live + $od) $reg
        NoShows         = $reg - $live - $od
    }
}

function Get-WebinarAttendance {
<#
.SYNOPSIS
Webinar attendance per registration source and overall: live rate, on-demand rate, total viewed rate and no-shows.
.DESCRIPTION
Reads a CSV with the columns source, registered, attended_live, watched_on_demand. Rows for the same source are added together.
Live rate = attended_live / registered. On-demand rate = watched_on_demand / registered.
Total viewed rate = (attended_live + watched_on_demand) / registered. No-shows = registered - attended_live - watched_on_demand.
Rates are percentages with one decimal. A row where attended_live + watched_on_demand is more than registered makes the function throw.
The last output row is ALL.
.EXAMPLE
Get-WebinarAttendance -Path sample.csv
#>

    [CmdletBinding()]
    param([Parameter(Mandatory)][string]$Path)

    if (-not (Test-Path -LiteralPath $Path)) { throw "file not found: $Path" }
    $rows = @(Import-Csv -LiteralPath $Path)
    if ($rows.Count -eq 0) { throw 'no data rows' }
    foreach ($need in 'source', 'registered', 'attended_live', 'watched_on_demand') {
        if (-not ($rows[0].PSObject.Properties.Name -contains $need)) { throw "missing column: $need" }
    }
    $order = New-Object System.Collections.Generic.List[string]
    $sum = @{}
    $line = 1
    foreach ($r in $rows) {
        $line++
        $name = "$($r.source)".Trim()
        if (-not $name) { throw "line ${line}: source is empty" }
        $n = @{}
        foreach ($f in 'registered', 'attended_live', 'watched_on_demand') {
            $x = [long]0
            if (-not [long]::TryParse("$($r.$f)".Trim(), [ref]$x) -or $x -lt 0) { throw "line ${line}: $f must be a whole number, zero or more (source $name)" }
            $n[$f] = $x
        }
        if ($n.attended_live + $n.watched_on_demand -gt $n.registered) {
            throw "line ${line}: attended_live + watched_on_demand cannot be more than registered (source $name)"
        }
        if (-not $sum.ContainsKey($name)) { $sum[$name] = @{ r = [long]0; l = [long]0; o = [long]0 }; $order.Add($name) }
        $sum[$name].r += $n.registered
        $sum[$name].l += $n.attended_live
        $sum[$name].o += $n.watched_on_demand
    }
    $tr = [long]0; $tl = [long]0; $to = [long]0
    foreach ($name in $order) {
        $s = $sum[$name]
        New-WebinarRow $name $s.r $s.l $s.o
        $tr += $s.r; $tl += $s.l; $to += $s.o
    }
    New-WebinarRow 'ALL' $tr $tl $to
}

Export-ModuleMember -Function Get-WebinarAttendance