Diff -- /var/www/vhosts/3dshawn.com/site1/dashboard.cgi
Diff
/var/www/vhosts/3dshawn.com/site1/dashboard.cgi
modified on local at 2026-07-10 23:21:52
Added
+16
lines
Removed
-7
lines
Context
164
unchanged
Blobs
from dc2eb4ad0c0f
to d9e3ea458c4f
to d9e3ea458c4f
Unified diff
Naive LCS-based line diff. Additions in emerald, deletions in rose. Both sides decompressed live from BLOB_STORE.| 1 | 1 | #!/usr/bin/perl |
| 2 | 2 | #====================================================================== |
| 3 | 3 | # DriftSense -- /dashboard.cgi |
| 4 | 4 | # |
| 5 | 5 | # Landing page. Shows portfolio-wide health at a glance: |
| 6 | 6 | # * KPI tiles: file changes today | schema changes 7d | named releases | drift alerts |
| 7 | 7 | # * Recent activity table (last 30 file + schema changes, merged, newest first) |
| 8 | 8 | # * Named releases strip (last 5 bookmarks) |
| 9 | 9 | # * Servers status strip |
| 10 | 10 | #====================================================================== |
| 11 | 11 | use strict; |
| 12 | 12 | use warnings; |
| 13 | 13 | use CGI (); |
| 14 | 14 | use MODS::Config; |
| 15 | 15 | use MODS::DBConnect; |
| 16 | 16 | use MODS::Template; |
| 17 | 17 | use MODS::PageWrapper; |
| 18 | 18 | use MODS::Charts; |
| 19 | 19 | |
| 20 | 20 | my $cfg = MODS::Config->new; |
| 21 | 21 | my $db = MODS::DBConnect->new; |
| 22 | 22 | my $tpl = MODS::Template->new; |
| 23 | 23 | my $wrap = MODS::PageWrapper->new; |
| 24 | 24 | |
| 25 | 25 | # Headers |
| 26 | 26 | $|=1; |
| 27 | 27 | print "Content-Type: text/html; charset=utf-8\nCache-Control: no-cache\n\n"; |
| 28 | 28 | |
| 29 | 29 | my $dbh = $db->db_connect |
| 30 | 30 | or die "DriftSense: cannot connect to storage DB. Check /etc/drift_sense/drift_sense.conf.\n"; |
| 31 | 31 | |
| 32 | 32 | # ----- KPI queries ---------------------------------------------------- |
| 33 | 33 | my $file_today = $db->db_readwrite($dbh, q~ |
| 34 | 34 | SELECT COUNT(*) AS n FROM FILE_CHANGES |
| 35 | 35 | WHERE date_time >= DATE_SUB(NOW(), INTERVAL 1 DAY) |
| 36 | 36 | ~, __FILE__, __LINE__); |
| 37 | 37 | |
| 38 | 38 | my $schema_7d = $db->db_readwrite($dbh, q~ |
| 39 | 39 | SELECT COUNT(*) AS n FROM SCHEMA_CHANGE |
| 40 | 40 | WHERE change_datetime >= DATE_SUB(NOW(), INTERVAL 7 DAY) |
| 41 | 41 | ~, __FILE__, __LINE__); |
| 42 | 42 | |
| 43 | 43 | my $releases_ct = $db->db_readwrite($dbh, q~ |
| 44 | 44 | SELECT COUNT(*) AS n FROM NAMED_RELEASES |
| 45 | 45 | ~, __FILE__, __LINE__); |
| 46 | 46 | |
| 47 | 47 | my $drifted_ct = $db->db_readwrite($dbh, q~ |
| 48 | 48 | SELECT COUNT(*) AS n FROM CONFIG_DRIFT_BASELINES WHERE is_drifted = 1 |
| 49 | 49 | ~, __FILE__, __LINE__); |
| 50 | 50 | |
| 51 | 51 | # ----- Recent activity (merged file + schema changes, newest first) -- |
| 52 | 52 | my @file_recent = $db->db_readwrite_multiple($dbh, q~ |
| 53 | 53 | SELECT 'file' AS kind, |
| 54 | 54 | file_changes_id AS id, |
| 55 | 55 | file_name AS what, |
| 56 | 56 | status AS detail, |
| 57 | 57 | date_time AS ts, |
| 58 | UNIX_TIMESTAMP(date_time) AS ts_epoch, | |
| 58 | 59 | server_id |
| 59 | 60 | FROM FILE_CHANGES |
| 60 | 61 | ORDER BY date_time DESC |
| 61 | 62 | LIMIT 20 |
| 62 | 63 | ~, __FILE__, __LINE__); |
| 63 | 64 | my @schema_recent = $db->db_readwrite_multiple($dbh, q~ |
| 64 | 65 | SELECT 'schema' AS kind, |
| 65 | 66 | schema_change_id AS id, |
| 66 | 67 | CONCAT(database_name, '.', table_name) AS what, |
| 67 | 68 | changes AS detail, |
| 68 | 69 | change_datetime AS ts, |
| 70 | UNIX_TIMESTAMP(change_datetime) AS ts_epoch, | |
| 69 | 71 | server_id |
| 70 | 72 | FROM SCHEMA_CHANGE |
| 71 | 73 | ORDER BY change_datetime DESC |
| 72 | 74 | LIMIT 20 |
| 73 | 75 | ~, __FILE__, __LINE__); |
| 74 | 76 | my @recent = sort { ($b->{ts} || '') cmp ($a->{ts} || '') } (@file_recent, @schema_recent); |
| 75 | 77 | @recent = splice(@recent, 0, 20) if @recent > 20; |
| 76 | 78 | foreach my $r (@recent) { |
| 77 | 79 | my $d = $r->{detail} // ''; |
| 78 | 80 | $d =~ s/\s+/ /g; |
| 79 | 81 | $r->{detail} = length($d) > 90 ? substr($d, 0, 87) . '...' : $d; |
| 80 | 82 | $r->{what} = length($r->{what} // '') > 60 ? substr($r->{what}, 0, 57) . '...' : $r->{what}; |
| 81 | 83 | $r->{diff_url} = ($r->{kind} eq 'file' |
| 82 | 84 | ? "/diff.cgi?kind=file&id=$r->{id}" |
| 83 | 85 | : "/diff.cgi?kind=schema&id=$r->{id}"); |
| 84 | $r->{ts} //= ''; | |
| 86 | $r->{ts} //= ''; | |
| 87 | $r->{ts_epoch} //= 0; | |
| 85 | 88 | $r->{kind_pill} = ($r->{kind} eq 'file') ? 'pill-info' : 'pill-warn'; |
| 86 | 89 | } |
| 87 | 90 | |
| 88 | 91 | # ----- 90-day activity timeline (stacked bars) ----------------------- |
| 89 | 92 | my $today_dow = 0; |
| 90 | 93 | my @file_by_day = $db->db_readwrite_multiple($dbh, q~ |
| 91 | 94 | SELECT DATE_FORMAT(date_time, '%Y-%m-%d') AS d, COUNT(*) AS n |
| 92 | 95 | FROM FILE_CHANGES |
| 93 | 96 | WHERE date_time >= DATE_SUB(NOW(), INTERVAL 90 DAY) |
| 94 | 97 | GROUP BY d |
| 95 | 98 | ~, __FILE__, __LINE__); |
| 96 | 99 | my @schema_by_day = $db->db_readwrite_multiple($dbh, q~ |
| 97 | 100 | SELECT DATE_FORMAT(change_datetime, '%Y-%m-%d') AS d, COUNT(*) AS n |
| 98 | 101 | FROM SCHEMA_CHANGE |
| 99 | 102 | WHERE change_datetime >= DATE_SUB(NOW(), INTERVAL 90 DAY) |
| 100 | 103 | GROUP BY d |
| 101 | 104 | ~, __FILE__, __LINE__); |
| 102 | 105 | my (%file_map, %schema_map); |
| 103 | 106 | $file_map{$_->{d}} = $_->{n} for @file_by_day; |
| 104 | 107 | $schema_map{$_->{d}} = $_->{n} for @schema_by_day; |
| 105 | 108 | |
| 106 | 109 | use POSIX (); |
| 107 | my (@days, @file_series, @schema_series); | |
| 110 | my (@days, @day_keys, @file_series, @schema_series); | |
| 108 | 111 | my $today = time; |
| 109 | 112 | for (my $i = 89; $i >= 0; $i--) { |
| 110 | 113 | my $t = $today - ($i * 86400); |
| 111 | 114 | my $key = POSIX::strftime('%Y-%m-%d', localtime($t)); |
| 112 | 115 | my $lbl = POSIX::strftime('%b %d', localtime($t)); |
| 113 | 116 | push @days, $lbl; |
| 117 | push @day_keys, $key; | |
| 114 | 118 | push @file_series, ($file_map{$key} || 0); |
| 115 | 119 | push @schema_series, ($schema_map{$key} || 0); |
| 116 | 120 | } |
| 117 | 121 | my $timeline_svg = MODS::Charts::stacked_bars( |
| 118 | days => \@days, | |
| 119 | series => { file => \@file_series, schema => \@schema_series }, | |
| 120 | width => 1080, | |
| 121 | height => 180, | |
| 122 | days => \@days, | |
| 123 | day_keys => \@day_keys, | |
| 124 | series => { file => \@file_series, schema => \@schema_series }, | |
| 125 | width => 1080, | |
| 126 | height => 180, | |
| 122 | 127 | ); |
| 123 | 128 | |
| 124 | 129 | # ----- Named releases strip (last 5) --------------------------------- |
| 125 | 130 | my @releases = $db->db_readwrite_multiple($dbh, q~ |
| 126 | 131 | SELECT named_release_id, name, description, |
| 127 | 132 | DATE_FORMAT(released_at, '%b %d, %Y') AS released_h, |
| 133 | UNIX_TIMESTAMP(released_at) AS released_epoch, | |
| 128 | 134 | released_by |
| 129 | 135 | FROM NAMED_RELEASES |
| 130 | 136 | ORDER BY released_at DESC |
| 131 | 137 | LIMIT 5 |
| 132 | 138 | ~, __FILE__, __LINE__); |
| 139 | $_->{released_epoch} //= 0 for @releases; | |
| 133 | 140 | |
| 134 | 141 | # ----- Server status strip ------------------------------------------- |
| 135 | 142 | my @servers = $db->db_readwrite_multiple($dbh, q~ |
| 136 | 143 | SELECT server_id, server_name, hostname, role, status, |
| 137 | DATE_FORMAT(last_heartbeat_at, '%b %d %H:%i') AS last_heartbeat_h | |
| 144 | DATE_FORMAT(last_heartbeat_at, '%b %d %H:%i') AS last_heartbeat_h, | |
| 145 | UNIX_TIMESTAMP(last_heartbeat_at) AS heartbeat_epoch | |
| 138 | 146 | FROM SERVERS |
| 139 | 147 | ORDER BY (status='active') DESC, server_id ASC |
| 140 | 148 | LIMIT 8 |
| 141 | 149 | ~, __FILE__, __LINE__); |
| 142 | 150 | foreach my $s (@servers) { |
| 143 | 151 | $s->{status_pill} = $s->{status} eq 'active' ? 'pill-ok' |
| 144 | 152 | : $s->{status} eq 'offline' ? 'pill-bad' |
| 145 | 153 | : 'pill-mute'; |
| 146 | 154 | $s->{last_heartbeat_h} //= 'never'; |
| 155 | $s->{heartbeat_epoch} //= 0; | |
| 147 | 156 | } |
| 148 | 157 | |
| 149 | 158 | $db->db_disconnect($dbh); |
| 150 | 159 | |
| 151 | 160 | # ----- Render body --------------------------------------------------- |
| 152 | 161 | my $tvars = { |
| 153 | 162 | kpi_file_today => ($file_today && $file_today->{n}) || 0, |
| 154 | 163 | kpi_schema_7d => ($schema_7d && $schema_7d->{n}) || 0, |
| 155 | 164 | kpi_releases => ($releases_ct && $releases_ct->{n}) || 0, |
| 156 | 165 | kpi_drifted => ($drifted_ct && $drifted_ct->{n}) || 0, |
| 157 | 166 | timeline_svg => $timeline_svg, |
| 158 | 167 | has_recent => scalar(@recent) ? 1 : 0, |
| 159 | 168 | recent => \@recent, |
| 160 | 169 | has_releases => scalar(@releases) ? 1 : 0, |
| 161 | 170 | releases => \@releases, |
| 162 | 171 | servers => \@servers, |
| 163 | 172 | }; |
| 164 | 173 | my @body = $tpl->template('dashboard.html', $tvars); |
| 165 | 174 | |
| 166 | 175 | $wrap->wrapper( |
| 167 | 176 | page_title => 'Dashboard', |
| 168 | 177 | page_key => 'dashboard', |
| 169 | 178 | body_html => join('', @body), |
| 170 | 179 | userinfo => { display_name => 'Operator' }, # single-user install; auth added later |
| 171 | 180 | ); |