Diff -- /var/www/vhosts/3dshawn.com/site1/MODS/PageWrapper.pm
Diff
/var/www/vhosts/3dshawn.com/site1/MODS/PageWrapper.pm
modified on local at 2026-07-12 00:48:42
Added
+116
lines
Removed
-0
lines
Context
104
unchanged
Blobs
from 65ecf633cf3a
to 9ddf4e89c804
to 9ddf4e89c804
Unified diff
Naive LCS-based line diff. Additions in emerald, deletions in rose. Both sides decompressed live from BLOB_STORE.| 1 | 1 | package MODS::PageWrapper; |
| 2 | 2 | #====================================================================== |
| 3 | 3 | # DriftSense -- page wrapper. |
| 4 | 4 | # |
| 5 | 5 | # Loads TEMPLATES/page_wrapper.html, injects the CGI's rendered body |
| 6 | 6 | # into $page_body, sets branding + active-nav-item tvars, and hands |
| 7 | 7 | # the whole thing back through MODS::Template so its normal tag |
| 8 | 8 | # resolution (variables, [if:...], [url:...]) runs. |
| 9 | 9 | # |
| 10 | 10 | # Usage from a CGI: |
| 11 | 11 | # my $load = MODS::PageWrapper->new; |
| 12 | 12 | # $load->wrapper( |
| 13 | 13 | # page_title => 'Dashboard', |
| 14 | 14 | # page_key => 'dashboard', # sets $active_dashboard = 1 for the sidebar |
| 15 | 15 | # body_html => $rendered_body, |
| 16 | 16 | # userinfo => $u, |
| 17 | 17 | # ); |
| 18 | 18 | #====================================================================== |
| 19 | 19 | use strict; |
| 20 | 20 | use warnings; |
| 21 | 21 | use MODS::Config; |
| 22 | use MODS::DBConnect; | |
| 22 | 23 | use MODS::Template; |
| 23 | 24 | |
| 24 | 25 | my $cfg = MODS::Config->new; |
| 25 | 26 | |
| 26 | 27 | # Cache-bust CSS/JS by the newest mtime among the assets we ship. Recomputed |
| 27 | 28 | # per-process; requests inside the same worker share the value, but a new |
| 28 | 29 | # deploy touches files and the next worker start picks it up automatically. |
| 29 | 30 | my $ASSET_VERSION; |
| 30 | 31 | sub _asset_version { |
| 31 | 32 | return $ASSET_VERSION if defined $ASSET_VERSION; |
| 32 | 33 | my $base = '/var/www/vhosts/3dshawn.com/site1'; |
| 33 | 34 | my $max = 0; |
| 34 | 35 | for my $p ("$base/assets/styles/drift_sense.css", |
| 35 | 36 | "$base/assets/styles/help_tips.css", |
| 36 | 37 | "$base/assets/js/tz_localize.js", |
| 37 | 38 | "$base/assets/js/ds_charts.js", |
| 38 | 39 | "$base/assets/js/ds_help.js") { |
| 39 | 40 | my @st = stat $p; |
| 40 | 41 | $max = $st[9] if @st && $st[9] > $max; |
| 41 | 42 | } |
| 42 | 43 | $ASSET_VERSION = $max || time; |
| 43 | 44 | return $ASSET_VERSION; |
| 44 | 45 | } |
| 45 | 46 | |
| 46 | 47 | sub new { return bless({}, shift); } |
| 47 | 48 | |
| 48 | 49 | sub wrapper { |
| 49 | 50 | my ($self, %args) = @_; |
| 50 | 51 | my $tpl = MODS::Template->new; |
| 51 | 52 | |
| 52 | 53 | my $userinfo = $args{userinfo} || {}; |
| 53 | 54 | my $initials = _initials($userinfo->{display_name} || $userinfo->{email} || 'User'); |
| 55 | ||
| 56 | # Load the "monitored sites" list for the sidebar. Cheap query -- | |
| 57 | # runs once per page render. | |
| 58 | my $sites = _sidebar_sites(); | |
| 59 | ||
| 60 | # Load "stable releases" for the sidebar (Wave 5). | |
| 61 | my $stables = _sidebar_stables(); | |
| 62 | ||
| 63 | my $active_mid = $args{active_mid} // 0; | |
| 64 | foreach my $s (@$sites) { | |
| 65 | $s->{is_active} = ($active_mid && $s->{mid} == $active_mid) ? 1 : 0; | |
| 66 | } | |
| 54 | 67 | |
| 55 | 68 | my $tvars = { |
| 56 | 69 | # Branding |
| 57 | 70 | brand_name => $cfg->settings('brand_name') || 'DriftSense', |
| 58 | 71 | brand_tagline => $cfg->settings('brand_tagline') || '', |
| 72 | ||
| 73 | # Sidebar: monitored sites + stable releases | |
| 74 | sites => $sites, | |
| 75 | has_sites => scalar(@$sites) ? 1 : 0, | |
| 76 | stable_releases=> $stables, | |
| 77 | has_stables => scalar(@$stables) ? 1 : 0, | |
| 59 | 78 | |
| 60 | 79 | # Asset cache-bust stamp -- max mtime of shipped CSS/JS |
| 61 | 80 | asset_version => _asset_version(), |
| 62 | 81 | |
| 63 | 82 | # Page identity |
| 64 | 83 | page_title => $args{page_title} || 'DriftSense', |
| 65 | 84 | page_body => $args{body_html} // (@{$args{body_lines} || []} ? join('', @{$args{body_lines}}) : ''), |
| 66 | 85 | |
| 67 | 86 | # User |
| 68 | 87 | user_name => $userinfo->{display_name} || $userinfo->{email} || 'Operator', |
| 69 | 88 | user_initials => $initials, |
| 70 | 89 | |
| 71 | 90 | # Sidebar active-item flags (page_key sets exactly one) |
| 72 | 91 | active_dashboard => 0, |
| 73 | 92 | active_file_changes => 0, |
| 74 | 93 | active_schema_changes => 0, |
| 75 | 94 | active_table_locks => 0, |
| 76 | 95 | active_named_releases => 0, |
| 77 | 96 | active_alerts => 0, |
| 78 | 97 | active_export => 0, |
| 79 | 98 | active_purge_log => 0, |
| 80 | 99 | active_databases => 0, |
| 81 | 100 | active_file_monitors => 0, |
| 82 | 101 | active_servers => 0, |
| 83 | 102 | active_containers => 0, |
| 84 | 103 | active_settings => 0, |
| 85 | 104 | }; |
| 86 | 105 | my $key = $args{page_key} || ''; |
| 87 | 106 | $tvars->{"active_$key"} = 1 if exists $tvars->{"active_$key"}; |
| 88 | 107 | |
| 89 | 108 | my @html = $tpl->template('page_wrapper.html', $tvars); |
| 90 | 109 | print @html; |
| 91 | 110 | return; |
| 111 | } | |
| 112 | ||
| 113 | #--------------------------------------------------------------------- | |
| 114 | # Sidebar sites list (cached per-worker; refreshes when process restarts) | |
| 115 | #--------------------------------------------------------------------- | |
| 116 | my @_SIDEBAR_SITES_CACHE; | |
| 117 | my $_SIDEBAR_CACHE_AT = 0; | |
| 118 | ||
| 119 | sub _sidebar_sites { | |
| 120 | my $now = time; | |
| 121 | return \@_SIDEBAR_SITES_CACHE if $_SIDEBAR_CACHE_AT && ($now - $_SIDEBAR_CACHE_AT) < 30; | |
| 122 | ||
| 123 | my @out; | |
| 124 | my $db = MODS::DBConnect->new; | |
| 125 | my $dbh = eval { $db->db_connect } or return \@out; | |
| 126 | my @rows = $db->db_readwrite_multiple($dbh, q~ | |
| 127 | SELECT m.file_monitor_list_id AS mid, | |
| 128 | m.scan_name, | |
| 129 | m.scan_path, | |
| 130 | s.server_name, | |
| 131 | s.kind AS server_kind, | |
| 132 | (SELECT COUNT(*) FROM FILE_CHANGES fc | |
| 133 | WHERE fc.file_monitor_list_id = m.file_monitor_list_id | |
| 134 | AND fc.date_time >= DATE_SUB(NOW(), INTERVAL 24 HOUR)) AS changes_24h | |
| 135 | FROM FILE_MONITOR_SETTINGS m | |
| 136 | LEFT JOIN SERVERS s ON s.server_id = m.server_id | |
| 137 | WHERE m.status = 1 | |
| 138 | ORDER BY (s.kind='ssh_agent') DESC, s.server_id ASC, m.file_monitor_list_id ASC | |
| 139 | ~, __FILE__, __LINE__); | |
| 140 | $db->db_disconnect($dbh); | |
| 141 | ||
| 142 | foreach my $r (@rows) { | |
| 143 | my $label = $r->{scan_name} // "monitor #$r->{mid}"; | |
| 144 | # Shorten if too long | |
| 145 | $r->{label} = length($label) > 26 ? substr($label, 0, 24) . '..' : $label; | |
| 146 | $r->{initials} = _site_initials($label); | |
| 147 | $r->{changes_24h} //= 0; | |
| 148 | $r->{activity_dot} = $r->{changes_24h} > 50 ? 'hot' | |
| 149 | : $r->{changes_24h} > 5 ? 'warm' | |
| 150 | : $r->{changes_24h} > 0 ? 'live' : 'quiet'; | |
| 151 | $r->{site_url} = "/site.cgi?mid=$r->{mid}"; | |
| 152 | push @out, $r; | |
| 153 | } | |
| 154 | @_SIDEBAR_SITES_CACHE = @out; | |
| 155 | $_SIDEBAR_CACHE_AT = $now; | |
| 156 | return \@out; | |
| 157 | } | |
| 158 | ||
| 159 | my @_SIDEBAR_STABLES_CACHE; | |
| 160 | my $_STABLES_CACHE_AT = 0; | |
| 161 | sub _sidebar_stables { | |
| 162 | my $now = time; | |
| 163 | return \@_SIDEBAR_STABLES_CACHE if $_STABLES_CACHE_AT && ($now - $_STABLES_CACHE_AT) < 30; | |
| 164 | my @out; | |
| 165 | my $db = MODS::DBConnect->new; | |
| 166 | my $dbh = eval { $db->db_connect } or return \@out; | |
| 167 | # NOTE: `tier` column added in wave 5; guard with a probe so this | |
| 168 | # code works on pre-wave5 schemas gracefully. | |
| 169 | my $has_tier = $db->db_readwrite($dbh, q~ | |
| 170 | SELECT 1 AS ok FROM INFORMATION_SCHEMA.COLUMNS | |
| 171 | WHERE TABLE_SCHEMA = DATABASE() | |
| 172 | AND TABLE_NAME = 'NAMED_RELEASES' AND COLUMN_NAME = 'tier' | |
| 173 | ~, __FILE__, __LINE__); | |
| 174 | if ($has_tier && $has_tier->{ok}) { | |
| 175 | my @rows = $db->db_readwrite_multiple($dbh, q~ | |
| 176 | SELECT named_release_id AS id, name, version_label, tier | |
| 177 | FROM NAMED_RELEASES | |
| 178 | WHERE tier = 'stable' | |
| 179 | ORDER BY named_release_id DESC | |
| 180 | LIMIT 8 | |
| 181 | ~, __FILE__, __LINE__); | |
| 182 | foreach my $r (@rows) { | |
| 183 | $r->{label} = $r->{version_label} || $r->{name}; | |
| 184 | $r->{label} = length($r->{label}) > 26 ? substr($r->{label}, 0, 24) . '..' : $r->{label}; | |
| 185 | $r->{restore_url} = "/restore_release.cgi?release_id=$r->{id}"; | |
| 186 | push @out, $r; | |
| 187 | } | |
| 188 | } | |
| 189 | $db->db_disconnect($dbh); | |
| 190 | @_SIDEBAR_STABLES_CACHE = @out; | |
| 191 | $_STABLES_CACHE_AT = $now; | |
| 192 | return \@out; | |
| 193 | } | |
| 194 | ||
| 195 | sub _site_initials { | |
| 196 | my $s = shift; $s //= ''; | |
| 197 | # Strip parenthetical parts like "(webstls.com)" | |
| 198 | my $core = $s; | |
| 199 | $core =~ s/\s*\(.*$//; | |
| 200 | my @tok = grep { length } split /[\s_\-\.\/]+/, $core; | |
| 201 | my $i = ''; | |
| 202 | if (@tok >= 2) { | |
| 203 | $i = uc(substr($tok[0], 0, 1) . substr($tok[1], 0, 1)); | |
| 204 | } elsif (@tok) { | |
| 205 | $i = uc(substr($tok[0], 0, 2)); | |
| 206 | } | |
| 207 | return length($i) ? $i : '??'; | |
| 92 | 208 | } |
| 93 | 209 | |
| 94 | 210 | sub _initials { |
| 95 | 211 | my $s = shift; $s //= ''; |
| 96 | 212 | my @p = split /[\s\@\.]+/, $s, 3; |
| 97 | 213 | my $i = ''; |
| 98 | 214 | $i .= uc(substr($p[0], 0, 1)) if defined $p[0] && length $p[0]; |
| 99 | 215 | $i .= uc(substr($p[1], 0, 1)) if defined $p[1] && length $p[1]; |
| 100 | 216 | $i = uc(substr($s, 0, 1)) unless length $i; |
| 101 | 217 | return $i; |
| 102 | 218 | } |
| 103 | 219 | |
| 104 | 220 | 1; |