Public/Watch-P1Commands.ps1

function Watch-P1Commands {
    <#
    .Synopsis
    Watch the commands on PlannerOne server.
 
    .Description
    Display for one environment loaded in PlannerOne the current command, queued commands and executed commands.
 
    .Parameter Tenant
    The tenant to use to retrieve connection informations.
 
    .Parameter HostName
    The hostname to query (default localhost)
 
    .Parameter ServicePort
    The hostname port of REST Service (default 80)
 
    .Parameter Product
    The product to query (PS or RP - Default PS)
 
    .Parameter NameFilter
    Filter for the environment name.
 
    .Example
    Watch-P1ServerModel -ServicePort 8073
    #>


    [cmdletbinding()]
    param(
        [string] $Tenant,
        [string] $HostName,
        [int] $ServicePort,
        [string] $Product,
        [string] $NameFilter
    )
    Process
    {
        if ($Tenant -ne "") {
            $info = Get-TenantInfo $Tenant
            if ($info -eq $null) {
                Write-Warning "No information registered for tenant $Tenant"
                return
            }

            $HostName = $info.ServiceHost
            $ServicePort = $info.WebServicePort
        } else {
            $Tenant = "NA"
        }

        if ($HostName -eq "") {
            $HostName = "localhost"
        }

        if ($ServicePort -eq 0) {
            $ServicePort = 80
        }

        [string] $message = ""
        $state = 0
        $auto = $true
        $continue = $true
        while($continue)
        {
            clear
            echo "Tenant: $Tenant - Host: $HostName - Port: $ServicePort"
            Get-Date
            if ($state -eq 0) {
                if ($Product -ne "RP") {
                    $Product = "PS"
                    $service = "commandproduction"
                }
                else {
                    $service = "commandproject"
                }

                $escapedService = EscapeQueryData $service
                $url = 'http://' + $HostName + ':' + $ServicePort + "/Services/$escapedService/"
                $serviceUrl = FixUrl $url
                Write-Verbose $serviceUrl

                echo "Product: $Product - Filter: $NameFilter"
                if ($message -ne "") {
                    echo $message
                    $message = ""
                }

                $ids = (wget $serviceUrl).Content
                Write-Verbose $ids
                $idsObj = $ids | ConvertFrom-Json

                $filteredArray = @()
                $displayArray = @()
                if ($idsObj.Length -eq 0) {
                    Write-Output "NO COMMAND EXECUTER FOR $Product"
                } else {
                    [int] $i = 1
                    foreach ($env in $idsObj) {
                        $envName = $env.Name
                        $peak = $true
                        if ($NameFilter -ne "") {
                            if (!($envName.Contains($NameFilter))) {
                                $peak = $false
                            }
                        }

                        if ($peak) {
                            $displayObj = New-Object System.Object
                            $displayObj | Add-Member -type NoteProperty -name No -value $i
                            $displayObj | Add-Member -type NoteProperty -name Name -value $envName
                            $displayObj | Add-Member -type NoteProperty -name Group -value $env.PlanningGroupContainer

                            $displayArray += $displayObj
                            $filteredArray += $env
                        }

                        $i++
                    }

                    $displayArray | Format-Table
                }

                echo "ESCAPE: Quit | R: Refresh | 1-9: Select environment | S: PS/RP switch"

                $x = [System.Console]::ReadKey()
                [int] $select = -1
                switch ($x.key)
                {
                    # http://msdn.microsoft.com/en-us/library/system.consolekey(v=vs.110).aspx
                    Escape { $continue = $false }
                    R {}
                    S {
                        if ($Product -eq "PS") {
                            $Product = "RP"
                        } else {
                            $Product = "PS"
                        }
                    }
                    D1 {
                        $select = 0
                    }
                    D2 {
                        $select = 1
                    }
                    D3 {
                        $select = 2
                    }
                    D4 {
                        $select = 3
                    }
                    D5 {
                        $select = 4
                    }
                    D6 {
                        $select = 5
                    }
                    D7 {
                        $select = 6
                    }
                    D8 {
                        $select = 7
                    }
                    D9 {
                        $select = 8
                    }
                    NumPad1 {
                        $select = 0
                    }
                    NumPad2 {
                        $select = 1
                    }
                    NumPad3 {
                        $select = 2
                    }
                    NumPad4 {
                        $select = 3
                    }
                    NumPad5 {
                        $select = 4
                    }
                    NumPad6 {
                        $select = 5
                    }
                    NumPad7 {
                        $select = 6
                    }
                    NumPad8 {
                        $select = 7
                    }
                    NumPad9 {
                        $select = 8
                    }
                }

                if ($select -ne -1) {
                    if ($select -gt $filteredArray.Length) {
                        $message = "Selected environment does not exist"
                    } else {
                        $selectedEnv = $filteredArray[$select]
                        $state = 1
                        clear
                    }
                }
            } else {
                if ($state -eq 1 -and $continue) {
                    $escapedService = EscapeQueryData $service
                    $escapedName = EscapeQueryData $selectedEnv.Name
                    $escapedGroup = EscapeQueryData $selectedEnv.PlanningGroupContainer

                    $url = 'http://' + $HostName + ':' + $ServicePort + "/Services/" + $escapedService + "/" + $escapedName + '/' + $escapedGroup
                    $serviceUrl = FixUrl $url
                    Write-Verbose $serviceUrl

                    $headLine = "Product: $Product - Environment: " + $selectedEnv.Name + " - " + $selectedEnv.PlanningGroupContainer
                    echo $headLine
                    $commands = (wget $serviceUrl).Content
                    Write-Verbose $commands
                    $commandsObj = $commands | ConvertFrom-Json
                    $commandsObj | Format-Table

                    echo "ESCAPE: Quit | R: Refresh | L: Got back to list | A: Auto-Refresh (5s): $auto"

                    $counter = 0
                    if ($auto) {
                        while(![console]::KeyAvailable -and ($counter++ -lt 10))
                        {
                            [Threading.Thread]::Sleep( 500 )
                        }
                    }

                    if ([console]::KeyAvailable -or !$auto)
                    {
                        $x = [System.Console]::ReadKey()

                        switch ($x.key)
                        {
                            # http://msdn.microsoft.com/en-us/library/system.consolekey(v=vs.110).aspx
                            Escape { $continue = $false }
                            R {}
                            L {
                                $selectedEnv = $null
                                $state = 0
                            }
                            A { $auto = !$auto }
                        }
                    }
                }
            }
        }

        Write-Output "Watch end"
        # Write-Host must be call for ReadKey to not corrupt the console
        Write-Host
    }
}