Hi - I’m creating a Dashboard to monitor the status of a scientific instrument (a telescope array) with many components. I’ve got a prototype Dashboard put together which I’m testing now, but when looking at my code I’m scratching my head wondering if I’m doing things in an optimal way. I’m a Universal Dashboard newbie (and I’m a scientist not a developer so am in a bit over my head) and if I’m doing something in a really silly way I’d like to fix it now before I go much further. Could I lean upon the wisdom of this group for your opinion?
A screenshot of my dashboard (such as it is) is shown here:
The part of my code that I’m rather dubious about is being used to make (and update, once a second) the colored boxes shown at the top, in the ‘Status’ card. The colors represent the state of various components of the instrument (white=inactive, green=active, red=error). Each of these boxes is a UDElement, and the colors are set by polling a cached data structure once every second. The cached data structure is itself constructed by polling a relational database (also once a second) but that’s done independently using a scheduled endpoint that is defined at the start of my script. (This approach was chosen because several other cards use the information from the database too, so I wanted to minimize the number of times we poll it).
The specific bit of my project that has me worried is this snippet, where I generate the card with the status lights and dynamically alter the colors according to the device states:
New-UDCard -Id StatusPanelCard -Title "Status" -Content {
New-UDElement -Tag 'div' -AutoRefresh -RefreshInterval 1 -Endpoint {
New-UDLayout -Columns 4 -Content {
ForEach ($device in ($Cache:BackgroundColor).keys) {
New-UDElement -Tag "p" -Attributes @{
style = @{"background-color" = ($Cache:BackgroundColor)[$device];
"border-color" = "black";
"border-style" = "solid";
"text-align" = "center";
width = '95%'
}
} -Content { $device } -Id $device
}
}
}
}
The colors of the boxes are stored in a hash, $Cache:BackgroundColor, and the keys of the hash are the device names which I’m using as IDs for the UDElements.
Although I’m creating 7 UDElements (the colored boxes) each second, I have all these New-UDElement calls wrapped in the endpoint of an outer New-UDElement so I only refresh a single endpoint (which is the technique suggested here: https://ironmansoftware.com/best-practices-for-universal-dashboard-performance/). However, it seems to me that making New-UDElements so many times (seven new elements each second) just to update the colors of some boxes might be a bit of overkill. Would it be smarter to just create them all once at the start of the script and then update their attributes (without re-creating the elements) each second instead? I have tried that approach (e.g. using Set-UDElement in the part of my code that updates the database) but can’t seem to get things to work. But it occurred to me that before spending more time trying to figure out why I can’t seem to change the colors dynamically using Set-UDElement, I should probably first ask here to see if people who are experienced with Universal Dashboard think my whole approach is silly. Is there an ‘obvious’ better approach to this problem that I should be adopting?
Thank you for any advice or suggestions - I’m having tremendous fun using Universal Dashboard but I’m painfully aware that this doesn’t mean I know what I’m doing!