-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathupdate.py
516 lines (449 loc) · 17.9 KB
/
update.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
"""Functions to update data in the database."""
import json
from psycopg import sql
from zeno_backend.classes.chart import (
Chart,
ChartConfig,
ConfigEncoder,
ParametersEncoder,
)
from zeno_backend.classes.filter import PredicatesEncoder
from zeno_backend.classes.folder import Folder
from zeno_backend.classes.metric import Metric
from zeno_backend.classes.project import Project
from zeno_backend.classes.report import Report, ReportElement
from zeno_backend.classes.slice import Slice
from zeno_backend.classes.tag import Tag
from zeno_backend.classes.user import Author, Organization, User
from zeno_backend.database.database import db_pool
async def folder(folder: Folder, project: str):
"""Update data for a specified folder.
Args:
folder (Folder): the folder data to use for the update.
project (str): the project the user is currently working with.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"UPDATE folders SET name = %s, project_uuid = %s WHERE id = %s;",
[folder.name, project, folder.id],
)
async def slice(slice: Slice, project: str):
"""Update data for a specified slice.
Args:
slice (Slice): the slice data to use for the update.
project (str): the project the user is currently working with.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"UPDATE slices SET project_uuid = %s, name = %s, folder_id = %s, "
"filter = %s WHERE id = %s;",
[
project,
slice.slice_name,
slice.folder_id,
json.dumps(slice.filter_predicates, cls=PredicatesEncoder),
slice.id,
],
)
async def chart(chart: Chart, project: str):
"""Update data for a specified chart.
Args:
chart (Chart): the chart data to use for the update.
project (str): the project the user is currently working with.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"UPDATE charts SET project_uuid = %s, name = %s, type = %s, "
"parameters = %s, data = NULL, updated_at = CURRENT_TIMESTAMP "
"WHERE id = %s;",
[
project,
chart.name,
chart.type,
json.dumps(chart.parameters, cls=ParametersEncoder),
chart.id,
],
)
async def chart_data(chart_id: int, data: str):
"""Add chart data to chart entry.
Args:
chart_id (int): the chart id.
data (str): the chart data to use for the update.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"UPDATE charts SET data = %s WHERE id = %s;",
[
data,
chart_id,
],
)
async def clear_chart_data(project_uuid: str):
"""Set chart data from all charts for a given project to NULL.
Args:
project_uuid (str): the id of the project to null chart data from.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"UPDATE charts SET data = NULL WHERE project_uuid = %s;",
[
project_uuid,
],
)
await conn.commit()
async def tag(tag: Tag, project: str):
"""Update data for a specified tag.
Args:
tag (Tag): the tag data to use for the update.
project (str): the project the user is currently working with.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"UPDATE tags SET project_uuid = %s, name = %s, folder_id = %s "
"WHERE id = %s;",
[
project,
tag.tag_name,
tag.folder_id,
tag.id,
],
)
await cur.execute(
sql.SQL("SELECT data_id FROM {} WHERE tag_id = %s;").format(
sql.Identifier(f"{project}_tags_datapoints")
),
[
tag.id,
],
)
data_ids_result = await cur.fetchall()
if data_ids_result is None:
return
existing_data = set(map(lambda d: d[0], data_ids_result))
new_data = set(tag.data_ids)
to_remove = list(existing_data.difference(new_data))
for data_id in to_remove:
await cur.execute(
sql.SQL(
"DELETE FROM {} WHERE tag_id = %s AND data_id = %s;"
).format(sql.Identifier(f"{project}_tags_datapoints")),
[
tag.id,
data_id,
],
)
to_add = list(new_data.difference(existing_data))
for data_id in to_add:
await cur.execute(
sql.SQL("INSERT INTO {} (tag_id, data_id) VALUES (%s,%s);").format(
sql.Identifier(f"{project}_tags_datapoints")
),
[tag.id, data_id],
)
await conn.commit()
async def user(user: User):
"""Update a user in the database.
Args:
user (User): the updated representation of the user.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"UPDATE users SET name = %s, display_name = %s, cognito_id = %s "
"WHERE id = %s",
[user.name, user.display_name, user.cognito_id, user.id],
)
async def organization(organization: Organization):
"""Update an organization in the database.
Includes updating the organization's members.
Args:
organization (Organization): the updated representation of the organization.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"UPDATE organizations SET name = %s WHERE id = %s",
[organization.name, organization.id],
)
await cur.execute(
"SELECT user_id, admin FROM user_organization "
"WHERE organization_id = %s",
[organization.id],
)
organization_users = await cur.fetchall()
if organization_users is None:
return
org_users = set(map(lambda user: user.id, organization.members))
existing_users = set(map(lambda user: user[0], organization_users))
to_remove = list(existing_users.difference(org_users))
for user in to_remove:
await cur.execute(
"DELETE FROM user_organization "
"WHERE user_id = %s AND organization_id = %s;",
[
user,
organization.id,
],
)
to_add = list(
filter(lambda user: user.id not in existing_users, organization.members)
)
for user in to_add:
await cur.execute(
"INSERT INTO user_organization (user_id, organization_id, admin) "
"VALUES (%s,%s,%s)",
[user.id, organization.id, user.admin],
)
to_edit = list(
filter(
lambda user: any(
user.id == org_user[0] and user.admin != org_user[1]
for org_user in organization_users
),
organization.members,
)
)
for user in to_edit:
await cur.execute(
"UPDATE user_organization SET admin = %s "
"WHERE user_id = %s AND organization_id = %s;",
[user.admin, user.id, organization.id],
)
await conn.commit()
async def project(project_config: Project):
"""Update a project's configuration.
Args:
project_config (Project): the configuration of the project.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
query = sql.SQL(
"UPDATE projects SET name = %s, view = %s, "
"updated_at = CURRENT_TIMESTAMP",
)
params: list[str | bool | int] = [
project_config.name,
project_config.view,
]
if project_config.public is not None:
query += sql.SQL(", public = %s")
params.append(project_config.public)
if project_config.description is not None:
query += sql.SQL(", description = %s")
params.append(project_config.description)
if project_config.samples_per_page is not None:
query += sql.SQL(", samples_per_page = %s")
params.append(project_config.samples_per_page)
query += sql.SQL(" WHERE uuid = %s;")
params.append(project_config.uuid)
await cur.execute(query, params)
await conn.commit()
async def project_metrics(project_config: Project):
"""Update a project's metrics.
Args:
project_config (Project): the configuration of the project.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"SELECT id, name, type, columns FROM metrics WHERE project_uuid = %s;",
[project_config.uuid],
)
metric_results = await cur.fetchall()
metrics = list(
map(
lambda metric: Metric(
id=metric[0],
name=metric[1],
type=metric[2],
columns=metric[3],
),
metric_results,
)
)
new_metrics = project_config.metrics
for metric in metrics:
# check if the metric matches one to be added
index = next(
(
i
for i, new_metric in enumerate(new_metrics)
if new_metric.name == metric.name
and set(new_metric.columns) == set(metric.columns)
and new_metric.type == metric.type
),
None,
)
# if there is no match with an existing metric, delete the metric
if index is None:
await cur.execute(
"DELETE FROM metrics WHERE project_uuid = %s AND id = %s;",
[project_config.uuid, metric.id],
)
# also reset chart data since definition changed
await cur.execute(
"UPDATE charts SET data = NULL WHERE project_uuid = %s;",
[
project_config.uuid,
],
)
# if there is a match, we don't have to add the metric
else:
new_metrics.pop(index)
for metric in new_metrics:
await cur.execute(
"INSERT INTO metrics (project_uuid, name, type, columns) VALUES "
"(%s, %s, %s, %s);",
[project_config.uuid, metric.name, metric.type, metric.columns],
)
await conn.commit()
async def report(report: Report):
"""Update a report's configuration.
Args:
report (Report): the configuration of the report.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"UPDATE reports SET name = %s, public = %s, description = %s, "
"updated_at = CURRENT_TIMESTAMP WHERE id = %s;",
[
report.name,
report.public,
report.description,
report.id,
],
)
async def report_projects(report_id: int, project_uuids: list):
"""Update a report's projects.
Args:
report_id (str): the id of the report.
project_uuids (list): the list of project ids.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"DELETE FROM report_project WHERE report_id = %s;",
[report_id],
)
for project_uuid in project_uuids:
await cur.execute(
"INSERT INTO report_project (report_id, project_uuid) "
"VALUES (%s,%s)",
[report_id, project_uuid],
)
await conn.commit()
async def project_user(project: str, user: User):
"""Update a user's project access in the database.
Args:
project (str): the project for which to update the access.
user (User): the user for which to update the access.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"UPDATE user_project SET editor = %s WHERE project_uuid = %s"
" AND user_id = %s;",
[user.admin, project, user.id],
)
async def project_org(project: str, organization: Organization):
"""Update a organization's project access in the database.
Args:
project (str): the project for which to update the access.
organization (Organization): the organization for which to update the access.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"UPDATE organization_project SET editor = %s WHERE project_uuid = %s "
"AND organization_id = %s;",
[organization.admin, project, organization.id],
)
async def report_element(element: ReportElement):
"""Update an elements for a report.
Args:
element (ReportElement): the element to be updated.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"UPDATE report_elements SET type = %s, data = %s, position = %s"
" WHERE id = %s RETURNING report_id;",
[
element.type,
element.data,
element.position,
element.id,
],
)
report_id = await cur.fetchall()
await cur.execute(
"UPDATE reports SET updated_at = CURRENT_TIMESTAMP WHERE id = %s;",
[report_id[0][0]],
)
await conn.commit()
async def report_author(report_id: int, author: Author):
"""Update an author for a report.
Args:
report_id (int): the id of the report.
author (Author): the author to be updated.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"UPDATE report_author SET position = %s"
" WHERE user_id = %s AND report_id = %s;",
[author.position, author.user.id, report_id],
)
await conn.commit()
async def report_user(report_id: int, user: User):
"""Update a user's report access in the database.
Args:
report_id (int): the report for which to update the access.
user (User): the user for which to update the access.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"UPDATE user_report SET editor = %s WHERE report_id = %s"
" AND user_id = %s;",
[user.admin, report_id, user.id],
)
async def report_org(report_id: int, organization: Organization):
"""Update a organization's report access in the database.
Args:
report_id (int): the report for which to update the access.
organization (Organization): the organization for which to update the access.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"UPDATE organization_report SET editor = %s WHERE report_id = %s "
"AND organization_id = %s;",
[organization.admin, report_id, organization.id],
)
async def chart_config(config: ChartConfig, chart_id: int | None = None):
"""Update a project's chart config.
Args:
config (ChartConfig): updated chart config for the project.
chart_id (int | None): the id of the chart this is linked to. Defaults to None.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
if chart_id is None:
await cur.execute(
"UPDATE chart_config SET config = %s WHERE project_uuid = %s "
"AND chart_id IS NULL;",
[json.dumps(config, cls=ConfigEncoder), config.project_uuid],
)
else:
await cur.execute(
"UPDATE chart_config SET config = %s WHERE chart_id = %s;",
[json.dumps(config, cls=ConfigEncoder), chart_id],
)