UniversalDashboardServer.psm1

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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) {
            Foreach($datasetDef in $Dataset) {
                $datasetDef.data = $Items | ForEach-Object { $_.($datasetDef.DataProperty) }
                $datasets += $datasetDef
            }
        }
        else {
            $datasets += 
                @{
                    label = $DatasetLabel
                    backgroundColor = $BackgroundColor.HtmlColor
                    borderColor = $BorderColor.HtmlColor
                    hoverBackgroundColor = $HoverBackgroundColor.HtmlColor
                    hoverBorderColor = $HoverBorderColor.HtmlColor
                    borderWidth = 1
                    data = $Items | ForEach-Object { $_.$DataProperty }
                }
        }

        @{
            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
    }
}