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