Diff -- /var/www/vhosts/3dshawn.com/admin.3dshawn.com/MODS/MetaAdmin/Aggregate.pm
Diff
/var/www/vhosts/3dshawn.com/admin.3dshawn.com/MODS/MetaAdmin/Aggregate.pm
added on local at 2026-07-10 10:51:45
Added
+0
lines
Removed
-0
lines
Context
261
unchanged
Blobs
from 70127f96072f
to 70127f96072f
to 70127f96072f
Unified diff
Naive LCS-based line diff. Additions in emerald, deletions in rose. Both sides decompressed live from BLOB_STORE.| 1 | 1 | package MODS::MetaAdmin::Aggregate; |
| 2 | 2 | #====================================================================== |
| 3 | 3 | # Meta-Admin -- per-site query templates for revenue + support |
| 4 | 4 | # roll-up. Each site's billing/support schema is slightly different |
| 5 | 5 | # because the codebases were forked, so we keep one set of SQL per |
| 6 | 6 | # slug. Anything we don't have a recipe for returns undef and the |
| 7 | 7 | # UI shows '--' instead of a fake zero. |
| 8 | 8 | # |
| 9 | 9 | # All queries are SELECT only -- SiteConn enforces that, and these |
| 10 | 10 | # templates are wrapped in eval() so a single broken site never |
| 11 | 11 | # breaks the whole roll-up. |
| 12 | 12 | #====================================================================== |
| 13 | 13 | use strict; |
| 14 | 14 | use warnings; |
| 15 | 15 | |
| 16 | 16 | use MODS::MetaAdmin::SiteConn; |
| 17 | 17 | |
| 18 | 18 | sub new { |
| 19 | 19 | my $class = shift; |
| 20 | 20 | return bless({ sc => MODS::MetaAdmin::SiteConn->new }, $class); |
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | # Per-slug template registry. Each entry returns a hashref of named |
| 24 | 24 | # SQL snippets we know how to interpret. |
| 25 | 25 | # |
| 26 | 26 | # Recognized keys: |
| 27 | 27 | # active_users_sql -> single value 'n' |
| 28 | 28 | # mtd_orders_sql -> single value { n => count, total_cents => sum } |
| 29 | 29 | # open_threads_sql -> single value 'n' |
| 30 | 30 | # |
| 31 | 31 | my %SITES = ( |
| 32 | 32 | contactforge => { |
| 33 | 33 | active_users_sql => q~ |
| 34 | 34 | SELECT COUNT(*) AS n FROM users WHERE COALESCE(account_status,'active') NOT IN ('closed','suspended') |
| 35 | 35 | ~, |
| 36 | 36 | mtd_orders_sql => q~ |
| 37 | 37 | SELECT COUNT(*) AS n, COALESCE(SUM(total_cents),0) AS total_cents |
| 38 | 38 | FROM invoices |
| 39 | 39 | WHERE status IN ('paid','open') AND issued_at >= DATE_FORMAT(NOW(),'%Y-%m-01') |
| 40 | 40 | ~, |
| 41 | 41 | open_threads_sql => q~ |
| 42 | 42 | SELECT COUNT(*) AS n FROM email_sends_log |
| 43 | 43 | WHERE status='queued' AND created_at >= DATE_SUB(NOW(), INTERVAL 30 DAY) |
| 44 | 44 | ~, |
| 45 | 45 | }, |
| 46 | 46 | repricer => { |
| 47 | 47 | active_users_sql => q~ |
| 48 | 48 | SELECT COUNT(*) AS n FROM users WHERE COALESCE(account_status,'active') NOT IN ('closed','suspended') |
| 49 | 49 | ~, |
| 50 | 50 | mtd_orders_sql => q~ |
| 51 | 51 | SELECT COUNT(*) AS n, COALESCE(SUM(amount_due_cents),0) AS total_cents |
| 52 | 52 | FROM invoices |
| 53 | 53 | WHERE status IN ('paid','open') AND created_at >= DATE_FORMAT(NOW(),'%Y-%m-01') |
| 54 | 54 | ~, |
| 55 | 55 | open_threads_sql => q~ |
| 56 | 56 | SELECT COUNT(*) AS n FROM email_sends_log |
| 57 | 57 | WHERE status='queued' AND created_at >= DATE_SUB(NOW(), INTERVAL 30 DAY) |
| 58 | 58 | ~, |
| 59 | 59 | }, |
| 60 | 60 | shopcart => { |
| 61 | 61 | active_users_sql => q~SELECT COUNT(*) AS n FROM users~, |
| 62 | 62 | mtd_orders_sql => q~ |
| 63 | 63 | SELECT COUNT(*) AS n, COALESCE(SUM(total_cents),0) AS total_cents |
| 64 | 64 | FROM orders |
| 65 | 65 | WHERE status='paid' AND created_at >= DATE_FORMAT(NOW(),'%Y-%m-01') |
| 66 | 66 | ~, |
| 67 | 67 | open_threads_sql => q~SELECT COUNT(*) AS n FROM email_sends_log WHERE status='queued' AND created_at >= DATE_SUB(NOW(), INTERVAL 30 DAY)~, |
| 68 | 68 | }, |
| 69 | 69 | abforge => { |
| 70 | 70 | active_users_sql => q~SELECT COUNT(*) AS n FROM users~, |
| 71 | 71 | mtd_orders_sql => q~ |
| 72 | 72 | SELECT COUNT(*) AS n, COALESCE(SUM(amount_due_cents),0) AS total_cents |
| 73 | 73 | FROM invoices |
| 74 | 74 | WHERE status IN ('paid','open') AND created_at >= DATE_FORMAT(NOW(),'%Y-%m-01') |
| 75 | 75 | ~, |
| 76 | 76 | open_threads_sql => q~SELECT COUNT(*) AS n FROM email_sends_log WHERE status='queued'~, |
| 77 | 77 | }, |
| 78 | 78 | affsoft => { |
| 79 | 79 | active_users_sql => q~SELECT COUNT(*) AS n FROM users~, |
| 80 | 80 | mtd_orders_sql => undef, |
| 81 | 81 | open_threads_sql => q~SELECT COUNT(*) AS n FROM email_sends_log WHERE status='queued'~, |
| 82 | 82 | }, |
| 83 | 83 | ptmatrix => { |
| 84 | 84 | active_users_sql => q~SELECT COUNT(*) AS n FROM users~, |
| 85 | 85 | mtd_orders_sql => q~ |
| 86 | 86 | SELECT COUNT(*) AS n, COALESCE(SUM(amount_due_cents),0) AS total_cents |
| 87 | 87 | FROM invoices |
| 88 | 88 | WHERE status IN ('paid','open') AND created_at >= DATE_FORMAT(NOW(),'%Y-%m-01') |
| 89 | 89 | ~, |
| 90 | 90 | open_threads_sql => q~SELECT COUNT(*) AS n FROM email_sends_log WHERE status='queued'~, |
| 91 | 91 | }, |
| 92 | 92 | webstls => { |
| 93 | 93 | active_users_sql => q~SELECT COUNT(*) AS n FROM users~, |
| 94 | 94 | mtd_orders_sql => q~ |
| 95 | 95 | SELECT COUNT(*) AS n, COALESCE(SUM(total_cents),0) AS total_cents |
| 96 | 96 | FROM orders |
| 97 | 97 | WHERE status='paid' AND created_at >= DATE_FORMAT(NOW(),'%Y-%m-01') |
| 98 | 98 | ~, |
| 99 | 99 | open_threads_sql => q~SELECT COUNT(*) AS n FROM email_sends_log WHERE status='queued'~, |
| 100 | 100 | }, |
| 101 | 101 | ); |
| 102 | 102 | |
| 103 | 103 | #--------------------------------------------------------------------- |
| 104 | 104 | # Roll up revenue across every active site. Returns: |
| 105 | 105 | # { total_users => N, total_revenue_cents => N, |
| 106 | 106 | # per_site => [ { slug, name, color, users, orders, revenue_cents, err } ] } |
| 107 | 107 | #--------------------------------------------------------------------- |
| 108 | 108 | sub revenue_rollup { |
| 109 | 109 | my $self = shift; |
| 110 | 110 | my $sites = $self->{sc}->list_sites(active => 1); |
| 111 | 111 | my %out = (total_users => 0, total_orders => 0, total_revenue_cents => 0, per_site => []); |
| 112 | 112 | foreach my $row (@$sites) { |
| 113 | 113 | my $slug = $row->{slug}; |
| 114 | 114 | my $tmpl = $SITES{$slug}; |
| 115 | 115 | my $entry = { |
| 116 | 116 | slug => $slug, |
| 117 | 117 | display_name => $row->{display_name}, |
| 118 | 118 | color => $row->{brand_color} || '#807aa8', |
| 119 | 119 | users => undef, |
| 120 | 120 | orders => undef, |
| 121 | 121 | revenue_cents=> undef, |
| 122 | 122 | err => '', |
| 123 | 123 | }; |
| 124 | 124 | unless ($tmpl) { |
| 125 | 125 | $entry->{err} = 'no template'; |
| 126 | 126 | push @{ $out{per_site} }, $entry; |
| 127 | 127 | next; |
| 128 | 128 | } |
| 129 | 129 | if ($tmpl->{active_users_sql}) { |
| 130 | 130 | my $r = eval { $self->{sc}->query_one($slug, $tmpl->{active_users_sql}) }; |
| 131 | 131 | if ($r) { $entry->{users} = $r->{n} + 0; $out{total_users} += $entry->{users}; } |
| 132 | 132 | else { $entry->{err} = $self->{sc}->last_error; } |
| 133 | 133 | } |
| 134 | 134 | if ($tmpl->{mtd_orders_sql}) { |
| 135 | 135 | my $r = eval { $self->{sc}->query_one($slug, $tmpl->{mtd_orders_sql}) }; |
| 136 | 136 | if ($r) { |
| 137 | 137 | $entry->{orders} = ($r->{n} || 0) + 0; |
| 138 | 138 | $entry->{revenue_cents} = ($r->{total_cents} || 0) + 0; |
| 139 | 139 | $out{total_orders} += $entry->{orders}; |
| 140 | 140 | $out{total_revenue_cents} += $entry->{revenue_cents}; |
| 141 | 141 | } else { |
| 142 | 142 | $entry->{err} ||= $self->{sc}->last_error; |
| 143 | 143 | } |
| 144 | 144 | } |
| 145 | 145 | push @{ $out{per_site} }, $entry; |
| 146 | 146 | } |
| 147 | 147 | return \%out; |
| 148 | 148 | } |
| 149 | 149 | |
| 150 | 150 | #--------------------------------------------------------------------- |
| 151 | 151 | # Roll up queued outbound emails per site (the "mail queue backlog" |
| 152 | 152 | # monitor rendered on /support.cgi). |
| 153 | 153 | # |
| 154 | 154 | # $range: either an integer day-count (rolling) or a hashref |
| 155 | 155 | # { from => 'YYYY-MM-DD', to => 'YYYY-MM-DD' } for a fixed window. |
| 156 | 156 | # Undef defaults to 30-day rolling. |
| 157 | 157 | #--------------------------------------------------------------------- |
| 158 | 158 | sub support_rollup { |
| 159 | 159 | my ($self, $range) = @_; |
| 160 | 160 | $range //= 30; |
| 161 | 161 | my ($where, $range_label, $range_days) = _build_range_where($range); |
| 162 | 162 | |
| 163 | 163 | my $sites = $self->{sc}->list_sites(active => 1); |
| 164 | 164 | my %out = ( |
| 165 | 165 | total_open => 0, |
| 166 | 166 | per_site => [], |
| 167 | 167 | range_label => $range_label, |
| 168 | 168 | range_days => $range_days, |
| 169 | 169 | ); |
| 170 | 170 | foreach my $row (@$sites) { |
| 171 | 171 | my $slug = $row->{slug}; |
| 172 | 172 | my $entry = { |
| 173 | 173 | slug => $slug, |
| 174 | 174 | display_name => $row->{display_name}, |
| 175 | 175 | color => $row->{brand_color} || '#807aa8', |
| 176 | 176 | open => undef, |
| 177 | 177 | err => '', |
| 178 | 178 | }; |
| 179 | 179 | my $sql = qq~SELECT COUNT(*) AS n FROM email_sends_log |
| 180 | 180 | WHERE status='queued' AND $where~; |
| 181 | 181 | my $r = eval { $self->{sc}->query_one($slug, $sql) }; |
| 182 | 182 | if ($r) { |
| 183 | 183 | $entry->{open} = ($r->{n} || 0) + 0; |
| 184 | 184 | $out{total_open} += $entry->{open}; |
| 185 | 185 | } else { |
| 186 | 186 | $entry->{err} = $self->{sc}->last_error; |
| 187 | 187 | } |
| 188 | 188 | push @{ $out{per_site} }, $entry; |
| 189 | 189 | } |
| 190 | 190 | return \%out; |
| 191 | 191 | } |
| 192 | 192 | |
| 193 | 193 | # Build a WHERE clause on `created_at` from either a day count or a |
| 194 | 194 | # custom {from,to} hash. Returns ($sql_where, $display_label, $days_or_zero). |
| 195 | 195 | sub _build_range_where { |
| 196 | 196 | my ($range) = @_; |
| 197 | 197 | if (ref($range) eq 'HASH' && $range->{from} && $range->{to} |
| 198 | 198 | && $range->{from} =~ /^\d{4}-\d{2}-\d{2}$/ |
| 199 | 199 | && $range->{to} =~ /^\d{4}-\d{2}-\d{2}$/) { |
| 200 | 200 | my $where = "created_at BETWEEN '$range->{from} 00:00:00' AND '$range->{to} 23:59:59'"; |
| 201 | 201 | return ($where, "$range->{from} to $range->{to}", 0); |
| 202 | 202 | } |
| 203 | 203 | my $days = (ref($range) eq 'HASH' && $range->{days}) ? $range->{days} : $range; |
| 204 | 204 | $days = 30 unless defined($days) && $days =~ /^\d+$/ && $days > 0; |
| 205 | 205 | $days = 730 if $days > 730; |
| 206 | 206 | my $label = $days == 1 ? 'Last 24h' |
| 207 | 207 | : $days == 7 ? 'Last 7 days' |
| 208 | 208 | : $days == 30 ? 'Last 30 days' |
| 209 | 209 | : $days == 90 ? 'Last 90 days' |
| 210 | 210 | : $days == 365 ? 'Last 1 year' |
| 211 | 211 | : "Last $days days"; |
| 212 | 212 | return ("created_at >= DATE_SUB(NOW(), INTERVAL $days DAY)", $label, $days); |
| 213 | 213 | } |
| 214 | 214 | |
| 215 | 215 | #--------------------------------------------------------------------- |
| 216 | 216 | # db_sizes -- per-site MySQL DB size (data + indexes) from each site's |
| 217 | 217 | # information_schema.TABLES. Returns arrayref of: |
| 218 | 218 | # { slug, display_name, brand_color, db_name, |
| 219 | 219 | # data_b, indexes_b, total_b, tables_ct, err } |
| 220 | 220 | # sorted total_b desc. On per-site failure, err is set and totals are 0. |
| 221 | 221 | #--------------------------------------------------------------------- |
| 222 | 222 | sub db_sizes { |
| 223 | 223 | my $self = shift; |
| 224 | 224 | my $sites = $self->{sc}->list_sites(active => 1); |
| 225 | 225 | my @out; |
| 226 | 226 | foreach my $srow (@$sites) { |
| 227 | 227 | my $slug = $srow->{slug} or next; |
| 228 | 228 | my $entry = { |
| 229 | 229 | slug => $slug, |
| 230 | 230 | display_name => $srow->{display_name}, |
| 231 | 231 | brand_color => $srow->{brand_color} || '#807aa8', |
| 232 | 232 | db_name => $srow->{db_name} || '', |
| 233 | 233 | data_b => 0, |
| 234 | 234 | indexes_b => 0, |
| 235 | 235 | total_b => 0, |
| 236 | 236 | tables_ct => 0, |
| 237 | 237 | err => '', |
| 238 | 238 | }; |
| 239 | 239 | my $r = eval { $self->{sc}->query_one($slug, q~ |
| 240 | 240 | SELECT COUNT(*) AS tables_ct, |
| 241 | 241 | COALESCE(SUM(DATA_LENGTH), 0) AS data_b, |
| 242 | 242 | COALESCE(SUM(INDEX_LENGTH), 0) AS indexes_b, |
| 243 | 243 | COALESCE(SUM(DATA_LENGTH + INDEX_LENGTH), 0) AS total_b |
| 244 | 244 | FROM information_schema.TABLES |
| 245 | 245 | WHERE TABLE_SCHEMA = DATABASE() |
| 246 | 246 | ~) }; |
| 247 | 247 | if ($r) { |
| 248 | 248 | $entry->{tables_ct} = ($r->{tables_ct} || 0) + 0; |
| 249 | 249 | $entry->{data_b} = ($r->{data_b} || 0) + 0; |
| 250 | 250 | $entry->{indexes_b} = ($r->{indexes_b} || 0) + 0; |
| 251 | 251 | $entry->{total_b} = ($r->{total_b} || 0) + 0; |
| 252 | 252 | } else { |
| 253 | 253 | $entry->{err} = $self->{sc}->last_error; |
| 254 | 254 | } |
| 255 | 255 | push @out, $entry; |
| 256 | 256 | } |
| 257 | 257 | @out = sort { $b->{total_b} <=> $a->{total_b} } @out; |
| 258 | 258 | return \@out; |
| 259 | 259 | } |
| 260 | 260 | |
| 261 | 261 | 1; |