DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'service_type') THEN CREATE TYPE cdb_dataservices_server.service_type AS ENUM ( 'isolines', 'hires_geocoder', 'routing', 'observatory', 'internal_geocoder' ); END IF; END $$; DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'service_params') THEN CREATE TYPE cdb_dataservices_server.service_params AS ( service cdb_dataservices_server.service_type, monthly_quota NUMERIC, used_quota NUMERIC, soft_limit BOOLEAN, provider TEXT ); END IF; END $$; CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_service_params( username TEXT, orgname TEXT) RETURNS SETOF cdb_dataservices_server.service_params AS $$ from cartodb_services.metrics.user import UserMetricsService from datetime import date plpy.execute("SELECT cdb_dataservices_server._connect_to_redis('{0}')".format(username)) redis_conn = GD["redis_connection_{0}".format(username)]['redis_metrics_connection'] today = date.today() ret = [] #-- Isolines service = 'isolines' 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)] user_service = UserMetricsService(user_isolines_config, redis_conn) monthly_quota = user_isolines_config.isolines_quota used_quota = user_service.used_quota(user_isolines_config.service_type, today) soft_limit = user_isolines_config.soft_isolines_limit provider = user_isolines_config.provider ret += [[service, monthly_quota, used_quota, soft_limit, provider]] #-- Hires Geocoder service = 'hires_geocoder' 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)] user_service = UserMetricsService(user_geocoder_config, redis_conn) monthly_quota = user_geocoder_config.geocoding_quota used_quota = user_service.used_quota(user_geocoder_config.service_type, today) soft_limit = user_geocoder_config.soft_geocoding_limit provider = user_geocoder_config.provider ret += [[service, monthly_quota, used_quota, soft_limit, provider]] #-- Routing service = 'routing' plpy.execute("SELECT cdb_dataservices_server._get_routing_config({0}, {1})".format(plpy.quote_nullable(username), plpy.quote_nullable(orgname))) user_routing_config = GD["user_routing_config_{0}".format(username)] user_service = UserMetricsService(user_routing_config, redis_conn) monthly_quota = user_routing_config.monthly_quota used_quota = user_service.used_quota(user_routing_config.service_type, today) soft_limit = user_routing_config.soft_limit provider = user_routing_config.provider ret += [[service, monthly_quota, used_quota, soft_limit, provider]] #-- Observatory service = 'observatory' plpy.execute("SELECT cdb_dataservices_server._get_obs_config({0}, {1})".format(plpy.quote_nullable(username), plpy.quote_nullable(orgname))) user_obs_config = GD["user_obs_config_{0}".format(username)] user_service = UserMetricsService(user_obs_config, redis_conn) monthly_quota = user_obs_config.monthly_quota used_quota = user_service.used_quota(user_obs_config.service_type, today) soft_limit = user_obs_config.soft_limit provider = user_obs_config.provider ret += [[service, monthly_quota, used_quota, soft_limit, provider]] #-- Internal geocoder service = 'internal_geocoder' plpy.execute("SELECT cdb_dataservices_server._get_internal_geocoder_config({0}, {1})".format(plpy.quote_nullable(username), plpy.quote_nullable(orgname))) user_geocoder_config = GD["user_internal_geocoder_config_{0}".format(username)] user_service = UserMetricsService(user_geocoder_config, redis_conn) monthly_quota = user_geocoder_config.monthly_quota used_quota = user_service.used_quota(user_geocoder_config.service_type, today) soft_limit = user_geocoder_config.soft_limit provider = user_geocoder_config.provider ret += [[service, monthly_quota, used_quota, soft_limit, provider]] return ret $$ LANGUAGE plpythonu; CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_enough_quota( username TEXT, orgname TEXT, service_ cdb_dataservices_server.service_type, 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) AS p WHERE p.service = service_; RETURN params.soft_limit OR params.monthly_quota IS NULL -- account for the internal_geocoder OR ((params.used_quota + input_size) <= params.monthly_quota); END $$ LANGUAGE plpgsql;