Added metrics context manager to public functions

This commit is contained in:
Mario de Frutos
2016-11-04 13:39:10 +01:00
parent cf3c6f2ce5
commit 708bee2912
13 changed files with 612 additions and 544 deletions
@@ -1,5 +1,5 @@
comment = 'CartoDB dataservices server extension' comment = 'CartoDB dataservices server extension'
default_version = '0.16.0' default_version = '0.17.0'
requires = 'plpythonu, plproxy, postgis, cdb_geocoder' requires = 'plpythonu, plproxy, postgis, cdb_geocoder'
superuser = true superuser = true
schema = cdb_dataservices_server schema = cdb_dataservices_server
@@ -7,15 +7,18 @@ CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_route_point_to_point(
options text[] DEFAULT ARRAY[]::text[], options text[] DEFAULT ARRAY[]::text[],
units text DEFAULT 'kilometers') units text DEFAULT 'kilometers')
RETURNS cdb_dataservices_server.simple_route AS $$ RETURNS cdb_dataservices_server.simple_route AS $$
from cartodb_services.metrics import metrics
from cartodb_services.tools import Logger
plpy.execute("SELECT cdb_dataservices_server._connect_to_redis('{0}')".format(username)) plpy.execute("SELECT cdb_dataservices_server._connect_to_redis('{0}')".format(username))
redis_conn = GD["redis_connection_{0}".format(username)]['redis_metrics_connection'] redis_conn = GD["redis_connection_{0}".format(username)]['redis_metrics_connection']
plpy.execute("SELECT cdb_dataservices_server._get_routing_config({0}, {1})".format(plpy.quote_nullable(username), plpy.quote_nullable(orgname))) 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_routing_config = GD["user_routing_config_{0}".format(username)]
waypoints = [origin, destination] with metrics('cdb_route_with_point', user_routing_config):
mapzen_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._cdb_mapzen_route_with_waypoints($1, $2, $3, $4, $5, $6) as route;", ["text", "text", "geometry(Point, 4326)[]", "text", "text[]", "text"]) waypoints = [origin, destination]
result = plpy.execute(mapzen_plan, [username, orgname, waypoints, mode, options, units]) mapzen_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._cdb_mapzen_route_with_waypoints($1, $2, $3, $4, $5, $6) as route;", ["text", "text", "geometry(Point, 4326)[]", "text", "text[]", "text"])
return [result[0]['shape'],result[0]['length'], result[0]['duration']] result = plpy.execute(mapzen_plan, [username, orgname, waypoints, mode, options, units])
return [result[0]['shape'],result[0]['length'], result[0]['duration']]
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
@@ -27,12 +30,15 @@ CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_route_with_waypoints(
options text[] DEFAULT ARRAY[]::text[], options text[] DEFAULT ARRAY[]::text[],
units text DEFAULT 'kilometers') units text DEFAULT 'kilometers')
RETURNS cdb_dataservices_server.simple_route AS $$ RETURNS cdb_dataservices_server.simple_route AS $$
from cartodb_services.metrics import metrics
from cartodb_services.tools import Logger
plpy.execute("SELECT cdb_dataservices_server._connect_to_redis('{0}')".format(username)) plpy.execute("SELECT cdb_dataservices_server._connect_to_redis('{0}')".format(username))
redis_conn = GD["redis_connection_{0}".format(username)]['redis_metrics_connection'] redis_conn = GD["redis_connection_{0}".format(username)]['redis_metrics_connection']
plpy.execute("SELECT cdb_dataservices_server._get_routing_config({0}, {1})".format(plpy.quote_nullable(username), plpy.quote_nullable(orgname))) 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_routing_config = GD["user_routing_config_{0}".format(username)]
mapzen_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._cdb_mapzen_route_with_waypoints($1, $2, $3, $4, $5, $6) as route;", ["text", "text", "geometry(Point, 4326)[]", "text", "text[]", "text"]) with metrics('cdb_route_with_waypoints', user_routing_config):
result = plpy.execute(mapzen_plan, [username, orgname, waypoints, mode, options, units]) mapzen_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._cdb_mapzen_route_with_waypoints($1, $2, $3, $4, $5, $6) as route;", ["text", "text", "geometry(Point, 4326)[]", "text", "text[]", "text"])
return [result[0]['shape'],result[0]['length'], result[0]['duration']] result = plpy.execute(mapzen_plan, [username, orgname, waypoints, mode, options, units])
return [result[0]['shape'],result[0]['length'], result[0]['duration']]
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
@@ -34,6 +34,7 @@ CREATE OR REPLACE FUNCTION cdb_dataservices_server.obs_get_demographic_snapshot(
time_span TEXT DEFAULT NULL, time_span TEXT DEFAULT NULL,
geometry_level TEXT DEFAULT NULL) geometry_level TEXT DEFAULT NULL)
RETURNS json AS $$ RETURNS json AS $$
from cartodb_services.metrics import metrics
from cartodb_services.metrics import QuotaService from cartodb_services.metrics import QuotaService
from cartodb_services.tools import Logger,LoggerConfig from cartodb_services.tools import Logger,LoggerConfig
import json import json
@@ -50,22 +51,23 @@ RETURNS json AS $$
if not quota_service.check_user_quota(): if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota') raise Exception('You have reached the limit of your quota')
try: with metrics('obs_getdemographicsnapshot', user_obs_snapshot_config):
obs_plan = plpy.prepare("SELECT cdb_dataservices_server._OBS_GetDemographicSnapshotJSON($1, $2, $3, $4, $5) as snapshot;", ["text", "text", "geometry(Geometry, 4326)", "text", "text"]) try:
result = plpy.execute(obs_plan, [username, orgname, geom, time_span, geometry_level]) obs_plan = plpy.prepare("SELECT cdb_dataservices_server._OBS_GetDemographicSnapshotJSON($1, $2, $3, $4, $5) as snapshot;", ["text", "text", "geometry(Geometry, 4326)", "text", "text"])
if result: result = plpy.execute(obs_plan, [username, orgname, geom, time_span, geometry_level])
quota_service.increment_success_service_use() if result:
return result[0]['snapshot'] quota_service.increment_success_service_use()
else: return result[0]['snapshot']
quota_service.increment_empty_service_use() else:
return None quota_service.increment_empty_service_use()
except BaseException as e: return None
import sys except BaseException as e:
quota_service.increment_failed_service_use() import sys
logger.error('Error trying to obs_get_demographic_snapshot', sys.exc_info(), data={"username": username, "orgname": orgname}) quota_service.increment_failed_service_use()
raise Exception('Error trying to obs_get_demographic_snapshot') logger.error('Error trying to obs_get_demographic_snapshot', sys.exc_info(), data={"username": username, "orgname": orgname})
finally: raise Exception('Error trying to obs_get_demographic_snapshot')
quota_service.increment_total_service_use() finally:
quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetDemographicSnapshot( CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetDemographicSnapshot(
@@ -86,6 +88,7 @@ CREATE OR REPLACE FUNCTION cdb_dataservices_server.OBS_GetDemographicSnapshot(
time_span TEXT DEFAULT NULL, time_span TEXT DEFAULT NULL,
geometry_level TEXT DEFAULT NULL) geometry_level TEXT DEFAULT NULL)
RETURNS SETOF JSON AS $$ RETURNS SETOF JSON AS $$
from cartodb_services.metrics import metrics
from cartodb_services.metrics import QuotaService from cartodb_services.metrics import QuotaService
from cartodb_services.tools import Logger,LoggerConfig from cartodb_services.tools import Logger,LoggerConfig
@@ -101,26 +104,27 @@ RETURNS SETOF JSON AS $$
if not quota_service.check_user_quota(): if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota') raise Exception('You have reached the limit of your quota')
try: with metrics('obs_getdemographicsnapshot', user_obs_snapshot_config):
obs_plan = plpy.prepare("SELECT cdb_dataservices_server._OBS_GetDemographicSnapshot($1, $2, $3, $4, $5) as snapshot;", ["text", "text", "geometry(Geometry, 4326)", "text", "text"]) try:
result = plpy.execute(obs_plan, [username, orgname, geom, time_span, geometry_level]) obs_plan = plpy.prepare("SELECT cdb_dataservices_server._OBS_GetDemographicSnapshot($1, $2, $3, $4, $5) as snapshot;", ["text", "text", "geometry(Geometry, 4326)", "text", "text"])
if result: result = plpy.execute(obs_plan, [username, orgname, geom, time_span, geometry_level])
resp = [] if result:
for element in result: resp = []
value = element['snapshot'] for element in result:
resp.append(value) value = element['snapshot']
quota_service.increment_success_service_use() resp.append(value)
return resp quota_service.increment_success_service_use()
else: return resp
quota_service.increment_empty_service_use() else:
return [] quota_service.increment_empty_service_use()
except BaseException as e: return []
import sys except BaseException as e:
quota_service.increment_failed_service_use() import sys
logger.error('Error trying to obs_get_demographic_snapshot', sys.exc_info(), data={"username": username, "orgname": orgname}) quota_service.increment_failed_service_use()
raise Exception('Error trying to obs_get_demographic_snapshot') logger.error('Error trying to obs_get_demographic_snapshot', sys.exc_info(), data={"username": username, "orgname": orgname})
finally: raise Exception('Error trying to obs_get_demographic_snapshot')
quota_service.increment_total_service_use() finally:
quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetSegmentSnapshotJSON( CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetSegmentSnapshotJSON(
@@ -139,6 +143,7 @@ CREATE OR REPLACE FUNCTION cdb_dataservices_server.obs_get_segment_snapshot(
geom geometry(Geometry, 4326), geom geometry(Geometry, 4326),
geometry_level TEXT DEFAULT NULL) geometry_level TEXT DEFAULT NULL)
RETURNS json AS $$ RETURNS json AS $$
from cartodb_services.metrics import metrics
from cartodb_services.metrics import QuotaService from cartodb_services.metrics import QuotaService
from cartodb_services.tools import Logger,LoggerConfig from cartodb_services.tools import Logger,LoggerConfig
import json import json
@@ -155,22 +160,23 @@ RETURNS json AS $$
if not quota_service.check_user_quota(): if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota') raise Exception('You have reached the limit of your quota')
try: with metrics('obs_getsegmentsnapshot', user_obs_snapshot_config):
obs_plan = plpy.prepare("SELECT cdb_dataservices_server._OBS_GetSegmentSnapshotJSON($1, $2, $3, $4) as snapshot;", ["text", "text", "geometry(Geometry, 4326)", "text"]) try:
result = plpy.execute(obs_plan, [username, orgname, geom, geometry_level]) obs_plan = plpy.prepare("SELECT cdb_dataservices_server._OBS_GetSegmentSnapshotJSON($1, $2, $3, $4) as snapshot;", ["text", "text", "geometry(Geometry, 4326)", "text"])
if result: result = plpy.execute(obs_plan, [username, orgname, geom, geometry_level])
quota_service.increment_success_service_use() if result:
return result[0]['snapshot'] quota_service.increment_success_service_use()
else: return result[0]['snapshot']
quota_service.increment_empty_service_use() else:
return None quota_service.increment_empty_service_use()
except BaseException as e: return None
import sys except BaseException as e:
quota_service.increment_failed_service_use() import sys
logger.error('Error trying to obs_get_segment_snapshot', sys.exc_info(), data={"username": username, "orgname": orgname}) quota_service.increment_failed_service_use()
raise Exception('Error trying to obs_get_segment_snapshot') logger.error('Error trying to obs_get_segment_snapshot', sys.exc_info(), data={"username": username, "orgname": orgname})
finally: raise Exception('Error trying to obs_get_segment_snapshot')
quota_service.increment_total_service_use() finally:
quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetSegmentSnapshot( CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetSegmentSnapshot(
@@ -189,6 +195,7 @@ CREATE OR REPLACE FUNCTION cdb_dataservices_server.OBS_GetSegmentSnapshot(
geom geometry(Geometry, 4326), geom geometry(Geometry, 4326),
geometry_level TEXT DEFAULT NULL) geometry_level TEXT DEFAULT NULL)
RETURNS SETOF JSON AS $$ RETURNS SETOF JSON AS $$
from cartodb_services.metrics import metrics
from cartodb_services.metrics import QuotaService from cartodb_services.metrics import QuotaService
from cartodb_services.tools import Logger,LoggerConfig from cartodb_services.tools import Logger,LoggerConfig
@@ -204,26 +211,27 @@ RETURNS SETOF JSON AS $$
if not quota_service.check_user_quota(): if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota') raise Exception('You have reached the limit of your quota')
try: with metrics('obs_getsegmentsnapshot', user_obs_snapshot_config):
obs_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._OBS_GetSegmentSnapshot($1, $2, $3, $4) as snapshot;", ["text", "text", "geometry(Geometry, 4326)", "text"]) try:
result = plpy.execute(obs_plan, [username, orgname, geom, geometry_level]) obs_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._OBS_GetSegmentSnapshot($1, $2, $3, $4) as snapshot;", ["text", "text", "geometry(Geometry, 4326)", "text"])
if result: result = plpy.execute(obs_plan, [username, orgname, geom, geometry_level])
resp = [] if result:
for element in result: resp = []
value = element['snapshot'] for element in result:
resp.append(value) value = element['snapshot']
quota_service.increment_success_service_use() resp.append(value)
return resp quota_service.increment_success_service_use()
else: return resp
quota_service.increment_empty_service_use() else:
return [] quota_service.increment_empty_service_use()
except BaseException as e: return []
import sys except BaseException as e:
quota_service.increment_failed_service_use() import sys
logger.error('Error trying to OBS_GetSegmentSnapshot', sys.exc_info(), data={"username": username, "orgname": orgname}) quota_service.increment_failed_service_use()
raise Exception('Error trying to OBS_GetSegmentSnapshot') logger.error('Error trying to OBS_GetSegmentSnapshot', sys.exc_info(), data={"username": username, "orgname": orgname})
finally: raise Exception('Error trying to OBS_GetSegmentSnapshot')
quota_service.increment_total_service_use() finally:
quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetMeasure( CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetMeasure(
@@ -248,6 +256,7 @@ CREATE OR REPLACE FUNCTION cdb_dataservices_server.OBS_GetMeasure(
boundary_id TEXT DEFAULT NULL, boundary_id TEXT DEFAULT NULL,
time_span TEXT DEFAULT NULL) time_span TEXT DEFAULT NULL)
RETURNS NUMERIC AS $$ RETURNS NUMERIC AS $$
from cartodb_services.metrics import metrics
from cartodb_services.metrics import QuotaService from cartodb_services.metrics import QuotaService
from cartodb_services.tools import Logger,LoggerConfig from cartodb_services.tools import Logger,LoggerConfig
@@ -263,22 +272,23 @@ RETURNS NUMERIC AS $$
if not quota_service.check_user_quota(): if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota') raise Exception('You have reached the limit of your quota')
try: with metrics('obs_getmeasure', user_obs_snapshot_config):
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"]) try:
result = plpy.execute(obs_plan, [username, orgname, geom, measure_id, normalize, boundary_id, time_span]) 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"])
if result: result = plpy.execute(obs_plan, [username, orgname, geom, measure_id, normalize, boundary_id, time_span])
quota_service.increment_success_service_use() if result:
return result[0]['measure'] quota_service.increment_success_service_use()
else: return result[0]['measure']
quota_service.increment_empty_service_use() else:
return None quota_service.increment_empty_service_use()
except BaseException as e: return None
import sys except BaseException as e:
quota_service.increment_failed_service_use() import sys
logger.error('Error trying to OBS_GetMeasure', sys.exc_info(), data={"username": username, "orgname": orgname}) quota_service.increment_failed_service_use()
raise Exception('Error trying to OBS_GetMeasure') logger.error('Error trying to OBS_GetMeasure', sys.exc_info(), data={"username": username, "orgname": orgname})
finally: raise Exception('Error trying to OBS_GetMeasure')
quota_service.increment_total_service_use() finally:
quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetCategory( CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetCategory(
@@ -301,6 +311,7 @@ CREATE OR REPLACE FUNCTION cdb_dataservices_server.OBS_GetCategory(
boundary_id TEXT DEFAULT NULL, boundary_id TEXT DEFAULT NULL,
time_span TEXT DEFAULT NULL) time_span TEXT DEFAULT NULL)
RETURNS TEXT AS $$ RETURNS TEXT AS $$
from cartodb_services.metrics import metrics
from cartodb_services.metrics import QuotaService from cartodb_services.metrics import QuotaService
from cartodb_services.tools import Logger,LoggerConfig from cartodb_services.tools import Logger,LoggerConfig
@@ -316,22 +327,23 @@ RETURNS TEXT AS $$
if not quota_service.check_user_quota(): if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota') raise Exception('You have reached the limit of your quota')
try: with metrics('obs_getcategory', user_obs_snapshot_config):
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"]) try:
result = plpy.execute(obs_plan, [username, orgname, geom, category_id, boundary_id, time_span]) 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"])
if result: result = plpy.execute(obs_plan, [username, orgname, geom, category_id, boundary_id, time_span])
quota_service.increment_success_service_use() if result:
return result[0]['category'] quota_service.increment_success_service_use()
else: return result[0]['category']
quota_service.increment_empty_service_use() else:
return None quota_service.increment_empty_service_use()
except BaseException as e: return None
import sys except BaseException as e:
quota_service.increment_failed_service_use() import sys
logger.error('Error trying to OBS_GetCategory', sys.exc_info(), data={"username": username, "orgname": orgname}) quota_service.increment_failed_service_use()
raise Exception('Error trying to OBS_GetCategory') logger.error('Error trying to OBS_GetCategory', sys.exc_info(), data={"username": username, "orgname": orgname})
finally: raise Exception('Error trying to OBS_GetCategory')
quota_service.increment_total_service_use() finally:
quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetUSCensusMeasure( CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetUSCensusMeasure(
@@ -356,6 +368,7 @@ CREATE OR REPLACE FUNCTION cdb_dataservices_server.OBS_GetUSCensusMeasure(
boundary_id TEXT DEFAULT NULL, boundary_id TEXT DEFAULT NULL,
time_span TEXT DEFAULT NULL) time_span TEXT DEFAULT NULL)
RETURNS NUMERIC AS $$ RETURNS NUMERIC AS $$
from cartodb_services.metrics import metrics
from cartodb_services.metrics import QuotaService from cartodb_services.metrics import QuotaService
from cartodb_services.tools import Logger,LoggerConfig from cartodb_services.tools import Logger,LoggerConfig
@@ -371,22 +384,23 @@ RETURNS NUMERIC AS $$
if not quota_service.check_user_quota(): if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota') raise Exception('You have reached the limit of your quota')
try: with metrics('obs_getuscensusmeasure', user_obs_snapshot_config):
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"]) try:
result = plpy.execute(obs_plan, [username, orgname, geom, name, normalize, boundary_id, time_span]) 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"])
if result: result = plpy.execute(obs_plan, [username, orgname, geom, name, normalize, boundary_id, time_span])
quota_service.increment_success_service_use() if result:
return result[0]['census_measure'] quota_service.increment_success_service_use()
else: return result[0]['census_measure']
quota_service.increment_empty_service_use() else:
return None quota_service.increment_empty_service_use()
except BaseException as e: return None
import sys except BaseException as e:
quota_service.increment_failed_service_use() import sys
logger.error('Error trying to OBS_GetUSCensusMeasure', sys.exc_info(), data={"username": username, "orgname": orgname}) quota_service.increment_failed_service_use()
raise Exception('Error trying to OBS_GetUSCensusMeasure') logger.error('Error trying to OBS_GetUSCensusMeasure', sys.exc_info(), data={"username": username, "orgname": orgname})
finally: raise Exception('Error trying to OBS_GetUSCensusMeasure')
quota_service.increment_total_service_use() finally:
quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetUSCensusCategory( CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetUSCensusCategory(
@@ -409,6 +423,7 @@ CREATE OR REPLACE FUNCTION cdb_dataservices_server.OBS_GetUSCensusCategory(
boundary_id TEXT DEFAULT NULL, boundary_id TEXT DEFAULT NULL,
time_span TEXT DEFAULT NULL) time_span TEXT DEFAULT NULL)
RETURNS TEXT AS $$ RETURNS TEXT AS $$
from cartodb_services.metrics import metrics
from cartodb_services.metrics import QuotaService from cartodb_services.metrics import QuotaService
from cartodb_services.tools import Logger,LoggerConfig from cartodb_services.tools import Logger,LoggerConfig
@@ -424,22 +439,23 @@ RETURNS TEXT AS $$
if not quota_service.check_user_quota(): if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota') raise Exception('You have reached the limit of your quota')
try: with metrics('obs_getuscensuscategory', user_obs_snapshot_config):
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"]) try:
result = plpy.execute(obs_plan, [username, orgname, geom, name, boundary_id, time_span]) 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"])
if result: result = plpy.execute(obs_plan, [username, orgname, geom, name, boundary_id, time_span])
quota_service.increment_success_service_use() if result:
return result[0]['census_category'] quota_service.increment_success_service_use()
else: return result[0]['census_category']
quota_service.increment_empty_service_use() else:
return None quota_service.increment_empty_service_use()
except BaseException as e: return None
import sys except BaseException as e:
quota_service.increment_failed_service_use() import sys
logger.error('Error trying to OBS_GetUSCensusCategory', sys.exc_info(), data={"username": username, "orgname": orgname}) quota_service.increment_failed_service_use()
raise Exception('Error trying to OBS_GetUSCensusCategory') logger.error('Error trying to OBS_GetUSCensusCategory', sys.exc_info(), data={"username": username, "orgname": orgname})
finally: raise Exception('Error trying to OBS_GetUSCensusCategory')
quota_service.increment_total_service_use() finally:
quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetPopulation( CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetPopulation(
@@ -462,6 +478,7 @@ CREATE OR REPLACE FUNCTION cdb_dataservices_server.OBS_GetPopulation(
boundary_id TEXT DEFAULT NULL, boundary_id TEXT DEFAULT NULL,
time_span TEXT DEFAULT NULL) time_span TEXT DEFAULT NULL)
RETURNS NUMERIC AS $$ RETURNS NUMERIC AS $$
from cartodb_services.metrics import metrics
from cartodb_services.metrics import QuotaService from cartodb_services.metrics import QuotaService
from cartodb_services.tools import Logger,LoggerConfig from cartodb_services.tools import Logger,LoggerConfig
@@ -477,22 +494,23 @@ RETURNS NUMERIC AS $$
if not quota_service.check_user_quota(): if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota') raise Exception('You have reached the limit of your quota')
try: with metrics('obs_getpopulation', user_obs_snapshot_config):
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"]) try:
result = plpy.execute(obs_plan, [username, orgname, geom, normalize, boundary_id, time_span]) 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"])
if result: result = plpy.execute(obs_plan, [username, orgname, geom, normalize, boundary_id, time_span])
quota_service.increment_success_service_use() if result:
return result[0]['population'] quota_service.increment_success_service_use()
else: return result[0]['population']
quota_service.increment_empty_service_use() else:
return None quota_service.increment_empty_service_use()
except BaseException as e: return None
import sys except BaseException as e:
quota_service.increment_failed_service_use() import sys
logger.error('Error trying to OBS_GetPopulation', sys.exc_info(), data={"username": username, "orgname": orgname}) quota_service.increment_failed_service_use()
raise Exception('Error trying to OBS_GetPopulation') logger.error('Error trying to OBS_GetPopulation', sys.exc_info(), data={"username": username, "orgname": orgname})
finally: raise Exception('Error trying to OBS_GetPopulation')
quota_service.increment_total_service_use() finally:
quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetMeasureById( CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetMeasureById(
@@ -515,6 +533,7 @@ CREATE OR REPLACE FUNCTION cdb_dataservices_server.OBS_GetMeasureById(
boundary_id TEXT, boundary_id TEXT,
time_span TEXT DEFAULT NULL) time_span TEXT DEFAULT NULL)
RETURNS NUMERIC AS $$ RETURNS NUMERIC AS $$
from cartodb_services.metrics import metrics
from cartodb_services.metrics import QuotaService from cartodb_services.metrics import QuotaService
from cartodb_services.tools import Logger,LoggerConfig from cartodb_services.tools import Logger,LoggerConfig
@@ -530,20 +549,21 @@ RETURNS NUMERIC AS $$
if not quota_service.check_user_quota(): if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota') raise Exception('You have reached the limit of your quota')
try: with metrics('obs_getmeasurebyid', user_obs_snapshot_config):
obs_plan = plpy.prepare("SELECT cdb_dataservices_server._OBS_GetMeasureById($1, $2, $3, $4, $5, $6) as measure;", ["text", "text", "text", "text", "text", "text"]) try:
result = plpy.execute(obs_plan, [username, orgname, geom_ref, measure_id, boundary_id, time_span]) obs_plan = plpy.prepare("SELECT cdb_dataservices_server._OBS_GetMeasureById($1, $2, $3, $4, $5, $6) as measure;", ["text", "text", "text", "text", "text", "text"])
if result: result = plpy.execute(obs_plan, [username, orgname, geom_ref, measure_id, boundary_id, time_span])
quota_service.increment_success_service_use() if result:
return result[0]['measure'] quota_service.increment_success_service_use()
else: return result[0]['measure']
quota_service.increment_empty_service_use() else:
return None quota_service.increment_empty_service_use()
except BaseException as e: return None
import sys except BaseException as e:
quota_service.increment_failed_service_use() import sys
logger.error('Error trying to OBS_GetMeasureById', sys.exc_info(), data={"username": username, "orgname": orgname}) quota_service.increment_failed_service_use()
raise Exception('Error trying to OBS_GetMeasureById') logger.error('Error trying to OBS_GetMeasureById', sys.exc_info(), data={"username": username, "orgname": orgname})
finally: raise Exception('Error trying to OBS_GetMeasureById')
quota_service.increment_total_service_use() finally:
quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
@@ -14,6 +14,7 @@ CREATE OR REPLACE FUNCTION cdb_dataservices_server.OBS_Search(
search_term TEXT, search_term TEXT,
relevant_boundary TEXT DEFAULT NULL) relevant_boundary TEXT DEFAULT NULL)
RETURNS TABLE(id text, description text, name text, aggregate text, source text) AS $$ RETURNS TABLE(id text, description text, name text, aggregate text, source text) AS $$
from cartodb_services.metrics import metrics
from cartodb_services.metrics import QuotaService from cartodb_services.metrics import QuotaService
from cartodb_services.tools import Logger,LoggerConfig from cartodb_services.tools import Logger,LoggerConfig
@@ -29,30 +30,31 @@ RETURNS TABLE(id text, description text, name text, aggregate text, source text)
if not quota_service.check_user_quota(): if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota') raise Exception('You have reached the limit of your quota')
try: with metrics('obs_search', user_obs_snapshot_config):
obs_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._OBS_Search($1, $2, $3, $4);", ["text", "text", "text", "text"]) try:
result = plpy.execute(obs_plan, [username, orgname, search_term, relevant_boundary]) obs_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._OBS_Search($1, $2, $3, $4);", ["text", "text", "text", "text"])
if result: result = plpy.execute(obs_plan, [username, orgname, search_term, relevant_boundary])
resp = [] if result:
for element in result: resp = []
id = element['id'] for element in result:
description = element['description'] id = element['id']
name = element['name'] description = element['description']
aggregate = element['aggregate'] name = element['name']
source = element['source'] aggregate = element['aggregate']
resp.append([id, description, name, aggregate, source]) source = element['source']
quota_service.increment_success_service_use() resp.append([id, description, name, aggregate, source])
return resp quota_service.increment_success_service_use()
else: return resp
quota_service.increment_empty_service_use() else:
return [None, None, None, None, None] quota_service.increment_empty_service_use()
except BaseException as e: return [None, None, None, None, None]
import sys except BaseException as e:
quota_service.increment_failed_service_use() import sys
logger.error('Error trying to OBS_Search', sys.exc_info(), data={"username": username, "orgname": orgname}) quota_service.increment_failed_service_use()
raise Exception('Error trying to OBS_Search') logger.error('Error trying to OBS_Search', sys.exc_info(), data={"username": username, "orgname": orgname})
finally: raise Exception('Error trying to OBS_Search')
quota_service.increment_total_service_use() finally:
quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetAvailableBoundaries( CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetAvailableBoundaries(
@@ -71,6 +73,7 @@ CREATE OR REPLACE FUNCTION cdb_dataservices_server.OBS_GetAvailableBoundaries(
geom geometry(Geometry, 4326), geom geometry(Geometry, 4326),
time_span TEXT DEFAULT NULL) time_span TEXT DEFAULT NULL)
RETURNS TABLE(boundary_id text, description text, time_span text, tablename text) AS $$ RETURNS TABLE(boundary_id text, description text, time_span text, tablename text) AS $$
from cartodb_services.metrics import metrics
from cartodb_services.metrics import QuotaService from cartodb_services.metrics import QuotaService
from cartodb_services.tools import Logger,LoggerConfig from cartodb_services.tools import Logger,LoggerConfig
@@ -86,27 +89,28 @@ RETURNS TABLE(boundary_id text, description text, time_span text, tablename text
if not quota_service.check_user_quota(): if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota') raise Exception('You have reached the limit of your quota')
try: with metrics('obs_getavailableboundaries', user_obs_snapshot_config):
obs_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._OBS_GetAvailableBoundaries($1, $2, $3, $4) as available_boundaries;", ["text", "text", "geometry(Geometry, 4326)", "text"]) try:
result = plpy.execute(obs_plan, [username, orgname, geom, time_span]) obs_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._OBS_GetAvailableBoundaries($1, $2, $3, $4) as available_boundaries;", ["text", "text", "geometry(Geometry, 4326)", "text"])
if result: result = plpy.execute(obs_plan, [username, orgname, geom, time_span])
resp = [] if result:
for element in result: resp = []
id = element['boundary_id'] for element in result:
description = element['description'] id = element['boundary_id']
tspan = element['time_span'] description = element['description']
tablename = element['tablename'] tspan = element['time_span']
resp.append([id, description, tspan, tablename]) tablename = element['tablename']
quota_service.increment_success_service_use() resp.append([id, description, tspan, tablename])
return resp quota_service.increment_success_service_use()
else: return resp
quota_service.increment_empty_service_use() else:
return [] quota_service.increment_empty_service_use()
except BaseException as e: return []
import sys except BaseException as e:
quota_service.increment_failed_service_use() import sys
logger.error('Error trying to OBS_GetMeasureById', sys.exc_info(), data={"username": username, "orgname": orgname}) quota_service.increment_failed_service_use()
raise Exception('Error trying to OBS_GetMeasureById') logger.error('Error trying to OBS_GetMeasureById', sys.exc_info(), data={"username": username, "orgname": orgname})
finally: raise Exception('Error trying to OBS_GetMeasureById')
quota_service.increment_total_service_use() finally:
quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
@@ -16,6 +16,7 @@ CREATE OR REPLACE FUNCTION cdb_dataservices_server.OBS_GetBoundary(
boundary_id TEXT, boundary_id TEXT,
time_span TEXT DEFAULT NULL) time_span TEXT DEFAULT NULL)
RETURNS geometry(Geometry, 4326) AS $$ RETURNS geometry(Geometry, 4326) AS $$
from cartodb_services.metrics import metrics
from cartodb_services.metrics import QuotaService from cartodb_services.metrics import QuotaService
from cartodb_services.tools import Logger,LoggerConfig from cartodb_services.tools import Logger,LoggerConfig
@@ -31,22 +32,23 @@ RETURNS geometry(Geometry, 4326) AS $$
if not quota_service.check_user_quota(): if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota') raise Exception('You have reached the limit of your quota')
try: with metrics('obs_getboundary', user_obs_snapshot_config):
obs_plan = plpy.prepare("SELECT cdb_dataservices_server._OBS_GetBoundary($1, $2, $3, $4) as boundary;", ["text", "text", "geometry(Point, 4326)", "text", "text"]) try:
result = plpy.execute(obs_plan, [username, orgname, geom, boundary_id, time_span]) obs_plan = plpy.prepare("SELECT cdb_dataservices_server._OBS_GetBoundary($1, $2, $3, $4) as boundary;", ["text", "text", "geometry(Point, 4326)", "text", "text"])
if result: result = plpy.execute(obs_plan, [username, orgname, geom, boundary_id, time_span])
quota_service.increment_success_service_use() if result:
return result[0]['boundary'] quota_service.increment_success_service_use()
else: return result[0]['boundary']
quota_service.increment_empty_service_use() else:
return None quota_service.increment_empty_service_use()
except BaseException as e: return None
import sys except BaseException as e:
quota_service.increment_failed_service_use() import sys
logger.error('Error trying to OBS_GetBoundary', sys.exc_info(), data={"username": username, "orgname": orgname}) quota_service.increment_failed_service_use()
raise Exception('Error trying to OBS_GetBoundary') logger.error('Error trying to OBS_GetBoundary', sys.exc_info(), data={"username": username, "orgname": orgname})
finally: raise Exception('Error trying to OBS_GetBoundary')
quota_service.increment_total_service_use() finally:
quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetBoundaryId( CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetBoundaryId(
@@ -67,6 +69,7 @@ CREATE OR REPLACE FUNCTION cdb_dataservices_server.OBS_GetBoundaryId(
boundary_id TEXT, boundary_id TEXT,
time_span TEXT DEFAULT NULL) time_span TEXT DEFAULT NULL)
RETURNS TEXT AS $$ RETURNS TEXT AS $$
from cartodb_services.metrics import metrics
from cartodb_services.metrics import QuotaService from cartodb_services.metrics import QuotaService
from cartodb_services.tools import Logger,LoggerConfig from cartodb_services.tools import Logger,LoggerConfig
@@ -82,22 +85,23 @@ RETURNS TEXT AS $$
if not quota_service.check_user_quota(): if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota') raise Exception('You have reached the limit of your quota')
try: with metrics('obs_getboundaryid', user_obs_snapshot_config):
obs_plan = plpy.prepare("SELECT cdb_dataservices_server._OBS_GetBoundaryId($1, $2, $3, $4, $5) as boundary;", ["text", "text", "geometry(Point, 4326)", "text", "text"]) try:
result = plpy.execute(obs_plan, [username, orgname, geom, boundary_id, time_span]) obs_plan = plpy.prepare("SELECT cdb_dataservices_server._OBS_GetBoundaryId($1, $2, $3, $4, $5) as boundary;", ["text", "text", "geometry(Point, 4326)", "text", "text"])
if result: result = plpy.execute(obs_plan, [username, orgname, geom, boundary_id, time_span])
quota_service.increment_success_service_use() if result:
return result[0]['boundary'] quota_service.increment_success_service_use()
else: return result[0]['boundary']
quota_service.increment_empty_service_use() else:
return None quota_service.increment_empty_service_use()
except BaseException as e: return None
import sys except BaseException as e:
quota_service.increment_failed_service_use() import sys
logger.error('Error trying to OBS_GetBoundaryId', sys.exc_info(), data={"username": username, "orgname": orgname}) quota_service.increment_failed_service_use()
raise Exception('Error trying to OBS_GetBoundaryId') logger.error('Error trying to OBS_GetBoundaryId', sys.exc_info(), data={"username": username, "orgname": orgname})
finally: raise Exception('Error trying to OBS_GetBoundaryId')
quota_service.increment_total_service_use() finally:
quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetBoundaryById( CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetBoundaryById(
@@ -118,6 +122,7 @@ CREATE OR REPLACE FUNCTION cdb_dataservices_server.OBS_GetBoundaryById(
boundary_id TEXT, boundary_id TEXT,
time_span TEXT DEFAULT NULL) time_span TEXT DEFAULT NULL)
RETURNS geometry(Geometry, 4326) AS $$ RETURNS geometry(Geometry, 4326) AS $$
from cartodb_services.metrics import metrics
from cartodb_services.metrics import QuotaService from cartodb_services.metrics import QuotaService
from cartodb_services.tools import Logger,LoggerConfig from cartodb_services.tools import Logger,LoggerConfig
@@ -133,22 +138,23 @@ RETURNS geometry(Geometry, 4326) AS $$
if not quota_service.check_user_quota(): if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota') raise Exception('You have reached the limit of your quota')
try: with metrics('obs_getboundarybyid', user_obs_snapshot_config):
obs_plan = plpy.prepare("SELECT cdb_dataservices_server._OBS_GetBoundaryById($1, $2, $3, $4, $5) as boundary;", ["text", "text", "text", "text", "text"]) try:
result = plpy.execute(obs_plan, [username, orgname, geometry_id, boundary_id, time_span]) obs_plan = plpy.prepare("SELECT cdb_dataservices_server._OBS_GetBoundaryById($1, $2, $3, $4, $5) as boundary;", ["text", "text", "text", "text", "text"])
if result: result = plpy.execute(obs_plan, [username, orgname, geometry_id, boundary_id, time_span])
quota_service.increment_success_service_use() if result:
return result[0]['boundary'] quota_service.increment_success_service_use()
else: return result[0]['boundary']
quota_service.increment_empty_service_use() else:
return None quota_service.increment_empty_service_use()
except BaseException as e: return None
import sys except BaseException as e:
quota_service.increment_failed_service_use() import sys
logger.error('Error trying to OBS_GetBoundaryById', sys.exc_info(), data={"username": username, "orgname": orgname}) quota_service.increment_failed_service_use()
raise Exception('Error trying to OBS_GetBoundaryById') logger.error('Error trying to OBS_GetBoundaryById', sys.exc_info(), data={"username": username, "orgname": orgname})
finally: raise Exception('Error trying to OBS_GetBoundaryById')
quota_service.increment_total_service_use() finally:
quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetBoundariesByGeometry( CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetBoundariesByGeometry(
@@ -171,6 +177,7 @@ CREATE OR REPLACE FUNCTION cdb_dataservices_server.OBS_GetBoundariesByGeometry(
time_span TEXT DEFAULT NULL, time_span TEXT DEFAULT NULL,
overlap_type TEXT DEFAULT NULL) overlap_type TEXT DEFAULT NULL)
RETURNS TABLE(the_geom geometry, geom_refs text) AS $$ RETURNS TABLE(the_geom geometry, geom_refs text) AS $$
from cartodb_services.metrics import metrics
from cartodb_services.metrics import QuotaService from cartodb_services.metrics import QuotaService
from cartodb_services.tools import Logger,LoggerConfig from cartodb_services.tools import Logger,LoggerConfig
@@ -186,27 +193,28 @@ RETURNS TABLE(the_geom geometry, geom_refs text) AS $$
if not quota_service.check_user_quota(): if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota') raise Exception('You have reached the limit of your quota')
try: with metrics('obs_getboundariesbygeometry', user_obs_snapshot_config):
obs_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._OBS_GetBoundariesByGeometry($1, $2, $3, $4, $5, $6) as boundary;", ["text", "text", "geometry(Point, 4326)", "text", "text", "text"]) try:
result = plpy.execute(obs_plan, [username, orgname, geom, boundary_id, time_span, overlap_type]) obs_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._OBS_GetBoundariesByGeometry($1, $2, $3, $4, $5, $6) as boundary;", ["text", "text", "geometry(Point, 4326)", "text", "text", "text"])
if result: result = plpy.execute(obs_plan, [username, orgname, geom, boundary_id, time_span, overlap_type])
resp = [] if result:
for element in result: resp = []
the_geom = element['the_geom'] for element in result:
geom_refs = element['geom_refs'] the_geom = element['the_geom']
resp.append([the_geom, geom_refs]) geom_refs = element['geom_refs']
quota_service.increment_success_service_use() resp.append([the_geom, geom_refs])
return resp quota_service.increment_success_service_use()
else: return resp
quota_service.increment_empty_service_use() else:
return [] quota_service.increment_empty_service_use()
except BaseException as e: return []
import sys except BaseException as e:
quota_service.increment_failed_service_use() import sys
logger.error('Error trying to OBS_GetBoundariesByGeometry', sys.exc_info(), data={"username": username, "orgname": orgname}) quota_service.increment_failed_service_use()
raise Exception('Error trying to OBS_GetBoundariesByGeometry') logger.error('Error trying to OBS_GetBoundariesByGeometry', sys.exc_info(), data={"username": username, "orgname": orgname})
finally: raise Exception('Error trying to OBS_GetBoundariesByGeometry')
quota_service.increment_total_service_use() finally:
quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetBoundariesByPointAndRadius( CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetBoundariesByPointAndRadius(
@@ -231,6 +239,7 @@ CREATE OR REPLACE FUNCTION cdb_dataservices_server.OBS_GetBoundariesByPointAndRa
time_span TEXT DEFAULT NULL, time_span TEXT DEFAULT NULL,
overlap_type TEXT DEFAULT NULL) overlap_type TEXT DEFAULT NULL)
RETURNS TABLE(the_geom geometry, geom_refs text) AS $$ RETURNS TABLE(the_geom geometry, geom_refs text) AS $$
from cartodb_services.metrics import metrics
from cartodb_services.metrics import QuotaService from cartodb_services.metrics import QuotaService
from cartodb_services.tools import Logger,LoggerConfig from cartodb_services.tools import Logger,LoggerConfig
@@ -246,27 +255,28 @@ RETURNS TABLE(the_geom geometry, geom_refs text) AS $$
if not quota_service.check_user_quota(): if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota') raise Exception('You have reached the limit of your quota')
try: with metrics('obs_getboundariesbypointandradius', user_obs_snapshot_config):
obs_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._OBS_GetBoundariesByPointAndRadius($1, $2, $3, $4, $5, $6, $7) as boundary;", ["text", "text", "geometry(Point, 4326)", "numeric", "text", "text", "text"]) try:
result = plpy.execute(obs_plan, [username, orgname, geom, radius, boundary_id, time_span, overlap_type]) obs_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._OBS_GetBoundariesByPointAndRadius($1, $2, $3, $4, $5, $6, $7) as boundary;", ["text", "text", "geometry(Point, 4326)", "numeric", "text", "text", "text"])
if result: result = plpy.execute(obs_plan, [username, orgname, geom, radius, boundary_id, time_span, overlap_type])
resp = [] if result:
for element in result: resp = []
the_geom = element['the_geom'] for element in result:
geom_refs = element['geom_refs'] the_geom = element['the_geom']
resp.append([the_geom, geom_refs]) geom_refs = element['geom_refs']
quota_service.increment_success_service_use() resp.append([the_geom, geom_refs])
return resp quota_service.increment_success_service_use()
else: return resp
quota_service.increment_empty_service_use() else:
return [] quota_service.increment_empty_service_use()
except BaseException as e: return []
import sys except BaseException as e:
quota_service.increment_failed_service_use() import sys
logger.error('Error trying to OBS_GetBoundariesByPointAndRadius', sys.exc_info(), data={"username": username, "orgname": orgname}) quota_service.increment_failed_service_use()
raise Exception('Error trying to OBS_GetBoundariesByPointAndRadius') logger.error('Error trying to OBS_GetBoundariesByPointAndRadius', sys.exc_info(), data={"username": username, "orgname": orgname})
finally: raise Exception('Error trying to OBS_GetBoundariesByPointAndRadius')
quota_service.increment_total_service_use() finally:
quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetPointsByGeometry( CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetPointsByGeometry(
@@ -289,6 +299,7 @@ CREATE OR REPLACE FUNCTION cdb_dataservices_server.OBS_GetPointsByGeometry(
time_span TEXT DEFAULT NULL, time_span TEXT DEFAULT NULL,
overlap_type TEXT DEFAULT NULL) overlap_type TEXT DEFAULT NULL)
RETURNS TABLE(the_geom geometry, geom_refs text) AS $$ RETURNS TABLE(the_geom geometry, geom_refs text) AS $$
from cartodb_services.metrics import metrics
from cartodb_services.metrics import QuotaService from cartodb_services.metrics import QuotaService
from cartodb_services.tools import Logger,LoggerConfig from cartodb_services.tools import Logger,LoggerConfig
@@ -304,27 +315,28 @@ RETURNS TABLE(the_geom geometry, geom_refs text) AS $$
if not quota_service.check_user_quota(): if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota') raise Exception('You have reached the limit of your quota')
try: with metrics('obs_getpointsbygeometry', user_obs_snapshot_config):
obs_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._OBS_GetPointsByGeometry($1, $2, $3, $4, $5, $6) as boundary;", ["text", "text", "geometry(Point, 4326)", "text", "text", "text"]) try:
result = plpy.execute(obs_plan, [username, orgname, geom, boundary_id, time_span, overlap_type]) obs_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._OBS_GetPointsByGeometry($1, $2, $3, $4, $5, $6) as boundary;", ["text", "text", "geometry(Point, 4326)", "text", "text", "text"])
if result: result = plpy.execute(obs_plan, [username, orgname, geom, boundary_id, time_span, overlap_type])
resp = [] if result:
for element in result: resp = []
the_geom = element['the_geom'] for element in result:
geom_refs = element['geom_refs'] the_geom = element['the_geom']
resp.append([the_geom, geom_refs]) geom_refs = element['geom_refs']
quota_service.increment_success_service_use() resp.append([the_geom, geom_refs])
return resp quota_service.increment_success_service_use()
else: return resp
quota_service.increment_empty_service_use() else:
return [] quota_service.increment_empty_service_use()
except BaseException as e: return []
import sys except BaseException as e:
quota_service.increment_failed_service_use() import sys
logger.error('Error trying to OBS_GetPointsByGeometry', sys.exc_info(), data={"username": username, "orgname": orgname}) quota_service.increment_failed_service_use()
raise Exception('Error trying to OBS_GetPointsByGeometry') logger.error('Error trying to OBS_GetPointsByGeometry', sys.exc_info(), data={"username": username, "orgname": orgname})
finally: raise Exception('Error trying to OBS_GetPointsByGeometry')
quota_service.increment_total_service_use() finally:
quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetPointsByPointAndRadius( CREATE OR REPLACE FUNCTION cdb_dataservices_server._OBS_GetPointsByPointAndRadius(
@@ -349,6 +361,7 @@ CREATE OR REPLACE FUNCTION cdb_dataservices_server.OBS_GetPointsByPointAndRadius
time_span TEXT DEFAULT NULL, time_span TEXT DEFAULT NULL,
overlap_type TEXT DEFAULT NULL) overlap_type TEXT DEFAULT NULL)
RETURNS TABLE(the_geom geometry, geom_refs text) AS $$ RETURNS TABLE(the_geom geometry, geom_refs text) AS $$
from cartodb_services.metrics import metrics
from cartodb_services.metrics import QuotaService from cartodb_services.metrics import QuotaService
from cartodb_services.tools import Logger,LoggerConfig from cartodb_services.tools import Logger,LoggerConfig
@@ -364,25 +377,26 @@ RETURNS TABLE(the_geom geometry, geom_refs text) AS $$
if not quota_service.check_user_quota(): if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota') raise Exception('You have reached the limit of your quota')
try: with metrics('obs_getpointsbypointandradius', user_obs_snapshot_config):
obs_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._OBS_GetPointsByPointAndRadius($1, $2, $3, $4, $5, $6, $7) as boundary;", ["text", "text", "geometry(Point, 4326)", "numeric", "text", "text", "text"]) try:
result = plpy.execute(obs_plan, [username, orgname, geom, radius, boundary_id, time_span, overlap_type]) obs_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server._OBS_GetPointsByPointAndRadius($1, $2, $3, $4, $5, $6, $7) as boundary;", ["text", "text", "geometry(Point, 4326)", "numeric", "text", "text", "text"])
if result: result = plpy.execute(obs_plan, [username, orgname, geom, radius, boundary_id, time_span, overlap_type])
resp = [] if result:
for element in result: resp = []
the_geom = element['the_geom'] for element in result:
geom_refs = element['geom_refs'] the_geom = element['the_geom']
resp.append([the_geom, geom_refs]) geom_refs = element['geom_refs']
quota_service.increment_success_service_use() resp.append([the_geom, geom_refs])
return resp quota_service.increment_success_service_use()
else: return resp
quota_service.increment_empty_service_use() else:
return None quota_service.increment_empty_service_use()
except BaseException as e: return None
import sys except BaseException as e:
quota_service.increment_failed_service_use() import sys
logger.error('Error trying to OBS_GetPointsByPointAndRadius', sys.exc_info(), data={"username": username, "orgname": orgname}) quota_service.increment_failed_service_use()
raise Exception('Error trying to OBS_GetPointsByPointAndRadius') logger.error('Error trying to OBS_GetPointsByPointAndRadius', sys.exc_info(), data={"username": username, "orgname": orgname})
finally: raise Exception('Error trying to OBS_GetPointsByPointAndRadius')
quota_service.increment_total_service_use() finally:
quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
+13 -11
View File
@@ -1,22 +1,24 @@
-- Geocodes a street address given a searchtext and a state and/or country -- Geocodes a street address given a searchtext and a state and/or country
CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_geocode_street_point(username TEXT, orgname TEXT, searchtext TEXT, city TEXT DEFAULT NULL, state_province TEXT DEFAULT NULL, country TEXT DEFAULT NULL) CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_geocode_street_point(username TEXT, orgname TEXT, searchtext TEXT, city TEXT DEFAULT NULL, state_province TEXT DEFAULT NULL, country TEXT DEFAULT NULL)
RETURNS Geometry AS $$ RETURNS Geometry AS $$
from cartodb_services.metrics import metrics
plpy.execute("SELECT cdb_dataservices_server._connect_to_redis('{0}')".format(username)) plpy.execute("SELECT cdb_dataservices_server._connect_to_redis('{0}')".format(username))
redis_conn = GD["redis_connection_{0}".format(username)]['redis_metrics_connection'] redis_conn = GD["redis_connection_{0}".format(username)]['redis_metrics_connection']
plpy.execute("SELECT cdb_dataservices_server._get_geocoder_config({0}, {1})".format(plpy.quote_nullable(username), plpy.quote_nullable(orgname))) 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_geocoder_config = GD["user_geocoder_config_{0}".format(username)]
if user_geocoder_config.heremaps_geocoder: with metrics('cdb_geocode_street_point', user_geocoder_config):
here_plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_here_geocode_street_point($1, $2, $3, $4, $5, $6) as point; ", ["text", "text", "text", "text", "text", "text"]) if user_geocoder_config.heremaps_geocoder:
return plpy.execute(here_plan, [username, orgname, searchtext, city, state_province, country], 1)[0]['point'] here_plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_here_geocode_street_point($1, $2, $3, $4, $5, $6) as point; ", ["text", "text", "text", "text", "text", "text"])
elif user_geocoder_config.google_geocoder: return plpy.execute(here_plan, [username, orgname, searchtext, city, state_province, country], 1)[0]['point']
google_plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_google_geocode_street_point($1, $2, $3, $4, $5, $6) as point; ", ["text", "text", "text", "text", "text", "text"]) elif user_geocoder_config.google_geocoder:
return plpy.execute(google_plan, [username, orgname, searchtext, city, state_province, country], 1)[0]['point'] google_plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_google_geocode_street_point($1, $2, $3, $4, $5, $6) as point; ", ["text", "text", "text", "text", "text", "text"])
elif user_geocoder_config.mapzen_geocoder: return plpy.execute(google_plan, [username, orgname, searchtext, city, state_province, country], 1)[0]['point']
mapzen_plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_mapzen_geocode_street_point($1, $2, $3, $4, $5, $6) as point; ", ["text", "text", "text", "text", "text", "text"]) elif user_geocoder_config.mapzen_geocoder:
return plpy.execute(mapzen_plan, [username, orgname, searchtext, city, state_province, country], 1)[0]['point'] mapzen_plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_mapzen_geocode_street_point($1, $2, $3, $4, $5, $6) as point; ", ["text", "text", "text", "text", "text", "text"])
else: return plpy.execute(mapzen_plan, [username, orgname, searchtext, city, state_province, country], 1)[0]['point']
raise Exception('Requested geocoder is not available') else:
raise Exception('Requested geocoder is not available')
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
+42 -36
View File
@@ -1,6 +1,7 @@
---- cdb_geocode_admin1_polygon(admin1_name text) ---- cdb_geocode_admin1_polygon(admin1_name text)
CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_geocode_admin1_polygon(username text, orgname text, admin1_name text) CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_geocode_admin1_polygon(username text, orgname text, admin1_name text)
RETURNS Geometry AS $$ RETURNS Geometry AS $$
from cartodb_services.metrics import metrics
from cartodb_services.metrics import QuotaService from cartodb_services.metrics import QuotaService
from cartodb_services.metrics import InternalGeocoderConfig from cartodb_services.metrics import InternalGeocoderConfig
from cartodb_services.tools import Logger,LoggerConfig from cartodb_services.tools import Logger,LoggerConfig
@@ -14,44 +15,11 @@ RETURNS Geometry AS $$
logger_config = GD["logger_config"] logger_config = GD["logger_config"]
logger = Logger(logger_config) logger = Logger(logger_config)
quota_service = QuotaService(user_geocoder_config, redis_conn) quota_service = QuotaService(user_geocoder_config, redis_conn)
try:
plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_geocode_admin1_polygon(trim($1)) AS mypolygon", ["text"])
rv = plpy.execute(plan, [admin1_name], 1)
result = rv[0]["mypolygon"]
if result:
quota_service.increment_success_service_use()
return result
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 geocode admin1 polygon', sys.exc_info(), data={"username": username, "orgname": orgname})
raise Exception('Error trying to geocode admin1 polygon')
finally:
quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu;
---- cdb_geocode_admin1_polygon(admin1_name text, country_name text) with metrics('cdb_geocode_admin1_polygon', user_geocoder_config):
CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_geocode_admin1_polygon(username text, orgname text, admin1_name text, country_name text)
RETURNS Geometry AS $$
from cartodb_services.metrics import QuotaService
from cartodb_services.metrics import InternalGeocoderConfig
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_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)]
plpy.execute("SELECT cdb_dataservices_server._get_logger_config()")
logger_config = GD["logger_config"]
logger = Logger(logger_config)
quota_service = QuotaService(user_geocoder_config, redis_conn)
try: try:
plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_geocode_admin1_polygon(trim($1), trim($2)) AS mypolygon", ["text", "text"]) plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_geocode_admin1_polygon(trim($1)) AS mypolygon", ["text"])
rv = plpy.execute(plan, [admin1_name, country_name], 1) rv = plpy.execute(plan, [admin1_name], 1)
result = rv[0]["mypolygon"] result = rv[0]["mypolygon"]
if result: if result:
quota_service.increment_success_service_use() quota_service.increment_success_service_use()
@@ -68,6 +36,44 @@ RETURNS Geometry AS $$
quota_service.increment_total_service_use() quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
---- cdb_geocode_admin1_polygon(admin1_name text, country_name text)
CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_geocode_admin1_polygon(username text, orgname text, admin1_name text, country_name text)
RETURNS Geometry AS $$
from cartodb_services.metrics import metrics
from cartodb_services.metrics import QuotaService
from cartodb_services.metrics import InternalGeocoderConfig
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_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)]
plpy.execute("SELECT cdb_dataservices_server._get_logger_config()")
logger_config = GD["logger_config"]
logger = Logger(logger_config)
quota_service = QuotaService(user_geocoder_config, redis_conn)
with metrics('cdb_geocode_admin1_polygon', user_geocoder_config):
try:
plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_geocode_admin1_polygon(trim($1), trim($2)) AS mypolygon", ["text", "text"])
rv = plpy.execute(plan, [admin1_name, country_name], 1)
result = rv[0]["mypolygon"]
if result:
quota_service.increment_success_service_use()
return result
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 geocode admin1 polygon', sys.exc_info(), data={"username": username, "orgname": orgname})
raise Exception('Error trying to geocode admin1 polygon')
finally:
quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu;
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Implementation of the server extension -- Implementation of the server extension
+52 -49
View File
@@ -35,7 +35,7 @@ CREATE OR REPLACE FUNCTION cdb_dataservices_server._cdb_mapzen_geocode_namedplac
RETURNS Geometry AS $$ RETURNS Geometry AS $$
from cartodb_services.mapzen import MapzenGeocoder from cartodb_services.mapzen import MapzenGeocoder
from cartodb_services.mapzen.types import country_to_iso3 from cartodb_services.mapzen.types import country_to_iso3
from cartodb_services.metrics import QuotaService from cartodb_services.metrics import QuotaService, metrics
from cartodb_services.tools import Logger,LoggerConfig from cartodb_services.tools import Logger,LoggerConfig
plpy.execute("SELECT cdb_dataservices_server._connect_to_redis('{0}')".format(username)) plpy.execute("SELECT cdb_dataservices_server._connect_to_redis('{0}')".format(username))
@@ -50,35 +50,36 @@ RETURNS Geometry AS $$
if not quota_service.check_user_quota(): if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota') raise Exception('You have reached the limit of your quota')
try: with metrics('cdb_geocode_namedplace_point', user_geocoder_config, logger):
geocoder = MapzenGeocoder(user_geocoder_config.mapzen_api_key, logger) try:
country_iso3 = None geocoder = MapzenGeocoder(user_geocoder_config.mapzen_api_key, logger)
if country_name: country_iso3 = None
country_iso3 = country_to_iso3(country_name) if country_name:
coordinates = geocoder.geocode(searchtext=city_name, city=None, country_iso3 = country_to_iso3(country_name)
state_province=admin1_name, coordinates = geocoder.geocode(searchtext=city_name, city=None,
country=country_iso3, search_type='locality') state_province=admin1_name,
if coordinates: country=country_iso3, search_type='locality')
quota_service.increment_success_service_use() if coordinates:
plan = plpy.prepare("SELECT ST_SetSRID(ST_MakePoint($1, $2), 4326); ", ["double precision", "double precision"]) quota_service.increment_success_service_use()
point = plpy.execute(plan, [coordinates[0], coordinates[1]], 1)[0] plan = plpy.prepare("SELECT ST_SetSRID(ST_MakePoint($1, $2), 4326); ", ["double precision", "double precision"])
return point['st_setsrid'] point = plpy.execute(plan, [coordinates[0], coordinates[1]], 1)[0]
else: return point['st_setsrid']
quota_service.increment_empty_service_use() else:
return None quota_service.increment_empty_service_use()
except BaseException as e: return None
import sys except BaseException as e:
quota_service.increment_failed_service_use() import sys
logger.error('Error trying to geocode city point using mapzen', sys.exc_info(), data={"username": username, "orgname": orgname}) quota_service.increment_failed_service_use()
raise Exception('Error trying to geocode city point using mapzen') logger.error('Error trying to geocode city point using mapzen', sys.exc_info(), data={"username": username, "orgname": orgname})
finally: raise Exception('Error trying to geocode city point using mapzen')
quota_service.increment_total_service_use() finally:
quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
CREATE OR REPLACE FUNCTION cdb_dataservices_server._cdb_internal_geocode_namedplace(username text, orgname text, city_name text, admin1_name text DEFAULT NULL, country_name text DEFAULT NULL) CREATE OR REPLACE FUNCTION cdb_dataservices_server._cdb_internal_geocode_namedplace(username text, orgname text, city_name text, admin1_name text DEFAULT NULL, country_name text DEFAULT NULL)
RETURNS Geometry AS $$ RETURNS Geometry AS $$
from cartodb_services.metrics import QuotaService from cartodb_services.metrics import QuotaService
from cartodb_services.metrics import InternalGeocoderConfig from cartodb_services.metrics import InternalGeocoderConfig, metrics
from cartodb_services.tools import Logger,LoggerConfig from cartodb_services.tools import Logger,LoggerConfig
plpy.execute("SELECT cdb_dataservices_server._connect_to_redis('{0}')".format(username)) plpy.execute("SELECT cdb_dataservices_server._connect_to_redis('{0}')".format(username))
@@ -90,30 +91,32 @@ RETURNS Geometry AS $$
logger_config = GD["logger_config"] logger_config = GD["logger_config"]
logger = Logger(logger_config) logger = Logger(logger_config)
quota_service = QuotaService(user_geocoder_config, redis_conn) quota_service = QuotaService(user_geocoder_config, redis_conn)
try:
if admin1_name and country_name: with metrics('cdb_geocode_namedplace_point', user_geocoder_config, logger):
plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_geocode_namedplace_point(trim($1), trim($2), trim($3)) AS mypoint", ["text", "text", "text"]) try:
rv = plpy.execute(plan, [city_name, admin1_name, country_name], 1) if admin1_name and country_name:
elif country_name: plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_geocode_namedplace_point(trim($1), trim($2), trim($3)) AS mypoint", ["text", "text", "text"])
plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_geocode_namedplace_point(trim($1), trim($2)) AS mypoint", ["text", "text"]) rv = plpy.execute(plan, [city_name, admin1_name, country_name], 1)
rv = plpy.execute(plan, [city_name, country_name], 1) elif country_name:
else: plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_geocode_namedplace_point(trim($1), trim($2)) AS mypoint", ["text", "text"])
plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_geocode_namedplace_point(trim($1)) AS mypoint", ["text"]) rv = plpy.execute(plan, [city_name, country_name], 1)
rv = plpy.execute(plan, [city_name], 1) else:
result = rv[0]["mypoint"] plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_geocode_namedplace_point(trim($1)) AS mypoint", ["text"])
if result: rv = plpy.execute(plan, [city_name], 1)
quota_service.increment_success_service_use() result = rv[0]["mypoint"]
return result if result:
else: quota_service.increment_success_service_use()
quota_service.increment_empty_service_use() return result
return None else:
except BaseException as e: quota_service.increment_empty_service_use()
import sys return None
quota_service.increment_failed_service_use() except BaseException as e:
logger.error('Error trying to geocode namedplace point', sys.exc_info(), data={"username": username, "orgname": orgname}) import sys
raise Exception('Error trying to geocode namedplace point') quota_service.increment_failed_service_use()
finally: logger.error('Error trying to geocode namedplace point', sys.exc_info(), data={"username": username, "orgname": orgname})
quota_service.increment_total_service_use() raise Exception('Error trying to geocode namedplace point')
finally:
quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
+76 -68
View File
@@ -1,5 +1,6 @@
CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_geocode_postalcode_point(username text, orgname text, code text) CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_geocode_postalcode_point(username text, orgname text, code text)
RETURNS Geometry AS $$ RETURNS Geometry AS $$
from cartodb_services.metrics import metrics
from cartodb_services.metrics import QuotaService from cartodb_services.metrics import QuotaService
from cartodb_services.metrics import InternalGeocoderConfig from cartodb_services.metrics import InternalGeocoderConfig
from cartodb_services.tools import Logger,LoggerConfig from cartodb_services.tools import Logger,LoggerConfig
@@ -13,27 +14,29 @@ RETURNS Geometry AS $$
logger_config = GD["logger_config"] logger_config = GD["logger_config"]
logger = Logger(logger_config) logger = Logger(logger_config)
quota_service = QuotaService(user_geocoder_config, redis_conn) quota_service = QuotaService(user_geocoder_config, redis_conn)
try: with metrics('cdb_geocode_postalcode_point', user_geocoder_config):
plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_geocode_postalcode_point(trim($1)) AS mypoint", ["text"]) try:
rv = plpy.execute(plan, [code], 1) plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_geocode_postalcode_point(trim($1)) AS mypoint", ["text"])
result = rv[0]["mypoint"] rv = plpy.execute(plan, [code], 1)
if result: result = rv[0]["mypoint"]
quota_service.increment_success_service_use() if result:
return result quota_service.increment_success_service_use()
else: return result
quota_service.increment_empty_service_use() else:
return None quota_service.increment_empty_service_use()
except BaseException as e: return None
import sys except BaseException as e:
quota_service.increment_failed_service_use() import sys
logger.error('Error trying to geocode postal code point', sys.exc_info(), data={"username": username, "orgname": orgname}) quota_service.increment_failed_service_use()
raise Exception('Error trying to geocode postal code point') logger.error('Error trying to geocode postal code point', sys.exc_info(), data={"username": username, "orgname": orgname})
finally: raise Exception('Error trying to geocode postal code point')
quota_service.increment_total_service_use() finally:
quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_geocode_postalcode_point(username text, orgname text, code text, country text) CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_geocode_postalcode_point(username text, orgname text, code text, country text)
RETURNS Geometry AS $$ RETURNS Geometry AS $$
from cartodb_services.metrics import metrics
from cartodb_services.metrics import QuotaService from cartodb_services.metrics import QuotaService
from cartodb_services.metrics import InternalGeocoderConfig from cartodb_services.metrics import InternalGeocoderConfig
from cartodb_services.tools import Logger,LoggerConfig from cartodb_services.tools import Logger,LoggerConfig
@@ -47,27 +50,29 @@ RETURNS Geometry AS $$
logger_config = GD["logger_config"] logger_config = GD["logger_config"]
logger = Logger(logger_config) logger = Logger(logger_config)
quota_service = QuotaService(user_geocoder_config, redis_conn) quota_service = QuotaService(user_geocoder_config, redis_conn)
try: with metrics('cdb_geocode_postalcode_point', user_geocoder_config):
plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_geocode_postalcode_point(trim($1), trim($2)) AS mypoint", ["TEXT", "TEXT"]) try:
rv = plpy.execute(plan, [code, country], 1) plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_geocode_postalcode_point(trim($1), trim($2)) AS mypoint", ["TEXT", "TEXT"])
result = rv[0]["mypoint"] rv = plpy.execute(plan, [code, country], 1)
if result: result = rv[0]["mypoint"]
quota_service.increment_success_service_use() if result:
return result quota_service.increment_success_service_use()
else: return result
quota_service.increment_empty_service_use() else:
return None quota_service.increment_empty_service_use()
except BaseException as e: return None
import sys except BaseException as e:
quota_service.increment_failed_service_use() import sys
logger.error('Error trying to geocode postal code point', sys.exc_info(), data={"username": username, "orgname": orgname}) quota_service.increment_failed_service_use()
raise Exception('Error trying to geocode postal code point') logger.error('Error trying to geocode postal code point', sys.exc_info(), data={"username": username, "orgname": orgname})
finally: raise Exception('Error trying to geocode postal code point')
quota_service.increment_total_service_use() finally:
quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_geocode_postalcode_polygon(username text, orgname text, code text) CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_geocode_postalcode_polygon(username text, orgname text, code text)
RETURNS Geometry AS $$ RETURNS Geometry AS $$
from cartodb_services.metrics import metrics
from cartodb_services.metrics import QuotaService from cartodb_services.metrics import QuotaService
from cartodb_services.metrics import InternalGeocoderConfig from cartodb_services.metrics import InternalGeocoderConfig
from cartodb_services.tools import Logger,LoggerConfig from cartodb_services.tools import Logger,LoggerConfig
@@ -81,27 +86,29 @@ RETURNS Geometry AS $$
logger_config = GD["logger_config"] logger_config = GD["logger_config"]
logger = Logger(logger_config) logger = Logger(logger_config)
quota_service = QuotaService(user_geocoder_config, redis_conn) quota_service = QuotaService(user_geocoder_config, redis_conn)
try: with metrics('cdb_geocode_postalcode_point', user_geocoder_config):
plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_geocode_postalcode_polygon(trim($1)) AS mypolygon", ["text"]) try:
rv = plpy.execute(plan, [code], 1) plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_geocode_postalcode_polygon(trim($1)) AS mypolygon", ["text"])
result = rv[0]["mypolygon"] rv = plpy.execute(plan, [code], 1)
if result: result = rv[0]["mypolygon"]
quota_service.increment_success_service_use() if result:
return result quota_service.increment_success_service_use()
else: return result
quota_service.increment_empty_service_use() else:
return None quota_service.increment_empty_service_use()
except BaseException as e: return None
import sys except BaseException as e:
quota_service.increment_failed_service_use() import sys
logger.error('Error trying to geocode postal code polygon', sys.exc_info(), data={"username": username, "orgname": orgname}) quota_service.increment_failed_service_use()
raise Exception('Error trying to geocode postal code polygon') logger.error('Error trying to geocode postal code polygon', sys.exc_info(), data={"username": username, "orgname": orgname})
finally: raise Exception('Error trying to geocode postal code polygon')
quota_service.increment_total_service_use() finally:
quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_geocode_postalcode_polygon(username text, orgname text, code text, country text) CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_geocode_postalcode_polygon(username text, orgname text, code text, country text)
RETURNS Geometry AS $$ RETURNS Geometry AS $$
from cartodb_services.metrics import metrics
from cartodb_services.metrics import QuotaService from cartodb_services.metrics import QuotaService
from cartodb_services.metrics import InternalGeocoderConfig from cartodb_services.metrics import InternalGeocoderConfig
from cartodb_services.tools import Logger,LoggerConfig from cartodb_services.tools import Logger,LoggerConfig
@@ -115,23 +122,24 @@ RETURNS Geometry AS $$
logger_config = GD["logger_config"] logger_config = GD["logger_config"]
logger = Logger(logger_config) logger = Logger(logger_config)
quota_service = QuotaService(user_geocoder_config, redis_conn) quota_service = QuotaService(user_geocoder_config, redis_conn)
try: with metrics('cdb_geocode_postalcode_point', user_geocoder_config):
plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_geocode_postalcode_polygon(trim($1), trim($2)) AS mypolygon", ["TEXT", "TEXT"]) try:
rv = plpy.execute(plan, [code, country], 1) plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_geocode_postalcode_polygon(trim($1), trim($2)) AS mypolygon", ["TEXT", "TEXT"])
result = rv[0]["mypolygon"] rv = plpy.execute(plan, [code, country], 1)
if result: result = rv[0]["mypolygon"]
quota_service.increment_success_service_use() if result:
return result quota_service.increment_success_service_use()
else: return result
quota_service.increment_empty_service_use() else:
return None quota_service.increment_empty_service_use()
except BaseException as e: return None
import sys except BaseException as e:
quota_service.increment_failed_service_use() import sys
logger.error('Error trying to geocode postal code polygon', sys.exc_info(), data={"username": username, "orgname": orgname}) quota_service.increment_failed_service_use()
raise Exception('Error trying to geocode postal code polygon') logger.error('Error trying to geocode postal code polygon', sys.exc_info(), data={"username": username, "orgname": orgname})
finally: raise Exception('Error trying to geocode postal code polygon')
quota_service.increment_total_service_use() finally:
quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
+19 -17
View File
@@ -1,5 +1,6 @@
CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_geocode_ipaddress_point(username text, orgname text, ip text) CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_geocode_ipaddress_point(username text, orgname text, ip text)
RETURNS Geometry AS $$ RETURNS Geometry AS $$
from cartodb_services.metrics import metrics
from cartodb_services.metrics import QuotaService from cartodb_services.metrics import QuotaService
from cartodb_services.metrics import InternalGeocoderConfig from cartodb_services.metrics import InternalGeocoderConfig
from cartodb_services.tools import Logger,LoggerConfig from cartodb_services.tools import Logger,LoggerConfig
@@ -13,23 +14,24 @@ RETURNS Geometry AS $$
logger_config = GD["logger_config"] logger_config = GD["logger_config"]
logger = Logger(logger_config) logger = Logger(logger_config)
quota_service = QuotaService(user_geocoder_config, redis_conn) quota_service = QuotaService(user_geocoder_config, redis_conn)
try: with metrics('cdb_geocode_ipaddress_point', user_geocoder_config):
plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_geocode_ipaddress_point(trim($1)) AS mypoint", ["TEXT"]) try:
rv = plpy.execute(plan, [ip], 1) plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_geocode_ipaddress_point(trim($1)) AS mypoint", ["TEXT"])
result = rv[0]["mypoint"] rv = plpy.execute(plan, [ip], 1)
if result: result = rv[0]["mypoint"]
quota_service.increment_success_service_use() if result:
return result quota_service.increment_success_service_use()
else: return result
quota_service.increment_empty_service_use() else:
return None quota_service.increment_empty_service_use()
except BaseException as e: return None
import sys except BaseException as e:
quota_service.increment_failed_service_use() import sys
logger.error('Error trying to geocode postal code polygon', sys.exc_info(), data={"username": username, "orgname": orgname}) quota_service.increment_failed_service_use()
raise Exception('Error trying to geocode postal code polygon') logger.error('Error trying to geocode postal code polygon', sys.exc_info(), data={"username": username, "orgname": orgname})
finally: raise Exception('Error trying to geocode postal code polygon')
quota_service.increment_total_service_use() finally:
quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
+11 -8
View File
@@ -1,5 +1,7 @@
CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_isochrone(username TEXT, orgname TEXT, source geometry(Geometry, 4326), mode TEXT, range integer[], options text[] DEFAULT array[]::text[]) CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_isochrone(username TEXT, orgname TEXT, source geometry(Geometry, 4326), mode TEXT, range integer[], options text[] DEFAULT array[]::text[])
RETURNS SETOF cdb_dataservices_server.isoline AS $$ RETURNS SETOF cdb_dataservices_server.isoline AS $$
from cartodb_services.metrics import metrics
plpy.execute("SELECT cdb_dataservices_server._connect_to_redis('{0}')".format(username)) plpy.execute("SELECT cdb_dataservices_server._connect_to_redis('{0}')".format(username))
redis_conn = GD["redis_connection_{0}".format(username)]['redis_metrics_connection'] redis_conn = GD["redis_connection_{0}".format(username)]['redis_metrics_connection']
plpy.execute("SELECT cdb_dataservices_server._get_isolines_routing_config({0}, {1})".format(plpy.quote_nullable(username), plpy.quote_nullable(orgname))) plpy.execute("SELECT cdb_dataservices_server._get_isolines_routing_config({0}, {1})".format(plpy.quote_nullable(username), plpy.quote_nullable(orgname)))
@@ -8,14 +10,15 @@ RETURNS SETOF cdb_dataservices_server.isoline AS $$
if user_isolines_config.google_services_user: if user_isolines_config.google_services_user:
raise Exception('This service is not available for google service users.') raise Exception('This service is not available for google service users.')
if user_isolines_config.heremaps_provider: with metrics('cb_isochrone', user_isolines_config):
here_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server.cdb_here_isochrone($1, $2, $3, $4, $5, $6) as isoline; ", ["text", "text", "geometry(geometry, 4326)", "text", "integer[]", "text[]"]) if user_isolines_config.heremaps_provider:
return plpy.execute(here_plan, [username, orgname, source, mode, range, options]) here_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server.cdb_here_isochrone($1, $2, $3, $4, $5, $6) as isoline; ", ["text", "text", "geometry(geometry, 4326)", "text", "integer[]", "text[]"])
elif user_isolines_config.mapzen_provider: return plpy.execute(here_plan, [username, orgname, source, mode, range, options])
mapzen_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server.cdb_mapzen_isochrone($1, $2, $3, $4, $5, $6) as isoline; ", ["text", "text", "geometry(geometry, 4326)", "text", "integer[]", "text[]"]) elif user_isolines_config.mapzen_provider:
return plpy.execute(mapzen_plan, [username, orgname, source, mode, range, options]) mapzen_plan = plpy.prepare("SELECT * FROM cdb_dataservices_server.cdb_mapzen_isochrone($1, $2, $3, $4, $5, $6) as isoline; ", ["text", "text", "geometry(geometry, 4326)", "text", "integer[]", "text[]"])
else: return plpy.execute(mapzen_plan, [username, orgname, source, mode, range, options])
raise Exception('Requested isolines provider is not available') else:
raise Exception('Requested isolines provider is not available')
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
-- heremaps isochrone -- heremaps isochrone