2016-11-23 23:38:47 +08:00
DO $ $
BEGIN
2018-01-17 01:28:54 +08:00
IF NOT EXISTS ( SELECT 1 FROM pg_type inner join pg_namespace ON ( pg_type . typnamespace = pg_namespace . oid )
WHERE pg_type . typname = ' service_type '
AND pg_namespace . nspname = ' cdb_dataservices_server ' ) THEN
2016-11-23 23:38:47 +08:00
CREATE TYPE cdb_dataservices_server . service_type AS ENUM (
' isolines ' ,
' hires_geocoder ' ,
2021-03-05 17:44:09 +08:00
' routing '
2016-11-23 23:38:47 +08:00
) ;
END IF ;
END $ $ ;
2016-11-11 01:12:24 +08:00
2016-11-23 23:38:47 +08:00
DO $ $
BEGIN
2018-01-17 01:28:54 +08:00
IF NOT EXISTS ( SELECT 1 FROM pg_type inner join pg_namespace ON ( pg_type . typnamespace = pg_namespace . oid )
WHERE pg_type . typname = ' service_quota_info '
AND pg_namespace . nspname = ' cdb_dataservices_server ' ) THEN
2016-11-25 00:46:07 +08:00
CREATE TYPE cdb_dataservices_server . service_quota_info AS (
2018-07-18 20:29:50 +08:00
service cdb_dataservices_server . service_type ,
monthly_quota NUMERIC ,
used_quota NUMERIC ,
soft_limit BOOLEAN ,
provider TEXT
) ;
END IF ;
END $ $ ;
DO $ $
BEGIN
IF NOT EXISTS ( SELECT 1 FROM pg_type inner join pg_namespace ON ( pg_type . typnamespace = pg_namespace . oid )
WHERE pg_type . typname = ' service_quota_info_batch '
AND pg_namespace . nspname = ' cdb_dataservices_server ' ) THEN
CREATE TYPE cdb_dataservices_server . service_quota_info_batch AS (
2016-11-23 23:38:47 +08:00
service cdb_dataservices_server . service_type ,
monthly_quota NUMERIC ,
used_quota NUMERIC ,
soft_limit BOOLEAN ,
2018-07-11 01:06:49 +08:00
provider TEXT ,
max_batch_size NUMERIC
2016-11-23 23:38:47 +08:00
) ;
END IF ;
END $ $ ;
2016-11-11 01:12:24 +08:00
2018-07-18 20:29:50 +08:00
2016-11-25 00:46:07 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . cdb_service_quota_info (
2016-11-09 01:29:06 +08:00
username TEXT ,
2016-11-11 02:23:01 +08:00
orgname TEXT )
2016-11-25 00:46:07 +08:00
RETURNS SETOF cdb_dataservices_server . service_quota_info 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
2018-07-18 20:29:50 +08:00
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
2018-07-18 20:29:50 +08:00
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
2018-07-18 20:29:50 +08:00
ret + = [ [ service , monthly_quota , used_quota , soft_limit , provider ] ]
2016-11-11 17:24:03 +08:00
2016-11-11 02:23:01 +08:00
return ret
2020-02-24 23:12:27 +08:00
$ $ LANGUAGE @ @ plpythonu @ @ STABLE PARALLEL RESTRICTED ;
2016-11-11 01:56:50 +08:00
2018-07-18 20:29:50 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . cdb_service_quota_info_batch (
username TEXT ,
orgname TEXT )
RETURNS SETOF cdb_dataservices_server . service_quota_info_batch AS $ $
from cartodb_services . bulk_geocoders import BATCH_GEOCODER_CLASS_BY_PROVIDER
from cartodb_services . tools import Logger , LoggerConfig
plpy . execute ( " SELECT cdb_dataservices_server._get_logger_config() " )
sqi = plpy . execute ( " SELECT * from cdb_dataservices_server.cdb_service_quota_info({0},{1}) " . format ( plpy . quote_nullable ( username ) , plpy . quote_nullable ( orgname ) ) )
ret = [ ]
for info in sqi :
if info [ ' service ' ] = = ' hires_geocoder ' :
provider = info [ ' provider ' ]
batch_geocoder_class = BATCH_GEOCODER_CLASS_BY_PROVIDER . get ( provider , None )
if batch_geocoder_class and hasattr ( batch_geocoder_class , ' MAX_BATCH_SIZE ' ) :
max_batch_size = batch_geocoder_class . MAX_BATCH_SIZE
else :
max_batch_size = 1
info [ ' max_batch_size ' ] = max_batch_size
else :
info [ ' max_batch_size ' ] = 1
ret + = [ [ info [ ' service ' ] , info [ ' monthly_quota ' ] , info [ ' used_quota ' ] , info [ ' soft_limit ' ] , info [ ' provider ' ] , info [ ' max_batch_size ' ] ] ]
return ret
2020-02-24 23:12:27 +08:00
$ $ LANGUAGE @ @ plpythonu @ @ STABLE PARALLEL RESTRICTED ;
2018-07-18 20:29:50 +08:00
2016-11-11 01:56:50 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . cdb_enough_quota (
username TEXT ,
orgname TEXT ,
2016-11-25 01:38:31 +08:00
service_ TEXT ,
2016-11-11 01:56:50 +08:00
input_size NUMERIC )
returns BOOLEAN AS $ $
DECLARE
2016-11-25 00:46:07 +08:00
params cdb_dataservices_server . service_quota_info ;
2016-11-11 01:56:50 +08:00
BEGIN
2016-11-11 02:23:39 +08:00
SELECT * INTO params
2016-11-25 00:46:07 +08:00
FROM cdb_dataservices_server . cdb_service_quota_info ( username , orgname ) AS p
2016-11-25 01:38:31 +08:00
WHERE p . service = service_ : : cdb_dataservices_server . service_type ;
2016-11-25 00:33:06 +08:00
RETURN params . soft_limit OR ( ( params . used_quota + input_size ) < = params . monthly_quota ) ;
2016-11-11 01:56:50 +08:00
END
2017-11-08 00:23:05 +08:00
$ $ LANGUAGE plpgsql STABLE PARALLEL RESTRICTED ;