fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2011-05-23 07:44 am

[dw-free] Business statistics

[commit: http://hg.dwscoalition.org/dw-free/rev/87c28d65348c]

http://bugs.dwscoalition.org/show_bug.cgi?id=124

Move calculations out of the template and into the controller. Also some
refactoring.

Patch by [personal profile] pauamma.

Files modified:
  • cgi-bin/DW/Controller/SiteStats.pm
  • views/stats/site.tt
--------------------------------------------------------------------------------
diff -r 392ed8679f47 -r 87c28d65348c cgi-bin/DW/Controller/SiteStats.pm
--- a/cgi-bin/DW/Controller/SiteStats.pm	Mon May 23 15:35:35 2011 +0800
+++ b/cgi-bin/DW/Controller/SiteStats.pm	Mon May 23 15:39:31 2011 +0800
@@ -8,7 +8,7 @@
 #      Afuna <coder.dw@afunamatata.com>
 #      Pau Amma <pauamma@dreamwidth.org>
 #
-# Copyright (c) 2009-2010 by Dreamwidth Studios, LLC.
+# Copyright (c) 2009-2011 by Dreamwidth Studios, LLC.
 #
 # This program is free software; you may redistribute it and/or modify it under
 # the same terms as Perl itself. For a copy of the license, please reference
@@ -119,105 +119,79 @@
 
     # Accounts by type
     my $accounts_by_type = DW::StatData::AccountsByType->load_latest( DW::StatStore->get( "accounts" ) );
-    $vars->{accounts_by_type}
-        = { map { _dashes_to_underlines( $_ )
-                  => _make_a_number( $accounts_by_type->value( $_ ) ) }
-                @{$accounts_by_type->keylist} }
-        if defined $accounts_by_type;
+    if ( defined $accounts_by_type ) {
+        $vars->{accounts_by_type}
+            = { map { _dashes_to_underlines( $_ )
+                      => _make_a_number( $accounts_by_type->value( $_ ) ) }
+                    @{$accounts_by_type->keylist} };
+
+        # Computed: total personal and community accounts
+        $vars->{accounts_by_type}->{total_PC} =
+            $vars->{accounts_by_type}->{personal} +
+            $vars->{accounts_by_type}->{community};
+    }
 
     # Active accounts by time since last active, level, and type
     my $active_accounts = DW::StatData::ActiveAccounts->load_latest( DW::StatStore->get( "active" ) );
-    $vars->{active_accounts}
-        = { map { _dashes_to_underlines( $_ )
-                  => _make_a_number( $active_accounts->value( $_ ) ) }
-                @{$active_accounts->keylist} }
-        if defined $active_accounts;
+    if ( defined $active_accounts ) {
+        $vars->{active_accounts}
+            = { map { _dashes_to_underlines( $_ )
+                      => _make_a_number( $active_accounts->value( $_ ) ) }
+                    @{$active_accounts->keylist} };
+
+        # Computed: total active personal and community accounts
+        $vars->{active_accounts}->{active_PC} =
+            $vars->{active_accounts}->{active_30d_free_P} +
+            $vars->{active_accounts}->{active_30d_paid_P} +
+            $vars->{active_accounts}->{active_30d_premium_P} +
+            $vars->{active_accounts}->{active_30d_seed_P} +
+            $vars->{active_accounts}->{active_30d_free_C} +
+            $vars->{active_accounts}->{active_30d_paid_C} +
+            $vars->{active_accounts}->{active_30d_premium_C} +
+            $vars->{active_accounts}->{active_30d_seed_C};
+
+        # Computed: total active allpaid (paid, premium, and seed) accounts
+        $vars->{active_accounts}->{active_allpaid} =
+            $vars->{active_accounts}->{active_30d_paid} +
+            $vars->{active_accounts}->{active_30d_premium} +
+            $vars->{active_accounts}->{active_30d_seed};
+
+        # Computed: total allpaid (paid, premium, and seed) personal accounts
+        # active in the last 1/7/30 days
+        $vars->{active_accounts}->{"active_${_}d_allpaid_P"} =
+            $vars->{active_accounts}->{"active_${_}d_paid_P"} +
+            $vars->{active_accounts}->{"active_${_}d_premium_P"} +
+            $vars->{active_accounts}->{"active_${_}d_seed_P"}
+            foreach qw( 1 7 30 );
+
+        # Computed: total allpaid community accounts
+        # active in the last 1/7/30 days
+        $vars->{active_accounts}->{"active_${_}d_allpaid_C"} =
+            $vars->{active_accounts}->{"active_${_}d_paid_C"} +
+            $vars->{active_accounts}->{"active_${_}d_premium_C"} +
+            $vars->{active_accounts}->{"active_${_}d_seed_C"}
+            foreach qw( 1 7 30 );
+
+        # Computed: total allpaid identity accounts
+        # active in the last 1/7/30 days
+        $vars->{active_accounts}->{"active_${_}d_allpaid_I"} =
+            $vars->{active_accounts}->{"active_${_}d_paid_I"} +
+            $vars->{active_accounts}->{"active_${_}d_premium_I"} +
+            $vars->{active_accounts}->{"active_${_}d_seed_I"}
+            foreach qw( 1 7 30 );
+    }
 
     # Paid accounts by level
     my $paid = DW::StatData::PaidAccounts->load_latest( DW::StatStore->get( "paid" ) );
-    $vars->{paid}
-        = { map { _dashes_to_underlines( $_ )
-                  => _make_a_number( $paid->value( $_ ) ) }
-                @{$paid->keylist} }
-        if defined $paid;
-
-    # Now we have all the raw data we need, but need to calculate some quantities:
-
-    # Total personal and community accounts
-    $vars->{accounts_by_type}->{total_PC} =
-        $vars->{accounts_by_type}->{personal} +
-        $vars->{accounts_by_type}->{community};
-
-    # Total active personal and community accounts
-    $vars->{active_accounts}->{active_PC} =
-        $vars->{active_accounts}->{active_30d_free_P} +
-        $vars->{active_accounts}->{active_30d_paid_P} +
-        $vars->{active_accounts}->{active_30d_premium_P} +
-        $vars->{active_accounts}->{active_30d_seed_P} +
-        $vars->{active_accounts}->{active_30d_free_C} +
-        $vars->{active_accounts}->{active_30d_paid_C} +
-        $vars->{active_accounts}->{active_30d_premium_C} +
-        $vars->{active_accounts}->{active_30d_seed_C};
-
-    # Total active allpaid (paid, premium, and seed) accounts
-    $vars->{active_accounts}->{active_allpaid} =
-        $vars->{active_accounts}->{active_30d_paid} +
-        $vars->{active_accounts}->{active_30d_premium} +
-        $vars->{active_accounts}->{active_30d_seed};
-
-    # Total active allpaid (paid, premium, and seed) personal accounts
-    $vars->{active_accounts}->{active_allpaid_P} =
-        $vars->{active_accounts}->{active_30d_paid_P} +
-        $vars->{active_accounts}->{active_30d_premium_P} +
-        $vars->{active_accounts}->{active_30d_seed_P};
-
-    # Total allpaid personal accounts active in the past 7 days
-    $vars->{active_accounts}->{active_7d_allpaid_P} =
-        $vars->{active_accounts}->{active_7d_paid_P} +
-        $vars->{active_accounts}->{active_7d_premium_P} +
-        $vars->{active_accounts}->{active_7d_seed_P};
-
-    # Total allpaid personal accounts active in the past 1 day
-    $vars->{active_accounts}->{active_1d_allpaid_P} =
-        $vars->{active_accounts}->{active_1d_paid_P} +
-        $vars->{active_accounts}->{active_1d_premium_P} +
-        $vars->{active_accounts}->{active_1d_seed_P};
-
-    # Total active allpaid community accounts
-    $vars->{active_accounts}->{active_allpaid_C} =
-        $vars->{active_accounts}->{active_30d_paid_C} +
-        $vars->{active_accounts}->{active_30d_premium_C} +
-        $vars->{active_accounts}->{active_30d_seed_C};
-
-    # Total allpaid community accounts active in the past 7 days
-    $vars->{active_accounts}->{active_7d_allpaid_C} =
-        $vars->{active_accounts}->{active_7d_paid_C} +
-        $vars->{active_accounts}->{active_7d_premium_C} +
-        $vars->{active_accounts}->{active_7d_seed_C};
-
-    # Total allpaid community accounts active in the past 1 day
-    $vars->{active_accounts}->{active_1d_allpaid_C} =
-        $vars->{active_accounts}->{active_1d_paid_C} +
-        $vars->{active_accounts}->{active_1d_premium_C} +
-        $vars->{active_accounts}->{active_1d_seed_C};
-
-    # Total active allpaid identity accounts
-    $vars->{active_accounts}->{active_allpaid_I} =
-        $vars->{active_accounts}->{active_30d_paid_I} +
-        $vars->{active_accounts}->{active_30d_premium_I} +
-        $vars->{active_accounts}->{active_30d_seed_I};
-
-    # Total allpaid identity accounts active in the past 7 days
-    $vars->{active_accounts}->{active_7d_allpaid_I} =
-        $vars->{active_accounts}->{active_7d_paid_I} +
-        $vars->{active_accounts}->{active_7d_premium_I} +
-        $vars->{active_accounts}->{active_7d_seed_I};
-
-    # Total allpaid identity accounts active in the past 1 day
-    $vars->{active_accounts}->{active_1d_allpaid_I} =
-        $vars->{active_accounts}->{active_1d_paid_I} +
-        $vars->{active_accounts}->{active_1d_premium_I} +
-        $vars->{active_accounts}->{active_1d_seed_I};
+    if ( defined $paid ) {
+        $vars->{paid}
+            = { map { _dashes_to_underlines( $_ )
+                      => _make_a_number( $paid->value( $_ ) ) }
+                    @{$paid->keylist} };
+        $vars->{paid}->{allpaid} = 0;
+        $vars->{paid}->{allpaid} += $vars->{paid}->{$_}
+            foreach @{$paid->keylist};
+    }
 
     return $vars;
 }
