r103947 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r103946‎ | r103947 | r103948 >
Date:19:37, 22 November 2011
Author:kaldari
Status:resolved (Comments)
Tags:
Comment:
follow-up to r103884, daily and yearly totals
Modified paths:
  • /trunk/extensions/ContributionReporting/ContributionReporting.php (modified) (history)
  • /trunk/extensions/ContributionReporting/DailyTotal_body.php (modified) (history)
  • /trunk/extensions/ContributionReporting/YearlyTotal_body.php (added) (history)

Diff [purge]

Index: trunk/extensions/ContributionReporting/ContributionReporting.php
@@ -42,6 +42,7 @@
4343 $wgAutoloadClasses['SpecialFundraiserStatistics'] = $dir . 'FundraiserStatistics_body.php';
4444 $wgAutoloadClasses['SpecialContributionTrackingStatistics'] = $dir . 'ContributionTrackingStatistics_body.php';
4545 $wgAutoloadClasses['SpecialDailyTotal'] = $dir . 'DailyTotal_body.php';
 46+$wgAutoloadClasses['SpecialYearlyTotal'] = $dir . 'YearlyTotal_body.php';
4647
4748 $wgSpecialPages['ContributionHistory'] = 'ContributionHistory';
4849 $wgSpecialPages['ContributionTotal'] = 'ContributionTotal';
@@ -49,6 +50,7 @@
5051 $wgSpecialPages['FundraiserStatistics'] = 'SpecialFundraiserStatistics';
5152 $wgSpecialPages['ContributionTrackingStatistics'] = 'SpecialContributionTrackingStatistics';
5253 $wgSpecialPages['DailyTotal'] = 'SpecialDailyTotal';
 54+$wgSpecialPages['YearlyTotal'] = 'SpecialYearlyTotal';
5355 $wgSpecialPageGroups['ContributionHistory'] = 'contribution';
5456 $wgSpecialPageGroups['ContributionTotal'] = 'contribution';
5557 $wgSpecialPageGroups['ContributionStatistics'] = 'contribution';
@@ -67,6 +69,7 @@
6870 $egContributionStatisticsViewDays = 7;
6971
7072 // Fundraiser dates
 73+// Please list these in chronological order
