UniversalDashboard.psm1

if (!$PSVersionTable.PSEdition -or $PSVersionTable.PSEdition -eq "Desktop") {
    Import-Module -Name "$PSScriptRoot/net451/PowerShellProTools.UniversalDashboard.dll" | Out-Null
}
else {
    Import-Module -Name "$PSScriptRoot/netcoreapp2.0/PowerShellProTools.UniversalDashboard.dll" | Out-Null
}

if ((Test-Path (Join-Path $PSScriptRoot "license.lic"))) {
    Set-UDLicense -License (Get-Content (Join-Path $PSScriptRoot "license.lic"))
}

function Out-UDMonitorData {
    [CmdletBinding()]
    param(
    [Parameter(ValueFromPipeline = $true)]
    $Data)

    Process {
        @{ 
            x = [DateTime]::UtcNow
            y = $Data
        } | ConvertTo-Json
    }
}

function Out-UDChartData {
    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline = $true)]
        $Data, 
        [Parameter()]
        [string]$DataProperty, 
        [Parameter()]
        [string]$LabelProperty,
        [Parameter()]
        [string]$DatasetLabel = "",
        [Parameter()]
        [Hashtable[]]$Dataset,
        [Parameter()]
        [PowerShellProTools.UniversalDashboard.Models.DashboardColor[]]$BackgroundColor = @("#808978FF"),
        [Parameter()]
        [PowerShellProTools.UniversalDashboard.Models.DashboardColor[]]$BorderColor = @("#FF8978FF"),
        [Parameter()]
        [PowerShellProTools.UniversalDashboard.Models.DashboardColor[]]$HoverBackgroundColor = @("#807B210C"),
        [Parameter()]
        [PowerShellProTools.UniversalDashboard.Models.DashboardColor[]]$HoverBorderColor = @("#FF7B210C")
    )
    
    Begin {
        New-Variable -Name Items -Value @()
    }

    Process {
        $Items += $Data
    }

    End {
        $datasets = @()

        if ($Dataset -ne $null) {
            $Dataset | ForEach-Object {
                $DatasetProperty = $_.DataProperty
                $_.data = $Items | ForEach-Object { $_.$DatasetProperty }
                $datasets += $_
            }
        }
        else {
            $data = foreach($item in $Items) {
                $item.$DataProperty
            }

            $datasets += 
                @{
                    label = $DatasetLabel
                    backgroundColor = $BackgroundColor.HtmlColor
                    borderColor = $BorderColor.HtmlColor
                    hoverBackgroundColor = $HoverBackgroundColor.HtmlColor
                    hoverBorderColor = $HoverBorderColor.HtmlColor
                    borderWidth = 1
                    data = $data
                }
        }

        @{
            labels = $Items.$LabelProperty
            datasets = $datasets
        } | ConvertTo-Json -Depth 10
    }
}

function New-UDChartDataset {
    [CmdletBinding()]
    param(
        [string]$DataProperty,
        [string]$Label,
        [PowerShellProTools.UniversalDashboard.Models.DashboardColor[]]$BackgroundColor = @("#807B210C"),
        [PowerShellProTools.UniversalDashboard.Models.DashboardColor[]]$BorderColor = @("#FF7B210C"),
        [int]$BorderWidth,
        [PowerShellProTools.UniversalDashboard.Models.DashboardColor[]]$HoverBackgroundColor = @("#807B210C"),
        [PowerShellProTools.UniversalDashboard.Models.DashboardColor[]]$HoverBorderColor = @("#FF7B210C"),
        [int]$HoverBorderWidth,
        [string]$XAxisId,
        [string]$YAxisId,
        [Hashtable]$AdditionalOptions
    )

    Begin {
        $datasetOptions = @{
            data = @()
            DataProperty = $DataProperty
            label = $Label
            backgroundColor = $BackgroundColor.HtmlColor
            borderColor = $BorderColor.HtmlColor
            borderWidth = $BorderWidth
            hoverBackgroundColor = $HoverBackgroundColor.HtmlColor
            hoverBorderColor = $HoverBorderColor.HtmlColor
            hoverBorderWidth = $HoverBorderWidth
            xAxisId = $XAxisId
            yAxisId = $YAxisId
        }

        if ($AdditionalOptions) {
            $AdditionalOptions.GetEnumerator() | ForEach-Object {
                $datasetOptions.($_.Key) = $_.Value
            }
        }

        $datasetOptions
    }
}

function Out-UDGridData {
    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline = $true)]
        $Data,
        [Switch]$NoAutoPage,
        $TotalItems
    )
    
    Begin {
        New-Variable -Name Items -Value @()
    }

    Process {
        $Items += $Data
    }

    End {

        if (-not $NoAutoPage) {
            $total = $Items.length
            $Items = $Items | Select-Object -First $take -Skip $skip
        } else {
            $total = $TotalItems
        }

        @{
            data = $Items
            recordsTotal = $total
            recordsFiltered = $total
            draw = $drawId
        } | ConvertTo-Json -Depth 10
    }
}

function Out-UDTableData {
    [CmdletBinding()]
    param(
    [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
    $Data,
    [Parameter(Mandatory = $true)]
    [string[]]$Property
    )
    
    Begin {
        New-Variable -Name Items -Value @()
    }

    Process {
        $Items += $Data
    }

    End {
        $rows = @()
        foreach($item in $items) {
            $row = @()

            foreach($itemProperty in $Property) {
                $row += $item.$itemProperty
            }
            $rows += , $row
        }

        $rows | ConvertTo-Json -Depth 10
    }
}

function Set-UDBackwardsCompatbility {
    Write-Warning "This cmdlet is a temporary workaround for scripts using the previous cmdlet names and will be removed in future versions."
    New-Alias -Name "New-Row" -Value "New-UDRow"
    New-Alias -Name "New-Chart" -Value "New-UDChart"
    New-Alias -Name "New-Monitor" -Value "New-UDMonitor"
    New-Alias -Name "New-Column" -Value "New-UDColumn"
    New-Alias -Name "Get-Dashboard" -Value "Get-UDDashboard"
    New-Alias -Name "New-Card" -Value "New-UDCard"
    New-Alias -Name "New-Counter" -Value "New-UDCounter"
    New-Alias -Name "New-Dashboard" -Value "New-UDDashboard"
    New-Alias -Name "New-Grid" -Value "New-UDGrid"
    New-Alias -Name "New-Html" -Value "New-UDHtml"
    New-Alias -Name "New-Image" -Value "New-UDImage"
    New-Alias -Name "New-Link" -Value "New-UDLink"
    New-Alias -Name "New-Page" -Value "New-UDPage"
    New-Alias -Name "New-Table" -Value "New-UDTable"
    New-Alias -Name "Start-Dashboard" -Value "Start-UDDashboard"
    New-Alias -Name "Stop-Dashboard" -Value "Stop-UDDashboard"
    New-Alias -Name "Out-ChartData" -Value "Out-UDChartData"
    New-Alias -Name "Out-MonitorData" -Value "Out-UDMonitorData"
    New-Alias -Name "New-ChartDataSet" -Value "New-UDChartDataSet"
    New-Alias -Name "Out-GridData" -Value "Out-UDGridData"
    New-Alias -Name "Out-TableData" -Value "Out-UDTableData"
}

New-Alias -Name "Row" -Value "New-UDRow"
New-Alias -Name "Column" -Value "New-UDColumn"
New-Alias -Name "Chart" -Value "New-UDChart"
New-Alias -Name "Table" -Value "New-UDTable"
New-Alias -Name "Grid" -Value "New-UDGrid"