Add Mapzen Matrix to config and metrics services

This commit is contained in:
Carla Iriberri
2016-07-06 12:40:31 +02:00
committed by Rafa de la Torre
parent cdcac2dc1f
commit a046d3ce97
6 changed files with 76 additions and 20 deletions

View File

@@ -26,6 +26,20 @@ RETURNS boolean AS $$
return True
$$ LANGUAGE plpythonu SECURITY DEFINER;
CREATE OR REPLACE FUNCTION cdb_dataservices_server._get_mapzen_isolines_config(username text, orgname text)
RETURNS boolean AS $$
cache_key = "user_mapzen_isolines_routing_config_{0}".format(username)
if cache_key in GD:
return False
else:
from cartodb_services.metrics import MapzenIsolinesRoutingConfig
plpy.execute("SELECT cdb_dataservices_server._connect_to_redis('{0}')".format(username))
redis_conn = GD["redis_connection_{0}".format(username)]['redis_metadata_connection']
mapzen_isolines_config = MapzenIsolinesRoutingConfig(redis_conn, plpy, username, orgname)
GD[cache_key] = mapzen_isolines_config
return True
$$ LANGUAGE plpythonu SECURITY DEFINER;
CREATE OR REPLACE FUNCTION cdb_dataservices_server._get_internal_geocoder_config(username text, orgname text)
RETURNS boolean AS $$
cache_key = "user_internal_geocoder_config_{0}".format(username)

View File

@@ -71,20 +71,15 @@ RETURNS SETOF cdb_dataservices_server.isoline AS $$
from cartodb_services.here.types import geo_polyline_to_multipolygon # TODO do we use the same types?
redis_conn = GD["redis_connection_{0}".format(username)]['redis_metrics_connection']
user_isolines_routing_config = GD["user_isolines_routing_config_{0}".format(username)]
user_mapzen_isolines_routing_config = GD["user_mapzen_isolines_routing_config_{0}".format(username)]
# -- Check the quota
#quota_service = QuotaService(user_isolines_routing_config, redis_conn)
#if not quota_service.check_user_quota():
# plpy.error('You have reached the limit of your quota')
quota_service = QuotaService(user_mapzen_isolines_routing_config, redis_conn)
if not quota_service.check_user_quota():
plpy.error('You have reached the limit of your quota')
try:
# --TODO: encapsulate or refactor this ugly code
mapzen_conf_str = plpy.execute("SELECT * FROM CDB_Conf_Getconf('mapzen_conf') AS mapzen_conf")[0]['mapzen_conf']
mapzen_conf = json.loads(mapzen_conf_str)
client = MatrixClient(mapzen_conf['matrix']['api_key'])
client = MatrixClient(user_mapzen_isolines_routing_config.mapzen_matrix_api_key)
mapzen_isolines = MapzenIsolines(client)
if source:
@@ -115,19 +110,18 @@ RETURNS SETOF cdb_dataservices_server.isoline AS $$
multipolygon = plpy.execute(sql, 1)[0]['geom']
result.append([source, r, multipolygon])
# --TODO take care of this quota/usage stuff
#quota_service.increment_success_service_use()
#quota_service.increment_isolines_service_use(len(resp))
quota_service.increment_success_service_use()
quota_service.increment_isolines_service_use(len(isolines))
return result
except BaseException as e:
import sys, traceback
type_, value_, traceback_ = sys.exc_info()
#quota_service.increment_failed_service_use()
quota_service.increment_failed_service_use()
error_msg = 'There was an error trying to obtain isolines using mapzen: {0}'.format(e)
plpy.debug(traceback.format_tb(traceback_))
raise e
#plpy.error(error_msg)
finally:
pass
#quota_service.increment_total_service_use()
quota_service.increment_total_service_use()
$$ LANGUAGE plpythonu SECURITY DEFINER;

View File

@@ -26,11 +26,11 @@ CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_mapzen_isochrone(username
RETURNS SETOF cdb_dataservices_server.isoline 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_isolines_routing_config({0}, {1})".format(plpy.quote_nullable(username), plpy.quote_nullable(orgname)))
user_isolines_config = GD["user_isolines_routing_config_{0}".format(username)]
plpy.execute("SELECT cdb_dataservices_server._get_mapzen_isolines_config({0}, {1})".format(plpy.quote_nullable(username), plpy.quote_nullable(orgname)))
user_isolines_config = GD["user_mapzen_isolines_routing_config_{0}".format(username)]
type = 'isochrone'
# if we were to add a config check, it'll go here
# If we were to add a config check, it'll go here
#if user_isolines_config.google_services_user:
# plpy.error('This service is not available for google service users.')