2016-11-23 23:38:47 +08:00
|
|
|
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 $$;
|
2016-11-11 01:12:24 +08:00
|
|
|
|
2016-11-23 23:38:47 +08:00
|
|
|
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 $$;
|
2016-11-11 01:12:24 +08:00
|
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_service_params(
|
2016-11-09 01:29:06 +08:00
|
|
|
username TEXT,
|
2016-11-11 02:23:01 +08:00
|
|
|
orgname TEXT)
|
|
|
|
RETURNS SETOF cdb_dataservices_server.service_params AS $$
|
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 02:23:01 +08:00
|
|
|
ret = []
|
2016-11-11 01:40:18 +08:00
|
|
|
|
2016-11-11 02:23:01 +08:00
|
|
|
#-- 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)
|
2016-11-11 01:40:18 +08:00
|
|
|
|
2016-11-11 02:23:01 +08:00
|
|
|
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]]
|
2016-11-11 01:40:18 +08:00
|
|
|
|
2016-11-11 02:23:01 +08:00
|
|
|
#-- 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)
|
2016-11-11 00:00:25 +08:00
|
|
|
|
2016-11-11 02:23:01 +08:00
|
|
|
monthly_quota = user_geocoder_config.geocoding_quota
|
2016-11-23 23:40:00 +08:00
|
|
|
used_quota = user_service.used_quota(user_geocoder_config.service_type, today)
|
2016-11-11 02:23:01 +08:00
|
|
|
soft_limit = user_geocoder_config.soft_geocoding_limit
|
|
|
|
provider = user_geocoder_config.provider
|
|
|
|
ret += [[service, monthly_quota, used_quota, soft_limit, provider]]
|
2016-11-09 01:47:22 +08:00
|
|
|
|
2016-11-11 17:24:03 +08:00
|
|
|
#-- 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
|
2016-11-23 23:40:00 +08:00
|
|
|
used_quota = user_service.used_quota(user_routing_config.service_type, today)
|
2016-11-11 17:24:03 +08:00
|
|
|
soft_limit = user_routing_config.soft_limit
|
|
|
|
provider = user_routing_config.provider
|
|
|
|
ret += [[service, monthly_quota, used_quota, soft_limit, provider]]
|
|
|
|
|
2016-11-11 18:27:57 +08:00
|
|
|
#-- 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
|
2016-11-23 23:40:00 +08:00
|
|
|
used_quota = user_service.used_quota(user_obs_config.service_type, today)
|
2016-11-11 18:27:57 +08:00
|
|
|
soft_limit = user_obs_config.soft_limit
|
|
|
|
provider = user_obs_config.provider
|
|
|
|
ret += [[service, monthly_quota, used_quota, soft_limit, provider]]
|
|
|
|
|
2016-11-24 00:07:23 +08:00
|
|
|
#-- 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]]
|
|
|
|
|
2016-11-11 02:23:01 +08:00
|
|
|
return ret
|
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,
|
2016-11-11 02:23:39 +08:00
|
|
|
service_ cdb_dataservices_server.service_type,
|
2016-11-11 01:56:50 +08:00
|
|
|
input_size NUMERIC)
|
|
|
|
returns BOOLEAN AS $$
|
|
|
|
DECLARE
|
|
|
|
params cdb_dataservices_server.service_params;
|
|
|
|
BEGIN
|
2016-11-11 02:23:39 +08:00
|
|
|
SELECT * INTO params
|
|
|
|
FROM cdb_dataservices_server.cdb_service_params(username, orgname) AS p
|
|
|
|
WHERE p.service = service_;
|
2016-11-24 01:10:44 +08:00
|
|
|
RETURN params.soft_limit
|
|
|
|
OR params.monthly_quota IS NULL -- account for the internal_geocoder
|
|
|
|
OR ((params.used_quota + input_size) <= params.monthly_quota);
|
2016-11-11 01:56:50 +08:00
|
|
|
END
|
|
|
|
$$ LANGUAGE plpgsql;
|