Awight has submitted this change and it was merged. Change subject: Add exchange-rate-backfill drush command ......................................................................
Add exchange-rate-backfill drush command Takes optional start (defaults to last bank update date) and end (defaults to now) parameters. Gets one rate per day in the range. Change-Id: I8821560ba447a1b02083fa10ff88df5de4a2b7ed --- M sites/all/modules/exchange_rates/exchange_rates.drush.inc M sites/all/modules/exchange_rates/exchange_rates.module M sites/all/modules/exchange_rates/retrievers/EcbRetriever.php M sites/all/modules/exchange_rates/retrievers/ExchangeRateRetriever.php M sites/all/modules/exchange_rates/retrievers/OandaRetriever.php 5 files changed, 55 insertions(+), 6 deletions(-) Approvals: Awight: Verified; Looks good to me, approved diff --git a/sites/all/modules/exchange_rates/exchange_rates.drush.inc b/sites/all/modules/exchange_rates/exchange_rates.drush.inc index af52394..a6e7dd3 100644 --- a/sites/all/modules/exchange_rates/exchange_rates.drush.inc +++ b/sites/all/modules/exchange_rates/exchange_rates.drush.inc @@ -8,6 +8,17 @@ 'description' => 'Updates exchange rates from provider.', 'examples' => array( 'drush exchange-rates-update' ), ); + $items['exchange-rates-backfill'] = array( + 'description' => 'Retrieves and stores daily exchange rates for a date range.', + 'examples' => array( + 'drush exchange-rates-backfill', + 'drush exchange-rates-backfill --start=2014-06-20' + ), + 'options' => array( + 'start' => 'Date of earliest rates to request in YYYY-MM-DD format', + 'end' => 'Date of latest rates to request in YYYY-MM-DD format', + ) + ); return $items; } @@ -18,9 +29,19 @@ switch ( $section ) { case 'drush:exchange-rates-update': return dt( 'Attempts to retrieve the latest exchange rates from a providers, stopping when a request succeeds and storing results in the database.' ); + case 'drush:exchange-rates-backfill': + return dt( 'Backfills daily exchanges rates across a range of dates. --start and --end parameters take a date in YYYYmmDD format. If run with no parameters, will backfill starting from last updated date.'); } } function drush_exchange_rates_update() { module_invoke( 'exchange_rates', 'update_all' ); } + +function drush_exchange_rates_backfill() { + $start_string = drush_get_option( 'start' ); + $end_string = drush_get_option( 'end' ); + $start = $start_string ? new DateTime( $start_string, new DateTimeZone( 'UTC' ) ) : null; + $end = $end_string ? new DateTime( $end_string, new DateTimeZone( 'UTC' ) ) : null; + module_invoke( 'exchange_rates', 'backfill', $start, $end ); +} diff --git a/sites/all/modules/exchange_rates/exchange_rates.module b/sites/all/modules/exchange_rates/exchange_rates.module index d0f5a4c..c280ef0 100644 --- a/sites/all/modules/exchange_rates/exchange_rates.module +++ b/sites/all/modules/exchange_rates/exchange_rates.module @@ -175,7 +175,7 @@ $_exchange_rate_cache[$key] = $rate; } -function exchange_rates_update_all() { +function exchange_rates_update_all( $date = null ) { // If you update this list, also update the list in the DonationInterface extension $currencies = array( @@ -392,7 +392,7 @@ foreach ( $retrievers as $retriever ) { try { - $result = $retriever->updateRates( $currencies ); + $result = $retriever->updateRates( $currencies, $date ); break; } catch ( ExchangeRateUpdateException $ex) { watchdog( 'exchange_rates', $ex ); @@ -404,9 +404,12 @@ } $date_set = false; + $last_update = variable_get( 'exchange_rates_bank_update' ); foreach( $result->rates as $code => $rate ) { exchange_rates_update_rate( $code, $rate['value'], $rate['date'] ); - if ( !$date_set ) { + if ( !$date_set && + ( !$last_update || $rate['date'] > $last_update ) + ) { variable_set( 'exchange_rates_bank_update', $rate['date'] ); $date_set = true; } @@ -427,5 +430,22 @@ ) )->execute(); } +function exchange_rates_backfill( $start, $end ) { + if ( $start == null ) { + $start_timestamp = db_query_range('SELECT MAX(bank_update) FROM {exchange_rates}', 0, 1 )->fetchField(); + if ( !start_timestamp ) { + throw new ExchangeRatesException( 'Backfill start date not specified and no records in database' ); + } + $start = new DateTime( "@$start_timestamp" ); // No TZ param + watchdog( 'exchange_rates', "Backfill start date not specified - using last bank update from db ({$start->format( 'Y-m-d' )})" ); + } + if ( $end == null ) { + $end = new DateTime( 'now', new DateTimeZone( 'UTC' ) ); + } + for ( $date = $start; $date <= $end; $date->add( new DateInterval( 'P1D' ) ) ) { + exchange_rates_update_all( $date ); + } +} + class ExchangeRatesException extends Exception { } diff --git a/sites/all/modules/exchange_rates/retrievers/EcbRetriever.php b/sites/all/modules/exchange_rates/retrievers/EcbRetriever.php index b4b92ba..4d6c2c4 100644 --- a/sites/all/modules/exchange_rates/retrievers/EcbRetriever.php +++ b/sites/all/modules/exchange_rates/retrievers/EcbRetriever.php @@ -3,7 +3,11 @@ class EcbRetriever extends ExchangeRateRetriever { - public function updateRates( $currencies ) { + public function updateRates( $currencies, $date = null ) { + if ( $date !== null ) { + throw new ExchangeRateUpdateException( 'ECB does not provide historical exchange rates' ); + } + $url = 'http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml'; // Retrieve and parse the XML results diff --git a/sites/all/modules/exchange_rates/retrievers/ExchangeRateRetriever.php b/sites/all/modules/exchange_rates/retrievers/ExchangeRateRetriever.php index 5a7a73e..93479af 100644 --- a/sites/all/modules/exchange_rates/retrievers/ExchangeRateRetriever.php +++ b/sites/all/modules/exchange_rates/retrievers/ExchangeRateRetriever.php @@ -21,9 +21,10 @@ /** * Retrieve updated rates using $this->httpRequester * @param array $currencies - list of currency codes to update + * @param DateTime $date - retrieve rates for this date. If omitted, get latest rates. * @return ExchangeRateUpdateResult */ - abstract function updateRates( $currencies ); + abstract function updateRates( $currencies, $date = null ); } class ExchangeRateUpdateResult { diff --git a/sites/all/modules/exchange_rates/retrievers/OandaRetriever.php b/sites/all/modules/exchange_rates/retrievers/OandaRetriever.php index ffc617b..db1e062 100644 --- a/sites/all/modules/exchange_rates/retrievers/OandaRetriever.php +++ b/sites/all/modules/exchange_rates/retrievers/OandaRetriever.php @@ -29,12 +29,15 @@ $this->fields = $quoteFields[$quote]; } - public function updateRates( $currencies ) { + public function updateRates( $currencies, $date = null ) { $url = $this->endpoint . '/v1/rates/USD.json' . '?fields=' . $this->fields . '&decimal_places=all' . '"e=' . implode ( '"e=', $currencies ); + if ( $date ) { + $url .= '&date=' . $date->format( 'Y-m-d' ); + } $response = call_user_func( $this->httpRequester, -- To view, visit https://gerrit.wikimedia.org/r/163311 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I8821560ba447a1b02083fa10ff88df5de4a2b7ed Gerrit-PatchSet: 4 Gerrit-Project: wikimedia/fundraising/crm Gerrit-Branch: master Gerrit-Owner: Ejegg <eeggles...@wikimedia.org> Gerrit-Reviewer: Awight <awi...@wikimedia.org> Gerrit-Reviewer: Ejegg <eeggles...@wikimedia.org> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits