Public/New-CsvFile.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<#
    .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-CsvFile.

    .OUTPUTS
    The new configuration

    .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-CsvFile

    .LINK
    Set-Fileconfig
    Get-MetricDefinitions
    Add-PatientScenario

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

function New-CsvFile {
    param($Filename = "data.csv", $MetricDefinitions)

    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 PSObject
        $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
}