Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
S
smogonet
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dominik Rosiek
smogonet
Commits
5ee60c43
Commit
5ee60c43
authored
Dec 08, 2016
by
Dominik Rosiek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
models
parent
8c35dc58
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
124 additions
and
28 deletions
+124
-28
db.py
db.py
+24
-28
models.py
models.py
+85
-0
test_models.py
test_models.py
+15
-0
No files found.
db.py
View file @
5ee60c43
...
@@ -152,14 +152,14 @@ class DDBTable(object):
...
@@ -152,14 +152,14 @@ class DDBTable(object):
def
create_table
(
self
):
def
create_table
(
self
):
try
:
try
:
message
=
self
.
_dynamodb
(
operation
=
'DescribeTable'
)
.
call
(
self
.
_dynamodb
(
operation
=
'DescribeTable'
)
.
call
(
TableName
=
self
.
_get_table_name
())
TableName
=
self
.
_get_table_name
())
except
AmazonException
as
e
:
except
AmazonException
as
e
:
if
e
.
code
!=
'ResourceNotFoundException'
:
if
e
.
code
!=
'ResourceNotFoundException'
:
raise
e
raise
e
logger
.
warning
(
'Creation {table_name} table ...'
.
format
(
logger
.
warning
(
'Creation {table_name} table ...'
.
format
(
table_name
=
self
.
_get_table_name
()))
table_name
=
self
.
_get_table_name
()))
message
=
self
.
_dynamodb
(
operation
=
'CreateTable'
)
.
call
(
self
.
_dynamodb
(
operation
=
'CreateTable'
)
.
call
(
**
self
.
_get_table_kwargs
())
**
self
.
_get_table_kwargs
())
else
:
else
:
logger
.
warning
(
'{table_name} table already exists.'
.
format
(
logger
.
warning
(
'{table_name} table already exists.'
.
format
(
...
@@ -218,7 +218,7 @@ class DDBstations(DDBTable):
...
@@ -218,7 +218,7 @@ class DDBstations(DDBTable):
}
}
FIELDS
=
{
FIELDS
=
{
'station_id'
:
DDBUUIDField
,
'station_id'
:
DDBUUIDField
,
'api_staion_id'
:
DDBIntField
,
'api_sta
t
ion_id'
:
DDBIntField
,
'city'
:
DDBStringField
,
'city'
:
DDBStringField
,
'longitude'
:
DDBIntField
,
'longitude'
:
DDBIntField
,
'latitude'
:
DDBIntField
,
'latitude'
:
DDBIntField
,
...
@@ -226,19 +226,18 @@ class DDBstations(DDBTable):
...
@@ -226,19 +226,18 @@ class DDBstations(DDBTable):
}
}
@
gen
.
coroutine
@
gen
.
coroutine
def
add
(
self
,
city
,
api_staion_id
,
longitude
,
latitude
,
name
):
def
add
(
self
,
station
):
station_id
=
uuid
.
uuid4
()
data
=
{
data
=
{
'city'
:
city
,
'city'
:
station
.
city
,
'api_sta
ion_id'
:
api_staion
_id
,
'api_sta
tion_id'
:
station
.
api
_id
,
'longitude'
:
longitude
,
'longitude'
:
station
.
longitude
,
'latitude'
:
latitude
,
'latitude'
:
station
.
latitude
,
'name'
:
name
'name'
:
station
.
name
}
}
message
=
yield
gen
.
Task
(
self
.
_dynamodb
(
operation
=
'UpdateItem'
)
.
call
,
message
=
yield
gen
.
Task
(
self
.
_dynamodb
(
operation
=
'UpdateItem'
)
.
call
,
TableName
=
self
.
_get_table_name
(),
TableName
=
self
.
_get_table_name
(),
Key
=
self
.
encode_item
(
data
=
{
'station_id'
:
station
_
id
}),
Key
=
self
.
encode_item
(
data
=
{
'station_id'
:
station
.
id
}),
AttributeUpdates
=
self
.
encode_item
(
data
=
data
,
update
=
True
))
AttributeUpdates
=
self
.
encode_item
(
data
=
data
,
update
=
True
))
raise
gen
.
Return
(
message
)
raise
gen
.
Return
(
message
)
...
@@ -268,23 +267,22 @@ class DDBtypes(DDBTable):
...
@@ -268,23 +267,22 @@ class DDBtypes(DDBTable):
'unit'
:
DDBStringField
,
'unit'
:
DDBStringField
,
'norm'
:
DDBIntField
,
'norm'
:
DDBIntField
,
'longname'
:
DDBStringField
,
'longname'
:
DDBStringField
,
'description
s
'
:
DDBStringField
'description'
:
DDBStringField
}
}
@
gen
.
coroutine
@
gen
.
coroutine
def
add
(
self
,
shortname
,
unit
,
norm
,
longname
,
descriptions
):
def
add
(
self
,
pollution_type
):
type_id
=
uuid
.
uuid4
()
data
=
{
data
=
{
'shortname'
:
shortname
,
'shortname'
:
pollution_type
.
shortname
,
'unit'
:
unit
,
'unit'
:
pollution_type
.
unit
,
'norm'
:
norm
,
'norm'
:
pollution_type
.
norm
,
'longname'
:
longname
,
'longname'
:
pollution_type
.
longname
,
'description
s'
:
descriptions
'description
'
:
pollution_type
.
description
}
}
message
=
yield
gen
.
Task
(
self
.
_dynamodb
(
operation
=
'UpdateItem'
)
.
call
,
message
=
yield
gen
.
Task
(
self
.
_dynamodb
(
operation
=
'UpdateItem'
)
.
call
,
TableName
=
self
.
_get_table_name
(),
TableName
=
self
.
_get_table_name
(),
Key
=
self
.
encode_item
(
data
=
{
'type_id'
:
type_
id
}),
Key
=
self
.
encode_item
(
data
=
{
'type_id'
:
pollution_type
.
id
}),
AttributeUpdates
=
self
.
encode_item
(
data
=
data
,
update
=
True
))
AttributeUpdates
=
self
.
encode_item
(
data
=
data
,
update
=
True
))
raise
gen
.
Return
(
message
)
raise
gen
.
Return
(
message
)
...
@@ -296,7 +294,6 @@ class DDBtypes(DDBTable):
...
@@ -296,7 +294,6 @@ class DDBtypes(DDBTable):
)
)
data
=
self
.
decode_item
(
item
=
message
[
'Item'
])
data
=
self
.
decode_item
(
item
=
message
[
'Item'
])
raise
gen
.
Return
(
data
)
raise
gen
.
Return
(
data
)
raise
gen
.
Return
(
data
)
class
DDBmeasurements
(
DDBTable
):
class
DDBmeasurements
(
DDBTable
):
...
@@ -318,18 +315,17 @@ class DDBmeasurements(DDBTable):
...
@@ -318,18 +315,17 @@ class DDBmeasurements(DDBTable):
}
}
@
gen
.
coroutine
@
gen
.
coroutine
def
add
(
self
,
station_id
,
type_id
,
value
,
measurement_time
):
def
add
(
self
,
measurement
):
measurement_id
=
uuid
.
uuid4
()
data
=
{
data
=
{
'station_id'
:
uuid
.
uuid4
()
,
'station_id'
:
measurement
.
station
.
id
,
'type_id'
:
uuid
.
uuid4
()
,
'type_id'
:
measurement
.
type
.
id
,
'value'
:
value
,
'value'
:
measurement
.
value
,
'time'
:
measurement
_
time
'time'
:
measurement
.
time
}
}
message
=
yield
gen
.
Task
(
self
.
_dynamodb
(
operation
=
'UpdateItem'
)
.
call
,
message
=
yield
gen
.
Task
(
self
.
_dynamodb
(
operation
=
'UpdateItem'
)
.
call
,
TableName
=
self
.
_get_table_name
(),
TableName
=
self
.
_get_table_name
(),
Key
=
self
.
encode_item
(
data
=
{
'measurement_id'
:
measurement
_
id
}),
Key
=
self
.
encode_item
(
data
=
{
'measurement_id'
:
measurement
.
id
}),
AttributeUpdates
=
self
.
encode_item
(
data
=
data
,
update
=
True
))
AttributeUpdates
=
self
.
encode_item
(
data
=
data
,
update
=
True
))
raise
gen
.
Return
(
message
)
raise
gen
.
Return
(
message
)
...
...
models.py
0 → 100644
View file @
5ee60c43
import
uuid
from
db
import
DDBtypes
,
DDBmeasurements
,
DDBstations
from
functools
import
partial
from
tornado.ioloop
import
IOLoop
class
DDBobject
(
object
):
@
classmethod
def
from_dict
(
cls
,
data
):
return
cls
(
**
data
)
class
Station
(
DDBobject
):
stored
=
{}
def
__init__
(
self
,
api_station_id
,
city
,
longitude
,
latitude
,
name
,
station_id
=
None
):
self
.
id
=
station_id
or
str
(
uuid
.
uuid4
())
self
.
api_id
=
api_station_id
self
.
city
=
city
self
.
longitude
=
longitude
self
.
latitude
=
latitude
self
.
name
=
name
@
classmethod
def
get
(
cls
,
station_id
):
if
cls
.
stored
.
get
(
station_id
,
None
):
return
cls
.
stored
.
get
(
station_id
)
return
cls
.
from_dict
(
IOLoop
.
instance
()
.
run_sync
(
partial
(
DDBstations
()
.
get
,
station_id
)))
def
save
(
self
):
return
IOLoop
.
instance
()
.
run_sync
(
partial
(
DDBstations
()
.
add
,
station
=
self
))
class
Type
(
DDBobject
):
stored
=
{}
def
__init__
(
self
,
shortname
,
unit
,
norm
,
longname
,
description
,
type_id
=
None
):
self
.
id
=
type_id
or
str
(
uuid
.
uuid4
())
self
.
shortname
=
shortname
self
.
unit
=
unit
self
.
norm
=
norm
self
.
longname
=
longname
self
.
description
=
description
@
classmethod
def
get
(
cls
,
type_id
):
if
cls
.
stored
.
get
(
type_id
,
None
):
return
cls
.
stored
.
get
(
type_id
)
return
cls
.
from_dict
(
IOLoop
.
instance
()
.
run_sync
(
partial
(
DDBtypes
()
.
get
,
type_id
)))
def
save
(
self
):
return
IOLoop
.
instance
()
.
run_sync
(
partial
(
DDBtypes
()
.
add
,
pollution_type
=
self
))
class
Measurement
(
DDBobject
):
stored
=
{}
def
__init__
(
self
,
station
,
pollution_type
,
value
,
time
,
measurement_id
=
None
):
self
.
id
=
measurement_id
or
str
(
uuid
.
uuid4
())
self
.
station
=
station
self
.
type
=
pollution_type
self
.
value
=
value
self
.
time
=
time
@
classmethod
def
get
(
cls
,
measurement_id
):
if
cls
.
stored
.
get
(
measurement_id
,
None
):
return
cls
.
stored
.
get
(
measurement_id
)
data
=
IOLoop
.
instance
()
.
run_sync
(
partial
(
DDBmeasurements
()
.
get
,
measurement_id
))
data
[
'station'
]
=
Station
.
get
(
data
[
'station_id'
])
del
data
[
'station_id'
]
data
[
'pollution_type'
]
=
Type
.
get
(
data
[
'type_id'
])
del
data
[
'type_id'
]
return
cls
.
from_dict
(
data
)
def
save
(
self
):
return
IOLoop
.
instance
()
.
run_sync
(
partial
(
DDBmeasurements
()
.
add
,
measurement
=
self
))
test_models.py
0 → 100644
View file @
5ee60c43
import
models
station
=
models
.
Station
(
1
,
"Kraków"
,
12
,
13
,
"Aleje"
)
station
.
save
()
station2
=
models
.
Station
.
get
(
station
.
id
)
assert
station
.
id
==
station2
.
id
pollution_type
=
models
.
Type
(
shortname
=
"pm2"
,
unit
=
"mg"
,
norm
=
120
,
longname
=
"long_pm2"
,
description
=
"description pm2"
)
pollution_type
.
save
()
pollution_type2
=
models
.
Type
.
get
(
pollution_type
.
id
)
assert
pollution_type
.
id
==
pollution_type2
.
id
measurement
=
models
.
Measurement
(
station
=
station
,
pollution_type
=
pollution_type
,
value
=
13
,
time
=
124
)
measurement
.
save
()
measurement2
=
models
.
Measurement
.
get
(
measurement
.
id
)
assert
measurement
.
id
==
measurement2
.
id
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment