-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
service_request.py
229 lines (200 loc) · 6.81 KB
/
service_request.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import datetime
from typing import List
from aiocache import cached
from sqlalchemy import sql, and_, desc, text
from . import db
from .request_type import RequestType
from .source import Source
class ServiceRequest(db.Model):
__tablename__ = 'service_requests'
request_id = db.Column(db.Integer, primary_key=True)
srnumber = db.Column(db.String, unique=True)
created_date = db.Column(db.Date)
closed_date = db.Column(db.Date)
type_id = db.Column(db.SmallInteger, db.ForeignKey('request_types.type_id'))
agency_id = db.Column(db.SmallInteger, db.ForeignKey('agencies.agency_id'))
source_id = db.Column(db.SmallInteger, db.ForeignKey('sources.source_id'))
council_id = db.Column(db.SmallInteger, db.ForeignKey('councils.council_id'))
region_id = db.Column(db.SmallInteger)
address = db.Column(db.String)
latitude = db.Column(db.Float)
longitude = db.Column(db.Float)
@classmethod
async def one(cls, id: int):
from .council import Council # noqa ... avoiding circular import
from .agency import Agency # noqa ... avoiding circular import
result = await (
db.select(
[
ServiceRequest,
RequestType.type_name,
Council.council_name,
Agency.agency_name,
Source.source_name
]
).select_from(
ServiceRequest.join(
RequestType
).join(
Council
).join(
Agency, ServiceRequest.agency_id == Agency.agency_id
).join(
Source, ServiceRequest.source_id == Source.source_id
)
).where(
ServiceRequest.request_id == id
).gino.first()
)
return result
@classmethod
async def get_request_reports(
cls,
start_date: datetime.date,
end_date: datetime.date
):
from .council import Council # noqa ... avoiding circular import
result = await (
db.select(
[
RequestType.type_name,
Council.council_name,
ServiceRequest.created_date,
db.func.count().label("counts")
]
).select_from(
ServiceRequest.join(RequestType).join(Council)
).where(
sql.and_(
ServiceRequest.created_date >= start_date,
ServiceRequest.created_date <= end_date
)
).group_by(
RequestType.type_name,
Council.council_name,
ServiceRequest.created_date
).gino.all()
)
return result
@classmethod
async def get_recent_requests(cls, start_date: datetime.date):
result = await (
db.select(
ServiceRequest
).where(
ServiceRequest.created_date >= start_date
).order_by(
desc(ServiceRequest.created_date)
).gino.all()
)
return result
async def get_full_request(srnumber: str):
# query the request table to get full record
query = db.text("SELECT * FROM requests WHERE srnumber = :num")
result = await db.first(query, num=srnumber)
return result
# TODO: no cache as this appears to be too big for redis (e.g. 2+ MB)
# @cached(key="Request.open", alias="default")
async def get_open_requests() -> List[ServiceRequest]:
result = await (
db.select(
[
ServiceRequest.request_id,
ServiceRequest.srnumber,
ServiceRequest.type_id,
ServiceRequest.latitude,
ServiceRequest.longitude
]
).where(
ServiceRequest.closed_date == None # noqa
).gino.all()
)
return result
async def get_id_from_srnumber(srnumber: str):
result = await (
db.select(
ServiceRequest
).where(
ServiceRequest.srnumber == srnumber
).gino.scalar()
)
return result
@cached(key="Request.open_type_counts", alias="default")
async def get_open_request_counts():
result = await (
db.select(
[
ServiceRequest.type_id,
RequestType.type_name,
db.func.count().label("type_count")
]
).select_from(
ServiceRequest.join(RequestType)
).where(
and_(
ServiceRequest.closed_date == None, # noqa
)
).group_by(
ServiceRequest.type_id,
RequestType.type_name
).gino.all()
)
return result
async def get_filtered_requests(
start_date: datetime.date = None,
end_date: datetime.date = None,
type_ids: List[int] = None,
council_ids: List[int] = None,
include_updated: bool = False,
offset: int = 0,
limit: int = 100000
):
from .council import Council # noqa ... avoiding circular import
from .agency import Agency # noqa ... avoiding circular import
where_text = []
if (start_date):
if include_updated:
where_text.append(f"(created_date >= '{start_date}' OR closed_date >= '{start_date}')") # noqa
else:
where_text.append(f"created_date >= '{start_date}'")
if (end_date):
# Making end date inclusive by adding one
end_date = end_date + datetime.timedelta(days=1)
if include_updated:
where_text.append(f"(created_date <= '{end_date}' OR closed_date <= '{end_date}')") # noqa
else:
where_text.append(f"created_date <= '{end_date}'")
if (type_ids):
where_text.append(f"service_requests.type_id IN ({', '.join([str(i) for i in type_ids])})") # noqa
if (council_ids):
where_text.append(f"service_requests.council_id IN ({', '.join([str(i) for i in council_ids])})") # noqa
result = await (
db.select(
[
ServiceRequest,
RequestType.type_name,
Council.council_name,
Agency.agency_name,
Source.source_name,
]
).select_from(
ServiceRequest.join(
RequestType
).join(
Council
).join(
Agency, ServiceRequest.agency_id == Agency.agency_id
).join(
Source, ServiceRequest.source_id == Source.source_id
)
).where(
text(" AND ".join(where_text))
).order_by(
desc(ServiceRequest.created_date)
).limit(
limit
).offset(
offset
).gino.all()
)
return result