diff -r 392ed8679f47 -r 87c28d65348c views/stats/site.tt
--- a/views/stats/site.tt	Mon May 23 15:35:35 2011 +0800
+++ b/views/stats/site.tt	Mon May 23 15:39:31 2011 +0800
@@ -7,7 +7,7 @@
      Afuna <coder.dw@afunamatata.com>
      Pau Amma <pauamma@dreamwidth.org>
 
-Copyright (c) 2009-2010 by Dreamwidth Studios, LLC.
+Copyright (c) 2009-2011 by Dreamwidth Studios, LLC.
 
 This program is free software; you may redistribute it and/or modify it under
 the same terms as Perl itself. For a copy of the license, please reference
@@ -29,7 +29,6 @@
         <li>[% ".accounts.bytype.$t" | ml %] [% accounts_by_type.$t %]</li>
     [% END %]
     </ul>
-    [% total_pc = accounts_by_type.personal + accounts_by_type.community %]
 
 [%# Accounts by type pie chart %]
 <h3 class="graphtitle">[% '.graphtitle.accounts' | ml %]</h3>
@@ -61,17 +60,6 @@
         <li>[% ".active.bytime.$t" | ml %] [% active_accounts.$t %]</li>
     [% END %]
     </ul>
-    [% active_pc = active_accounts.active_30d_free_P
-                   + active_accounts.active_30d_paid_P
-                   + active_accounts.active_30d_premium_P
-                   + active_accounts.active_30d_seed_P
-                   + active_accounts.active_30d_free_C
-                   + active_accounts.active_30d_paid_C
-                   + active_accounts.active_30d_premium_C
-                   + active_accounts.active_30d_seed_C %]
-    [% active_allpaid = active_accounts.active_30d_paid
-                        + active_accounts.active_30d_premium
-                        + active_accounts.active_30d_seed %]
 [% ELSE %]
     [% '.error.notavailable' | ml %]
 [% END %]
@@ -79,7 +67,7 @@
 [%# Active personal accounts bar chart %]
 <h3 class="graphtitle">[% '.graphtitle.active.personal' | ml %]</h3>
 <img src="active_personal_accounts?active_free_p=[% active_accounts.active_30d_free_P ~%]
-    &active_allpaid_p=[% active_accounts.active_allpaid_P ~%]
+    &active_allpaid_p=[% active_accounts.active_30d_allpaid_P ~%]
     &active_7d_free_p=[% active_accounts.active_7d_free_P ~%]
     &active_7d_allpaid_p=[% active_accounts.active_7d_allpaid_P ~%]
     &active_1d_free_p=[% active_accounts.active_1d_free_P ~%]
@@ -90,7 +78,7 @@
     &bar_7d_label=[% '.label.bar.7d' | ml ~%]
     &bar_1d_label=[% '.label.bar.1d' | ml %]"
 alt="[% '.active.free30' | ml %] [% active_accounts.active_30d_free_P %];
-    [%= '.active.paid30' | ml %] [% active_accounts.active_allpaid_P %];
+    [%= '.active.paid30' | ml %] [% active_accounts.active_30d_allpaid_P %];
     [%= '.active.free7' | ml %] [% active_accounts.active_7d_free_P %];
     [%= '.active.paid7' | ml %] [% active_accounts.active_7d_allpaid_P %];
     [%= '.active.free1' | ml %] [% active_accounts.active_1d_free_P %];
@@ -100,7 +88,7 @@
 [%# Active community accounts bar chart %]
 <h3 class="graphtitle">[% '.graphtitle.active.community' | ml %]</h3>
 <img src="active_community_accounts?active_free_c=[% active_accounts.active_30d_free_C ~%]
-    &active_allpaid_c=[% active_accounts.active_allpaid_C ~%]
+    &active_allpaid_c=[% active_accounts.active_30d_allpaid_C ~%]
     &active_7d_free_c=[% active_accounts.active_7d_free_C ~%]
     &active_7d_allpaid_c=[% active_accounts.active_7d_allpaid_C ~%]
     &active_1d_free_c=[% active_accounts.active_1d_free_C ~%]
@@ -111,7 +99,7 @@
     &bar_7d_label=[% '.label.bar.7d' | ml ~%]
     &bar_1d_label=[% '.label.bar.1d' | ml %]"
 alt="[% '.active.free30' | ml %] [% active_accounts.active_30d_free_C %];
-    [%= '.active.paid30' | ml %] [% active_accounts.active_allpaid_C %];
+    [%= '.active.paid30' | ml %] [% active_accounts.active_30d_allpaid_C %];
     [%= '.active.free7' | ml %] [% active_accounts.active_7d_free_C %];
     [%= '.active.paid7' | ml %] [% active_accounts.active_7d_allpaid_C %];
     [%= '.active.free1' | ml %] [% active_accounts.active_1d_free_C %];
@@ -121,7 +109,7 @@
 [%# Active identity accounts bar graph %]
 <h3 class="graphtitle">[% '.graphtitle.active.identity' | ml %]</h3>
 <img src="active_identity_accounts?active_free_i=[% active_accounts.active_30d_free_I ~%]
-    &active_allpaid_i=[% active_accounts.active_allpaid_I ~%]
+    &active_allpaid_i=[% active_accounts.active_30d_allpaid_I ~%]
     &active_7d_free_i=[% active_accounts.active_7d_free_I ~%]
     &active_7d_allpaid_i=[% active_accounts.active_7d_allpaid_I ~%]
     &active_1d_free_i=[% active_accounts.active_1d_free_I ~%]
@@ -132,7 +120,7 @@
     &bar_7d_label=[% '.label.bar.7d' | ml ~%]
     &bar_1d_label=[% '.label.bar.1d' | ml %]"
 alt="[% '.active.free30' | ml %] [% active_accounts.active_30d_free_I %];
-    [%= '.active.paid30' | ml %] [% active_accounts.active_allpaid_I %];
+    [%= '.active.paid30' | ml %] [% active_accounts.active_30d_allpaid_I %];
     [%= '.active.free7' | ml %] [% active_accounts.active_7d_free_I %];
     [%= '.active.paid7' | ml %] [% active_accounts.active_7d_allpaid_I %];
     [%= '.active.free1' | ml %] [% active_accounts.active_1d_free_I %];
@@ -148,23 +136,26 @@
         <th>[% ".paid.colhdr.$h" | ml %]</th>
     [% END %]
     </tr></thead>
-    [% allpaid = 0 %]
     [% FOREACH level = [ 'paid' 'premium' 'seed' ] %]
         <tr><th>[% ".paid.rowhdr.$level" | ml %]</th>
         [% n = paid.$level.defined ? paid.$level : 0 %]
-        [% allpaid = allpaid + n %]
         <td class='stats'>[% n %]</td>
         <td class='stats'>
-        [%- 100 * n / total_pc | format "%.1f" IF total_pc != 0 %]</td>
+        [%- 100 * n / accounts_by_type.total_PC | format "%.1f"
+            IF accounts_by_type.total_PC != 0 -%]
+        </td>
         <td class='stats'>
-        [%- 100 * n / active_pc | format "%.1f" IF active_pc != 0 %]</td>
-        </tr>
+        [%- 100 * n / active_accounts.active_PC | format "%.1f"
+            IF active_accounts.active_PC != 0 -%]
+        </td></tr>
     [% END %]
     <tr><th>[% '.paid.rowhdr.activepaid' | ml %]</th><td class='stats'>
-    [% active_allpaid IF active_allpaid.defined %]
+    [%- active_accounts.active_allpaid
+        IF active_accounts.active_allpaid.defined -%]
     </td></tr><tr><th>[% '.paid.rowhdr.inactivepaid' | ml %]</th>
     <td class='stats'>
-    [% allpaid - active_allpaid IF active_allpaid.defined %]
+    [%- paid.allpaid - active_accounts.active_allpaid
+        IF active_accounts.active_allpaid.defined -%]
     </td></tr></table>
 [% ELSE %]
     [% '.error.notavailable' | ml %]
--------------------------------------------------------------------------------