2016-05-11 17:50:21 +08:00
--
-- Observatory connection config
--
-- The purpose of this function is provide to the PL/Proxy functions
-- the connection string needed to connect with the current production database
CREATE OR REPLACE FUNCTION cdb_dataservices_server . _obs_server_conn_str (
username TEXT ,
orgname TEXT )
RETURNS text AS $ $
plpy . execute ( " SELECT cdb_dataservices_server._connect_to_redis('{0}') " . format ( username ) )
redis_conn = GD [ " redis_connection_{0} " . format ( username ) ] [ ' redis_metrics_connection ' ]
plpy . execute ( " SELECT cdb_dataservices_server._get_obs_snapshot_config({0}, {1}) " . format ( plpy . quote_nullable ( username ) , plpy . quote_nullable ( orgname ) ) )
2016-11-07 17:31:53 +08:00
user_obs_config = GD [ " user_obs_snapshot_config_{0} " . format ( username ) ]
2016-05-11 17:50:21 +08:00
2016-11-02 16:58:35 +08:00
return user_obs_config . connection_str
2017-11-08 00:23:05 +08:00
$ $ LANGUAGE plpythonu STABLE PARALLEL RESTRICTED ;
2016-05-11 17:50:21 +08:00
2016-05-16 16:24:22 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . _OBS_GetDemographicSnapshotJSON (
username TEXT ,
orgname TEXT ,
geom geometry ( Geometry , 4326 ) ,
time_span TEXT DEFAULT NULL ,
geometry_level TEXT DEFAULT NULL )
RETURNS json AS $ $
CONNECT cdb_dataservices_server . _obs_server_conn_str ( username , orgname ) ;
SELECT cdb_observatory . OBS_GetDemographicSnapshot ( geom , time_span , geometry_level ) ;
2017-11-07 22:32:07 +08:00
$ $ LANGUAGE plproxy VOLATILE PARALLEL UNSAFE ;
2016-05-16 16:24:22 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . obs_get_demographic_snapshot (
username TEXT ,
orgname TEXT ,
geom geometry ( Geometry , 4326 ) ,
time_span TEXT DEFAULT NULL ,
geometry_level TEXT DEFAULT NULL )
RETURNS json AS $ $
2016-10-29 00:12:04 +08:00
from cartodb_services . metrics import metrics
2016-05-16 16:24:22 +08:00
from cartodb_services . metrics import QuotaService
2016-08-02 23:28:48 +08:00
from cartodb_services . tools import Logger , LoggerConfig
2016-05-16 16:24:22 +08:00
import json
plpy . execute ( " SELECT cdb_dataservices_server._connect_to_redis('{0}') " . format ( username ) )
redis_conn = GD [ " redis_connection_{0} " . format ( username ) ] [ ' redis_metrics_connection ' ]
plpy . execute ( " SELECT cdb_dataservices_server._get_obs_snapshot_config({0}, {1}) " . format ( plpy . quote_nullable ( username ) , plpy . quote_nullable ( orgname ) ) )
2016-11-07 17:31:53 +08:00
user_obs_config = GD [ " user_obs_snapshot_config_{0} " . format ( username ) ]
2016-05-16 16:24:22 +08:00
2016-08-03 23:37:40 +08:00
plpy . execute ( " SELECT cdb_dataservices_server._get_logger_config() " )
logger_config = GD [ " logger_config " ]
2016-08-02 23:28:48 +08:00
logger = Logger ( logger_config )
2016-11-02 16:58:35 +08:00
quota_service = QuotaService ( user_obs_config , redis_conn )
2016-05-16 16:24:22 +08:00
if not quota_service . check_user_quota ( ) :
2016-08-02 01:04:56 +08:00
raise Exception ( ' You have reached the limit of your quota ' )
2016-05-16 16:24:22 +08:00
2016-11-02 16:58:35 +08:00
with metrics ( ' obs_getdemographicsnapshot ' , user_obs_config , logger ) :
2016-10-29 00:12:04 +08:00
try :
obs_plan = plpy . prepare ( " SELECT cdb_dataservices_server._OBS_GetDemographicSnapshotJSON($1, $2, $3, $4, $5) as snapshot; " , [ " text " , " text " , " geometry(Geometry, 4326) " , " text " , " text " ] )
result = plpy . execute ( obs_plan , [ username , orgname , geom , time_span , geometry_level ] )
if result :
quota_service . increment_success_service_use ( )
return result [ 0 ] [ ' snapshot ' ]
else :
quota_service . increment_empty_service_use ( )
return None
except BaseException as e :
import sys
quota_service . increment_failed_service_use ( )
logger . error ( ' Error trying to obs_get_demographic_snapshot ' , sys . exc_info ( ) , data = { " username " : username , " orgname " : orgname } )
raise Exception ( ' Error trying to obs_get_demographic_snapshot ' )
finally :
quota_service . increment_total_service_use ( )
2017-11-08 00:23:05 +08:00
$ $ LANGUAGE plpythonu STABLE PARALLEL RESTRICTED ;
2016-05-16 16:24:22 +08:00
2016-05-11 17:50:21 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . _OBS_GetDemographicSnapshot (
username TEXT ,
orgname TEXT ,
geom geometry ( Geometry , 4326 ) ,
time_span TEXT DEFAULT NULL ,
geometry_level TEXT DEFAULT NULL )
2016-05-13 00:17:26 +08:00
RETURNS SETOF json AS $ $
2016-05-11 17:50:21 +08:00
CONNECT cdb_dataservices_server . _obs_server_conn_str ( username , orgname ) ;
2016-05-13 00:17:26 +08:00
SELECT * FROM cdb_observatory . OBS_GetDemographicSnapshot ( geom , time_span , geometry_level ) ;
2017-11-07 22:32:07 +08:00
$ $ LANGUAGE plproxy VOLATILE PARALLEL UNSAFE ;
2016-05-11 17:50:21 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . OBS_GetDemographicSnapshot (
username TEXT ,
orgname TEXT ,
geom geometry ( Geometry , 4326 ) ,
time_span TEXT DEFAULT NULL ,
geometry_level TEXT DEFAULT NULL )
2016-05-13 00:17:26 +08:00
RETURNS SETOF JSON AS $ $
2016-10-29 00:12:04 +08:00
from cartodb_services . metrics import metrics
2016-05-11 17:50:21 +08:00
from cartodb_services . metrics import QuotaService
2016-08-02 23:28:48 +08:00
from cartodb_services . tools import Logger , LoggerConfig
2016-05-11 17:50:21 +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 ' ]
plpy . execute ( " SELECT cdb_dataservices_server._get_obs_snapshot_config({0}, {1}) " . format ( plpy . quote_nullable ( username ) , plpy . quote_nullable ( orgname ) ) )
2016-11-07 17:31:53 +08:00
user_obs_config = GD [ " user_obs_snapshot_config_{0} " . format ( username ) ]
2016-05-11 17:50:21 +08:00
2016-08-03 23:37:40 +08:00
plpy . execute ( " SELECT cdb_dataservices_server._get_logger_config() " )
logger_config = GD [ " logger_config " ]
2016-08-02 23:28:48 +08:00
logger = Logger ( logger_config )
2016-11-02 16:58:35 +08:00
quota_service = QuotaService ( user_obs_config , redis_conn )
2016-05-11 17:50:21 +08:00
if not quota_service . check_user_quota ( ) :
2016-08-02 01:04:56 +08:00
raise Exception ( ' You have reached the limit of your quota ' )
2016-05-11 17:50:21 +08:00
2016-11-02 16:58:35 +08:00
with metrics ( ' obs_getdemographicsnapshot ' , user_obs_config , logger ) :
2016-10-29 00:12:04 +08:00
try :
obs_plan = plpy . prepare ( " SELECT cdb_dataservices_server._OBS_GetDemographicSnapshot($1, $2, $3, $4, $5) as snapshot; " , [ " text " , " text " , " geometry(Geometry, 4326) " , " text " , " text " ] )
result = plpy . execute ( obs_plan , [ username , orgname , geom , time_span , geometry_level ] )
if result :
resp = [ ]
for element in result :
value = element [ ' snapshot ' ]
resp . append ( value )
quota_service . increment_success_service_use ( )
return resp
else :
quota_service . increment_empty_service_use ( )
return [ ]
except BaseException as e :
import sys
quota_service . increment_failed_service_use ( )
logger . error ( ' Error trying to obs_get_demographic_snapshot ' , sys . exc_info ( ) , data = { " username " : username , " orgname " : orgname } )
raise Exception ( ' Error trying to obs_get_demographic_snapshot ' )
finally :
quota_service . increment_total_service_use ( )
2017-11-08 00:23:05 +08:00
$ $ LANGUAGE plpythonu STABLE PARALLEL RESTRICTED ;
2016-05-16 16:24:22 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . _OBS_GetSegmentSnapshotJSON (
username TEXT ,
orgname TEXT ,
geom geometry ( Geometry , 4326 ) ,
geometry_level TEXT DEFAULT NULL )
RETURNS json AS $ $
CONNECT cdb_dataservices_server . _obs_server_conn_str ( username , orgname ) ;
SELECT cdb_observatory . OBS_GetSegmentSnapshot ( geom , geometry_level ) ;
2017-11-07 22:32:07 +08:00
$ $ LANGUAGE plproxy VOLATILE PARALLEL UNSAFE ;
2016-05-16 16:24:22 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . obs_get_segment_snapshot (
username TEXT ,
orgname TEXT ,
geom geometry ( Geometry , 4326 ) ,
geometry_level TEXT DEFAULT NULL )
RETURNS json AS $ $
2016-10-29 00:12:04 +08:00
from cartodb_services . metrics import metrics
2016-05-16 16:24:22 +08:00
from cartodb_services . metrics import QuotaService
2016-08-02 23:28:48 +08:00
from cartodb_services . tools import Logger , LoggerConfig
2016-05-16 16:24:22 +08:00
import json
plpy . execute ( " SELECT cdb_dataservices_server._connect_to_redis('{0}') " . format ( username ) )
redis_conn = GD [ " redis_connection_{0} " . format ( username ) ] [ ' redis_metrics_connection ' ]
plpy . execute ( " SELECT cdb_dataservices_server._get_obs_snapshot_config({0}, {1}) " . format ( plpy . quote_nullable ( username ) , plpy . quote_nullable ( orgname ) ) )
2016-11-07 17:31:53 +08:00
user_obs_config = GD [ " user_obs_snapshot_config_{0} " . format ( username ) ]
2016-05-16 16:24:22 +08:00
2016-08-03 23:37:40 +08:00
plpy . execute ( " SELECT cdb_dataservices_server._get_logger_config() " )
logger_config = GD [ " logger_config " ]
2016-08-02 23:28:48 +08:00
logger = Logger ( logger_config )
2016-11-02 16:58:35 +08:00
quota_service = QuotaService ( user_obs_config , redis_conn )
2016-05-16 16:24:22 +08:00
if not quota_service . check_user_quota ( ) :
2016-08-02 01:04:56 +08:00
raise Exception ( ' You have reached the limit of your quota ' )
2016-05-16 16:24:22 +08:00
2016-11-02 16:58:35 +08:00
with metrics ( ' obs_getsegmentsnapshot ' , user_obs_config , logger ) :
2016-10-29 00:12:04 +08:00
try :
obs_plan = plpy . prepare ( " SELECT cdb_dataservices_server._OBS_GetSegmentSnapshotJSON($1, $2, $3, $4) as snapshot; " , [ " text " , " text " , " geometry(Geometry, 4326) " , " text " ] )
result = plpy . execute ( obs_plan , [ username , orgname , geom , geometry_level ] )
if result :
quota_service . increment_success_service_use ( )
return result [ 0 ] [ ' snapshot ' ]
else :
quota_service . increment_empty_service_use ( )
return None
except BaseException as e :
import sys
quota_service . increment_failed_service_use ( )
logger . error ( ' Error trying to obs_get_segment_snapshot ' , sys . exc_info ( ) , data = { " username " : username , " orgname " : orgname } )
raise Exception ( ' Error trying to obs_get_segment_snapshot ' )
finally :
quota_service . increment_total_service_use ( )
2017-11-08 00:23:05 +08:00
$ $ LANGUAGE plpythonu STABLE PARALLEL RESTRICTED ;
2016-05-11 17:50:21 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . _OBS_GetSegmentSnapshot (
username TEXT ,
orgname TEXT ,
geom geometry ( Geometry , 4326 ) ,
geometry_level TEXT DEFAULT NULL )
2016-05-13 00:17:26 +08:00
RETURNS SETOF json AS $ $
2016-05-11 17:50:21 +08:00
CONNECT cdb_dataservices_server . _obs_server_conn_str ( username , orgname ) ;
2016-05-13 00:17:26 +08:00
SELECT * FROM cdb_observatory . OBS_GetSegmentSnapshot ( geom , geometry_level ) ;
2017-11-07 22:32:07 +08:00
$ $ LANGUAGE plproxy VOLATILE PARALLEL UNSAFE ;
2016-05-11 17:50:21 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . OBS_GetSegmentSnapshot (
username TEXT ,
orgname TEXT ,
geom geometry ( Geometry , 4326 ) ,
geometry_level TEXT DEFAULT NULL )
2016-05-13 00:17:26 +08:00
RETURNS SETOF JSON AS $ $
2016-10-29 00:12:04 +08:00
from cartodb_services . metrics import metrics
2016-05-11 17:50:21 +08:00
from cartodb_services . metrics import QuotaService
2016-08-02 23:28:48 +08:00
from cartodb_services . tools import Logger , LoggerConfig
2016-05-11 17:50:21 +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 ' ]
plpy . execute ( " SELECT cdb_dataservices_server._get_obs_snapshot_config({0}, {1}) " . format ( plpy . quote_nullable ( username ) , plpy . quote_nullable ( orgname ) ) )
2016-11-07 17:31:53 +08:00
user_obs_config = GD [ " user_obs_snapshot_config_{0} " . format ( username ) ]
2016-05-11 17:50:21 +08:00
2016-08-03 23:37:40 +08:00
plpy . execute ( " SELECT cdb_dataservices_server._get_logger_config() " )
logger_config = GD [ " logger_config " ]
2016-08-02 23:28:48 +08:00
logger = Logger ( logger_config )
2016-11-02 16:58:35 +08:00
quota_service = QuotaService ( user_obs_config , redis_conn )
2016-05-11 17:50:21 +08:00
if not quota_service . check_user_quota ( ) :
2016-08-02 01:04:56 +08:00
raise Exception ( ' You have reached the limit of your quota ' )
2016-05-11 17:50:21 +08:00
2016-11-02 16:58:35 +08:00
with metrics ( ' obs_getsegmentsnapshot ' , user_obs_config , logger ) :
2016-10-29 00:12:04 +08:00
try :
obs_plan = plpy . prepare ( " SELECT * FROM cdb_dataservices_server._OBS_GetSegmentSnapshot($1, $2, $3, $4) as snapshot; " , [ " text " , " text " , " geometry(Geometry, 4326) " , " text " ] )
result = plpy . execute ( obs_plan , [ username , orgname , geom , geometry_level ] )
if result :
resp = [ ]
for element in result :
value = element [ ' snapshot ' ]
resp . append ( value )
quota_service . increment_success_service_use ( )
return resp
else :
quota_service . increment_empty_service_use ( )
return [ ]
except BaseException as e :
import sys
quota_service . increment_failed_service_use ( )
logger . error ( ' Error trying to OBS_GetSegmentSnapshot ' , sys . exc_info ( ) , data = { " username " : username , " orgname " : orgname } )
raise Exception ( ' Error trying to OBS_GetSegmentSnapshot ' )
finally :
quota_service . increment_total_service_use ( )
2017-11-08 00:23:05 +08:00
$ $ LANGUAGE plpythonu STABLE PARALLEL RESTRICTED ;
2016-05-11 17:50:21 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . _OBS_GetMeasure (
username TEXT ,
orgname TEXT ,
geom geometry ( Geometry , 4326 ) ,
measure_id TEXT ,
2016-07-26 03:41:48 +08:00
normalize TEXT DEFAULT NULL ,
2016-05-11 17:50:21 +08:00
boundary_id TEXT DEFAULT NULL ,
time_span TEXT DEFAULT NULL )
RETURNS NUMERIC AS $ $
CONNECT cdb_dataservices_server . _obs_server_conn_str ( username , orgname ) ;
SELECT cdb_observatory . OBS_GetMeasure ( geom , measure_id , normalize , boundary_id , time_span ) ;
2017-11-07 22:32:07 +08:00
$ $ LANGUAGE plproxy VOLATILE PARALLEL UNSAFE ;
2016-05-11 17:50:21 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . OBS_GetMeasure (
username TEXT ,
orgname TEXT ,
geom geometry ( Geometry , 4326 ) ,
measure_id TEXT ,
2016-07-26 03:41:48 +08:00
normalize TEXT DEFAULT NULL ,
2016-05-11 17:50:21 +08:00
boundary_id TEXT DEFAULT NULL ,
time_span TEXT DEFAULT NULL )
RETURNS NUMERIC AS $ $
2016-10-29 00:12:04 +08:00
from cartodb_services . metrics import metrics
2016-05-11 17:50:21 +08:00
from cartodb_services . metrics import QuotaService
2016-08-02 23:28:48 +08:00
from cartodb_services . tools import Logger , LoggerConfig
2016-05-11 17:50:21 +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 ' ]
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 ) ]
2016-08-03 23:37:40 +08:00
plpy . execute ( " SELECT cdb_dataservices_server._get_logger_config() " )
logger_config = GD [ " logger_config " ]
2016-08-02 23:28:48 +08:00
logger = Logger ( logger_config )
2016-05-11 17:50:21 +08:00
quota_service = QuotaService ( user_obs_config , redis_conn )
if not quota_service . check_user_quota ( ) :
2016-08-02 01:04:56 +08:00
raise Exception ( ' You have reached the limit of your quota ' )
2016-05-11 17:50:21 +08:00
2016-11-02 16:58:35 +08:00
with metrics ( ' obs_getmeasure ' , user_obs_config , logger ) :
2016-10-29 00:12:04 +08:00
try :
obs_plan = plpy . prepare ( " SELECT cdb_dataservices_server._OBS_GetMeasure($1, $2, $3, $4, $5, $6, $7) as measure; " , [ " text " , " text " , " geometry(Geometry, 4326) " , " text " , " text " , " text " , " text " ] )
result = plpy . execute ( obs_plan , [ username , orgname , geom , measure_id , normalize , boundary_id , time_span ] )
if result :
quota_service . increment_success_service_use ( )
return result [ 0 ] [ ' measure ' ]
else :
quota_service . increment_empty_service_use ( )
return None
except BaseException as e :
import sys
quota_service . increment_failed_service_use ( )
logger . error ( ' Error trying to OBS_GetMeasure ' , sys . exc_info ( ) , data = { " username " : username , " orgname " : orgname } )
raise Exception ( ' Error trying to OBS_GetMeasure ' )
finally :
quota_service . increment_total_service_use ( )
2017-11-08 00:23:05 +08:00
$ $ LANGUAGE plpythonu STABLE PARALLEL RESTRICTED ;
2016-05-11 17:50:21 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . _OBS_GetCategory (
username TEXT ,
orgname TEXT ,
geom geometry ( Geometry , 4326 ) ,
category_id TEXT ,
boundary_id TEXT DEFAULT NULL ,
time_span TEXT DEFAULT NULL )
RETURNS TEXT AS $ $
CONNECT cdb_dataservices_server . _obs_server_conn_str ( username , orgname ) ;
SELECT cdb_observatory . OBS_GetCategory ( geom , category_id , boundary_id , time_span ) ;
2017-11-07 22:32:07 +08:00
$ $ LANGUAGE plproxy VOLATILE PARALLEL UNSAFE ;
2016-05-11 17:50:21 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . OBS_GetCategory (
username TEXT ,
orgname TEXT ,
geom geometry ( Geometry , 4326 ) ,
category_id TEXT ,
boundary_id TEXT DEFAULT NULL ,
time_span TEXT DEFAULT NULL )
RETURNS TEXT AS $ $
2016-10-29 00:12:04 +08:00
from cartodb_services . metrics import metrics
2016-05-11 17:50:21 +08:00
from cartodb_services . metrics import QuotaService
2016-08-02 23:28:48 +08:00
from cartodb_services . tools import Logger , LoggerConfig
2016-05-11 17:50:21 +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 ' ]
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 ) ]
2016-08-03 23:37:40 +08:00
plpy . execute ( " SELECT cdb_dataservices_server._get_logger_config() " )
logger_config = GD [ " logger_config " ]
2016-08-02 23:28:48 +08:00
logger = Logger ( logger_config )
2016-05-11 17:50:21 +08:00
quota_service = QuotaService ( user_obs_config , redis_conn )
if not quota_service . check_user_quota ( ) :
2016-08-02 01:04:56 +08:00
raise Exception ( ' You have reached the limit of your quota ' )
2016-05-11 17:50:21 +08:00
2016-11-02 16:58:35 +08:00
with metrics ( ' obs_getcategory ' , user_obs_config , logger ) :
2016-10-29 00:12:04 +08:00
try :
obs_plan = plpy . prepare ( " SELECT cdb_dataservices_server._OBS_GetCategory($1, $2, $3, $4, $5, $6) as category; " , [ " text " , " text " , " geometry(Geometry, 4326) " , " text " , " text " , " text " ] )
result = plpy . execute ( obs_plan , [ username , orgname , geom , category_id , boundary_id , time_span ] )
if result :
quota_service . increment_success_service_use ( )
return result [ 0 ] [ ' category ' ]
else :
quota_service . increment_empty_service_use ( )
return None
except BaseException as e :
import sys
quota_service . increment_failed_service_use ( )
logger . error ( ' Error trying to OBS_GetCategory ' , sys . exc_info ( ) , data = { " username " : username , " orgname " : orgname } )
raise Exception ( ' Error trying to OBS_GetCategory ' )
finally :
quota_service . increment_total_service_use ( )
2017-11-08 00:23:05 +08:00
$ $ LANGUAGE plpythonu STABLE PARALLEL RESTRICTED ;
2016-05-11 17:50:21 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . _OBS_GetUSCensusMeasure (
username TEXT ,
orgname TEXT ,
geom geometry ( Geometry , 4326 ) ,
name TEXT ,
2016-07-26 03:41:48 +08:00
normalize TEXT DEFAULT NULL ,
2016-05-11 17:50:21 +08:00
boundary_id TEXT DEFAULT NULL ,
time_span TEXT DEFAULT NULL )
RETURNS NUMERIC AS $ $
CONNECT cdb_dataservices_server . _obs_server_conn_str ( username , orgname ) ;
SELECT cdb_observatory . OBS_GetUSCensusMeasure ( geom , name , normalize , boundary_id , time_span ) ;
2017-11-07 22:32:07 +08:00
$ $ LANGUAGE plproxy VOLATILE PARALLEL UNSAFE ;
2016-05-11 17:50:21 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . OBS_GetUSCensusMeasure (
username TEXT ,
orgname TEXT ,
geom geometry ( Geometry , 4326 ) ,
name TEXT ,
2016-07-26 03:41:48 +08:00
normalize TEXT DEFAULT NULL ,
2016-05-11 17:50:21 +08:00
boundary_id TEXT DEFAULT NULL ,
time_span TEXT DEFAULT NULL )
RETURNS NUMERIC AS $ $
2016-10-29 00:12:04 +08:00
from cartodb_services . metrics import metrics
2016-05-11 17:50:21 +08:00
from cartodb_services . metrics import QuotaService
2016-08-02 23:28:48 +08:00
from cartodb_services . tools import Logger , LoggerConfig
2016-05-11 17:50:21 +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 ' ]
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 ) ]
2016-08-03 23:37:40 +08:00
plpy . execute ( " SELECT cdb_dataservices_server._get_logger_config() " )
logger_config = GD [ " logger_config " ]
2016-08-02 23:28:48 +08:00
logger = Logger ( logger_config )
2016-05-11 17:50:21 +08:00
quota_service = QuotaService ( user_obs_config , redis_conn )
if not quota_service . check_user_quota ( ) :
2016-08-02 01:04:56 +08:00
raise Exception ( ' You have reached the limit of your quota ' )
2016-05-11 17:50:21 +08:00
2016-11-02 16:58:35 +08:00
with metrics ( ' obs_getuscensusmeasure ' , user_obs_config , logger ) :
2016-10-29 00:12:04 +08:00
try :
obs_plan = plpy . prepare ( " SELECT cdb_dataservices_server._OBS_GetUSCensusMeasure($1, $2, $3, $4, $5, $6, $7) as census_measure; " , [ " text " , " text " , " geometry(Geometry, 4326) " , " text " , " text " , " text " , " text " ] )
result = plpy . execute ( obs_plan , [ username , orgname , geom , name , normalize , boundary_id , time_span ] )
if result :
quota_service . increment_success_service_use ( )
return result [ 0 ] [ ' census_measure ' ]
else :
quota_service . increment_empty_service_use ( )
return None
except BaseException as e :
import sys
quota_service . increment_failed_service_use ( )
logger . error ( ' Error trying to OBS_GetUSCensusMeasure ' , sys . exc_info ( ) , data = { " username " : username , " orgname " : orgname } )
raise Exception ( ' Error trying to OBS_GetUSCensusMeasure ' )
finally :
quota_service . increment_total_service_use ( )
2017-11-08 00:23:05 +08:00
$ $ LANGUAGE plpythonu STABLE PARALLEL RESTRICTED ;
2016-05-11 17:50:21 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . _OBS_GetUSCensusCategory (
username TEXT ,
orgname TEXT ,
geom geometry ( Geometry , 4326 ) ,
name TEXT ,
boundary_id TEXT DEFAULT NULL ,
time_span TEXT DEFAULT NULL )
RETURNS TEXT AS $ $
CONNECT cdb_dataservices_server . _obs_server_conn_str ( username , orgname ) ;
SELECT cdb_observatory . OBS_GetUSCensusCategory ( geom , name , boundary_id , time_span ) ;
2017-11-07 22:32:07 +08:00
$ $ LANGUAGE plproxy VOLATILE PARALLEL UNSAFE ;
2016-05-11 17:50:21 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . OBS_GetUSCensusCategory (
username TEXT ,
orgname TEXT ,
geom geometry ( Geometry , 4326 ) ,
name TEXT ,
boundary_id TEXT DEFAULT NULL ,
time_span TEXT DEFAULT NULL )
RETURNS TEXT AS $ $
2016-10-29 00:12:04 +08:00
from cartodb_services . metrics import metrics
2016-05-11 17:50:21 +08:00
from cartodb_services . metrics import QuotaService
2016-08-02 23:28:48 +08:00
from cartodb_services . tools import Logger , LoggerConfig
2016-05-11 17:50:21 +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 ' ]
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 ) ]
2016-08-03 23:37:40 +08:00
plpy . execute ( " SELECT cdb_dataservices_server._get_logger_config() " )
logger_config = GD [ " logger_config " ]
2016-08-02 23:28:48 +08:00
logger = Logger ( logger_config )
2016-05-11 17:50:21 +08:00
quota_service = QuotaService ( user_obs_config , redis_conn )
if not quota_service . check_user_quota ( ) :
2016-08-02 01:04:56 +08:00
raise Exception ( ' You have reached the limit of your quota ' )
2016-05-11 17:50:21 +08:00
2016-11-02 16:58:35 +08:00
with metrics ( ' obs_getuscensuscategory ' , user_obs_config , logger ) :
2016-10-29 00:12:04 +08:00
try :
obs_plan = plpy . prepare ( " SELECT cdb_dataservices_server._OBS_GetUSCensusCategory($1, $2, $3, $4, $5, $6) as census_category; " , [ " text " , " text " , " geometry(Geometry, 4326) " , " text " , " text " , " text " ] )
result = plpy . execute ( obs_plan , [ username , orgname , geom , name , boundary_id , time_span ] )
if result :
quota_service . increment_success_service_use ( )
return result [ 0 ] [ ' census_category ' ]
else :
quota_service . increment_empty_service_use ( )
return None
except BaseException as e :
import sys
quota_service . increment_failed_service_use ( )
logger . error ( ' Error trying to OBS_GetUSCensusCategory ' , sys . exc_info ( ) , data = { " username " : username , " orgname " : orgname } )
raise Exception ( ' Error trying to OBS_GetUSCensusCategory ' )
finally :
quota_service . increment_total_service_use ( )
2017-11-08 00:23:05 +08:00
$ $ LANGUAGE plpythonu STABLE PARALLEL RESTRICTED ;
2016-05-11 17:50:21 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . _OBS_GetPopulation (
username TEXT ,
orgname TEXT ,
geom geometry ( Geometry , 4326 ) ,
2016-07-26 03:41:48 +08:00
normalize TEXT DEFAULT NULL ,
2016-05-11 17:50:21 +08:00
boundary_id TEXT DEFAULT NULL ,
time_span TEXT DEFAULT NULL )
RETURNS NUMERIC AS $ $
CONNECT cdb_dataservices_server . _obs_server_conn_str ( username , orgname ) ;
SELECT cdb_observatory . OBS_GetPopulation ( geom , normalize , boundary_id , time_span ) ;
2017-11-07 22:32:07 +08:00
$ $ LANGUAGE plproxy VOLATILE PARALLEL UNSAFE ;
2016-05-11 17:50:21 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . OBS_GetPopulation (
username TEXT ,
orgname TEXT ,
geom geometry ( Geometry , 4326 ) ,
2016-07-26 03:41:48 +08:00
normalize TEXT DEFAULT NULL ,
2016-05-11 17:50:21 +08:00
boundary_id TEXT DEFAULT NULL ,
time_span TEXT DEFAULT NULL )
RETURNS NUMERIC AS $ $
2016-10-29 00:12:04 +08:00
from cartodb_services . metrics import metrics
2016-05-11 17:50:21 +08:00
from cartodb_services . metrics import QuotaService
2016-08-02 23:28:48 +08:00
from cartodb_services . tools import Logger , LoggerConfig
2016-05-11 17:50:21 +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 ' ]
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 ) ]
2016-08-03 23:37:40 +08:00
plpy . execute ( " SELECT cdb_dataservices_server._get_logger_config() " )
logger_config = GD [ " logger_config " ]
2016-08-02 23:28:48 +08:00
logger = Logger ( logger_config )
2016-05-11 17:50:21 +08:00
quota_service = QuotaService ( user_obs_config , redis_conn )
if not quota_service . check_user_quota ( ) :
2016-08-02 01:04:56 +08:00
raise Exception ( ' You have reached the limit of your quota ' )
2016-05-11 17:50:21 +08:00
2016-11-02 16:58:35 +08:00
with metrics ( ' obs_getpopulation ' , user_obs_config , logger ) :
2016-10-29 00:12:04 +08:00
try :
obs_plan = plpy . prepare ( " SELECT cdb_dataservices_server._OBS_GetPopulation($1, $2, $3, $4, $5, $6) as population; " , [ " text " , " text " , " geometry(Geometry, 4326) " , " text " , " text " , " text " ] )
result = plpy . execute ( obs_plan , [ username , orgname , geom , normalize , boundary_id , time_span ] )
if result :
quota_service . increment_success_service_use ( )
return result [ 0 ] [ ' population ' ]
else :
quota_service . increment_empty_service_use ( )
return None
except BaseException as e :
import sys
quota_service . increment_failed_service_use ( )
logger . error ( ' Error trying to OBS_GetPopulation ' , sys . exc_info ( ) , data = { " username " : username , " orgname " : orgname } )
raise Exception ( ' Error trying to OBS_GetPopulation ' )
finally :
quota_service . increment_total_service_use ( )
2017-11-08 00:23:05 +08:00
$ $ LANGUAGE plpythonu STABLE PARALLEL RESTRICTED ;
2016-05-31 00:13:45 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . _OBS_GetMeasureById (
username TEXT ,
orgname TEXT ,
geom_ref TEXT ,
measure_id TEXT ,
boundary_id TEXT ,
time_span TEXT DEFAULT NULL )
RETURNS NUMERIC AS $ $
CONNECT cdb_dataservices_server . _obs_server_conn_str ( username , orgname ) ;
SELECT cdb_observatory . OBS_GetMeasureById ( geom_ref , measure_id , boundary_id , time_span ) ;
2017-11-07 22:32:07 +08:00
$ $ LANGUAGE plproxy VOLATILE PARALLEL UNSAFE ;
2016-05-31 00:13:45 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . OBS_GetMeasureById (
username TEXT ,
orgname TEXT ,
geom_ref TEXT ,
measure_id TEXT ,
boundary_id TEXT ,
time_span TEXT DEFAULT NULL )
RETURNS NUMERIC AS $ $
2016-10-29 00:12:04 +08:00
from cartodb_services . metrics import metrics
2016-05-31 00:13:45 +08:00
from cartodb_services . metrics import QuotaService
2016-08-02 23:28:48 +08:00
from cartodb_services . tools import Logger , LoggerConfig
2016-05-31 00:13:45 +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 ' ]
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 ) ]
2016-08-03 23:37:40 +08:00
plpy . execute ( " SELECT cdb_dataservices_server._get_logger_config() " )
logger_config = GD [ " logger_config " ]
2016-08-02 23:28:48 +08:00
logger = Logger ( logger_config )
2016-05-31 00:13:45 +08:00
quota_service = QuotaService ( user_obs_config , redis_conn )
if not quota_service . check_user_quota ( ) :
2016-08-02 01:04:56 +08:00
raise Exception ( ' You have reached the limit of your quota ' )
2016-05-31 00:13:45 +08:00
2016-11-02 16:58:35 +08:00
with metrics ( ' obs_getmeasurebyid ' , user_obs_config , logger ) :
2016-10-29 00:12:04 +08:00
try :
obs_plan = plpy . prepare ( " SELECT cdb_dataservices_server._OBS_GetMeasureById($1, $2, $3, $4, $5, $6) as measure; " , [ " text " , " text " , " text " , " text " , " text " , " text " ] )
result = plpy . execute ( obs_plan , [ username , orgname , geom_ref , measure_id , boundary_id , time_span ] )
if result :
quota_service . increment_success_service_use ( )
return result [ 0 ] [ ' measure ' ]
else :
quota_service . increment_empty_service_use ( )
return None
except BaseException as e :
import sys
quota_service . increment_failed_service_use ( )
logger . error ( ' Error trying to OBS_GetMeasureById ' , sys . exc_info ( ) , data = { " username " : username , " orgname " : orgname } )
raise Exception ( ' Error trying to OBS_GetMeasureById ' )
finally :
quota_service . increment_total_service_use ( )
2017-11-08 00:23:05 +08:00
$ $ LANGUAGE plpythonu STABLE PARALLEL RESTRICTED ;
2017-01-12 12:03:01 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . _OBS_GetData (
username TEXT ,
orgname TEXT ,
geomvals geomval [ ] ,
params JSON ,
merge BOOLEAN DEFAULT True )
RETURNS TABLE (
id INT ,
data JSON
) AS $ $
CONNECT cdb_dataservices_server . _obs_server_conn_str ( username , orgname ) ;
SELECT * FROM cdb_observatory . OBS_GetData ( geomvals , params , merge ) ;
2017-11-07 22:32:07 +08:00
$ $ LANGUAGE plproxy VOLATILE PARALLEL UNSAFE ;
2017-01-12 12:03:01 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . OBS_GetData (
username TEXT ,
orgname TEXT ,
geomvals geomval [ ] ,
params JSON ,
merge BOOLEAN DEFAULT True )
RETURNS TABLE (
id INT ,
data JSON
) AS $ $
from cartodb_services . metrics import metrics
2017-02-06 17:44:02 +08:00
from cartodb_services . metrics import QuotaService
2017-01-12 12:03:01 +08:00
from cartodb_services . tools import Logger , LoggerConfig
plpy . execute ( " SELECT cdb_dataservices_server._connect_to_redis('{0}') " . format ( username ) )
redis_conn = GD [ " redis_connection_{0} " . format ( username ) ] [ ' redis_metrics_connection ' ]
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 ) ]
plpy . execute ( " SELECT cdb_dataservices_server._get_logger_config() " )
logger_config = GD [ " logger_config " ]
logger = Logger ( logger_config )
2017-02-06 17:44:02 +08:00
quota_service = QuotaService ( user_obs_config , redis_conn )
if not quota_service . check_user_quota ( ) :
raise Exception ( ' You have reached the limit of your quota ' )
2017-01-12 12:03:01 +08:00
2018-01-11 18:13:37 +08:00
with metrics ( ' obs_getdata ' , user_obs_config , logger , params ) :
2017-01-12 12:03:01 +08:00
try :
obs_plan = plpy . prepare ( " SELECT * FROM cdb_dataservices_server._OBS_GetData($1, $2, $3, $4, $5); " , [ " text " , " text " , " geomval[] " , " json " , " boolean " ] )
result = plpy . execute ( obs_plan , [ username , orgname , geomvals , params , merge ] )
2017-02-06 17:44:02 +08:00
empty_results = len ( geomvals ) - len ( result )
if empty_results > 0 :
quota_service . increment_empty_service_use ( empty_results )
2017-01-12 12:03:01 +08:00
if result :
2017-02-06 17:44:02 +08:00
quota_service . increment_success_service_use ( len ( result ) )
2017-01-12 12:03:01 +08:00
return result
else :
2017-05-16 22:23:13 +08:00
return [ ]
2017-01-12 12:03:01 +08:00
except BaseException as e :
import sys
2017-02-06 17:44:02 +08:00
quota_service . increment_failed_service_use ( len ( geomvals ) )
2017-01-12 12:03:01 +08:00
logger . error ( ' Error trying to OBS_GetData ' , sys . exc_info ( ) , data = { " username " : username , " orgname " : orgname } )
raise Exception ( ' Error trying to OBS_GetData ' )
2017-02-06 17:44:02 +08:00
finally :
quota_service . increment_total_service_use ( len ( geomvals ) )
2017-11-08 00:23:05 +08:00
$ $ LANGUAGE plpythonu STABLE PARALLEL RESTRICTED ;
2017-01-12 12:03:01 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . _OBS_GetData (
username TEXT ,
orgname TEXT ,
geomrefs TEXT [ ] ,
params JSON )
RETURNS TABLE (
id TEXT ,
data JSON
) AS $ $
CONNECT cdb_dataservices_server . _obs_server_conn_str ( username , orgname ) ;
SELECT * FROM cdb_observatory . OBS_GetData ( geomrefs , params ) ;
2017-11-07 22:32:07 +08:00
$ $ LANGUAGE plproxy VOLATILE PARALLEL UNSAFE ;
2017-01-12 12:03:01 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . OBS_GetData (
username TEXT ,
orgname TEXT ,
geomrefs TEXT [ ] ,
params JSON )
RETURNS TABLE (
id TEXT ,
data JSON
) AS $ $
from cartodb_services . metrics import metrics
2017-02-06 17:44:02 +08:00
from cartodb_services . metrics import QuotaService
2017-01-12 12:03:01 +08:00
from cartodb_services . tools import Logger , LoggerConfig
plpy . execute ( " SELECT cdb_dataservices_server._connect_to_redis('{0}') " . format ( username ) )
redis_conn = GD [ " redis_connection_{0} " . format ( username ) ] [ ' redis_metrics_connection ' ]
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 ) ]
plpy . execute ( " SELECT cdb_dataservices_server._get_logger_config() " )
logger_config = GD [ " logger_config " ]
logger = Logger ( logger_config )
2017-02-06 17:44:02 +08:00
quota_service = QuotaService ( user_obs_config , redis_conn )
if not quota_service . check_user_quota ( ) :
raise Exception ( ' You have reached the limit of your quota ' )
2017-01-12 12:03:01 +08:00
2018-01-11 18:13:37 +08:00
with metrics ( ' obs_getdata ' , user_obs_config , logger , params ) :
2017-01-12 12:03:01 +08:00
try :
obs_plan = plpy . prepare ( " SELECT * FROM cdb_dataservices_server._OBS_GetData($1, $2, $3, $4); " , [ " text " , " text " , " text[] " , " json " ] )
result = plpy . execute ( obs_plan , [ username , orgname , geomrefs , params ] )
2017-02-06 17:44:02 +08:00
empty_results = len ( geomrefs ) - len ( result )
if empty_results > 0 :
quota_service . increment_empty_service_use ( empty_results )
2017-01-12 12:03:01 +08:00
if result :
2017-02-06 17:44:02 +08:00
quota_service . increment_success_service_use ( len ( result ) )
2017-01-12 12:03:01 +08:00
return result
else :
2017-05-16 23:20:16 +08:00
return [ ]
2017-01-12 12:03:01 +08:00
except BaseException as e :
import sys
2017-02-06 17:44:02 +08:00
quota_service . increment_failed_service_use ( len ( geomrefs ) )
2017-01-12 12:03:01 +08:00
exc_info = sys . exc_info ( )
logger . error ( ' %s, %s, %s ' % ( exc_info [ 0 ] , exc_info [ 1 ] , exc_info [ 2 ] ) )
logger . error ( ' Error trying to OBS_GetData ' , exc_info , data = { " username " : username , " orgname " : orgname } )
raise Exception ( ' Error trying to OBS_GetData ' )
2017-02-06 17:44:02 +08:00
finally :
quota_service . increment_total_service_use ( len ( geomrefs ) )
2017-11-08 00:23:05 +08:00
$ $ LANGUAGE plpythonu STABLE PARALLEL RESTRICTED ;
2017-01-12 12:03:01 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . _OBS_GetMeta (
username TEXT ,
orgname TEXT ,
geom Geometry ( Geometry , 4326 ) ,
params JSON ,
max_timespan_rank INTEGER DEFAULT NULL ,
max_score_rank INTEGER DEFAULT NULL ,
target_geoms INTEGER DEFAULT NULL )
RETURNS JSON AS $ $
CONNECT cdb_dataservices_server . _obs_server_conn_str ( username , orgname ) ;
SELECT cdb_observatory . OBS_GetMeta ( geom , params , max_timespan_rank , max_score_rank , target_geoms ) ;
2017-11-07 22:32:07 +08:00
$ $ LANGUAGE plproxy VOLATILE PARALLEL UNSAFE ;
2017-01-12 12:03:01 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . OBS_GetMeta (
username TEXT ,
orgname TEXT ,
geom Geometry ( Geometry , 4326 ) ,
params JSON ,
max_timespan_rank INTEGER DEFAULT NULL ,
max_score_rank INTEGER DEFAULT NULL ,
target_geoms INTEGER DEFAULT NULL )
RETURNS JSON AS $ $
from cartodb_services . metrics import metrics
from cartodb_services . tools import Logger , LoggerConfig
plpy . execute ( " SELECT cdb_dataservices_server._connect_to_redis('{0}') " . format ( username ) )
redis_conn = GD [ " redis_connection_{0} " . format ( username ) ] [ ' redis_metrics_connection ' ]
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 ) ]
plpy . execute ( " SELECT cdb_dataservices_server._get_logger_config() " )
logger_config = GD [ " logger_config " ]
logger = Logger ( logger_config )
2018-01-11 18:13:37 +08:00
with metrics ( ' obs_getmeta ' , user_obs_config , logger , params ) :
2017-01-12 12:03:01 +08:00
try :
obs_plan = plpy . prepare ( " SELECT cdb_dataservices_server._OBS_GetMeta($1, $2, $3, $4, $5, $6, $7) as meta; " , [ " text " , " text " , " Geometry (Geometry, 4326) " , " json " , " integer " , " integer " , " integer " ] )
result = plpy . execute ( obs_plan , [ username , orgname , geom , params , max_timespan_rank , max_score_rank , target_geoms ] )
if result :
return result [ 0 ] [ ' meta ' ]
else :
return None
except BaseException as e :
import sys
logger . error ( ' Error trying to OBS_GetMeta ' , sys . exc_info ( ) , data = { " username " : username , " orgname " : orgname } )
raise Exception ( ' Error trying to OBS_GetMeta ' )
2017-11-08 00:23:05 +08:00
$ $ LANGUAGE plpythonu STABLE PARALLEL RESTRICTED ;
2017-08-09 00:55:26 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . _OBS_MetadataValidation (
username TEXT ,
orgname TEXT ,
geometry_extent Geometry ( Geometry , 4326 ) ,
geometry_type text ,
params JSON ,
target_geoms INTEGER DEFAULT NULL )
RETURNS TABLE ( valid boolean , errors text [ ] ) AS $ $
CONNECT cdb_dataservices_server . _obs_server_conn_str ( username , orgname ) ;
SELECT * FROM cdb_observatory . OBS_MetadataValidation ( geometry_extent , geometry_type , params , target_geoms ) ;
2017-11-07 22:32:07 +08:00
$ $ LANGUAGE plproxy VOLATILE PARALLEL UNSAFE ;
2017-08-09 00:55:26 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . OBS_MetadataValidation (
username TEXT ,
orgname TEXT ,
geometry_extent Geometry ( Geometry , 4326 ) ,
geometry_type text ,
params JSON ,
target_geoms INTEGER DEFAULT NULL )
RETURNS TABLE ( valid boolean , errors text [ ] ) AS $ $
from cartodb_services . metrics import metrics
from cartodb_services . tools import Logger , LoggerConfig
plpy . execute ( " SELECT cdb_dataservices_server._connect_to_redis('{0}') " . format ( username ) )
redis_conn = GD [ " redis_connection_{0} " . format ( username ) ] [ ' redis_metrics_connection ' ]
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 ) ]
plpy . execute ( " SELECT cdb_dataservices_server._get_logger_config() " )
logger_config = GD [ " logger_config " ]
logger = Logger ( logger_config )
with metrics ( ' obs_metadatavalidation ' , user_obs_config , logger ) :
try :
obs_plan = plpy . prepare ( " SELECT * FROM cdb_dataservices_server._OBS_MetadataValidation($1, $2, $3, $4, $5, $6); " , [ " text " , " text " , " Geometry (Geometry, 4326) " , " text " , " json " , " integer " ] )
result = plpy . execute ( obs_plan , [ username , orgname , geometry_extent , geometry_type , params , target_geoms ] )
if result :
return result
else :
return [ ]
except BaseException as e :
import sys
logger . error ( ' Error trying to OBS_MetadataValidation ' , sys . exc_info ( ) , data = { " username " : username , " orgname " : orgname } )
raise Exception ( ' Error trying to OBS_MetadataValidation ' )
2017-11-08 00:23:05 +08:00
$ $ LANGUAGE plpythonu STABLE PARALLEL RESTRICTED ;