Created
March 12, 2024 06:09
-
-
Save bryder/3912f909676a68a31689fac12e57ff62 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TrapInfo { | |
# Trap name - eg 18 or 16-1 etc | |
$Name | |
# if it was checked | |
$Done | |
# What was found | |
$Catch | |
# free form notes | |
$Notes | |
# Group - there can be multiple lines under a group - eg Group 1, Group 2 | |
$Group | |
# Line - groups can be broken into multiple lines - | |
# eg Group 1 - MW 18-10a, and Group 2, MW 10-4a, MSR3 | |
$Line | |
# TODO: static methods to get the long and short titles for the things | |
} | |
$groups = @{ | |
"Group 1" = @( | |
@{ | |
Name = "MW" | |
Value = @( | |
"18", "17", "16a", "16-1", "16", | |
"15a", "15-1", "15", "14a", "14", "13b", | |
"13a", "13-1", "13", "12a", "12", "11b", | |
"11a", "11", "10b", "10a" | |
) | |
} | |
) | |
"Group 2" = @( | |
@{ | |
Name = "MW" | |
Value = @( | |
"10", "9a", "9", "8a", "8", "7a", | |
"7", "6a", "6", "5a", "5", "4a" | |
) | |
} | |
@{ | |
Name = "MSR3" | |
Value = @( | |
"1", "2", "3", "4", "5", "6", "7", "8" | |
) | |
} | |
) | |
} | |
class ClearingTask { | |
# Start of the weekend to clear the traps | |
[string]$date | |
# Name of the task - eg MW-MSR3 | |
[string]$Name | |
# What to do for each group | |
[hashtable]$task | |
# groups to clear | |
$groups | |
ClearingTask(){ | |
write-debug "In clearingtask with no params" | |
$this.PSObject.TypeNames.Insert(0,"ClearingTask") | |
} | |
ClearingTask([string]$Name, [Roster]$roster, $groups){ | |
write-debug "In ClearingTask name ${Name} roster ${roster}" | |
$this.Name = $Name | |
$this.date = $roster.sat_date.ToString("ddd, d MMM yyyy") | |
$this.task = @{ | |
"Group 1" = $roster.eggs_group -eq 1 ? "Eggs + Erayz" : "Erayz" | |
"Group 2" = $roster.eggs_group -eq 2 ? "Eggs + Erayz" : "Erayz" | |
} | |
$this.groups = $groups | |
$this.PSObject.TypeNames.Insert(0,"ClearingTask") | |
} | |
} | |
function get-trap-line(){ | |
<# | |
.SYNOPSIS | |
get an array of objects for the named group/line | |
#> | |
[Cmdletbinding()] | |
param ( | |
# Name of the group - eg "Group 1" | |
$group, | |
# Name of the line - eg MSR3 | |
$line, | |
# hashtable containing the data | |
[hashtable]$groups = $groups | |
) | |
$group_record = $groups[$group] | |
if ($null -eq $group_record) { | |
throw "Could not find a record for group $group" | |
} | |
$trap_line = ($group_record.where({$_.Name -eq $line})).Value | |
$traps = $trap_line.foreach({ [TrapInfo]@{'Name'=$_; 'Group'=$group; 'Line'=$line }}) | |
$traps | |
} | |
function get-trap-lines(){ | |
[cmdletbinding()] | |
param () | |
# lines in order | |
$lines = @{} | |
write-debug "group keys $($groups.keys)" | |
foreach ($group_name in ($groups.keys | sort-object)){ | |
$lines[$group_name] = @() | |
write-debug "Group: $group_name" | |
$group = $groups[$group_name] | |
foreach ($line in $group) { | |
write-debug "line: $($line | out-string) " | |
$lines[$group_name] += ,(get-trap-line $group_name $line.Name) | |
} | |
$convert_this += $group_sub | |
} | |
write-debug "lines result: $($lines | convertto-json -depth 10)" | |
$lines | |
} | |
function get-tables(){ | |
[cmdletbinding()] | |
param ( | |
# array of lines to convert to html tables | |
[Parameter(Mandatory)] | |
$lines, | |
# About the tasks etc | |
[Parameter(Mandatory)] | |
[ClearingTask]$line_info | |
) | |
foreach ($group_name in ($lines.keys | sort-object)) { | |
$group = $lines[$group_name] | |
$group_trap_count = @($group |foreach-object{$_}).length | |
@" | |
<div class=`"$($group_name -replace ' ','')`"> | |
<h2>$group_name - $($line_info.task.$group_name)</h2> | |
<p>$group_trap_count Traps</p> | |
"@ | |
write-debug "group $($group | convertto-json -depth 10)" | |
foreach ($line in $group) { | |
"<h3>$($line[0].Line)</h3>" | |
# Note that align does not work - see https://stackoverflow.com/a/18232365/1543555 | |
$line | ConvertTo-Html -fragment -property ` | |
@{ name = 'Trap'; expr = { $_.Name} }, ` | |
@{ name = 'Done'; expr = { $_.clear }; }, ` | |
# @{ name = '<span class="short-text">Catch</span><span class="long-text">Catch (R/S/W)</span>'; expr = | |
@{ name = '<span class="short-text">Catch (R/S/W)</span><span class="long-text">Catch (R/S/W)</span>'; expr = | |
{ $_.victim }; width=100 }, ` | |
@{ name = 'Notes'; expr = { $_.notes }; width=300 } | |
} | |
'</div>' | |
} | |
} | |
function get-page(){ | |
[cmdletbinding()] | |
param ( | |
# What this task is | |
[Parameter(Mandatory)] | |
[ClearingTask]$clearing_task, | |
# If extra note needed for this one | |
[string]$extra_note | |
) | |
# https://4sysops.com/archives/building-html-reports-in-powershell-with-convertto-html/ | |
# https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout | |
# web says style needs to be in the header. | |
# You're supposed to put it in a separate file actually | |
# When using -head with convertto-html the title arg is ignored | |
# so you have to put the title in | |
$head = @" | |
<title>$($clearing_task.name) $($clearing_task.date)</title> | |
<style> | |
BODY {font-family: Arial} | |
TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;} | |
TH {border-width: 1px; padding: 3px; border-style: solid; border-color: black; font-size: 18px;} | |
TH:nth-child(2) {font-size: 16px } | |
TH:nth-child(3) {word-wrap: normal; font-size: 16px } | |
TH:nth-child(4) {text-align: left; } | |
TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black; font-size: 18px } | |
.short-text {display:none;} | |
@media (max-width: 1200px){ | |
.short-text { display: inline-block; } | |
.long-text { display: none; } | |
} | |
P { font-size: 18px} | |
.groups { | |
display: grid; | |
grid-template-columns: repeat(2, 1fr); | |
gap: 10px; | |
} | |
</style> | |
"@ | |
$body_start = @" | |
<h1>$($clearing_task.name) $($clearing_task.date)</h1> | |
<ul> | |
<li>Stoats have a black tip on their tail.The tail is half the length of the body. Weasels have a shorter tail - no black tip</li> | |
<li>Frozen Trigger. Unset the trap and then move the trigger back and forth. If the trigger does not swing freely work it back and forth vigorously about a dozen | |
times to free it up. Reset the trap.</li> | |
</ul> | |
"@ | |
if ($extra_note){ | |
$body_start += "<b>${extra_note}</b>" | |
} | |
$body_start += @" | |
<div class="groups"> | |
"@ | |
$body_trailer = "</div>" | |
$tables = get-tables (get-trap-lines) $clearing_task | |
# reminder - title is ignored if you use -head | |
$(ConvertTo-Html ` | |
-head $head ` | |
-body "$body_start $tables" ` | |
-PostContent $body_trailer) -replace '(?m)^</?table>$', '' | |
} | |
class Roster { | |
# Date on Saturday for the task | |
[DateTime]$sat_date | |
# Which group gets eggs and erayz | |
[ValidateSet(1,2)] | |
[Int16]$eggs_group | |
} | |
[Roster[]]$roster = @( | |
# 0 - doing this for Mike | |
[Roster]@{ | |
sat_date = '23 Mar 2024' | |
eggs_group = 1 | |
}, | |
# 1 | |
[Roster]@{ | |
sat_date = '20 Apr 2024' | |
eggs_group = 1 | |
}, | |
# 2 | |
[Roster]@{ | |
sat_date = '4 May 2024' | |
eggs_group = 2 | |
}, | |
# 3 | |
[Roster]@{ | |
sat_date = '29 Jun 2024' | |
eggs_group = 2 | |
}, | |
# 4 | |
[Roster]@{ | |
sat_date = '24 Aug 2024' | |
eggs_group = 2 | |
} | |
) | |
# If you want debug mode: | |
# $debugPreference = "Continue" | |
### | |
### EDIT which $roster element to use below. Indexing starts at 0. | |
### | |
$line_info = [ClearingTask]::new('MW-MSR3', $roster[2], $groups) | |
# get-page $line_info "Take extra eggs for MW11a and MW10a" | |
$out_file = "trap_lines.html" | |
get-page $line_info | out-file $out_file | |
write-host "wrote to $(Resolve-Path $out_file)" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment