import redis
from datetime import date

class QuotaService:
    """ Class to manage all the quota operation for the Geocoder SQL API Extension """

    GEOCODING_QUOTA_KEY = "geocoding_quota"

    def __init__(self, logger, user_id, transaction_id):
        self.logger = logger
        self.user_id = user_id
        self.transaction_id = transaction_id
        self.redis_conn = self.__get_redis_connection()

    def check_user_quota(self):
        """ Get the user quota and add it to redis in order to cache it """
        # TODO: Check if the redis key geocoder::user_id::tx_id exists:
        #       a) If exists check the quota
        user_quota = self.get_user_quota()
        current_used = self.get_current_used_quota()
        return True if (current_used + 1) < user_quota else False

    def get_user_quota(self):
        return self.redis_conn.hget(self.__get_user_redis_key(), self.GEOCODING_QUOTA_KEY)

    def get_current_used_quota(self):
        # TODO: Check if exist geocoder:user_id:year_month , if yes sum up all of it
        current_used = 0
        for _, value in self.redis_conn.hscan_iter(self.__get_month_redis_key()):
            current_used += int(value)
        return current_used

    def increment_georeference_use(self):
        pass

    def __get_redis_connection(self):
        pool = redis.ConnectionPool(host='localhost', port=6379, db=5)
        return redis.Redis(connection_pool=pool)

    def __get_month_redis_key(self):
        today = date.today()
        return "geocoder:{0}:{1}".format(self.user_id, today.strftime("%Y%m"))

    def __get_user_redis_key(self):
        return "geocoder:{0}".format(self.user_id)
@@ -240,16 +240,71 @@
 ction_id
+, redis_host='localhost', redis_port=6379, redis_db = 5
 ):%0A     
@@ -426,365 +426,533 @@
 dis_
-conn = self.__get_redis_connection()%0A%0A    def check_user_quota(self):%0A        %22%22%22 Get the user quota and add it to redis in order to cache it %22%22%22%0A        # TODO: Check if the redis key geocoder::user_id::tx_id exists:%0A        #       a) If exists check the quota%0A        user_quota = self.get_user_quota()%0A        current_used = self.get_current_used_quota(
+host = redis_host%0A        self.redis_port = redis_port%0A        self.redis_db = redis_db%0A        self.redis_conn = self.__get_redis_connection()%0A%0A    def check_user_quota(self):%0A        %22%22%22 Check if the current user quota surpasses the current quota %22%22%22%0A        # TODO We need to add the hard/soft limit flag for the geocoder%0A        user_quota = self.get_user_quota()%0A        current_used = self.get_current_used_quota()%0A        self.logger.debug(%22User quota: %7B0%7D --- Current used quota: %7B1%7D%22.format(user_quota, current_used)
 )%0A  
@@ -1054,22 +1054,76 @@
         
-return
+# Check for exceptions or redis timeout%0A        user_quota =
  self.re
@@ -1190,16 +1190,47 @@
 OTA_KEY)
+%0A        return int(user_quota)
 %0A%0A    de
@@ -1273,83 +1273,119 @@
     
-# TODO: Check if exist geocoder:user_id:year_month , if yes sum up all of i
+%22%22%22 Recover the used quota for the user in the current month %22%22%22%0A        # Check for exceptions or redis timeou
 t%0A  
@@ -1592,24 +1592,34 @@
 nce_use(self
+, amount=1
 ):%0A        p
@@ -1617,20 +1617,189 @@
         
-pass
+%22%22%22 Increment the geocoder use in 1 %22%22%22%0A        # TODO Manage exceptions or timeout%0A        self.redis_conn.hincrby(self.__get_month_redis_key(), self.transaction_id,amount)
 %0A%0A    de
@@ -1875,36 +1875,63 @@
 ost=
-'local
+self.redis_
 host
-'
 , port=
-6379, db=5
+self.redis_port, db=self.redis_db
 )%0A  

Increment every time a row is georeferenced successfully