-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathGet-StaReductions_ChartHtml.ps1
136 lines (117 loc) · 5.2 KB
/
Get-StaReductions_ChartHtml.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
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
#Requires -Version 5.0
<#
.SYNOPSIS
Generates a chart report about the reductions
.DESCRIPTION
.NOTES
This PowerShell script was developed and optimized for ScriptRunner. The use of the scripts requires ScriptRunner.
The customer or user is authorized to copy the script from the repository and use them in ScriptRunner.
The terms of use for ScriptRunner do not apply to this script. In particular, ScriptRunner Software GmbH assumes no liability for the function,
the use and the consequences of the use of this freely available script.
PowerShell is a product of Microsoft Corporation. ScriptRunner is a product of ScriptRunner Software GmbH.
© ScriptRunner Software GmbH
.COMPONENT
Requires the library script StatisticLib.ps1
Requires Library Script SRReportsLib from the Action Pack Reporting\_LIB_
.LINK
https://github.com/scriptrunner/ActionPacks/tree/master/Statistics/_REPORTS_
.Parameter StartDate
[sr-en] Start date of the report
[sr-de] Startdatum des Reports
.Parameter EndDate
[sr-en] End date of the report
[sr-de] Endedatum des Reports
.Parameter ActionName
[sr-en] Only executions of this action
[sr-de] Nur Ausführungen dieser Aktion
.Parameter TargetName
[sr-en] Only executions on this target
[sr-de] Nur Ausführungen auf diesem Zielsystem
.Parameter ReportLanguage
[sr-en] Display language and Datie formation culture
[sr-de] Anzeige-Sprache und Datums-Formatierung
#>
[CmdLetBinding()]
Param(
[Parameter(HelpMessage="ASRDisplay(Date)")]
[datetime]$StartDate,
[Parameter(HelpMessage="ASRDisplay(Date)")]
[datetime]$EndDate,
[string]$ActionName,
[string]$TargetName,
[ValidateSet('en','de','fr','es','it','hu')]
[string]$ReportLanguage = 'en'
)
$con = $null
[string]$Script:ReductionColor = '#4C81B5' # bar color to use remove AutoBarColors in line 110
[string]$Script:ReductionBorderColor = '#195DA0'
try{
$Script:rep = New-Object 'System.Collections.Generic.List[System.Object]'
OpenSqlConnection -SqlCon ([ref]$con)
if($null -eq $StartDate){
$StartDate = Get-Date -Year 2019 -Month 1 -Day 1
}
if($null -eq $EndDate){
$EndDate = Get-Date
}
$rep.Add((Open-REPDocument -Language $ReportLanguage)) # Get document Html code
[hashtable]$cmdArgs = @{
'ActionName' = $SRXEnv.SRXDisplayName
'StartedBy' = $SRXEnv.SRXStartedBy
'TimeStamp' = $SRXEnv.SRXStarted
'DateTimeCulture' = $ReportLanguage
}
if($PSBoundParameters.ContainsKey('ActionName') -eq $true){
$cmdArgs.Add("Action filter:",$ActionName)
}
if($PSBoundParameters.ContainsKey('TargetName') -eq $true){
$cmdArgs.Add("Target filter:",$TargetName)
}
$rep.Add((GET-REPHeader @cmdArgs)) # get header html
# execute stored procedure
$sqlCmd = New-Object System.Data.SqlClient.SqlCommand
$sqlDS = New-Object System.Data.DataSet
$sqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$sqlCmd.CommandText = 'GetExecutions'
$sqlCmd.CommandType = [System.Data.CommandType]::StoredProcedure
$null = $sqlCmd.Parameters.AddWithValue('StartDate',$StartDate.Date.ToFileTimeUtc())
$null = $sqlCmd.Parameters.AddWithValue('EndDate',$EndDate.AddDays(1).Date.ToFileTimeUtc())
if($PSBoundParameters.ContainsKey('ActionName') -eq $true){
$null = $sqlCmd.Parameters.AddWithValue('Action',$ActionName)
}
if($PSBoundParameters.ContainsKey('TargetName') -eq $true){
$null = $sqlCmd.Parameters.AddWithValue('Target',$TargetName)
}
$sqlCmd.Connection = $con
$sqlAdapter.SelectCommand = $sqlCmd
$null = $sqlAdapter.Fill($sqlDS) # fill dataset
if(($null -ne $sqlDS) -and ($sqlDS.Tables.Count -gt 0) -and ($sqlDS.Tables[0].Rows.Count -gt 0)){
$acGrouped = ($sqlDS.Tables[0].rows | Sort-Object ActionName | Group-Object Action | Select-Object Name)
$bars = New-Object System.Collections.Generic.Queue[BarChartProperties]
# create bar for action
foreach($item in $acGrouped){
$acObj = $sqlDS.Tables[0].rows | Where-Object {$_.Action -eq $item.Name}
$bars.Enqueue(([BarChartProperties]::New( ($acObj | Select-Object -ExpandProperty ActionName -Unique),
([math]::Round((($acObj | Measure-Object -Property CostReduction -Sum).Sum /60),0)),
$Script:ReductionColor,$Script:ReductionBorderColor)))
}
# get bar chart Html
$Script:rep.Add((Get-BarChart -AutoBarColors -Bars $bars -LabelXAxis 'Time saved' -LabelYAxis 'Min.' -WidthPx 750 -HeightPx 750))
$sqlDS.Dispose()
}
$sqlAdapter.Dispose()
$sqlCmd.Dispose()
$Script:rep.Add((Close-REPDocument)) # close document tags
if($SRXEnv){
$SRXEnv.ResultHTML = $Script:rep | Out-String
}
else {
Write-Output "Not running on ScriptRunner PowerShell Host"
}
}
catch{
throw
}
finally{
CloseSqlConnection -SqlCon $con
}