2016-11-11 01:12:24 +08:00
|
|
|
CREATE TYPE cdb_dataservices_server.service_params AS (
|
|
|
|
monthly_quota NUMERIC,
|
|
|
|
used_quota NUMERIC,
|
|
|
|
soft_limit BOOLEAN,
|
|
|
|
provider TEXT
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_service_params(
|
2016-11-09 01:29:06 +08:00
|
|
|
username TEXT,
|
|
|
|
orgname TEXT,
|
|
|
|
service TEXT)
|
2016-11-11 01:12:24 +08:00
|
|
|
RETURNS cdb_dataservices_server.service_params AS $$
|
2016-11-11 00:00:25 +08:00
|
|
|
import cartodb_services.metrics.quota as quota
|
2016-11-11 01:12:24 +08:00
|
|
|
from cartodb_services.metrics.user import UserMetricsService
|
|
|
|
from datetime import date
|
|
|
|
|
2016-11-09 01:29:06 +08:00
|
|
|
plpy.execute("SELECT cdb_dataservices_server._connect_to_redis('{0}')".format(username))
|
|
|
|
redis_conn = GD["redis_connection_{0}".format(username)]['redis_metrics_connection']
|
|
|
|
|
2016-11-11 01:40:18 +08:00
|
|
|
today = date.today()
|
|
|
|
|
2016-11-11 00:00:25 +08:00
|
|
|
if service == quota.Service.ISOLINES:
|
2016-11-09 01:29:06 +08:00
|
|
|
plpy.execute("SELECT cdb_dataservices_server._get_isolines_routing_config({0}, {1})".format(plpy.quote_nullable(username), plpy.quote_nullable(orgname)))
|
|
|
|
user_isolines_config = GD["user_isolines_routing_config_{0}".format(username)]
|
2016-11-11 01:12:24 +08:00
|
|
|
user_service = UserMetricsService(user_isolines_config, redis_conn)
|
2016-11-11 01:40:18 +08:00
|
|
|
|
|
|
|
monthly_quota = user_isolines_config.isolines_quota
|
|
|
|
used_quota = user_service.used_quota(user_isolines_config.service_type, today)
|
2016-11-11 01:12:24 +08:00
|
|
|
soft_limit = user_isolines_config.soft_isolines_limit
|
|
|
|
provider = user_isolines_config.provider
|
2016-11-11 01:40:18 +08:00
|
|
|
elif service == quota.Service.HIRES_GEOCODER:
|
2016-11-11 00:00:25 +08:00
|
|
|
plpy.execute("SELECT cdb_dataservices_server._get_geocoder_config({0}, {1})".format(plpy.quote_nullable(username), plpy.quote_nullable(orgname)))
|
|
|
|
user_geocoder_config = GD["user_geocoder_config_{0}".format(username)]
|
2016-11-11 01:40:18 +08:00
|
|
|
user_service = UserMetricsService(user_geocoder_config, redis_conn)
|
|
|
|
|
2016-11-11 00:00:25 +08:00
|
|
|
monthly_quota = user_geocoder_config.geocoding_quota
|
2016-11-11 01:40:18 +08:00
|
|
|
used_quota = user_service.used_quota(user_geocoder_config, today)
|
|
|
|
soft_limit = user_geocoder_config.soft_geocoding_limit
|
|
|
|
provider = user_geocoder_config.provider
|
2016-11-09 01:29:06 +08:00
|
|
|
else:
|
|
|
|
raise 'not implemented'
|
2016-11-11 00:00:25 +08:00
|
|
|
|
2016-11-11 01:40:18 +08:00
|
|
|
return [monthly_quota, used_quota, soft_limit, provider]
|
2016-11-09 01:47:22 +08:00
|
|
|
|
|
|
|
$$ LANGUAGE plpythonu;
|
2016-11-11 01:56:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_enough_quota(
|
|
|
|
username TEXT,
|
|
|
|
orgname TEXT,
|
|
|
|
service TEXT,
|
|
|
|
input_size NUMERIC)
|
|
|
|
returns BOOLEAN AS $$
|
|
|
|
DECLARE
|
|
|
|
params cdb_dataservices_server.service_params;
|
|
|
|
BEGIN
|
|
|
|
SELECT * INTO params FROM cdb_dataservices_server.cdb_service_params(username, orgname, service);
|
|
|
|
RETURN params.soft_limit OR ((params.used_quota + input_size) <= params.monthly_quota);
|
|
|
|
END
|
|
|
|
$$ LANGUAGE plpgsql;
|