-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- standardize color mapping in landing page: issue inasafe/inasafe-realtime#48 - default highlight latest flood report: issue inasafe/inasafe-realtime#42 - improvements for flood table: issue inasafe/inasafe-realtime#11
- Loading branch information
Showing
8 changed files
with
229 additions
and
15 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
django_project/realtime/management/commands/recalculateimpactdata.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# coding=utf-8 | ||
import datetime | ||
|
||
import pytz | ||
from django.core.management.base import BaseCommand | ||
from realtime.tasks.flood import recalculate_impact_info | ||
|
||
from realtime.models.flood import Flood | ||
|
||
__author__ = 'Rizky Maulana Nugraha "lucernae" <[email protected]>' | ||
__date__ = '07/02/17' | ||
|
||
|
||
class Command(BaseCommand): | ||
"""Script to recalculate impact data. | ||
""" | ||
help = ( | ||
'Command to re-calculate total affected and boundary flooded in ' | ||
'flood.') | ||
|
||
def handle(self, *args, **options): | ||
using_range = False | ||
if len(args) == 3: | ||
if args[0] == 'range': | ||
using_range = True | ||
Command.recalculate_flood_from_range(args[1], args[2]) | ||
|
||
if len(args) > 0 and not using_range: | ||
for a in args: | ||
print 'Process flood : %s' % a | ||
flood = Flood.objects.get(event_id=a) | ||
try: | ||
recalculate_impact_info(flood) | ||
except Exception as e: | ||
print e | ||
|
||
elif not using_range: | ||
floods = Flood.objects.all().order_by('-time') | ||
print 'Process flood (%s)' % len(floods) | ||
for flood in floods: | ||
try: | ||
recalculate_impact_info(flood) | ||
except Exception as e: | ||
print e | ||
|
||
@staticmethod | ||
def recalculate_flood_from_range(start_event_id, end_event_id): | ||
format_str = '%Y%m%d%H-6-rw' | ||
start_time = datetime.datetime.strptime(start_event_id, format_str) | ||
if not end_event_id == 'now': | ||
end_time = datetime.datetime.strptime(end_event_id, format_str) | ||
else: | ||
end_time = datetime.datetime.utcnow() | ||
# convert to UTC | ||
start_time = start_time.replace(tzinfo=pytz.UTC) | ||
end_time = end_time.replace(tzinfo=pytz.UTC) | ||
time_diff = end_time - start_time | ||
total_hours = int(time_diff.total_seconds() / 3600) | ||
success = 0 | ||
failed = 0 | ||
for i in range(0, total_hours): | ||
hour_diff = datetime.timedelta(hours=i + 1) | ||
target_time = start_time + hour_diff | ||
event_id = target_time.strftime(format_str) | ||
try: | ||
print 'Processing flood: %s' % event_id | ||
flood = Flood.objects.get(event_id=event_id) | ||
recalculate_impact_info(flood) | ||
success += 1 | ||
except Exception as e: | ||
failed += 1 | ||
print e | ||
|
||
print 'Recalculate process done' | ||
print 'Success: %s. Failed: %s' % (success, failed) |
26 changes: 26 additions & 0 deletions
26
django_project/realtime/migrations/0025_auto_20170206_2046.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import models, migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('realtime', '0024_auto_20170206_2044'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='flood', | ||
name='boundary_flooded', | ||
field=models.IntegerField(default=0, help_text=b'Total boundary affected by flood', verbose_name=b'Total boundary flooded'), | ||
preserve_default=True, | ||
), | ||
migrations.AddField( | ||
model_name='flood', | ||
name='total_affected', | ||
field=models.IntegerField(default=0, help_text=b'Total affected people by flood', verbose_name=b'Total affected people by flood'), | ||
preserve_default=True, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,10 @@ | |
|
||
from realtime.app_settings import LOGGER_NAME | ||
from realtime.models.flood import Flood | ||
from realtime.tasks.flood import process_hazard_layer, process_impact_layer | ||
from realtime.tasks.flood import ( | ||
process_hazard_layer, | ||
process_impact_layer, | ||
recalculate_impact_info) | ||
|
||
__author__ = 'Rizky Maulana Nugraha <[email protected]>' | ||
__date__ = '12/4/15' | ||
|
@@ -20,12 +23,20 @@ | |
|
||
|
||
@receiver(post_save, sender=Flood) | ||
def flood_post_save(sender, **kwargs): | ||
def flood_post_save(sender, instance, created=None, update_fields=None, **kwargs): | ||
"""Extract impact layer of the flood""" | ||
try: | ||
instance = kwargs.get('instance') | ||
chain( | ||
process_hazard_layer.si(instance), | ||
process_impact_layer.si(instance))() | ||
fields = ['total_affected', 'boundary_flooded'] | ||
update_fields = update_fields or [] | ||
for field in fields: | ||
# if total_affected or boundary_flooded is updated, | ||
# do not recalculate | ||
if field in update_fields: | ||
break | ||
else: | ||
chain( | ||
process_hazard_layer.si(instance), | ||
process_impact_layer.si(instance), | ||
recalculate_impact_info(instance))() | ||
except Exception as e: | ||
LOGGER.exception(e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters