Public/Get-MetricDefinitions.ps1

<#
    .SYNOPSIS
    Retrieve the metadata from the salesforce org related to measurements, metrics, units, and device models.
 
    .DESCRIPTION
    This function returns a hastable with the following keys:
        measurements - the contents of the table "phecc__Measurement_Type__c"
        measurementMetrics - the contents of the table "phecc__Measurement_Type_Metric__c"
        metrics - the contents of the table "phecc__Metric__c"
        metricUnits - the contents of the table "phecc__Metric_Unit__c"
        units - the contents of the table "phecc__Unit__c"
        models - the contents of the table "phecc__Device_Model__c"
        capabilities - the contents of the table "phecc__Measurement_Type_Capability__c"
 
    The multiple queries can 10 seconds or more so the result is cached in a file as specified by the CacheFile parameter.
 
    .INPUTS
    None. You cannot pipe objects to Get-MetricDefinitions.
 
    .OUTPUTS
    An Hashtable with keys contains metadata related to measurements, metrics, units, and device models in the org
 
    .PARAMETER CacheFile
    An existing cache file to avoid the call if exists. Defaults to "./measurement-metric-cache.json"
 
    .EXAMPLE
    C:\PS> $def = Get-MetricDefinitions
 
    .LINK
    See Get-Patients
 
    .NOTES
    Assumes config is initialized for org access.
#>

function Get-MetricDefinitions {
    param($CacheFile = "./measurement-metric-cache.json")
    $metadata = $null
    if ($CacheFile -and (Test-Path $CacheFile -PathType Leaf)) {
        $metadata = Get-Content -Path $CacheFile | ConvertFrom-Json -AsHashtable
    } else {
        $metadata = @{
            measurements = (Invoke-SfQuery "SELECT Id,Name,phecc__Device_Type_Code__c,phecc__Device_Type__c FROM phecc__Measurement_Type__c")
            measurementMetrics = (Invoke-SfQuery "SELECT Id,phecc__Measurement_Type_Metric_Code__c,phecc__Measurement_Type__c,phecc__Metric__c FROM phecc__Measurement_Type_Metric__c")
            metrics = (Invoke-SfQuery "SELECT Id,Name,phecc__Metric_Type_Code__c FROM phecc__Metric__c")
            metricUnits = (Invoke-SfQuery "SELECT Id,phecc__Metric_Unit_Code__c,phecc__Metric__c,phecc__Unit__c FROM phecc__Metric_Unit__c")
            units = (Invoke-SfQuery "SELECT Id,Name,phecc__Unit_Type_Code__c FROM phecc__Unit__c")
            models = (Invoke-SfQuery "SELECT Id,Name FROM phecc__Device_Model__c")
            capabilities = (Invoke-SfQuery "SELECT Id,phecc__Device_Model__c,phecc__Measurement_Type__c FROM phecc__Measurement_Type_Capability__c")
        }
        $metadata | ConvertTo-Json -Depth 100 | Set-Content -Path $CacheFile | Out-Null
    }
    $metadata
}