def _clear_postgres(config, prefix, verbose=False, sysname=None): cfg_copy = dict(config) if "password" in cfg_copy: cfg_copy["password"] = "******" if "admin_password" in cfg_copy: cfg_copy["admin_password"] = "******" print 'clear_db: Clearing PostgreSQL databases using config=', cfg_copy import getpass db_name = prefix if not sysname else sysname + "_" + config.get('database', 'ion') username = config.get("admin_username", None) or getpass.getuser() password = config.get("admin_password", None) or "" host = config.get('host', None) or 'localhost' port = str(config.get('port', None) or '5432') default_database = config.get('default_database', None) or 'postgres' import psycopg2 from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT dsn = "host=%s port=%s dbname=%s user=%s password=%s" % (host, port, default_database, username, password) with psycopg2.connect(dsn) as conn: print "clear_db: Connected to PostgreSQL as:", dsn.rsplit("=", 1)[0] + "=***" conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) with conn.cursor() as cur: cur.execute("SELECT pid, datname FROM pg_stat_activity") rows = cur.fetchall() conn_by_db = {} for row in rows: conn_id, dbn = row[0], row[1] conn_by_db.setdefault(dbn, []).append(conn_id) print "clear_db: Found %s open connections" % len(rows) cur.execute("SELECT datname FROM pg_database") rows = cur.fetchall() ignored_num = 0 for row in rows: try: db_name = row[0] if (prefix == '*' and not db_name.startswith('_')) or db_name.lower().startswith(prefix.lower()): print "clear_db: (PostgreSQL) DROP DATABASE", db_name if conn_by_db.get(db_name, None): for conn_id in conn_by_db[db_name]: cur.execute("SELECT pg_terminate_backend(%s)", (conn_id, )) print "clear_db: Dropped %s open connections to database '%s'" % (len(conn_by_db[db_name]), db_name) cur.execute("DROP DATABASE %s" % db_name) else: ignored_num += 1 except Exception as ex: log.exception("") print "Could not drop database '%s'" % db_name print 'clear_db: Ignored %s existing databases' % ignored_num
def get_obj_geospatial_point(doc, calculate=True): """Extracts a geospatial point (lat, lon, elev) from given object dict, by looking for an attribute with GeospatialIndex or GeospatialPoint or GeospatialLocation type or computing the center from a bounds """ geo_center = None # TODO: Be more flexible about finding attributes with the right types if "location" in doc: geo_center = doc["location"] if "geospatial_point_center" in doc: geo_center = doc["geospatial_point_center"] if "details" in doc and type( doc["details"]) is dict and "location" in doc["details"]: geo_center = doc["details"]["location"] if not geo_center and calculate: # Try to calculate center point from bounds present, geo_bounds = get_obj_geospatial_bounds(doc, calculate=False, return_geo_bounds=True) if present: try: from ion.util.geo_utils import GeoUtils geo_bounds_obj = DotDict(**geo_bounds) geo_center = GeoUtils.calc_geospatial_point_center( geo_bounds_obj) except Exception: log.exception("Could not calculate geospatial center point") if geo_center and isinstance(geo_center, dict): if "lat" in geo_center and "lon" in geo_center: lat, lon = geo_center.get("lat", 0), geo_center.get("lon", 0) if lat or lon: return True, (lat, lon, 0) elif "latitude" in geo_center and "longitude" in geo_center: lat, lon = geo_center.get("latitude", 0), geo_center.get("longitude", 0) elev = geo_center.get("elevation", 0) if lat or lon or elev: return True, (lat, lon, elev) elif "geospatial_latitude" in geo_center and "geospatial_longitude" in geo_center: lat, lon = geo_center.get("geospatial_latitude", 0), geo_center.get( "geospatial_longitude", 0) elev = geo_center.get("geospatial_vertical_location", 0) if lat or lon: return True, (lat, lon, elev) return False, (0, 0, 0)
def get_obj_geospatial_point(doc, calculate=True): """Extracts a geospatial point (lat, lon, elev) from given object dict, by looking for an attribute with GeospatialIndex or GeospatialPoint or GeospatialLocation type or computing the center from a bounds """ geo_center = None # TODO: Be more flexible about finding attributes with the right types if "location" in doc: geo_center = doc["location"] if "geospatial_point_center" in doc: geo_center = doc["geospatial_point_center"] if "details" in doc and type( doc["details"]) is dict and "location" in doc["details"]: geo_center = doc["details"]["location"] if not geo_center and calculate: # Try to calculate center point from bounds present, geo_bounds = get_obj_geospatial_bounds( doc, calculate=False, return_geo_bounds=True) if present: try: from ion.util.geo_utils import GeoUtils geo_bounds_obj = DotDict(**geo_bounds) geo_center = GeoUtils.calc_geospatial_point_center( geo_bounds_obj) except Exception: log.exception("Could not calculate geospatial center point") if geo_center and isinstance(geo_center, dict): if "lat" in geo_center and "lon" in geo_center: lat, lon = geo_center.get("lat", 0), geo_center.get("lon", 0) if lat or lon: return True, (lat, lon, 0) elif "latitude" in geo_center and "longitude" in geo_center: lat, lon = geo_center.get("latitude", 0), geo_center.get( "longitude", 0) elev = geo_center.get("elevation", 0) if lat or lon or elev: return True, (lat, lon, elev) elif "geospatial_latitude" in geo_center and "geospatial_longitude" in geo_center: lat, lon = geo_center.get("geospatial_latitude", 0), geo_center.get( "geospatial_longitude", 0) elev = geo_center.get("geospatial_vertical_location", 0) if lat or lon: return True, (lat, lon, elev) return False, (0, 0, 0)