-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/new-models' into develop
- Loading branch information
Showing
12 changed files
with
349 additions
and
155 deletions.
There are no files selected for viewing
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,7 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | ||
|
||
RESTRICCION_CONFIG="$DIR/configs/tests.json" nosetests --with-coverage --cover-package=restriccion $@ |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# -*- coding: utf-8 -*- | ||
from .base_report import BaseReport | ||
|
||
|
||
class AirQuality(BaseReport): | ||
|
||
@staticmethod | ||
def get_mongo_collection(): | ||
return 'reports_air_quality' | ||
|
||
@staticmethod | ||
def get_fields(): | ||
return ['ciudad', 'fecha', 'estado'] | ||
|
||
@staticmethod | ||
def get_unique_fields(): | ||
return ['ciudad', 'fecha'] |
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,101 @@ | ||
# -*- coding: utf-8 -*- | ||
import collections | ||
import hashlib | ||
|
||
import moment | ||
|
||
from restriccion import CONFIG | ||
|
||
|
||
class BaseReport(object): | ||
|
||
@classmethod | ||
def get_mongo_collection(cls): | ||
raise NotImplementedError() | ||
|
||
@classmethod | ||
def get_fields(cls): | ||
raise NotImplementedError() | ||
|
||
@classmethod | ||
def get_unique_fields(cls): | ||
raise NotImplementedError() | ||
|
||
@classmethod | ||
def dict(cls, source, data): | ||
new_dict = {} | ||
|
||
fields = cls.get_fields() | ||
|
||
for field in fields: | ||
new_dict[field] = data[field] | ||
|
||
# Hash data to detect changes | ||
sha1_message = hashlib.sha1() | ||
for field in fields: | ||
if isinstance(data[field], collections.Hashable): | ||
string = data[field] | ||
else: | ||
string = str(data[field]) | ||
sha1_message.update(string.encode('utf-8')) | ||
|
||
new_dict['hash'] = sha1_message.hexdigest() | ||
new_dict['fuente'] = source | ||
|
||
return new_dict | ||
|
||
@classmethod | ||
def get(cls, mongo_db, query=None, limit=10): | ||
if query is None: | ||
query = {} | ||
|
||
mongo_query = { | ||
'$query': query, | ||
'$orderby': {field: -1 for field in cls.get_unique_fields()} | ||
} | ||
|
||
reports = [] | ||
rows = mongo_db[cls.get_mongo_collection()].find( | ||
mongo_query, | ||
{'_id': 0}, | ||
limit=limit | ||
) | ||
for row in rows: | ||
reports.append(row) | ||
return reports | ||
|
||
@classmethod | ||
def insert_many(cls, mongo_db, reports): | ||
update_time = moment.utcnow().timezone(CONFIG['moment']['timezone']).isoformat() | ||
|
||
projection = { | ||
'_id': 0, | ||
'hash': 1, | ||
} | ||
for field in ['hash', 'fuente'] + cls.get_fields(): | ||
projection[field] = 1 | ||
|
||
class_collection = cls.get_mongo_collection() | ||
unique_fields = cls.get_unique_fields() | ||
|
||
for report in reports: | ||
find_query = {field: report[field] for field in unique_fields} | ||
row = mongo_db[class_collection].find_one(find_query, projection) | ||
|
||
if row == report: | ||
continue | ||
|
||
report['actualizacion'] = update_time | ||
|
||
if row is None: | ||
mongo_db[class_collection].insert_one(report) | ||
else: | ||
update_query = {field: row[field] for field in unique_fields} | ||
|
||
mongo_db[class_collection].update_one( | ||
update_query, | ||
{'$set': report} | ||
) | ||
|
||
if '_id' in report: | ||
del report['_id'] |
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 |
---|---|---|
@@ -1,74 +1,17 @@ | ||
# -*- coding: utf-8 -*- | ||
import hashlib | ||
from .base_report import BaseReport | ||
|
||
import moment | ||
|
||
from restriccion import CONFIG | ||
class Restriction(BaseReport): | ||
|
||
@classmethod | ||
def get_mongo_collection(cls): | ||
return 'reports_restriction' | ||
|
||
class Restriction(object): | ||
@classmethod | ||
def get_fields(cls): | ||
return ['ciudad', 'fecha', 'sin_sello_verde', 'con_sello_verde'] | ||
|
||
@staticmethod | ||
def dict(date, with_green_seal, without_green_seal, source): | ||
data = { | ||
'fecha': date, | ||
'sin_sello_verde': with_green_seal, | ||
'con_sello_verde': without_green_seal, | ||
'fuente': source, | ||
} | ||
|
||
# Clear empty data | ||
for key in ['sin_sello_verde', 'con_sello_verde']: | ||
if data[key] is None or data[key] == '': | ||
data[key] = [] | ||
|
||
# Hash data to detect changes | ||
sha1_message = hashlib.sha1() | ||
sha1_message.update(data['fecha'].encode('utf-8')) | ||
sha1_message.update('-'.join(data['sin_sello_verde']).encode('utf-8')) | ||
sha1_message.update('-'.join(data['con_sello_verde']).encode('utf-8')) | ||
|
||
data['hash'] = sha1_message.hexdigest() | ||
return data | ||
|
||
@staticmethod | ||
def get(mongo_db, query=None, limit=10): | ||
if query is None: | ||
query = {} | ||
|
||
mongo_query = {'$query': query, '$orderby': {'fecha': -1}} | ||
|
||
restrictions = [] | ||
rows = mongo_db.restrictions.find(mongo_query, {'_id': 0}, limit=limit) | ||
for row in rows: | ||
restrictions.append(row) | ||
return restrictions | ||
|
||
@staticmethod | ||
def insert_many(mongo_db, restrictions_list): | ||
update_time = moment.utcnow().timezone(CONFIG['moment']['timezone']).isoformat() | ||
|
||
projection = { | ||
'_id': 0, | ||
'fecha': 1, | ||
'sin_sello_verde': 1, | ||
'con_sello_verde': 1, | ||
'hash': 1, | ||
'fuente': 1, | ||
} | ||
|
||
for restriction in restrictions_list: | ||
row = mongo_db.restrictions.find_one({'fecha': restriction['fecha']}, projection) | ||
|
||
if row == restriction: | ||
continue | ||
|
||
restriction['actualizacion'] = update_time | ||
|
||
if row is None: | ||
mongo_db.restrictions.insert_one(restriction) | ||
else: | ||
mongo_db.restrictions.update_one({'fecha': row['fecha']}, {'$set': restriction}) | ||
|
||
if '_id' in restriction: | ||
del restriction['_id'] | ||
@classmethod | ||
def get_unique_fields(cls): | ||
return ['ciudad', 'fecha'] |
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
Oops, something went wrong.