Diff -- /var/www/vhosts/3dshawn.com/site1/MODS/PageWrapper.pm

O Operator
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
Restore this content →
Unified diff
Naive LCS-based line diff. Additions in emerald, deletions in rose. Both sides decompressed live from BLOB_STORE.
11package MODS::PageWrapper;
22#======================================================================
33# DriftSense -- page wrapper.
44#
55# Loads TEMPLATES/page_wrapper.html, injects the CGI's rendered body
66# into $page_body, sets branding + active-nav-item tvars, and hands
77# the whole thing back through MODS::Template so its normal tag
88# resolution (variables, [if:...], [url:...]) runs.
99#
1010# Usage from a CGI:
1111# my $load = MODS::PageWrapper->new;
1212# $load->wrapper(
1313# page_title => 'Dashboard',
1414# page_key => 'dashboard', # sets $active_dashboard = 1 for the sidebar
1515# body_html => $rendered_body,
1616# userinfo => $u,
1717# );
1818#======================================================================
1919use strict;
2020use warnings;
2121use MODS::Config;
2222use MODS::DBConnect;
2323use MODS::Template;
2424
2525my $cfg = MODS::Config->new;
2626
2727# Cache-bust CSS/JS by the newest mtime among the assets we ship. Recomputed
2828# per-process; requests inside the same worker share the value, but a new
2929# deploy touches files and the next worker start picks it up automatically.
3030my $ASSET_VERSION;
3131sub _asset_version {
3232 return $ASSET_VERSION if defined $ASSET_VERSION;
3333 my $base = '/var/www/vhosts/3dshawn.com/site1';
3434 my $max = 0;
3535 for my $p ("$base/assets/styles/drift_sense.css",
3636 "$base/assets/styles/help_tips.css",
3737 "$base/assets/js/tz_localize.js",
3838 "$base/assets/js/ds_charts.js",
3939 "$base/assets/js/ds_help.js") {
4040 my @st = stat $p;
4141 $max = $st[9] if @st && $st[9] > $max;
4242 }
4343 $ASSET_VERSION = $max || time;
4444 return $ASSET_VERSION;
4545}
4646
4747sub new { return bless({}, shift); }
4848
4949sub wrapper {
5050 my ($self, %args) = @_;
5151 my $tpl = MODS::Template->new;
5252
5353 my $userinfo = $args{userinfo} || {};
5454 my $initials = _initials($userinfo->{display_name} || $userinfo->{email} || 'User');
5555
5656 # Load the "monitored sites" list for the sidebar. Cheap query --
5757 # runs once per page render.
5858 my $sites = _sidebar_sites();
5959
6060 # Load "stable releases" for the sidebar (Wave 5).
6161 my $stables = _sidebar_stables();
6262
6363 my $active_mid = $args{active_mid} // 0;
6464 foreach my $s (@$sites) {
6565 $s->{is_active} = ($active_mid && $s->{mid} == $active_mid) ? 1 : 0;
6666 }
6767
6868 my $tvars = {
6969 # Branding
7070 brand_name => $cfg->settings('brand_name') || 'DriftSense',
7171 brand_tagline => $cfg->settings('brand_tagline') || '',
7272
7373 # Sidebar: monitored sites + stable releases
7474 sites => $sites,
7575 has_sites => scalar(@$sites) ? 1 : 0,
76 sites_ct => scalar(@$sites),
7677 stable_releases=> $stables,
7778 has_stables => scalar(@$stables) ? 1 : 0,
79 stables_ct => scalar(@$stables),
7880
7981 # Asset cache-bust stamp -- max mtime of shipped CSS/JS
8082 asset_version => _asset_version(),
8183
8284 # Page identity
8385 page_title => $args{page_title} || 'DriftSense',
8486 page_body => $args{body_html} // (@{$args{body_lines} || []} ? join('', @{$args{body_lines}}) : ''),
8587
8688 # User
8789 user_name => $userinfo->{display_name} || $userinfo->{email} || 'Operator',
8890 user_initials => $initials,
8991
9092 # Sidebar active-item flags (page_key sets exactly one)
9193 active_dashboard => 0,
9294 active_file_changes => 0,
9395 active_schema_changes => 0,
9496 active_table_locks => 0,
9597 active_named_releases => 0,
9698 active_alerts => 0,
9799 active_export => 0,
98100 active_purge_log => 0,
99101 active_databases => 0,
100102 active_file_monitors => 0,
101103 active_servers => 0,
102104 active_containers => 0,
103105 active_settings => 0,
104106 };
105107 my $key = $args{page_key} || '';
106108 $tvars->{"active_$key"} = 1 if exists $tvars->{"active_$key"};
107109
108110 my @html = $tpl->template('page_wrapper.html', $tvars);
109111 print @html;
110112 return;
111113}
112114
113115#---------------------------------------------------------------------
114116# Sidebar sites list (cached per-worker; refreshes when process restarts)
115117#---------------------------------------------------------------------
116118my @_SIDEBAR_SITES_CACHE;
117119my $_SIDEBAR_CACHE_AT = 0;
118120
119121sub _sidebar_sites {
120122 my $now = time;
121123 return \@_SIDEBAR_SITES_CACHE if $_SIDEBAR_CACHE_AT && ($now - $_SIDEBAR_CACHE_AT) < 30;
122124
123125 my @out;
124126 my $db = MODS::DBConnect->new;
125127 my $dbh = eval { $db->db_connect } or return \@out;
126128 my @rows = $db->db_readwrite_multiple($dbh, q~
127129 SELECT m.file_monitor_list_id AS mid,
128130 m.scan_name,
129131 m.scan_path,
130132 s.server_name,
131133 s.kind AS server_kind,
132134 (SELECT COUNT(*) FROM FILE_CHANGES fc
133135 WHERE fc.file_monitor_list_id = m.file_monitor_list_id
134136 AND fc.date_time >= DATE_SUB(NOW(), INTERVAL 24 HOUR)) AS changes_24h
135137 FROM FILE_MONITOR_SETTINGS m
136138 LEFT JOIN SERVERS s ON s.server_id = m.server_id
137139 WHERE m.status = 1
138140 ORDER BY (s.kind='ssh_agent') DESC, s.server_id ASC, m.file_monitor_list_id ASC
139141 ~, __FILE__, __LINE__);
140142 $db->db_disconnect($dbh);
141143
142144 foreach my $r (@rows) {
143145 my $label = $r->{scan_name} // "monitor #$r->{mid}";
144146 # Shorten if too long
145147 $r->{label} = length($label) > 26 ? substr($label, 0, 24) . '..' : $label;
146148 $r->{initials} = _site_initials($label);
147149 $r->{changes_24h} //= 0;
148150 $r->{activity_dot} = $r->{changes_24h} > 50 ? 'hot'
149151 : $r->{changes_24h} > 5 ? 'warm'
150152 : $r->{changes_24h} > 0 ? 'live' : 'quiet';
151153 $r->{site_url} = "/site.cgi?mid=$r->{mid}";
152154 push @out, $r;
153155 }
154156 @_SIDEBAR_SITES_CACHE = @out;
155157 $_SIDEBAR_CACHE_AT = $now;
156158 return \@out;
157159}
158160
159161my @_SIDEBAR_STABLES_CACHE;
160162my $_STABLES_CACHE_AT = 0;
161163sub _sidebar_stables {
162164 my $now = time;
163165 return \@_SIDEBAR_STABLES_CACHE if $_STABLES_CACHE_AT && ($now - $_STABLES_CACHE_AT) < 30;
164166 my @out;
165167 my $db = MODS::DBConnect->new;
166168 my $dbh = eval { $db->db_connect } or return \@out;
167169 # NOTE: `tier` column added in wave 5; guard with a probe so this
168170 # code works on pre-wave5 schemas gracefully.
169171 my $has_tier = $db->db_readwrite($dbh, q~
170172 SELECT 1 AS ok FROM INFORMATION_SCHEMA.COLUMNS
171173 WHERE TABLE_SCHEMA = DATABASE()
172174 AND TABLE_NAME = 'NAMED_RELEASES' AND COLUMN_NAME = 'tier'
173175 ~, __FILE__, __LINE__);
174176 if ($has_tier && $has_tier->{ok}) {
175177 my @rows = $db->db_readwrite_multiple($dbh, q~
176178 SELECT named_release_id AS id, name, version_label, tier
177179 FROM NAMED_RELEASES
178180 WHERE tier = 'stable'
179181 ORDER BY named_release_id DESC
180182 LIMIT 8
181183 ~, __FILE__, __LINE__);
182184 foreach my $r (@rows) {
183185 $r->{label} = $r->{version_label} || $r->{name};
184186 $r->{label} = length($r->{label}) > 26 ? substr($r->{label}, 0, 24) . '..' : $r->{label};
185187 $r->{restore_url} = "/restore_release.cgi?release_id=$r->{id}";
186188 push @out, $r;
187189 }
188190 }
189191 $db->db_disconnect($dbh);
190192 @_SIDEBAR_STABLES_CACHE = @out;
191193 $_STABLES_CACHE_AT = $now;
192194 return \@out;
193195}
194196
195197sub _site_initials {
196198 my $s = shift; $s //= '';
197199 # Strip parenthetical parts like "(webstls.com)"
198200 my $core = $s;
199201 $core =~ s/\s*\(.*$//;
200202 my @tok = grep { length } split /[\s_\-\.\/]+/, $core;
201203 my $i = '';
202204 if (@tok >= 2) {
203205 $i = uc(substr($tok[0], 0, 1) . substr($tok[1], 0, 1));
204206 } elsif (@tok) {
205207 $i = uc(substr($tok[0], 0, 2));
206208 }
207209 return length($i) ? $i : '??';
208210}
209211
210212sub _initials {
211213 my $s = shift; $s //= '';
212214 my @p = split /[\s\@\.]+/, $s, 3;
213215 my $i = '';
214216 $i .= uc(substr($p[0], 0, 1)) if defined $p[0] && length $p[0];
215217 $i .= uc(substr($p[1], 0, 1)) if defined $p[1] && length $p[1];
216218 $i = uc(substr($s, 0, 1)) unless length $i;
217219 return $i;
218220}
219221
2202221;