-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrecord.py
More file actions
983 lines (851 loc) · 31.2 KB
/
record.py
File metadata and controls
983 lines (851 loc) · 31.2 KB
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
from __future__ import with_statement
from typing import List, Dict, Any, Optional, Tuple, Iterable
from sqlalchemy import cast, Text, String
from sqlalchemy.orm.attributes import flag_modified
from sqlalchemy.sql.expression import bindparam
from sqlalchemy import update
from . import attribute, general
from .. import models, enums
from ..models import (
Record,
RecordLabelAssociation,
LabelingTaskLabel,
RecordAttributeTokenStatistics,
Attribute,
RecordTokenized,
)
from ..integration_objects.helper import (
REFINERY_ATTRIBUTE_ACCESS_GROUPS,
REFINERY_ATTRIBUTE_ACCESS_USERS,
)
from ..session import session
from ..util import prevent_sql_injection
def get(project_id: str, record_id: str) -> Record:
return (
session.query(Record)
.filter(Record.project_id == project_id, Record.id == record_id)
.first()
)
def get_one(project_id: str) -> Record:
return session.query(Record).filter(Record.project_id == project_id).first()
def get_by_record_ids(project_id: str, record_ids: Iterable[str]) -> List[Record]:
return (
session.query(Record)
.filter(Record.project_id == project_id, Record.id.in_(record_ids))
.all()
)
def get_without_project_id(record_id: str) -> Record:
"""
Attention: instead of this method use get(project_id, record_id),
this is a temporary solution and should be fixed by adding
the project id to every record query/mutation
"""
return session.query(Record).filter(Record.id == record_id).first()
def get_all(project_id: str) -> List[Record]:
return session.query(Record).filter(Record.project_id == project_id).all()
def get_all_ids(project_id: str) -> List[str]:
record_ids = (
session.query(cast(Record.id, Text))
.filter(Record.project_id == project_id)
.all()
)
return [record_id for record_id, in record_ids]
def get_sample_data_of(
project_id: str,
attribute_name: str,
limit: int = 10,
add_condition: Optional[str] = None,
) -> List[Any]:
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
attribute_name = prevent_sql_injection(
attribute_name, isinstance(attribute_name, str)
)
limit = prevent_sql_injection(limit, isinstance(limit, int))
# any return since json value is return (-> vs ->>) so casting isn't necessary
where_add = ""
if add_condition:
where_add = "AND " + add_condition
query = f"""
SELECT r.data->'{attribute_name}'
FROM record r
WHERE r.project_id = '{project_id}'
{where_add}
ORDER BY RANDOM()
LIMIT {limit}
"""
return [row[0] for row in general.execute_all(query)]
def get_max_running_id(project_id: str) -> int:
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
query = f"""
SELECT max(CAST(DATA->>'running_id' AS INTEGER)) max_running_id
FROM record
WHERE project_id = '{project_id}'
"""
return general.execute_first(query)[0]
def get_existing_records_by_composite_key(
project_id: str,
records_data: List[Dict[str, Any]],
primary_keys: List[Attribute],
category: str,
) -> Dict[str, Record]:
query = (
session.query(Record)
.filter(Record.project_id == project_id)
.filter(Record.category == category)
)
for primary_key in primary_keys:
query = query.filter(
Record.data[primary_key.name]
.as_string()
.in_([str(record_item[primary_key.name]) for record_item in records_data])
)
existing_records = query.all()
return {
__infer_concatenated_primary_key_of_record(
record=record, primary_keys=primary_keys
): record
for record in existing_records
}
def get_ids_by_keys(keys: Dict[str, Any]) -> List[Tuple[str, str]]:
return session.query(cast(Record.id, Text)).filter(Record.id.in_(keys.keys())).all()
def get_ids_of_manual_records_by_labeling_task(
project_id: str, labeling_task_id: str
) -> List[str]:
return (
session.query(RecordLabelAssociation.record_id)
.filter(
RecordLabelAssociation.source_type == enums.LabelSource.MANUAL.value,
RecordLabelAssociation.project_id == project_id,
RecordLabelAssociation.labeling_task_label_id == LabelingTaskLabel.id,
LabelingTaskLabel.labeling_task_id == labeling_task_id,
)
.group_by(RecordLabelAssociation.record_id)
.all()
)
def count_records_without_tokenization(project_id: str) -> int:
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
query = f"""
SELECT count(r.id) c
FROM ({get_records_without_tokenization(project_id, 0, True)}) r
"""
return general.execute_first(query).c
def count_records_without_manual_label(project_id: str) -> int:
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
query = f"""
SELECT COUNT(r.id) c
FROM record r
LEFT JOIN (
SELECT record_id
FROM record_label_association rla
WHERE rla.project_id = '{project_id}' AND rla.source_type = '{enums.LabelSource.MANUAL.value}'
GROUP BY record_id ) rla
ON r.id = rla.record_id
WHERE r.project_id = '{project_id}' AND rla.record_id IS NULL
"""
return general.execute_first(query).c
def get_attribute_data_with_doc_bins_of_records(
project_id: str, attribute_name: str
) -> List[Any]:
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
attribute_name = prevent_sql_injection(
attribute_name, isinstance(attribute_name, str)
)
query = f"""
SELECT
rt.id,
r."data" ->> '{attribute_name}' attribute_data,
rt.bytes
FROM record r
INNER JOIN record_tokenized rt
ON r.project_id = rt.project_id AND r.id = rt.record_id
WHERE r.project_id = '{project_id}'
"""
return general.execute_all(query)
def update_bytes_of_record_tokenized(
values: List[Dict[str, Any]], project_id: str
) -> None:
query = (
update(RecordTokenized)
.where(
RecordTokenized.id == bindparam("_id"),
RecordTokenized.project_id == project_id,
)
.values({"bytes": bindparam("bytes")})
)
general.execute(query, values)
general.flush()
def update_columns_of_tokenized_records(rt_ids: str, attribute_name: str) -> None:
# rt_ids = prevent_sql_injection(rt_ids, isinstance(rt_ids, str)) # excluded since already prepared beforehand
attribute_name = prevent_sql_injection(
attribute_name, isinstance(attribute_name, str)
)
query = f"""
UPDATE record_tokenized
SET columns = array_append(columns, '{attribute_name}')
WHERE id IN {rt_ids}
"""
general.execute(query)
general.flush()
def get_missing_rats_records(
project_id: str, attribute_id: str, limit: int = 0
) -> List[Any]:
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
attribute_id = prevent_sql_injection(attribute_id, isinstance(attribute_id, str))
limit = prevent_sql_injection(limit, isinstance(limit, int))
attribute_add = f"AND att.data_type = '{enums.TokenizerTask.TYPE_TEXT.value}'"
attribute_add += f"AND att.state IN ('{enums.AttributeState.UPLOADED.value}', '{enums.AttributeState.USABLE.value}', '{enums.AttributeState.RUNNING.value}')"
if attribute_id:
attribute_add += f" AND att.id = '{attribute_id}'"
query = f"""
SELECT r.id record_id, array_agg(att.id) attribute_ids, rt.bytes bytes, rt.columns "columns"
FROM record r
INNER JOIN attribute att
ON r.project_id = att.project_id {attribute_add}
LEFT JOIN record_attribute_token_statistics rats
ON r.id = rats.record_id
AND att.id = rats.attribute_id
LEFT JOIN record_tokenized rt
ON rt.record_id = r.id
AND rt.project_id = r.project_id
WHERE r.project_id = '{project_id}'
AND rats.id IS NULL
GROUP BY r.id, rt.bytes, rt.columns
"""
if limit > 0:
query += f"LIMIT {limit} "
return general.execute_all(query)
def get_records_without_tokenization(
project_id: str, limit: int = 0, query_only: bool = False
) -> List[Any]:
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
limit = prevent_sql_injection(limit, isinstance(limit, int))
query = f"""
SELECT r.id, r.data, rt."columns"
FROM record r
LEFT JOIN record_tokenized rt
ON r.id = rt.record_id
AND r.project_id = rt.project_id
AND rt.project_id = '{project_id}'
WHERE r.project_id = '{project_id}'
AND rt.id IS NULL
"""
if query_only:
return query
if limit > 0:
query += f"LIMIT {limit} "
return general.execute_all(query)
def get_missing_tokenized_records(
project_id: str,
attribute_names_string: str,
limit: int = 0,
query_only: bool = False,
) -> List[Any]:
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
# attribute_names_string = prevent_sql_injection(attribute_names_string, isinstance(attribute_names_string, str)) # excluded since prepared string not single value
limit = prevent_sql_injection(limit, isinstance(limit, int))
query = f"""
SELECT r.id, r.data, rt."columns"
FROM record r
LEFT JOIN record_tokenized rt
ON r.id = rt.record_id
AND r.project_id = rt.project_id
AND rt.project_id = '{project_id}'
WHERE r.project_id = '{project_id}'
AND (
NOT rt."columns" @> '{attribute_names_string}'
OR rt."columns" IS NULL
)
"""
if query_only:
return query
if limit > 0:
query += f"LIMIT {limit} "
return general.execute_all(query)
def get_count_scale_uploaded(project_id: str) -> int:
return (
session.query(models.Record)
.filter(
models.Record.category == enums.RecordCategory.SCALE.value,
models.Record.project_id == project_id,
)
.count()
)
def get_count_all_records(project_id: str) -> int:
return (
session.query(models.Record)
.filter(
models.Record.project_id == project_id,
)
.count()
)
def get_count_test_uploaded(project_id: str) -> int:
return (
session.query(models.Record)
.filter(
models.Record.category == enums.RecordCategory.TEST.value,
models.Record.project_id == project_id,
)
.count()
)
def get_token_statistics_by_project_id(project_id: str) -> List[Any]:
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
query = f"""
SELECT
statistics.id,
statistics.record_id,
statistics.attribute_id,
statistics.num_token
FROM
record_attribute_token_statistics as statistics
INNER JOIN
attribute
ON
statistics.attribute_id=attribute.id
WHERE
attribute.project_id='{project_id}'
;
"""
return general.execute_all(query)
def get_attribute_calculation_sample_records(project_id: str, n: int = 10) -> List[Any]:
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
n = prevent_sql_injection(n, isinstance(n, int))
query = f"""
SELECT record.id::TEXT, record."data"
FROM record
INNER JOIN record_tokenized rt
ON record.id = rt.record_id AND record.project_id = rt.project_id
WHERE record.project_id='{project_id}' AND record.category = '{enums.RecordCategory.SCALE.value}'
ORDER BY RANDOM()
LIMIT {n}
"""
return general.execute_all(query)
def get_missing_columns_str(project_id: str) -> str:
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
query = f"""
SELECT att.name
FROM attribute att
LEFT JOIN (
SELECT unnest(columns) col, project_id
FROM (
SELECT columns, project_id
FROM record_tokenized rt
WHERE rt.project_id = '{project_id}'
LIMIT 1
) i
) used_attributes
ON att.project_id = used_attributes.project_id AND att.name = used_attributes.col
WHERE att.project_id = '{project_id}' AND used_attributes.project_id IS NULL
AND att.state IN ('{enums.AttributeState.UPLOADED.value}','{enums.AttributeState.USABLE.value}','{enums.AttributeState.AUTOMATICALLY_CREATED.value}')
"""
missing_columns = general.execute_all(query)
if not missing_columns:
return ""
return ",\n".join([f"'{k[0]}',r.data->'{k[0]}'" for k in missing_columns])
def get_record_id_groups(project_id: str, group_size: int = 20) -> List[List[str]]:
if group_size <= 0:
return None
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
group_size = prevent_sql_injection(group_size, isinstance(group_size, int))
query = f"""
SELECT array_agg(id) record_ids
FROM (
SELECT id::TEXT, FLOOR((ROW_NUMBER() OVER())/{group_size}) gn
FROM record r
WHERE r.project_id = '{project_id}'
)x
GROUP BY gn
"""
groups = general.execute_all(query)
return [g[0] for g in groups] if groups else None
def get_record_data_for_id_group(
project_id: str, record_ids: List[str], attribute_name: str
) -> Dict[str, str]:
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
record_ids = [prevent_sql_injection(r, isinstance(r, str)) for r in record_ids]
attribute_name = prevent_sql_injection(
attribute_name, isinstance(attribute_name, str)
)
record_where = " id IN ('" + "','".join(record_ids) + "')"
query = f"""
SELECT id::TEXT, data::JSON->'{attribute_name}' AS "{attribute_name}"
FROM record
WHERE project_id = '{project_id}' AND {record_where}
AND data::JSON->'{attribute_name}' IS NOT NULL
AND LENGTH((data::JSON->'{attribute_name}')::TEXT) > 5
"""
data = general.execute_all(query)
return {row[0]: row[1] for row in data} if data else None
def get_full_record_data_for_id_group(
project_id: str, record_ids: List[str]
) -> Dict[str, str]:
if len(record_ids) == 0:
return []
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
record_ids = [prevent_sql_injection(r, isinstance(r, str)) for r in record_ids]
record_where = " id IN ('" + "','".join(record_ids) + "')"
query = f"""
SELECT id::TEXT, data::JSON
FROM record
WHERE project_id = '{project_id}' AND {record_where}
"""
data = general.execute_all(query)
return {row[0]: row[1] for row in data} if data else None
def get_attribute_data(
project_id: str,
attribute_name: str,
only_missing: bool = False,
embedding_id: Optional[str] = None,
) -> Tuple[List[str], List[str]]:
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
attribute_name = prevent_sql_injection(
attribute_name, isinstance(attribute_name, str)
)
if embedding_id:
embedding_id = prevent_sql_injection(
embedding_id, isinstance(embedding_id, str)
)
query = None
order = __get_order_by(project_id, prefix="r.")
join_extension, where_add = "", ""
if only_missing:
if not embedding_id:
raise ValueError("embedding_id must be provided if only_missing is True")
join_extension, where_add = (
f"""
LEFT JOIN embedding_tensor et
ON et.project_id = r.project_id
AND et.record_id = r.id
AND et.project_id = '{project_id}' AND et.embedding_id = '{embedding_id}'
""",
"AND et.id IS NULL",
)
if attribute.get_by_name(project_id, attribute_name).data_type == "EMBEDDING_LIST":
query = f"""
SELECT id::TEXT || '@' || sub_key id, att AS "{attribute_name}"
FROM (
SELECT r.id, value as att, ordinality - 1 as sub_key
FROM record r
{join_extension}
cross join json_array_elements_text((r.data::JSON->'{attribute_name}')) with ordinality
WHERE r.project_id = '{project_id}' {where_add}
{order}
)x """
else:
query = f"""
SELECT r.id::TEXT, r.data::JSON->'{attribute_name}' AS "{attribute_name}"
FROM record r
{join_extension}
WHERE r.project_id = '{project_id}' {where_add}
{order}
"""
result = general.execute_all(query)
record_ids, attribute_values = list(zip(*result))
return record_ids, attribute_values
def count(project_id: str) -> int:
return session.query(Record).filter(Record.project_id == project_id).count()
def count_missing_delta(project_id: str, attribute_id: str) -> int:
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
attribute_id = prevent_sql_injection(attribute_id, isinstance(attribute_id, str))
query = f"""
WITH n AS (
SELECT NAME
FROM attribute a
WHERE id = '{attribute_id}'
)
SELECT COUNT(*)
FROM record r, n
WHERE r.project_id = '{project_id}'
AND r.data->>n.name IS NULL
"""
value = general.execute_first(query)
if not value or not value[0]:
return 0
return value[0]
def get_missing_delta_record_ids(project_id: str, attribute_id: str) -> List[str]:
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
attribute_id = prevent_sql_injection(attribute_id, isinstance(attribute_id, str))
query = f"""
WITH n AS (
SELECT NAME
FROM attribute a
WHERE id = '{attribute_id}'
)
SELECT r.id::TEXT
FROM record r, n
WHERE r.project_id = '{project_id}'
AND r.data->>n.name IS NULL
"""
return [row[0] for row in general.execute_all(query)]
def count_attribute_list_entries(project_id: str, attribute_name: str) -> int:
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
attribute_name = prevent_sql_injection(
attribute_name, isinstance(attribute_name, str)
)
query = f"""
SELECT sum(json_array_length(r.data->'{attribute_name}'))
FROM record r
WHERE project_id = '{project_id}'
"""
value = general.execute_first(query)
if not value or not value[0]:
return 0
return value[0]
def count_by_project_and_source(
project_id: str, record_category: str, label_source: str
) -> int:
return (
session.query(models.Record)
.filter(
models.Record.category == record_category,
models.Record.project_id == project_id,
models.Record.record_label_associations.any(
models.RecordLabelAssociation.source_type == label_source
),
)
.count()
)
# rats = record_attribute_token_statistics
def count_missing_rats_records(
project_id: str, attribute_id: Optional[str] = None
) -> int:
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
attribute_id = prevent_sql_injection(attribute_id, isinstance(attribute_id, str))
attribute_add = f"AND att.data_type = '{enums.TokenizerTask.TYPE_TEXT.value}'"
attribute_add += f"AND att.state IN ('{enums.AttributeState.UPLOADED.value}', '{enums.AttributeState.USABLE.value}', '{enums.AttributeState.RUNNING.value}')"
if attribute_id:
attribute_add += f" AND att.id = '{attribute_id}'"
query = f"""
SELECT COUNT(*) c
FROM (
SELECT r.id
FROM record r
INNER JOIN attribute att
ON r.project_id = att.project_id {attribute_add}
LEFT JOIN record_attribute_token_statistics rats
ON r.id = rats.record_id
AND att.id = rats.attribute_id
WHERE r.project_id = '{project_id}'
AND rats.id IS NULL
GROUP BY r.id)x
"""
result = general.execute_first(query)
return result.c
def count_missing_tokenized_records(project_id: str) -> int:
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
query = f"""
SELECT COUNT(*)
FROM (
{get_records_without_tokenization(project_id, None, query_only=True)}
) record_query
"""
return general.execute_first(query)[0]
def count_tokenized_records(project_id: str) -> int:
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
query = f"""
SELECT COUNT(*) c
FROM record_tokenized rt
WHERE rt.project_id = '{project_id}'
"""
result = general.execute_first(query)
return result.c
def create_or_update_token_statistic(
project_id: str,
record_id: str,
attribute_id: str,
amount: int,
with_commit: bool = False,
) -> None:
# currently project_id isnt in the table -- this will be changed at some point
tbl_entry = (
session.query(models.RecordAttributeTokenStatistics)
.filter(
models.RecordAttributeTokenStatistics.attribute_id == attribute_id,
models.RecordAttributeTokenStatistics.record_id == record_id,
)
.first()
)
if tbl_entry:
tbl_entry.num_token = amount
else:
tbl_entry = models.RecordAttributeTokenStatistics(
project_id=project_id,
record_id=record_id,
attribute_id=attribute_id,
num_token=amount,
)
general.add(tbl_entry)
general.flush_or_commit(with_commit)
def create(
project_id: str,
record_data: Dict,
category: str,
with_commit: bool = False,
) -> Record:
record = Record(
project_id=project_id,
data=record_data,
category=category,
)
general.add(record, with_commit)
return record
def create_records(
project_id: str,
records_data: List[Dict[str, Any]],
category: str,
with_commit: bool = False,
) -> List[Record]:
records = [
Record(
project_id=project_id,
data=record_item,
category=category,
)
for record_item in records_data
]
general.add_all(records, with_commit)
return records
def create_record_attribute_token_statistics(
project_id: str,
record_id: str,
attribute_id: str,
num_token: int,
with_commit: bool = False,
) -> RecordAttributeTokenStatistics:
stats = RecordAttributeTokenStatistics(
project_id=project_id,
record_id=record_id,
attribute_id=attribute_id,
num_token=num_token,
)
general.add(stats, with_commit)
return stats
def update_records(
records_data: List[Dict[str, Any]],
labels_data: List[Dict[str, Any]],
existing_records_by_key: Dict[str, Record],
primary_keys: List[Attribute],
) -> Tuple[
List[Dict[Any, Any]],
List[Dict[Any, Any]],
List[Dict[Any, Any]],
List[Dict[Any, Any]],
]:
updated_records = []
labels_data_of_updated_records = []
records_data_without_db_entries = []
labels_of_records_without_db_entries = []
for record_item, label_item in zip(records_data, labels_data):
record_item_primary_key = __infer_concatenated_primary_key_of_record_item(
record_item, primary_keys=primary_keys
)
if record_item_primary_key in existing_records_by_key:
record = existing_records_by_key[record_item_primary_key]
record.data = record_item
updated_records.append(record)
labels_data_of_updated_records.append(label_item)
else:
records_data_without_db_entries.append(record_item)
labels_of_records_without_db_entries.append(label_item)
return (
records_data_without_db_entries,
labels_of_records_without_db_entries,
updated_records,
labels_data_of_updated_records,
)
def update_add_user_created_attribute(
project_id: str,
attribute_id: str,
calculated_attributes: Dict[str, str],
with_commit: bool = False,
) -> None:
attribute_item = attribute.get(project_id, attribute_id)
changed = 0
for record_id, attribute_value in calculated_attributes.items():
record_item = get(project_id=project_id, record_id=record_id)
if not record_item:
# this can happen if an record was deleted or the tokenizer file isn't up to date
continue
record_item.data[attribute_item.name] = attribute_value
flag_modified(record_item, "data")
if changed > 1000:
changed = 0
general.flush_or_commit(with_commit)
changed += 1
general.flush_or_commit(with_commit)
def delete(project_id: str, record_id: str, with_commit: bool = False) -> None:
session.delete(
session.query(Record)
.filter(Record.project_id == project_id, Record.id == record_id)
.first()
)
general.flush_or_commit(with_commit)
def delete_many(
project_id: str, record_ids: Iterable[str], with_commit: bool = False
) -> int:
res = (
session.query(Record)
.filter(Record.project_id == project_id, Record.id.in_(record_ids))
.delete()
)
general.flush_or_commit(with_commit)
return res
def delete_all(project_id: str, with_commit: bool = False) -> None:
session.query(Record).filter(Record.project_id == project_id).delete()
general.flush_or_commit(with_commit)
def delete_user_created_attribute(
project_id: str, attribute_id: str, with_commit: bool = False
) -> None:
attribute_item = attribute.get(project_id, attribute_id)
if not attribute_item.user_created:
return
record_items = get_all(project_id=project_id)
for i, record_item in enumerate(record_items):
del record_item.data[attribute_item.name]
flag_modified(record_item, "data")
if (i + 1) % 1000 == 0:
general.flush_or_commit(with_commit)
general.flush_or_commit(with_commit)
def delete_access_management_attributes(
project_id: str, with_commit: bool = True
) -> None:
access_groups_attribute_item = attribute.get_by_name(
project_id, REFINERY_ATTRIBUTE_ACCESS_GROUPS
)
access_users_attribute_item = attribute.get_by_name(
project_id, REFINERY_ATTRIBUTE_ACCESS_USERS
)
if access_users_attribute_item and access_groups_attribute_item:
record_items = get_all(project_id=project_id)
for i, record_item in enumerate(record_items):
if record_item.data.get(access_groups_attribute_item.name):
del record_item.data[access_groups_attribute_item.name]
if record_item.data.get(access_users_attribute_item.name):
del record_item.data[access_users_attribute_item.name]
flag_modified(record_item, "data")
if (i + 1) % 1000 == 0:
general.flush_or_commit(with_commit)
general.flush_or_commit(with_commit)
def delete_duplicated_rats(with_commit: bool = False) -> None:
# no project so run for all to prevent expensive join with record table
query = """
DELETE FROM record_tokenized rt
USING (
SELECT record_id, attribute_id, (array_agg(id))[1] AS id_to_del
FROM record_attribute_token_statistics rt
GROUP BY record_id,attribute_id
HAVING COUNT(*) >1) AS del_helper
WHERE rt.id = del_helper.id_to_del
"""
general.execute(query)
general.flush_or_commit(with_commit)
def has_byte_data(project_id: str, record_id: str) -> bool:
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
record_id = prevent_sql_injection(record_id, isinstance(record_id, str))
query = f"""
SELECT id
FROM record_tokenized
WHERE project_id = '{project_id}'
AND record_id = '{record_id}'
"""
return general.execute_first(query) is not None
def __infer_concatenated_primary_key_of_record_item(
record_item: Dict, primary_keys: List[Attribute]
) -> str:
concatenated_key = ""
for idx, primary_key in enumerate(primary_keys):
concatenated_key = f"{concatenated_key}{idx}{record_item[primary_key.name]}"
return concatenated_key
def __infer_concatenated_primary_key_of_record(
record: Record, primary_keys: List[Attribute]
) -> str:
concatenated_key = ""
for idx, primary_key in enumerate(primary_keys):
concatenated_key = f"{concatenated_key}{idx}{record.data[primary_key.name]}"
return concatenated_key
def get_tokenized_record_from_db(
project_id: str, record_id: str
) -> models.RecordTokenized:
return (
session.query(models.RecordTokenized)
.filter(
models.RecordTokenized.project_id == project_id,
models.RecordTokenized.record_id == record_id,
)
.first()
)
def get_tokenized_records_from_db(
project_id: str, record_ids: List[str]
) -> List[models.RecordTokenized]:
return (
session.query(models.RecordTokenized)
.filter(
models.RecordTokenized.project_id == project_id,
models.RecordTokenized.record_id.in_(record_ids),
)
.all()
)
def __get_order_by(project_id: str, first_x: int = 3, prefix: str = "") -> str:
query = f"""
SELECT name, data_type
FROM attribute a
WHERE a.project_id = '{project_id}'
ORDER BY a.relative_position
LIMIT {first_x}
"""
values = general.execute_all(query)
order = ""
for x in values:
if order != "":
order += ", "
tmp = f"{prefix}data->>'{x.name}'"
r_id = attribute.get_running_id_name(project_id)
if x.data_type == "INTEGER" and x.name == r_id:
# only running_id gets cast as other aren't sure to be integers (e.g. empty fields)
tmp = f"({tmp})::INTEGER"
order += tmp
if order != "":
order = "ORDER BY " + order
return order
def get_first_no_text_column(project_id: str, record_id: str) -> str:
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
record_id = prevent_sql_injection(record_id, isinstance(record_id, str))
query = f"""
SELECT '''' || x.name || ': ' || (r.data ->>x.name) || '''' AS name_col
FROM record r,
(
SELECT a.name
FROM attribute a
WHERE data_type NOT IN('{enums.DataTypes.TEXT.value}', '{enums.DataTypes.CATEGORY.value}', '{enums.DataTypes.LLM_RESPONSE.value}', '{enums.DataTypes.TEXT_LIST.value}')
AND a.state IN ('{enums.AttributeState.AUTOMATICALLY_CREATED.value}','{enums.AttributeState.UPLOADED.value}','{enums.AttributeState.USABLE.value}')
AND a.project_id = '{project_id}'
ORDER BY a.relative_position
LIMIT 1
)x
WHERE r.project_id = '{project_id}' AND r.id = '{record_id}'
"""
return general.execute_first(query)[0]
def get_record_ids_by_running_ids(project_id: str, running_ids: List[int]) -> List[str]:
return [
row[0]
for row in (
session.query(cast(Record.id, String))
.filter(
Record.project_id == project_id,
Record.data[attribute.get_running_id_name(project_id)]
.as_integer()
.in_(running_ids),
)
.all()
)
]
def get_records_by_running_ids(project_id: str, running_ids: List[int]) -> List[str]:
return (
session.query(Record)
.filter(
Record.project_id == project_id,
Record.data[attribute.get_running_id_name(project_id)]
.as_integer()
.in_(running_ids),
)
.all()
)