def test_cache_script_doesnt_run_if_0_times_per_day(self): dtnow = dates.utc_now().replace(hour=1, minute=10) dtlast = dates.utc_now().replace(hour=0, minute=5) with mock.patch('lizard_datasource.dates.utc_now', return_value=dtnow): self.assertFalse(DatasourceModelF.build( script_times_to_run_per_day=0, script_last_run_started=dtlast, script_run_next_opportunity=False).cache_script_is_due())
def test_cache_script_is_not_due_if_last_run_was_later(self): """It's 1:10am, the last run was at 1:05am.""" dtnow = dates.utc_now().replace(hour=1, minute=10) dtlast = dates.utc_now().replace(hour=1, minute=5) with mock.patch('lizard_datasource.dates.utc_now', return_value=dtnow): self.assertFalse(DatasourceModelF.build( script_times_to_run_per_day=24, script_last_run_started=dtlast, script_run_next_opportunity=False).cache_script_is_due())
def test_cache_script_is_due_if_last_run_was_earlier(self): """We have to run each hour, it's 1:05 am and the last run was at 0:05 am.""" dtnow = dates.utc_now().replace(hour=1, minute=5) dtlast = dates.utc_now().replace(hour=0, minute=5) with mock.patch('lizard_datasource.dates.utc_now', return_value=dtnow): self.assertTrue(DatasourceModelF.build( script_times_to_run_per_day=24, script_last_run_started=dtlast, script_run_next_opportunity=False).cache_script_is_due())
def cache_latest_values(ds): """IF the datasource has both LAYER_POINTS and DATA_CAN_HAVE_VALUE_LAYER_SCRIPT source, then we can make an instance of DatasourceLayer for each of its layers, get a timeseries for each location in each layer and cache its latest value. Then this information can be used for colouring, thresholding, et cetera.""" if not ds.has_property(properties.LAYER_POINTS) or not ds.has_property(properties.DATA_CAN_HAVE_VALUE_LAYER_SCRIPT): return # For now, we don't know what to do in this case # Only actually do something if the script is due. if not ds.activation_for_cache_script(): return for drawable in _yield_drawable_datasources(ds): # This creates the datasource layer in the database, if it # didn't exist yet datasource_layer = drawable.datasource_layer # Cache the datasource layer's unit, if it wasn't filled in yet drawable.cached_unit() # If we don't actually use the latest values of this layer, we # should skip it. if not datasource_layer.latest_values_used: continue locations = drawable.locations() for location in locations: try: cache = models.DatasourceCache.objects.get( datasource_layer=datasource_layer, locationid=location.identifier ) except models.DatasourceCache.DoesNotExist: cache = models.DatasourceCache(datasource_layer=datasource_layer, locationid=location.identifier) if cache.timestamp: start_datetime = cache.timestamp else: start_datetime = dates.utc_now() - datetime.timedelta(days=60) timeseries = drawable.timeseries( location.identifier, start_datetime=start_datetime, end_datetime=dates.utc_now() ) if timeseries is None or len(timeseries) == 0: continue latest = timeseries.latest() cache.timestamp = latest.keys()[0] cache.value = latest[0] cache.save() time.sleep(1)
def cache_script_is_due(self): if self.script_run_next_opportunity: return True if self.script_last_run_started is None: return True if not (0 < self.script_times_to_run_per_day <= (24 * 60)): return False minutes_between_scripts = ( (60 * 24) // self.script_times_to_run_per_day) current_time = dates.utc_now() minutes_since_midnight = ( 60 * current_time.hour + current_time.minute) script_should_run_at_minutes = ( minutes_since_midnight - (minutes_since_midnight % minutes_between_scripts)) script_should_run_at = current_time.replace( hour=script_should_run_at_minutes // 60, minute=script_should_run_at_minutes % 60) return ( script_should_run_at > dates.to_utc(self.script_last_run_started))
def test_cache_script_is_due_if_not_run_yet(self): # Say it's 0:30 AM now and it hasn't run yet, it should dt = dates.utc_now().replace(hour=0, minute=30) with mock.patch('lizard_datasource.dates.utc_now', return_value=dt): self.assertTrue(DatasourceModelF.build( script_times_to_run_per_day=24, script_last_run_started=None, script_run_next_opportunity=False).cache_script_is_due())
def activation_for_cache_script(self): """Return True if the cache script should active. If it shouldn't, if will do nothing this time. If True is returned, it is assumed that the script will run now, and that is recorded.""" if self.cache_script_is_due(): self.script_last_run_started = dates.utc_now() self.script_run_next_opportunity = False self.save() return True else: return False
def test_returns_utc(self): now = dates.utc_now() self.assertEquals(now.tzinfo, pytz.UTC)