def get(self): response = { "jormungandr_version": __version__, "regions": [] } regions = i_manager.get_regions() for key_region in regions: req = request_pb2.Request() req.requested_api = type_pb2.STATUS try: resp = i_manager.instances[key_region].send_and_receive(req, timeout=1000) raw_resp_dict = protobuf_to_dict(resp, use_enum_labels=True) resp_dict = marshal(raw_resp_dict['status'], fields.instance_status) except DeadSocketException: resp_dict = { "status": "dead", "error": { "code": "dead_socket", "value": "The region {} is dead".format(key_region) } } resp_dict['region_id'] = key_region response['regions'].append(resp_dict) return response
def get(self): response = { "jormungandr_version": __version__, "regions": [], "bss_providers": [provider.status() for provider in bss_provider_manager.get_providers()], } regions = i_manager.get_regions() for key_region in regions: req = request_pb2.Request() instance = i_manager.instances[key_region] req.requested_api = type_pb2.STATUS try: resp = instance.send_and_receive(req, timeout=1000) raw_resp_dict = protobuf_to_dict(resp, use_enum_labels=True) add_common_status(raw_resp_dict, instance) if USE_SERPY: resp_dict = CommonStatusSerializer(raw_resp_dict['status']).data else: resp_dict = marshal(raw_resp_dict['status'], instance_status) except DeadSocketException: resp_dict = { "status": "dead", "realtime_proxies": [], "error": {"code": "dead_socket", "value": "The region {} is dead".format(key_region)}, } resp_dict['region_id'] = key_region response['regions'].append(resp_dict) return response
def get(self, region=None, lon=None, lat=None, uri=None, id=None): args = self.parsers["get"].parse_args() if args['disable_geojson']: g.disable_geojson = True # for retrocompatibility purpose for forbid_id in args['__temporary_forbidden_id[]']: args['forbidden_uris[]'].append(forbid_id) if "odt_level" in args and args["odt_level"] != "all" and "lines" not in self.collection: abort(404, message="bad request: odt_level filter can only be applied to lines") if region is None and lat is None and lon is None: if "external_code" in args and args["external_code"]: type_ = collections_to_resource_type[self.collection] for instance in i_manager.get_regions(): res = i_manager.instances[instance].has_external_code(type_, args["external_code"]) if res: region = instance id = res break if not region: abort(404, message="Unable to find an object for the uri %s" % args["external_code"]) else: abort(503, message="Not implemented yet") self.region = i_manager.get_region(region, lon, lat) # we store the region in the 'g' object, which is local to a request set_request_timezone(self.region) # change dt to utc if args['since']: args['_original_since'] = args['since'] args['since'] = date_to_timestamp(self.convert_to_utc(args['since'])) if args['until']: args['_original_until'] = args['until'] args['until'] = date_to_timestamp(self.convert_to_utc(args['until'])) if not self.region: return {"error": "No region"}, 404 uris = [] if uri: if uri[-1] == "/": uri = uri[:-1] uris = uri.split("/") if self.collection is None: self.collection = uris[-1] if len(uris) % 2 != 0 else uris[-2] args["filter"] = self.get_filter(uris, args) if self.collection and id: f = u'{o}.uri={v}'.format(o=collections_to_resource_type[self.collection], v=protect(id)) if args.get("filter"): args["filter"] = '({}) and {}'.format(args["filter"], f) else: args["filter"] = f response = i_manager.dispatch(args, self.collection, instance_name=self.region) return response
def get(self): response = { "jormungandr_version": __version__, "regions": [], "bss_providers": [provider.status() for provider in bss_provider_manager.get_providers()], } regions = i_manager.get_regions() for key_region in regions: req = request_pb2.Request() instance = i_manager.instances[key_region] req.requested_api = type_pb2.STATUS try: resp = instance.send_and_receive(req, timeout=1000) raw_resp_dict = protobuf_to_dict(resp, use_enum_labels=True) add_common_status(raw_resp_dict, instance) resp_dict = CommonStatusSerializer(raw_resp_dict['status']).data except DeadSocketException: resp_dict = { "status": "dead", "realtime_proxies": [], "error": {"code": "dead_socket", "value": "The region {} is dead".format(key_region)}, } resp_dict['region_id'] = key_region response['regions'].append(resp_dict) # if we use our implementation of redisCache display the status cache_status_op = getattr(cache.cache, "status", None) if callable(cache_status_op): response['redis'] = cache_status_op() return response
def compute_regions(args): """ method computing the region the journey has to be computed on The complexity comes from the fact that the regions in jormungandr can overlap. return the kraken instance key rules are easy: we fetch the different regions the user can use for 'origin' and 'destination' we do the intersection and sort the list """ _region = None possible_regions = set() from_regions = set() to_regions = set() if args['origin']: from_regions = set(i_manager.get_regions(object_id=args['origin'])) #Note: if get_regions does not find any region, it raises a RegionNotFoundException if args['destination']: to_regions = set(i_manager.get_regions(object_id=args['destination'])) if not from_regions: #we didn't get any origin, the region is in the destination's list possible_regions = to_regions elif not to_regions: #we didn't get any origin, the region is in the destination's list possible_regions = from_regions else: #we need the intersection set possible_regions = from_regions.intersection(to_regions) logging.getLogger(__name__).debug( "orig region = {o}, dest region = {d} => set = {p}".format( o=from_regions, d=to_regions, p=possible_regions)) if not possible_regions: raise RegionNotFound( custom_msg="cannot find a region with {o} and {d} in the same time" .format(o=args['origin'], d=args['destination'])) sorted_regions = list(possible_regions) regions = sorted(sorted_regions, key=cmp_to_key(instances_comparator)) return regions
def get(self, region=None, lon=None, lat=None, id=None, *args, **kwargs): if id is not None: splitted = id.split(";") if len(splitted) != 2: abort( 404, message='invalid coords [{}], should be <lon:lon>;<lat:lat>' .format(id)) lon, lat = splitted try: float(lon) float(lat) except ValueError: abort( 404, message='invalid coords [{}], should be <lon:lon>;<lat:lat>' .format(id)) if region is None: regions = i_manager.get_regions("", lon, lat) else: regions = [region] args = { "uri": "coord:{}:{}".format(lon, lat), "count": 1, "distance": 200, "type[]": ["address"], "depth": 1, "start_page": 0, "filter": "", "_current_datetime": datetime.datetime.utcnow() } self._register_interpreted_parameters(args) result = OrderedDict() for r in regions: self.region = r result.update(regions=[r]) pb_result = i_manager.dispatch(args, "places_nearby", instance_name=r) if len(pb_result.places_nearby) > 0: e_type = pb_result.places_nearby[0].embedded_type if _NAVITIATYPE.values_by_name["ADDRESS"].number == e_type: new_address = marshal(pb_result.places_nearby[0].address, address) result.update(address=new_address) return result, 200 result.update(regions=regions) result.update(message="No address for these coords") return result, 404
def get(self, region=None, lon=None, lat=None, uri=None, id=None): collection = self.collection args = self.parsers["get"].parse_args() if "odt_level" in args and args[ "odt_level"] != "all" and "lines" not in collection: abort(404, message= "bad request: odt_level filter can only be applied to lines") if region is None and lat is None and lon is None: if "external_code" in args and args["external_code"]: type_ = collections_to_resource_type[collection] for instance in i_manager.get_regions(): res = i_manager.instances[instance].has_external_code( type_, args["external_code"]) if res: region = instance id = res break if not region: abort(404, message="Unable to find an object for the uri %s" % args["external_code"]) else: abort(503, message="Not implemented yet") else: user = authentication.get_user(token=authentication.get_token()) authentication.has_access(region, 'ALL', abort=True, user=user) self.region = i_manager.get_region(region, lon, lat) #we store the region in the 'g' object, which is local to a request set_request_timezone(self.region) if not self.region: return {"error": "No region"}, 404 if collection and id: args["filter"] = collections_to_resource_type[collection] + ".uri=" args["filter"] += '"' + id + '"' elif uri: if uri[-1] == "/": uri = uri[:-1] uris = uri.split("/") if collection is None: collection = uris[-1] if len(uris) % 2 != 0 else uris[-2] args["filter"] = self.get_filter(uris, args) #else: # abort(503, message="Not implemented") response = i_manager.dispatch(args, collection, instance_name=self.region) return response
def get(self): response = { "jormungandr_version": __version__, "regions": [], "bss_providers": [ provider.status() for provider in bss_provider_manager.bss_providers ] } regions = i_manager.get_regions() for key_region in regions: req = request_pb2.Request() req.requested_api = type_pb2.STATUS try: resp = i_manager.instances[key_region].send_and_receive( req, timeout=1000) raw_resp_dict = protobuf_to_dict(resp, use_enum_labels=True) raw_resp_dict['status'][ "is_open_service"] = i_manager.instances[ key_region].is_free raw_resp_dict['status']["is_open_data"] = i_manager.instances[ key_region].is_open_data raw_resp_dict['status']['realtime_proxies'] = [] for realtime_proxy in i_manager.instances[ key_region].realtime_proxy_manager.realtime_proxies.values( ): raw_resp_dict['status']['realtime_proxies'].append( realtime_proxy.status()) resp_dict = marshal(raw_resp_dict['status'], fields.instance_status) except DeadSocketException: resp_dict = { "status": "dead", "error": { "code": "dead_socket", "value": "The region {} is dead".format(key_region) } } resp_dict['region_id'] = key_region response['regions'].append(resp_dict) return response
def _get_regions(self, region=None, lon=None, lat=None): return [region] if region else i_manager.get_regions("", lon, lat)
def get(self, region=None, lon=None, lat=None, uri=None, id=None): collection = self.collection args = self.parsers["get"].parse_args() # handle headsign if args.get("headsign"): f = u"vehicle_journey.has_headsign({})".format( protect(args["headsign"])) if args.get("filter"): args["filter"] += " and " + f else: args["filter"] = f # for retrocompatibility purpose for forbid_id in args.get('forbidden_ids[]', []): args.get('forbidden_uris[]', []).append(forbid_id) if "odt_level" in args and args[ "odt_level"] != "all" and "lines" not in collection: abort(404, message= "bad request: odt_level filter can only be applied to lines") if region is None and lat is None and lon is None: if "external_code" in args and args["external_code"]: type_ = collections_to_resource_type[collection] for instance in i_manager.get_regions(): res = i_manager.instances[instance].has_external_code( type_, args["external_code"]) if res: region = instance id = res break if not region: abort(404, message="Unable to find an object for the uri %s" % args["external_code"]) else: abort(503, message="Not implemented yet") else: user = authentication.get_user(token=authentication.get_token()) authentication.has_access(region, 'ALL', abort=True, user=user) self.region = i_manager.get_region(region, lon, lat) #we store the region in the 'g' object, which is local to a request set_request_timezone(self.region) # change dt to utc if args['since']: args['_original_since'] = args['since'] args['since'] = date_to_timestamp( self.convert_to_utc(args['since'])) if args['until']: args['_original_until'] = args['until'] args['until'] = date_to_timestamp( self.convert_to_utc(args['until'])) if not self.region: return {"error": "No region"}, 404 if uri: if uri[-1] == "/": uri = uri[:-1] uris = uri.split("/") if collection is None: collection = uris[-1] if len(uris) % 2 != 0 else uris[-2] args["filter"] = self.get_filter(uris, args) if collection and id: f = '{o}.uri={v}'.format( o=collections_to_resource_type[collection], v=protect(id)) if args.get("filter"): args["filter"] += " and " + f else: args["filter"] = f response = i_manager.dispatch(args, collection, instance_name=self.region) return response