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 01:04:52

Added
+1
lines
Removed
-0
lines
Context
222
unchanged
Blobs
from cc06f327b82e
to 65c126296f9c
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,
7676 sites_ct => scalar(@$sites),
7777 stable_releases=> $stables,
7878 has_stables => scalar(@$stables) ? 1 : 0,
7979 stables_ct => scalar(@$stables),
8080
8181 # Asset cache-bust stamp -- max mtime of shipped CSS/JS
8282 asset_version => _asset_version(),
8383
8484 # Page identity
8585 page_title => $args{page_title} || 'DriftSense',
8686 page_body => $args{body_html} // (@{$args{body_lines} || []} ? join('', @{$args{body_lines}}) : ''),
8787
8888 # User
8989 user_name => $userinfo->{display_name} || $userinfo->{email} || 'Operator',
9090 user_initials => $initials,
9191
9292 # Sidebar active-item flags (page_key sets exactly one)
9393 active_dashboard => 0,
9494 active_file_changes => 0,
9595 active_schema_changes => 0,
9696 active_table_locks => 0,
9797 active_named_releases => 0,
9898 active_alerts => 0,
9999 active_export => 0,
100100 active_purge_log => 0,
101 active_sites => 0,
101102 active_databases => 0,
102103 active_file_monitors => 0,
103104 active_servers => 0,
104105 active_containers => 0,
105106 active_settings => 0,
106107 };
107108 my $key = $args{page_key} || '';
108109 $tvars->{"active_$key"} = 1 if exists $tvars->{"active_$key"};
109110
110111 my @html = $tpl->template('page_wrapper.html', $tvars);
111112 print @html;
112113 return;
113114}
114115
115116#---------------------------------------------------------------------
116117# Sidebar sites list (cached per-worker; refreshes when process restarts)
117118#---------------------------------------------------------------------
118119my @_SIDEBAR_SITES_CACHE;
119120my $_SIDEBAR_CACHE_AT = 0;
120121
121122sub _sidebar_sites {
122123 my $now = time;
123124 return \@_SIDEBAR_SITES_CACHE if $_SIDEBAR_CACHE_AT && ($now - $_SIDEBAR_CACHE_AT) < 30;
124125
125126 my @out;
126127 my $db = MODS::DBConnect->new;
127128 my $dbh = eval { $db->db_connect } or return \@out;
128129 my @rows = $db->db_readwrite_multiple($dbh, q~
129130 SELECT m.file_monitor_list_id AS mid,
130131 m.scan_name,
131132 m.scan_path,
132133 s.server_name,
133134 s.kind AS server_kind,
134135 (SELECT COUNT(*) FROM FILE_CHANGES fc
135136 WHERE fc.file_monitor_list_id = m.file_monitor_list_id
136137 AND fc.date_time >= DATE_SUB(NOW(), INTERVAL 24 HOUR)) AS changes_24h
137138 FROM FILE_MONITOR_SETTINGS m
138139 LEFT JOIN SERVERS s ON s.server_id = m.server_id
139140 WHERE m.status = 1
140141 ORDER BY (s.kind='ssh_agent') DESC, s.server_id ASC, m.file_monitor_list_id ASC
141142 ~, __FILE__, __LINE__);
142143 $db->db_disconnect($dbh);
143144
144145 foreach my $r (@rows) {
145146 my $label = $r->{scan_name} // "monitor #$r->{mid}";
146147 # Shorten if too long
147148 $r->{label} = length($label) > 26 ? substr($label, 0, 24) . '..' : $label;
148149 $r->{initials} = _site_initials($label);
149150 $r->{changes_24h} //= 0;
150151 $r->{activity_dot} = $r->{changes_24h} > 50 ? 'hot'
151152 : $r->{changes_24h} > 5 ? 'warm'
152153 : $r->{changes_24h} > 0 ? 'live' : 'quiet';
153154 $r->{site_url} = "/site.cgi?mid=$r->{mid}";
154155 push @out, $r;
155156 }
156157 @_SIDEBAR_SITES_CACHE = @out;
157158 $_SIDEBAR_CACHE_AT = $now;
158159 return \@out;
159160}
160161
161162my @_SIDEBAR_STABLES_CACHE;
162163my $_STABLES_CACHE_AT = 0;
163164sub _sidebar_stables {
164165 my $now = time;
165166 return \@_SIDEBAR_STABLES_CACHE if $_STABLES_CACHE_AT && ($now - $_STABLES_CACHE_AT) < 30;
166167 my @out;
167168 my $db = MODS::DBConnect->new;
168169 my $dbh = eval { $db->db_connect } or return \@out;
169170 # NOTE: `tier` column added in wave 5; guard with a probe so this
170171 # code works on pre-wave5 schemas gracefully.
171172 my $has_tier = $db->db_readwrite($dbh, q~
172173 SELECT 1 AS ok FROM INFORMATION_SCHEMA.COLUMNS
173174 WHERE TABLE_SCHEMA = DATABASE()
174175 AND TABLE_NAME = 'NAMED_RELEASES' AND COLUMN_NAME = 'tier'
175176 ~, __FILE__, __LINE__);
176177 if ($has_tier && $has_tier->{ok}) {
177178 my @rows = $db->db_readwrite_multiple($dbh, q~
178179 SELECT named_release_id AS id, name, version_label, tier
179180 FROM NAMED_RELEASES
180181 WHERE tier = 'stable'
181182 ORDER BY named_release_id DESC
182183 LIMIT 8
183184 ~, __FILE__, __LINE__);
184185 foreach my $r (@rows) {
185186 $r->{label} = $r->{version_label} || $r->{name};
186187 $r->{label} = length($r->{label}) > 26 ? substr($r->{label}, 0, 24) . '..' : $r->{label};
187188 $r->{restore_url} = "/restore_release.cgi?release_id=$r->{id}";
188189 push @out, $r;
189190 }
190191 }
191192 $db->db_disconnect($dbh);
192193 @_SIDEBAR_STABLES_CACHE = @out;
193194 $_STABLES_CACHE_AT = $now;
194195 return \@out;
195196}
196197
197198sub _site_initials {
198199 my $s = shift; $s //= '';
199200 # Strip parenthetical parts like "(webstls.com)"
200201 my $core = $s;
201202 $core =~ s/\s*\(.*$//;
202203 my @tok = grep { length } split /[\s_\-\.\/]+/, $core;
203204 my $i = '';
204205 if (@tok >= 2) {
205206 $i = uc(substr($tok[0], 0, 1) . substr($tok[1], 0, 1));
206207 } elsif (@tok) {
207208 $i = uc(substr($tok[0], 0, 2));
208209 }
209210 return length($i) ? $i : '??';
210211}
211212
212213sub _initials {
213214 my $s = shift; $s //= '';
214215 my @p = split /[\s\@\.]+/, $s, 3;
215216 my $i = '';
216217 $i .= uc(substr($p[0], 0, 1)) if defined $p[0] && length $p[0];
217218 $i .= uc(substr($p[1], 0, 1)) if defined $p[1] && length $p[1];
218219 $i = uc(substr($s, 0, 1)) unless length $i;
219220 return $i;
220221}
221222
2222231;