-
Notifications
You must be signed in to change notification settings - Fork 1
/
application.py
319 lines (280 loc) · 10.2 KB
/
application.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import json
import os
import os.path
from datetime import date, timedelta
from os import path
from random import randint
import time
import requests
import httpx
import asyncio
from flask_httpauth import HTTPBasicAuth
from werkzeug.utils import header_property
from requests_futures.sessions import FuturesSession
from concurrent.futures import as_completed
from werkzeug.datastructures import Headers
from dicttoxml import dicttoxml
from flask import Flask, Response, app, jsonify, render_template, request
from folioclient.FolioClient import FolioClient
from lxml import etree
from werkzeug.exceptions import HTTPException
application = Flask(__name__)
auth = HTTPBasicAuth()
@auth.verify_password
def verify_password(username, password):
try:
folio_client = FolioClient(
os.environ["OKAPI_URL"],
os.environ["TENANT_ID"],
os.environ["FOLIO_USERNAME"],
os.environ["FOLIO_PASSWORD"],
)
login_payload = {"username": username, "password": password}
login_path = "/authn/login"
resp = requests.post(
folio_client.okapi_url + login_path,
headers=folio_client.okapi_headers,
data=json.dumps(login_payload),
)
return resp.status_code == 201
except:
return False
def get_file_path():
path_debug = os.environ.get("DATA_FOLDER_PATH", "/MADE_UP")
path_real = "/home"
if os.path.exists(path_debug):
return os.path.join(path_debug, "stats.json")
if os.path.exists(path_real):
return os.path.join(path_real, "stats.json")
raise Exception("None of the paths exists")
@application.route("/rtac", methods=["GET"])
def rtac():
folio_client = FolioClient(
os.environ["OKAPI_URL"],
os.environ["TENANT_ID"],
os.environ["FOLIO_USERNAME"],
os.environ["FOLIO_PASSWORD"],
)
bib_id = request.args.get("Bib_ID")
onr = request.args.get("ONR")
issn = request.args.get("ISSN")
isbn = request.args.get("ISBN")
resp = create_rtac_response(folio_client, bib_id)
if not bib_id:
raise ValueError("bib id missing. other identifiers not yet implemented")
root = etree.Element("Item_Information")
for itemd in resp:
item = etree.fromstring(itemd)
root.append(item)
return Response(etree.tostring(root), mimetype="text/xml")
@application.route("/")
def hello_world():
return "RTAC and statistics app from FOLIO"
@application.route("/statistics/")
@application.route("/statistics/<date>")
@auth.login_required
def hi(date=date.today()):
definitions = []
with open("stats_definitions.json") as jf:
definitions = json.load(jf)
names = [f["name"] for f in definitions]
names.sort()
return render_template(
"stats_main.html",
date=date,
measures=names,
seven_days_back=date.today() - timedelta(days=7),
thirty_days_back=date.today() - timedelta(days=30),
)
@application.route("/reset")
@auth.login_required
def reset():
saved_stats = {}
with open(get_file_path(), "w+") as f:
f.write(json.dumps(saved_stats))
return "reset"
@application.route("/ninety")
def create_90():
folio_client = get_folio_client()
saved_stats = {}
with open(get_file_path(), "r") as f:
saved_stats = json.load(f)
i = 0
for d in range(1, 90):
cd = date.today() - timedelta(days=d)
if str(cd) not in saved_stats:
i += 1
saved_stats[str(cd)] = get_date_data(cd, folio_client)
with open(get_file_path(), "w+") as f:
f.write(json.dumps(saved_stats))
if i > 7:
break
return "done"
def get_folio_client():
return FolioClient(
os.environ["OKAPI_URL"],
os.environ["TENANT_ID"],
os.environ["FOLIO_USERNAME"],
os.environ["FOLIO_PASSWORD"],
)
@application.route("/seven")
def create_7():
saved_stats = {}
folio_client = get_folio_client()
with open(get_file_path(), "r") as f:
saved_stats = json.load(f)
for d in range(1, 7):
cd = date.today() - timedelta(days=d)
if str(cd) not in saved_stats:
saved_stats[str(cd)] = get_date_data(cd, folio_client)
with open(get_file_path(), "w+") as f:
f.write(json.dumps(saved_stats))
return "done"
@application.route("/status")
@auth.login_required
def get_status():
saved_stats = {}
folio_client = get_folio_client()
if not path.exists(get_file_path()):
with open(get_file_path(), "w+") as f:
f.write(json.dumps(saved_stats))
with open(get_file_path(), "r") as f:
saved_stats = json.load(f)
yesterday = date.today() - timedelta(days=1)
if str(yesterday) not in saved_stats:
saved_stats[str(yesterday)] = get_date_data(yesterday, folio_client)
with open(get_file_path(), "w+") as f:
f.write(json.dumps(saved_stats))
return jsonify(list(saved_stats.values()))
@application.route("/status2")
def get_status2():
saved_stats = {}
folio_client = get_folio_client()
if not path.exists(get_file_path()):
with open(get_file_path(), "w+") as f:
f.write(json.dumps(saved_stats))
with open(get_file_path(), "r") as f:
saved_stats = json.load(f)
yesterday = date.today() - timedelta(days=1)
if str(yesterday) not in saved_stats:
saved_stats[str(yesterday)] = get_date_data(yesterday, folio_client)
with open(get_file_path(), "w+") as f:
f.write(json.dumps(saved_stats))
return "done"
@application.route("/today")
def get_today():
saved_stats = {}
folio_client = get_folio_client()
if not path.exists(get_file_path()):
with open(get_file_path(), "w+") as f:
f.write(json.dumps(saved_stats))
with open(get_file_path(), "r") as f:
saved_stats = json.load(f)
yesterday = date.today() - timedelta(days=1)
if str(yesterday) not in saved_stats:
saved_stats[str(yesterday)] = get_date_data(yesterday, folio_client)
with open(get_file_path(), "w+") as f:
f.write(json.dumps(saved_stats))
saved_stats[str(date.today())] = get_date_data(date.today(), folio_client)
return jsonify(list(saved_stats.values()))
def get_date_data(date, folio_client: FolioClient):
tomorrow = date + timedelta(days=1)
definitions = []
with open("stats_definitions.json") as jf:
definitions = json.load(jf)
res = {"date": str(date)}
session = FuturesSession()
futures = []
for idx, defi in enumerate(definitions):
date_q = date if defi["when"] == "today" else tomorrow
path = defi["path"].format(date_q)
try:
url = folio_client.okapi_url + path
future = session.get(url, headers=folio_client.okapi_headers)
future.idx = idx
future.path = path
future.namet = defi["name"]
future.tic = time.perf_counter()
futures.append(future)
except Exception as ee:
raise ee
for idx, future in enumerate(as_completed(futures)):
toc = time.perf_counter()
resp = future.result()
res[future.namet] = resp.json()["totalRecords"]
return res
@application.route("/totals")
def get_totals():
folio_client = FolioClient(
os.environ["OKAPI_URL"],
os.environ["TENANT_ID"],
os.environ["FOLIO_USERNAME"],
os.environ["FOLIO_PASSWORD"],
)
loans_path = "/circulation/loans?limit=0"
requests_path = "/circulation/requests?limit=0"
users_path = "/users?limit=0"
instances_path = "/instance-storage/instances?limit=0"
holdings_path = "/holdings-storage/holdings?limit=0"
items_path = "/item-storage/items?limit=0"
return jsonify(
{
"total_users": folio_client.folio_get_single_object(users_path)[
"totalRecords"
],
"total_loans": folio_client.folio_get_single_object(loans_path)[
"totalRecords"
],
"total_requests": folio_client.folio_get_single_object(requests_path)[
"totalRecords"
],
"total_instances": folio_client.folio_get_single_object(instances_path)[
"totalRecords"
],
"total_holdings": folio_client.folio_get_single_object(holdings_path)[
"totalRecords"
],
"total_items": folio_client.folio_get_single_object(items_path)[
"totalRecords"
],
}
)
@application.errorhandler(Exception)
def handle_error(e):
if "/rtac" in request.url:
empty = {
"Item": {
"Item_no": None,
"UniqueItemId": None,
"Location": None,
"Call_No": None,
"Loan_Policy": None,
"Status": "Okänd",
"Status_Date_Description": None,
"Status_Date": None,
}
}
empty_root = dicttoxml(empty, custom_root="Item_Information", attr_type=False)
return Response(empty_root, mimetype="text/xml")
return jsonify(error=str(e))
def create_rtac_response(folio_client, bib_id):
query = '?query=(identifiers=/@value/@identifierTypeId="4f3c4c2c-8b04-4b54-9129-f732f1eb3e14" "{}" or identifiers=/@value/@identifierTypeId="28c170c6-3194-4cff-bfb2-ee9525205cf7" "{}")'
path = "/instance-storage/instances"
q = query.format(bib_id, bib_id)
print(f"looking for instances with Libris ids: {q}")
instances = folio_client.folio_get(path, "instances", q)
instance_id = instances[0]["id"]
holdings = folio_client.folio_get("/rtac/{}".format(instance_id), "holdings")
for i, holding in enumerate(holdings):
date = holding.get("dueDate", "")[:10]
my_dict = {
"Item_no": 1,
"UniqueItemId": holding.get("id", ""),
"Location": holding.get("location", ""),
"Call_No": holding.get("callNumber", ""),
"Loan_Policy": "",
"Status": holding.get("status", ""),
"Status_Date_Description": None,
"Status_Date": date,
}
yield dicttoxml(my_dict, custom_root="Item", attr_type=False)