pages/job.ps1

New-UDPage -Url "job/:jobid" -Endpoint {
    param($jobid)

    $Job = Get-UAJob -Id $JobId
    $Identity = Get-UAIdentity -Id $Job.Identity.Id
    $Script = Get-UAScript -Id $Job.Script.Id
    $ManualTime = $Script.$ManualTime
    $JobFeedback = $Job | Get-UAJobFeedback
    $JobOutput = Get-UAJobOutput -JobId $Job.Id

    $Session:FeedbackShown = $false

    New-UDElement -Tag 'span' -Id 'refresher' -Content {
        New-UDElement -Tag 'span' -Endpoint {
            $UpdatedJob = Get-UAJob -Id $JobId

            Set-UDElement -Id 'icon' -Content { New-UDJobIcon -Job $UpdatedJob -NoSpan }
            Set-UDElement -Id 'progress' -Content { New-UDJobProgress -Job $UpdatedJob }

            if ($UpdatedJob.Status -eq 'WaitingOnFeedback' -and -not $Session:FeedbackShown)
            {
                $Session:FeedbackShown = $true

                $Feedback = (Get-UAJobFeedback -JobId $JobId) | Where-Object Complete -eq $false

                Show-UDModal -Header { $Feedback.Message } -Content {
                    New-UDInput -Title $Feedback.Message -Endpoint {
                        param($Response)

                        Set-UAJobFeedback -JobFeedback $Feedback -Response $Response

                        Hide-UDModal
                    }
                }
            }

            if ($UpdatedJob.Status -eq 'Completed' -or $UpdatedJob.Status -eq 'Failed')
            {
                Clear-UDElement -Id 'refresher'   
            }
        } -AutoRefresh -RefreshInterval 5
    }
    
    New-UDHelmet -Content {
        New-UDHtmlTag -Tag 'title' -Content { "Universal Automation / Jobs / $($Job.Id)" }
    }

    New-UDRow -Columns {
        New-UDColumn -Size 4 -Content {
                        
            New-UDElement -Tag 'div' -Content {
                #New-UDIcon -Icon Terminal
                New-UDPageHeader -Text ("Job: $JobId - Script: " + $($Script.Name))
                New-UDElement -Tag div -Content { 
                    New-UDLink -Text "Script ID: $($Script.Id)" -Url "/script/$($Script.Id)"
                }
                New-UDElement -Tag 'b' -Content { "Status: " }
                New-UDJobIcon -Id 'icon' -Job $Job
                New-UDHtml -Markup ("<b>Created</b>: " + ($Job.CreatedTime.ToString()))
                if($Job.UserName -ne $null)
                {
                    New-UDHtml -Markup ("<b>User Name</b>: " + ($Job.UserName))
                }
                
            }
        }
        New-UDColumn -Size 6 -Content {
            New-UDElement -Tag 'div' -Id 'progress' -Content {
                New-UDJobProgress -Job $Job
            }
        }
        New-UDColumn -Size 2 -Content {
            New-UDElement -Tag div -Attributes @{
                textAlign = 'right'
                width = '100cw'
            } -Content {
                New-UDPadding -Padding '5px' -Content {

                    if ($Job.Status -eq 'Running' -or $Job.Status -eq 'WaitingOnFeedback' -or $Job.Status -eq 'Queued')
                    {
                        if($null -eq $Session:UserRole -or ($Session:UserRole -eq "Administrator" -or $Session:UserRole -eq "Operator"))
                        {
                            New-UDButton -Id 'btnCancelJob' -Text Cancel -Width 150 -OnClick {
                                New-JobCancelModal -Job $Job
                            } -Icon stop
                        }
                    }

                    if ($Job.Status -eq 'Completed' -or $Job.Status -eq 'Canceled' -or $Job.Status -eq 'Failed')
                    {
                        # Todo Implement Job ReRunning
                        #New-UDButton -Id 'btnReRunJob' -Text ReRun -OnClick {
                        # New-JobReRunModal -Job $Job
                        #} -Icon redo
                    }


                }
            }
        }
    }

    New-UDTabContainer -Tabs {
        New-UDTab -Text "Details" -Content {

            # Prepping Status
            If($Job.EndTime -eq '1/1/0001 12:00:00 AM')
            {
                $Duration = ((Get-Date) - $Job.StartTime).TotalSeconds
                $EndTime = "N/A"
            }
            else {
                $Duration = ($Job.EndTime - $Job.StartTime).TotalSeconds 
                $EndTime = $Job.EndTime
            }

            If($ManualTime -gt 0 -and $Duration -gt 0)
            {
                $TimeSaved = [TimeSpan]::FromSeconds($ManualTime - $Duration)
            }
            else {
                $TimeSaved = "N/A"
            }
            
            

            New-UDCard -Title "Job Details" -Content {
                New-UDLayout -Columns 3 -Content {
                    #New-UDElement -Tag "div" -Content { "Duration: $Duration seconds" }
                    #New-UDElement -Tag "div" -Content { "Triggered at: $($Job.StartTime)"}
                    #New-UDElement -Tag "div" -Content { "Time Saved: $TimeSaved"}
                    #New-UDElement -Tag "div" -Content { "PowerShell Version: $($Job.PowershellVersion)"}
                    
                    # Trigger Details
                    New-UDHtml -Markup ("<b>Triggered by</b>: $($Identity.Name)")
                    New-UDHtml -Markup ("<b>Triggered at</b>: " + ($Job.StartTime))
                    New-UDHtml -Markup ("<b>PowerShell Version</b>: $($Job.PowershellVersion)")

                    # Run Details
                    New-UDHtml -Markup ("<b>Duration</b>: $Duration seconds")
                    New-UDHtml -Markup ("<b>Started</b>: $($Job.StartTime)")
                    New-UDHtml -Markup ("<b>Ended</b>: $EndTime")

                    # Extra Details
                    if($TimeSaved -ne "N/A")
                    {
                        New-UDHtml -Markup ("<b>Time Saved</b>: $TimeSaved Minutes")
                    }
                    else {
                        New-UDHtml -Markup ("<b>Time Saved</b>: $TimeSaved")
                    }

                    
                    New-UDElement -Tag 'div' -Content {
                        
                        if($null -ne $JobOutput)
                        {
                            New-UDHtml -Markup ("<b>Has Output</b>: YES")
                        }
                        else {
                            New-UDHtml -Markup ("<b>Has Output</b>: NO")
                        }
                                                        
                    }

                    New-UDElement -Tag 'div' -Content {
                        
                        if($null -ne $JobFeedback)
                        {
                            New-UDHtml -Markup ("<b>Has/Had Feedback</b>: YES")
                        }
                        else {
                            New-UDHtml -Markup ("<b>Has/Had Feedback</b>: NO")
                        }
                    }
                    
                }
            }
        }

        if ($Job.Status -eq 'WaitingOnFeedback')
        {
            if($null -eq $Session:UserRole -or ($Session:UserRole -eq "Administrator" -or $Session:UserRole -eq "Operator"))
            {
                New-UDTab -Text "Feedback" -Content {
                $Feedback = (Get-UAJobFeedback -JobId $JobId) | Where-Object Complete -eq $false
                    
                    New-UDInput -Title $Feedback.Message -Endpoint {
                        param($Response)
                        Set-UAJobFeedback -JobFeedback $Feedback -Response $Response
                    }
                }
            }
        }
        
        New-UDTab -Text "Output" -Content {
             New-UDCard -Content {
                  New-UDElement -Tag 'div' -Attributes @{
                      style = @{
                           height = "500px"
                       }
                } -Content {
                    $Message = (Get-UAJobOutput -JobId $jobid | ForEach-Object { "[$($_.Timestamp)] [$($_.Type.ToString().ToUpper())] $($_.Message)" }) -join "`n"
                   New-UDCodeEditor -Language "powershell" -ReadOnly -Code $Message -Theme 'vs-dark' -Autosize
                }
            }
        }
        
        $PipelineOutput = Get-UAJobPipelineOutput -Job $Job 
        if ($null -ne $PipelineOutput)
        {
            New-UDTab -Text "Pipeline Output" -Content {
                New-PipelineGrid -Job $Job
            }
        }
        
        $Parameters = Get-UAJobParameter -JobId $JobId

        if ($null -ne $Parameters)
        {
            New-UDTab -Text "Parameters" -Content {
                New-UDTable -Title "Parameters" -Headers @("Name", "Value") -Content {
                    $Parameters | Out-UDTableData -Property @("Name", "Value")
                }
            }
        }

        # $Tree = Get-UAJob -Id $JobId -Tree
        
        # if ($Tree.Children.Count -gt 1)
        # {
        # New-UDTab -Text "Job Tree" -Content {
        # New-UDJobTree -Tree $Tree
        # }
        # }
    } 
}