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
|
"""Module that contains functions for editing case-attribute data"""
from pathlib import Path
from typing import Any, Optional, Tuple
from dataclasses import dataclass
from enum import Enum, auto
import os
import json
import pickle
import lmdb
import MySQLdb
@dataclass
class CaseAttributeEdit:
"""Represents an edit operation for case attributes in the database.
Attributes:
inbredset_id (int): The ID of the inbred set associated with
the edit.
user_id (str): The ID of the user performing the edit.
changes (dict): A dictionary containing the changes to be
applied to the case attributes.
"""
inbredset_id: int
user_id: str
changes: dict
class EditStatus(Enum):
"""Enumeration for the status of the edits."""
review = auto() # pylint: disable=[invalid-name]
approved = auto() # pylint: disable=[invalid-name]
rejected = auto() # pylint: disable=[invalid-name]
def __str__(self):
"""Print out human-readable form."""
return self.name
def queue_edit(cursor, directory: Path, edit: CaseAttributeEdit) -> int:
"""Queues a case attribute edit for review by inserting it into
the audit table and storing its review ID in an LMDB database.
Args:
cursor: A database cursor for executing SQL queries.
directory (Path): The base directory path for the LMDB database.
edit (CaseAttributeEdit): A dataclass containing the edit details, including
inbredset_id, user_id, and changes.
Returns:
int: An id the particular case-attribute that was updated.
Notes:
- Inserts the edit into the `caseattributes_audit` table with status set to
`EditStatus.review`.
- Uses LMDB to store review IDs under the key b"review" for the given
inbredset_id.
- The LMDB map_size is set to 8 MB.
"""
cursor.execute(
"INSERT INTO "
"caseattributes_audit(status, editor, json_diff_data) "
"VALUES (%s, %s, %s) "
"ON DUPLICATE KEY UPDATE status=%s",
(str(EditStatus.review),
edit.user_id, json.dumps(edit.changes), str(EditStatus.review),))
directory = f"{directory}/case-attributes/{edit.inbredset_id}"
if not os.path.exists(directory):
os.makedirs(directory)
env = lmdb.open(directory, map_size=8_000_000) # 1 MB
with env.begin(write=True) as txn:
review_ids = set()
if reviews := txn.get(b"review"):
review_ids = pickle.loads(reviews)
review_ids.add(cursor.lastrowid)
txn.put(b"review", pickle.dumps(review_ids))
return review_ids
|