Public/New-MeasurementDataCsvFile.ps1

<#
    .SYNOPSIS
    Create a new template scenario CSV file to contain measurement data.

    .DESCRIPTION
    Used to create a new template metric CSV file. The command will prompt with metric definitions and possible units
    from the org. Select one or more rows and the file will be created those in template form.

    The file created is used by the Add-PatientScenario cmdlet.

    .INPUTS
    None. You cannot pipe objects to New-MeasurementDataCsvFile.

    .OUTPUTS
    None

    .PARAMETER Filename
    The file name to create. Defaults to "data.csv"

    .PARAMETER MetricDefinitions
    The metric definitions from Get-MetricDefinitions. Calls Get-MetricDefinitions is not supplied.

    .EXAMPLE
    PS> New-MeasurementDataCsvFile

    .LINK
    Set-Config
    Get-MetricDefinitions
    Add-PatientScenario

    .NOTES
    Assumes config is initialized for org access.
#>

function New-MeasurementDataCsvFile {

    [CmdletBinding()]
    [OutputType([System.Void])]
    param(
        [Parameter(Mandatory = $false, Position = 0, ValueFromPipeline)]
        [ValidateNotNullOrEmpty()]
        [String]
        $Filename = "data.csv",

        [Parameter(Mandatory = $false, Position = 1)]
        [ValidateNotNull()]
        [PSCustomObject]
        $MetricDefinitions
    )

    begin {
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] Function started"
    }

    end {
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] Complete"
    }

    process {
        Write-Debug "[$($MyInvocation.MyCommand.Name)] PSBoundParameters: $($PSBoundParameters | Out-String)"
        if (-not $PSBoundParameters.ContainsKey('MetricDefinitions')) {
            $MetricDefinitions = Get-MetricDefinitions
        }
        $options = @()
        foreach ($measurement in $MetricDefinitions.measurements) {
            $measurementMetrics = $MetricDefinitions.measurementMetrics | Where-Object { $_.phecc__Measurement_Type__c -eq $measurement.Id }
            foreach ($measurementMetric in $measurementMetrics) {
                $metrics = $MetricDefinitions.metrics | Where-Object { $_.Id -eq $measurementMetric.phecc__Metric__c }
                foreach ($metric in $metrics) {
                    #$column1 = "$($metric.Name)"
                    $metricUnits = $MetricDefinitions.metricUnits | Where-Object { $_.phecc__Metric__c -eq $metric.Id }
                    foreach ($metricUnit in $metricUnits) {
                        #$column2 = $Row."$($metric.Name):units"
                        $units = $MetricDefinitions.units | Where-Object { $_.Id -eq $metricUnit.phecc__Unit__c }
                        foreach ($unit in $units) {
                            $options += @{
                                measurement = $measurement
                                metric      = $metric
                                unit        = $unit
                            }
                        }
                    }
                }
            }
        }
        $selected = $options | Select-Object @{Name = 'Measurement'; expression = { ($_.measurement.Name) } }, @{Name = 'Metric'; expression = { ($_.metric.Name) } }, @{Name = 'Unit'; expression = { ($_.unit.Name) } }, @{Name = 'Object'; expression = { ($_) } }  | Out-GridView -PassThru
        $Columns = @("MinuteOffset", "Type", "Name")
        $selected | ForEach-Object {
            $Columns += "$($_.Object.metric.Name)"
            $Columns += "$($_.Object.metric.Name):units"
        }
        $Rows = @()
        $selected | ForEach-Object {
            $nextSelected = $_
            $Row = New-Object PSCustomObject
            $Columns | ForEach-Object {
                $value = ""
                if ($selected.Object.metric.Name) {
                    switch ($_) {
                        "MinuteOffset" { $value = 0 }
                        "Type" { $value = "Measurement" }
                        "Name" { $value = "$($nextSelected.Object.measurement.Name)" }
                        "$($nextSelected.Object.metric.Name)" { $value = "0" }
                        "$($nextSelected.Object.metric.Name):units" { $value = $nextSelected.Object.unit.Name }
                    }
                    $Row | Add-Member -NotePropertyName $_ -NotePropertyValue $value
                }
            }
            $Rows += $Row
        }
        $Rows | ConvertTo-Csv | Set-Content -Path $Filename
    }
}