Merge pull request #205 from CartoDB/204-write-zero-padded-keys

204 write zero padded keys
This commit is contained in:
Rafa de la Torre 2016-06-15 10:42:00 +02:00 committed by GitHub
commit b33730aae3
4 changed files with 29 additions and 5 deletions

View File

@ -40,7 +40,11 @@ OK
``` ```
## Running the integration tests ## Running the integration tests
TBD See the [[../../../../test/README.md]]. Basically, move to the `/test` directory at the top level of this repo and execute the `run_tests.py` script:
```sh
cd $(git rev-parse --show-toplevel)/test
python run_tests.py --host=$YOUR_HOST $YOUR_USERNAME $YOUR_API_KEY
```
## TODO ## TODO
- Move dependencies expressed in `requirements.txt` to `setup.py` - Move dependencies expressed in `requirements.txt` to `setup.py`

View File

@ -8,6 +8,7 @@ class UserMetricsService:
SERVICE_GEOCODER_NOKIA = 'geocoder_here' SERVICE_GEOCODER_NOKIA = 'geocoder_here'
SERVICE_GEOCODER_CACHE = 'geocoder_cache' SERVICE_GEOCODER_CACHE = 'geocoder_cache'
SERVICE_HERE_ISOLINES = 'here_isolines' SERVICE_HERE_ISOLINES = 'here_isolines'
DAY_OF_MONTH_ZERO_PADDED = '%d'
def __init__(self, user_geocoder_config, redis_connection): def __init__(self, user_geocoder_config, redis_connection):
self._user_geocoder_config = user_geocoder_config self._user_geocoder_config = user_geocoder_config
@ -85,7 +86,7 @@ class UserMetricsService:
service, metric, date) service, metric, date)
score = self._redis_connection.zscore(redis_prefix, date.day) score = self._redis_connection.zscore(redis_prefix, date.day)
aggregated_metric += score if score else 0 aggregated_metric += score if score else 0
zero_padded_day = date.strftime('%d') zero_padded_day = date.strftime(self.DAY_OF_MONTH_ZERO_PADDED)
if str(date.day) != zero_padded_day: if str(date.day) != zero_padded_day:
score = self._redis_connection.zscore(redis_prefix, zero_padded_day) score = self._redis_connection.zscore(redis_prefix, zero_padded_day)
aggregated_metric += score if score else 0 aggregated_metric += score if score else 0
@ -97,12 +98,16 @@ class UserMetricsService:
def __increment_user_uses(self, service_type, metric, date, amount): def __increment_user_uses(self, service_type, metric, date, amount):
redis_prefix = self.__parse_redis_prefix("user", self._username, redis_prefix = self.__parse_redis_prefix("user", self._username,
service_type, metric, date) service_type, metric, date)
self._redis_connection.zincrby(redis_prefix, date.day, amount) self._redis_connection.zincrby(redis_prefix,
date.strftime(self.DAY_OF_MONTH_ZERO_PADDED),
amount)
def __increment_organization_uses(self, service_type, metric, date, amount): def __increment_organization_uses(self, service_type, metric, date, amount):
redis_prefix = self.__parse_redis_prefix("org", self._orgname, redis_prefix = self.__parse_redis_prefix("org", self._orgname,
service_type, metric, date) service_type, metric, date)
self._redis_connection.zincrby(redis_prefix, date.day, amount) self._redis_connection.zincrby(redis_prefix,
date.strftime(self.DAY_OF_MONTH_ZERO_PADDED),
amount)
def __parse_redis_prefix(self, prefix, entity_name, service_type, metric, def __parse_redis_prefix(self, prefix, entity_name, service_type, metric,
date): date):

View File

@ -10,7 +10,7 @@ from setuptools import setup, find_packages
setup( setup(
name='cartodb_services', name='cartodb_services',
version='0.6.3', version='0.6.4',
description='CartoDB Services API Python Library', description='CartoDB Services API Python Library',

View File

@ -116,6 +116,21 @@ class TestUserService(TestCase):
#('user:test_user:geocoder_cache:success_responses:201506', 15) #('user:test_user:geocoder_cache:success_responses:201506', 15)
assert self.redis_conn.zscore_counter() == 3 assert self.redis_conn.zscore_counter() == 3
def test_should_write_zero_padded_dates(self):
us = self.__build_user_service('test_user')
us.increment_service_use(self.NOKIA_GEOCODER, 'success_responses',
date=date(2015,6,1))
assert self.redis_conn.zscore('user:test_user:geocoder_here:success_responses:201506', '01') == 1
assert self.redis_conn.zscore('user:test_user:geocoder_here:success_responses:201506', '1') == None
def test_orgs_should_write_zero_padded_dates(self):
us = self.__build_user_service('test_user', orgname='test_org')
us.increment_service_use(self.NOKIA_GEOCODER, 'success_responses',
amount=400,
date=date(2015,6,1))
assert self.redis_conn.zscore('org:test_org:geocoder_here:success_responses:201506', '01') == 400
assert self.redis_conn.zscore('org:test_org:geocoder_here:success_responses:201506', '1') == None
def __build_user_service(self, username, quota=100, service='heremaps', def __build_user_service(self, username, quota=100, service='heremaps',
orgname=None, soft_limit=False, orgname=None, soft_limit=False,