|
| 1 | +# Copyright (c) 2001-2024, Hove and/or its affiliates. All rights reserved. |
| 2 | +# |
| 3 | +# This file is part of Navitia, |
| 4 | +# the software to build cool stuff with public transport. |
| 5 | +# |
| 6 | +# Hope you'll enjoy and contribute to this project, |
| 7 | +# powered by Hove (www.hove.com). |
| 8 | +# Help us simplify mobility and open public transport: |
| 9 | +# a non ending quest to the responsive locomotion way of traveling! |
| 10 | +# |
| 11 | +# LICENCE: This program is free software; you can redistribute it and/or modify |
| 12 | +# it under the terms of the GNU Affero General Public License as published by |
| 13 | +# the Free Software Foundation, either version 3 of the License, or |
| 14 | +# (at your option) any later version. |
| 15 | +# |
| 16 | +# This program is distributed in the hope that it will be useful, |
| 17 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | +# GNU Affero General Public License for more details. |
| 20 | +# |
| 21 | +# You should have received a copy of the GNU Affero General Public License |
| 22 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | +# |
| 24 | +# Stay tuned using |
| 25 | +# twitter @navitia |
| 26 | +# channel `#navitia` on riot https://riot.im/app/#/room/#navitia:matrix.org |
| 27 | +# https://groups.google.com/d/forum/navitia |
| 28 | +# www.navitia.io |
| 29 | + |
| 30 | +import boto3 |
| 31 | +from botocore.client import Config |
| 32 | +import logging |
| 33 | +import json |
| 34 | + |
| 35 | +from jormungandr import app, memory_cache, cache |
| 36 | +from jormungandr.resource_s3_object import ResourceS3Object |
| 37 | + |
| 38 | + |
| 39 | +class ExcludedZonesManager: |
| 40 | + @staticmethod |
| 41 | + @cache.memoize(app.config[str('CACHE_CONFIGURATION')].get(str('ASGARD_S3_DATA_TIMEOUT'), 24 * 60)) |
| 42 | + def get_object(resource_s3_object): |
| 43 | + logger = logging.getLogger(__name__) |
| 44 | + try: |
| 45 | + file_content = resource_s3_object.s3_object.get()['Body'].read().decode('utf-8') |
| 46 | + return json.loads(file_content) |
| 47 | + except Exception: |
| 48 | + logger.exception('Error while loading file: {}'.format(resource_s3_object.s3_object.key)) |
| 49 | + return {} |
| 50 | + |
| 51 | + @staticmethod |
| 52 | + @memory_cache.memoize( |
| 53 | + app.config[str('MEMORY_CACHE_CONFIGURATION')].get(str('ASGARD_S3_DATA_TIMEOUT'), 5 * 60) |
| 54 | + ) |
| 55 | + def get_excluded_zones(instance_name=None, mode=None): |
| 56 | + bucket_name = app.config.get(str("ASGARD_S3_BUCKET")) |
| 57 | + folder = "excluded_zones" |
| 58 | + |
| 59 | + logger = logging.getLogger(__name__) |
| 60 | + args = {"connect_timeout": 2, "read_timeout": 2, "retries": {'max_attempts': 0}} |
| 61 | + s3_resource = boto3.resource('s3', config=Config(**args)) |
| 62 | + excluded_zones = [] |
| 63 | + try: |
| 64 | + my_bucket = s3_resource.Bucket(bucket_name) |
| 65 | + for obj in my_bucket.objects.filter(Prefix="{}/".format(folder)): |
| 66 | + if not obj.key.endswith('.json'): |
| 67 | + continue |
| 68 | + try: |
| 69 | + json_content = ExcludedZonesManager.get_object(ResourceS3Object(obj, None)) |
| 70 | + |
| 71 | + if instance_name is not None and json_content.get('instance') != instance_name: |
| 72 | + continue |
| 73 | + if mode is not None and mode not in json_content.get("modes", []): |
| 74 | + continue |
| 75 | + |
| 76 | + excluded_zones.append(json_content) |
| 77 | + except Exception: |
| 78 | + logger.exception( |
| 79 | + "Error on fetching excluded zones: bucket: {}, instance: {}, mode ={}", |
| 80 | + bucket_name, |
| 81 | + instance_name, |
| 82 | + mode, |
| 83 | + ) |
| 84 | + continue |
| 85 | + |
| 86 | + except Exception: |
| 87 | + logger.exception( |
| 88 | + "Error on fetching excluded zones: bucket: {}, instance: {}, mode ={}", |
| 89 | + bucket_name, |
| 90 | + instance_name, |
| 91 | + mode, |
| 92 | + ) |
| 93 | + |
| 94 | + return excluded_zones |
0 commit comments