7174 $egFundraiserStatisticsFundraisers = array(
7275 array(
7376 'id' => '2007',
@@ -162,6 +165,7 @@
163166
164167 function efContributionReportingTotal( $start, $fudgeFactor ) {
165168 $db = efContributionReportingConnection();
 169+ #$db = wfGetDB( DB_MASTER );
166170
167171 $sql = 'SELECT ROUND( SUM(converted_amount) ) AS ttl FROM public_reporting';
168172
Index: trunk/extensions/ContributionReporting/DailyTotal_body.php
@@ -10,7 +10,6 @@
1111
1212 protected $sharedMaxAge = 300; // Cache for 5 minutes on the server side
1313 protected $maxAge = 300; // Cache for 5 minutes on the client side
14 - public $timezone;
1514
1615 /* Functions */
1716
@@ -21,18 +20,18 @@
2221 public function execute( $sub ) {
2322 global $wgRequest, $wgOut;
2423
25 - $this->timezone = $wgRequest->getVal( 'timezone', '0' );
 24+ $timezone = $wgRequest->getVal( 'timezone', '0' );
2625 // Make sure it's a number and reasonable
27 - if ( is_nan( $this->timezone ) || abs( $this->timezone ) > 100 ) {
28 - $this->timezone = 0;
 26+ if ( is_nan( $timezone ) || abs( $timezone ) > 100 ) {
 27+ $timezone = 0;
2928 }
3029
3130 /* Setup */
3231 $wgOut->disable();
3332 $this->sendHeaders();
3433
35 - $start = date( 'Y-m-d' ); // Get the current date
36 - $total = $this->query( $start );
 34+ $start = time(); // Get the current unix timestamp
 35+ $total = $this->query( $timezone, $start );
3736
3837 $content = "wgFundraisingDailyTotal = $total;";
3938 echo $content;
@@ -40,21 +39,21 @@
4140
4241 /* Private Functions */
4342
44 - private function query( $start ) {
 43+ private function query( $timezone, $start ) {
4544 global $wgMemc, $egFundraiserStatisticsMinimum, $egFundraiserStatisticsMaximum, $egFundraiserStatisticsCacheTimeout;
4645
47 - $key = wfMemcKey( 'fundraiserstatistics', $this->timezone, $start );
 46+ $key = wfMemcKey( 'fundraiserstatistics', $timezone, $start );
4847 $cache = $wgMemc->get( $key );
4948 if ( $cache != false && $cache != -1 ) {
5049 return $cache;
5150 }
52 - $timeShift = $this->timezone * 60 * 60;
 51+ $timeShift = $timezone * 60 * 60;
5352 // Use database
54 - //$dbr = efContributionReportingConnection();
55 - $dbr = wfGetDB( DB_MASTER );
 53+ $dbr = efContributionReportingConnection();
 54+ #$dbr = wfGetDB( DB_MASTER );
5655 $conditions = array(
57 - 'received >= ' . $dbr->addQuotes( wfTimestamp( TS_UNIX, strtotime( $start ) + $timeShift ) ),
58 - 'received <= ' . $dbr->addQuotes( wfTimestamp( TS_UNIX, strtotime( $start ) + 24 * 60 * 60 + $timeShift ) ),
 56+ 'received >= ' . $dbr->addQuotes( wfTimestamp( TS_UNIX, $start + $timeShift ) ),
 57+ 'received <= ' . $dbr->addQuotes( wfTimestamp( TS_UNIX, $start + 24 * 60 * 60 + $timeShift ) ),
5958 'converted_amount >= ' . $egFundraiserStatisticsMinimum,
6059 'converted_amount <= ' . $egFundraiserStatisticsMaximum
6160 );
@@ -71,12 +70,10 @@
7271 );
7372 $row = $dbr->fetchRow( $select );
7473 $total = $row['sum(converted_amount)'];
 74+ if ( !$total ) $total = 0;
7575
76 - if ( $total ) {
77 - $wgMemc->set( $key, $total, $egFundraiserStatisticsCacheTimeout );
78 - return $total;
79 - }
80 - return null;
 76+ $wgMemc->set( $key, $total, $egFundraiserStatisticsCacheTimeout );
 77+ return $total;
8178 }
8279
8380 private function sendHeaders() {
Index: trunk/extensions/ContributionReporting/YearlyTotal_body.php
@@ -0,0 +1,70 @@
 2+<?php
 3+/**
 4+ * Special Page for Contribution statistics extension
 5+ *
 6+ * @file
 7+ * @ingroup Extensions
 8+ */
 9+
 10+class SpecialYearlyTotal extends IncludableSpecialPage {
 11+
 12+ protected $sharedMaxAge = 600; // Cache for 10 minutes on the server side
 13+ protected $maxAge = 600; // Cache for 10 minutes on the client side
 14+
 15+ /* Functions */
 16+
 17+ public function __construct() {
 18+ parent::__construct( 'YearlyTotal' );
 19+ }
 20+
 21+ public function execute( $sub ) {
 22+ global $wgRequest, $wgOut, $egFundraiserStatisticsFundraisers;
 23+
 24+ $adjustment = $wgRequest->getVal( 'adjustment' );
 25+ // Make sure it's a number
 26+ if ( is_nan( $adjustment ) ) {
 27+ $adjustment = 0;
 28+ }
 29+
 30+ /* Setup */
 31+ $wgOut->disable();
 32+ $this->sendHeaders();
 33+
 34+ $total = $this->query( $adjustment );
 35+
 36+ $content = "wgFundraisingYearlyTotal = $total;";
 37+ echo $content;
 38+ }
 39+
 40+ /* Private Functions */
 41+
 42+ private function query( $adjustment ) {
 43+ global $wgMemc, $egFundraiserStatisticsCacheTimeout, $egFundraiserStatisticsFundraisers;
 44+
 45+ $currenctFundraiserIndex = count( $egFundraiserStatisticsFundraisers ) - 1;
 46+ $year = $egFundraiserStatisticsFundraisers[$currenctFundraiserIndex]['id'];
 47+
 48+ $key = wfMemcKey( 'fundraiserstatistics', $year, $adjustment );
 49+ $cache = $wgMemc->get( $key );
 50+ if ( $cache != false && $cache != -1 ) {
 51+ return $cache;
 52+ }
 53+
 54+ // Get the timestamp for the start of the current fundraiser
 55+ // Note: This depends on the fundraisers being listed in chronological order
 56+ $start = strtotime( $egFundraiserStatisticsFundraisers[$currenctFundraiserIndex]['start'] );
 57+ $start = intval( wfTimestampOrNull( TS_UNIX, $start ) );
 58+
 59+ $total = efContributionReportingTotal( $start, $adjustment );
 60+ if ( !$total ) $total = 0;
 61+
 62+ $wgMemc->set( $key, $total, $egFundraiserStatisticsCacheTimeout );
 63+ return $total;
 64+ }
 65+
 66+ private function sendHeaders() {
 67+ global $wgJsMimeType;
 68+ header( "Content-type: $wgJsMimeType; charset=utf-8" );
 69+ header( "Cache-Control: public, s-maxage=$this->sharedMaxAge, max-age=$this->maxAge" );
 70+ }
 71+}

Follow-up revisions

RevisionCommit summaryAuthorDate
r103998MFT r103884, r103947, r103959, r103967, r103991, r103996awjrichards01:24, 23 November 2011
r104000MFT r103884, r103947, r103959, r103967, r103991, r103996awjrichards01:51, 23 November 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r103884adding daily total reporting systemkaldari02:22, 22 November 2011

Comments

#Comment by Awjrichards (talk | contribs)   22:05, 22 November 2011

We just talked about this IRL, but for the record...

+		$start = time(); // Get the current unix timestamp
+		$total = $this->query( $timezone, $start );
 		
 		$content = "wgFundraisingDailyTotal = $total;";
 		echo $content;
@@ -40,21 +39,21 @@
 
 	/* Private Functions */
 
-	private function query( $start ) {
+	private function query( $timezone, $start ) {
 		global $wgMemc, $egFundraiserStatisticsMinimum, $egFundraiserStatisticsMaximum, $egFundraiserStatisticsCacheTimeout;
 
-		$key = wfMemcKey( 'fundraiserstatistics', $this->timezone, $start );
+		$key = wfMemcKey( 'fundraiserstatistics', $timezone, $start );
 		$cache = $wgMemc->get( $key );
 		if ( $cache != false && $cache != -1 ) {
 			return $cache;
 		}

This will effectively render caching useless since you're using the output of time() as part of the key and time() will give you something different every second. Looks like you had it more sane the first time :p

You might consider changing the cache expiration for SpecialYearlyTotal:query() since theoretically, results from years other than this year should be more-or-less unchanging.

Also, disabling output, setting your headers, etc to just echo a value out to the screen seems kind of like a funky way to reproduce behavior that you would normally find in an API. In the future if we need to add more similar features to ContributionReporting, we should just go ahead and build a proper API.

#Comment by Kaldari (talk | contribs)   22:09, 22 November 2011

caching fixed in r103959.

#Comment by Awjrichards (talk | contribs)   22:20, 22 November 2011

Actually... putting back to fixme - r103959 has been fixme'd

Status & tagging log