-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrentals.py
138 lines (119 loc) · 4.56 KB
/
rentals.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import json
from bson import json_util
import psycopg2.extras
import zones
class Rentals():
def __init__(self):
self.zones = zones.Zones()
def get_start_trips(self, conn, d_filter):
cur = conn.cursor()
stmt = """
WITH temp_a (filter_area) AS
(
SELECT st_union(area)
FROM zones WHERE zone_id IN %s
)
SELECT system_id, bike_id, st_y(location), st_x(location), start_time
FROM park_events, temp_a
WHERE
end_time >= %s
AND end_time <= %s
AND (false = %s or
(ST_Within(location, temp_a.filter_area)))
AND (false = %s or system_id IN %s)
"""
cur.execute(stmt, (d_filter.get_zones(), d_filter.get_start_time(),
d_filter.get_end_time(), d_filter.has_zone_filter(),
d_filter.has_operator_filter(), d_filter.get_operators()))
return self.serialize_rentals(cur.fetchall(), False)
def get_end_trips(self, conn, d_filter):
cur = conn.cursor()
stmt = """
WITH temp_a (filter_area) AS
(
SELECT st_union(area)
FROM zones WHERE zone_id IN %s
)
SELECT system_id, bike_id, st_y(location), st_x(location), end_time
FROM park_events, temp_a
WHERE
start_time >= %s
AND start_time <= %s
AND (false = %s or
(ST_Within(location, temp_a.filter_area)))
AND (false = %s or system_id IN %s)
"""
cur.execute(stmt, (d_filter.get_zones(), d_filter.get_start_time(),
d_filter.get_end_time(), d_filter.has_zone_filter(),
d_filter.has_operator_filter(), d_filter.get_operators()))
return self.serialize_rentals(cur.fetchall(), True)
def query_stats_start_trip(self, conn, zone_id, d_filter):
cur = conn.cursor()
stmt = """WITH temp_a (filter_area) AS
(SELECT st_union(area)
FROM zones WHERE zone_id = %s)
SELECT SUM(CASE WHEN ST_Within(location, temp_a.filter_area) THEN 1 ELSE 0 END)
FROM park_events, temp_a
WHERE
end_time >= %s
AND end_time <= %s
AND (ST_Within(location, temp_a.filter_area))
AND (false = %s or system_id IN %s) ;
"""
cur.execute(stmt, (zone_id,
d_filter.get_start_time(), d_filter.get_end_time(),
d_filter.has_operator_filter(), d_filter.get_operators()))
result = cur.fetchone()
if (result):
return result[0]
return 0
def query_stats_end_trip(self, conn, zone_id, d_filter):
cur = conn.cursor()
stmt = """WITH temp_a (filter_area) AS
(SELECT st_union(area)
FROM zones WHERE zone_id = %s)
SELECT SUM(CASE WHEN ST_Within(location, temp_a.filter_area) THEN 1 ELSE 0 END)
FROM park_events, temp_a
WHERE
start_time >= %s
AND start_time <= %s
AND (ST_Within(location, temp_a.filter_area))
AND (false = %s or system_id IN %s) ;
"""
cur.execute(stmt, (zone_id,
d_filter.get_start_time(), d_filter.get_end_time(),
d_filter.has_operator_filter(), d_filter.get_operators()))
result = cur.fetchone()
if (result):
return result[0]
return 0
def query_stats(self, conn, zone_id, d_filter):
result = {}
values = [self.query_stats_end_trip(conn, zone_id, d_filter), self.query_stats_start_trip(conn, zone_id, d_filter)]
result["zone_id"] = zone_id
result["number_of_trips"] = values
result["zone"] = self.zones.get_zone(conn, zone_id)
return result
def get_stats(self, conn, d_filter):
records = []
for zone_id in d_filter.get_zones():
result = self.query_stats(conn, zone_id, d_filter)
records.append(result)
return records
def serialize_rentals(self, rentals, is_arrival):
result = []
for rental in rentals:
result.append(self.serialize_rental(rental, is_arrival))
return result
def serialize_rental(self, rental, is_arrival):
data = {}
data["system_id"] = rental[0]
data["bike_id"] = rental[1]
data["location"] = {}
data["location"]["latitude"] = rental[2]
data["location"]["longitude"] = rental[3]
if is_arrival:
data["arrival_time"] = rental[4]
else:
data["departure_time"] = rental[4]
return data