modules/teamcityExtensions/HTML/ConvertTo-EnhancedHTMLFragmentRickshawJavascriptData.ps1

<#
The MIT License (MIT)
 
Copyright (c) 2015 Objectivity Bespoke Software Specialists
 
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
 
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
#>


function ConvertTo-EnhancedHTMLFragmentRickshawJavascriptData {

    <#
    .SYNOPSIS
    Creates a HTML fragment containing a Javascript array data.
 
    .PARAMETER InputObject
    Input array that will be converted to Javascript array.
 
    .PARAMETER JavascriptVariableName
    Name of Javascript variable name where the array will be assigned to.
 
    .PARAMETER PropertySeriesName
    Property name of InputObject that contains series name.
 
    .PARAMETER PrefixCode
    Custom Javascript code that will be prepended to the code generated by this function.
 
    .PARAMETER BuildIdMap
    Hashmap mapping build_id to sql row.
 
    .EXAMPLE
    ConvertTo-EnhancedHTMLFragmentRickshawJavascriptData -InputObject $trendData -JavascriptVariableName 'TestData' -PropertySeriesName 'test_name' -BuildIdMap $buildIdMap `
        -PrefixCode "var palette = new Rickshaw.Color.Palette({ scheme: 'munin' } );"
    #>


    [CmdletBinding()]
    [OutputType([string])]
    param(
        [Parameter(Mandatory=$True,ValueFromPipeline=$True)]
        [object[]]
        $InputObject,

        [Parameter(Mandatory=$True)]
        [string]
        $JavascriptVariableName,

        [Parameter(Mandatory=$True)]
        [string]
        $PropertySeriesName,

        [Parameter(Mandatory=$False)]
        [string]
        $PrefixCode,

        [Parameter(Mandatory=$False)]
        [hashtable]
        $BuildIdMap
    )
    BEGIN {
        $out = New-Object System.Text.StringBuilder

        [void]($out.Append(('<script type="text/javascript">{0}var {1} = [' -f "$PrefixCode`n", $JavascriptVariableName)))
    }
    PROCESS {

        foreach ($object in $inputobject) {
            $name = $object.$PropertySeriesName -replace '"', '\"'
            [void]($out.Append('{{ name: "{0}", color: palette.color(), data: [' -f $name))
            $properties = $object | Get-Member -MemberType Properties | Where-Object { $_.Name -ne $PropertySeriesName } | Foreach-Object { if ($_.Name -match '^\d+$') { [int]$_.Name} else { $_.Name } } | Sort
            $i = 1
            foreach ($property in $properties) {
                $value = $object.$property
                if ($value -eq [System.DBNull]::Value) {
                    $value = "null"
                }
                if ($BuildIdMap -and $BuildIdMap.ContainsKey([string]$property)) {
                    $success = $BuildIdMap[[string]$property].success.ToString().ToLower()
                    $buildNumber = $BuildIdMap[[string]$property].build_number
                } else {
                    $buildNumber = $property
                    $success = 'null'
                }
                [void]($out.Append(('{{ x: {0}, y: {1}, xLabel: ''{2}'', success: {3} }},' -f $i++, $value, $buildNumber, $success)))
            }
            [void]($out.Append('] },'))
        }
    }
    END {
        [void]($out.Append('];'))
        [void]($out.Append('</script>'))
        $out.ToString()
        
    }
}