28
28
# www.navitia.io
29
29
30
30
import boto3
31
- import pytz
32
31
import shapely .iterops
33
32
from dateutil import parser
34
33
from botocore .client import Config
35
34
import logging
36
35
import json
37
36
import shapely
38
- import datetime
39
37
from typing import Dict
38
+ from datetime import timedelta
40
39
41
40
from jormungandr import app , memory_cache , cache
42
41
from jormungandr .resource_s3_object import ResourceS3Object
43
42
43
+ # local memory cache
44
+ MEMORY_CACHE_ASGARD_S3_DATA_TIMEOUT = app .config [str ('MEMORY_CACHE_CONFIGURATION' )].get (
45
+ str ('ASGARD_S3_DATA_TIMEOUT' ), timedelta (minutes = 30 ).total_seconds ()
46
+ )
47
+ # Redis cache
48
+ CACHE_ASGARD_S3_DATA_TIMEOUT = app .config [str ('CACHE_CONFIGURATION' )].get (
49
+ str ('ASGARD_S3_DATA_TIMEOUT' ), timedelta (hours = 2 ).total_seconds ()
50
+ )
51
+
44
52
45
53
class ExcludedZonesManager :
46
54
excluded_shapes = dict () # type: Dict[str, shapely.geometry]
47
55
asgard_s3_bucket_folder = "excluded_zones"
48
56
49
57
@staticmethod
50
- @cache .memoize (app .config [str ('CACHE_CONFIGURATION' )].get (str ('ASGARD_S3_DATA_TIMEOUT' ), 24 * 60 ))
51
- def get_object (resource_s3_object ):
58
+ def _get_object (resource_s3_object ):
52
59
logger = logging .getLogger (__name__ )
53
60
try :
54
61
file_content = resource_s3_object .s3_object .get ()['Body' ].read ().decode ('utf-8' )
@@ -58,7 +65,7 @@ def get_object(resource_s3_object):
58
65
return {}
59
66
60
67
@staticmethod
61
- def is_activated (activation_period , date ):
68
+ def _is_activated (activation_period , date ):
62
69
if activation_period is None :
63
70
return False
64
71
@@ -70,10 +77,9 @@ def is_between(period, d):
70
77
return any ((is_between (period , date ) for period in activation_period ))
71
78
72
79
@classmethod
73
- @memory_cache .memoize (
74
- app .config [str ('MEMORY_CACHE_CONFIGURATION' )].get (str ('ASGARD_S3_DATA_TIMEOUT' ), 10 * 60 )
75
- )
76
- def get_all_excluded_zones (cls ):
80
+ @memory_cache .memoize (MEMORY_CACHE_ASGARD_S3_DATA_TIMEOUT )
81
+ @cache .memoize (CACHE_ASGARD_S3_DATA_TIMEOUT )
82
+ def _get_all_excluded_zones (cls ):
77
83
bucket_name = app .config .get (str ("ASGARD_S3_BUCKET" ))
78
84
79
85
logger = logging .getLogger (__name__ )
@@ -86,7 +92,7 @@ def get_all_excluded_zones(cls):
86
92
if not obj .key .endswith ('.json' ):
87
93
continue
88
94
try :
89
- json_content = ExcludedZonesManager .get_object (ResourceS3Object (obj , None ))
95
+ json_content = ExcludedZonesManager ._get_object (ResourceS3Object (obj , None ))
90
96
excluded_zones .append (json_content )
91
97
except Exception :
92
98
logger .exception ("Error on fetching excluded zones: bucket: {}" , bucket_name )
@@ -114,25 +120,24 @@ def get_all_excluded_zones(cls):
114
120
return excluded_zones
115
121
116
122
@staticmethod
117
- @memory_cache .memoize (
118
- app .config [str ('MEMORY_CACHE_CONFIGURATION' )].get (str ('ASGARD_S3_DATA_TIMEOUT' ), 10 * 60 )
119
- )
123
+ @memory_cache .memoize (MEMORY_CACHE_ASGARD_S3_DATA_TIMEOUT )
124
+ @cache .memoize (CACHE_ASGARD_S3_DATA_TIMEOUT )
120
125
def get_excluded_zones (mode = None , date = None ):
121
126
excluded_zones = []
122
- for json_content in ExcludedZonesManager .get_all_excluded_zones ():
127
+ for json_content in ExcludedZonesManager ._get_all_excluded_zones ():
123
128
if mode is not None and mode not in json_content .get ("modes" , []):
124
129
continue
125
- if date is not None and not ExcludedZonesManager .is_activated (
130
+ if date is not None and not ExcludedZonesManager ._is_activated (
126
131
json_content .get ('activation_periods' ), date
127
132
):
128
133
continue
129
134
excluded_zones .append (json_content )
130
135
return excluded_zones
131
136
132
137
@classmethod
133
- @memory_cache .memoize (app . config [ str ( 'CACHE_CONFIGURATION' )]. get ( str ( 'ASGARD_S3_DATA_TIMEOUT' ), 10 * 60 ) )
134
- def is_excluded ( cls , obj , mode , timestamp ):
135
- date = datetime . datetime . fromtimestamp ( timestamp , tz = pytz . timezone ( "UTC" )). date ()
138
+ @memory_cache .memoize (MEMORY_CACHE_ASGARD_S3_DATA_TIMEOUT )
139
+ @ cache . memoize ( CACHE_ASGARD_S3_DATA_TIMEOUT )
140
+ def is_excluded ( cls , obj , mode , date ):
136
141
# update excluded zones
137
142
excluded_zones = ExcludedZonesManager .get_excluded_zones (mode = mode , date = date )
138
143
poi_ids = set ((zone .get ("poi" ) for zone in excluded_zones ))
0 commit comments