about summary refs log tree commit diff
path: root/gn3/db/case_attributes.py
blob: 46c4480cd12a7ce7348ac603b5848df9b4de7346 (plain)
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
"""Module that contains functions for editing case-attribute data"""
from pathlib import Path
from typing import Optional
from dataclasses import dataclass
from enum import Enum, auto

import json
import pickle
import lmdb


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


@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.
        - status: (EditStatus): The status of this 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
    status: EditStatus
    user_id: str
    changes: dict


def queue_edit(cursor, directory: Path, edit: CaseAttributeEdit) -> Optional[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, status, user_id, and changes.

    Returns:
        int: An id the particular case-attribute that was updated.
    """
    cursor.execute(
        "INSERT INTO "
        "caseattributes_audit(status, editor, json_diff_data) "
        "VALUES (%s, %s, %s) "
        "ON DUPLICATE KEY UPDATE status=%s",
        (str(edit.status), edit.user_id,
         json.dumps(edit.changes), str(EditStatus.review),))
    directory.mkdir(parents=True, exist_ok=True)
    env = lmdb.open(directory.as_posix(), 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)
        _id = cursor.lastrowid
        review_ids.add(_id)
        txn.put(b"review", pickle.dumps(review_ids))
        return _id


def __fetch_case_attrs_changes__(cursor, change_ids: tuple) -> list:
    """Fetches case attribute change records from the audit table for
    given change IDs.

    Retrieves records from the `caseattributes_audit` table for the
    specified `change_ids`, including the editor, JSON diff data, and
    timestamp. The JSON diff data is deserialized into a Python
    dictionary for each record. Results are ordered by timestamp in
    descending order (most recent first).

    Args:
        cursor: A MySQLdb cursor for executing SQL queries.
        change_ids (tuple): A tuple of integers representing the IDs
        of changes to fetch.

    Returns:
        list: A list of dictionaries, each containing the `editor`,
            `json_diff_data` (as a deserialized dictionary), and `time_stamp`
            for the matching change IDs. Returns an empty list if no records
            are found.

    Notes:
        - The function assumes `change_ids` is a non-empty tuple of valid integers.
        - The `json_diff_data` column in `caseattributes_audit` is expected to contain valid
          JSON strings, which are deserialized into dictionaries.
        - The query uses parameterized placeholders to prevent SQL injection.
        - This is an internal helper function (indicated by double underscores) used by
          other functions like `get_changes`.

    Raises:
        json.JSONDecodeError: If any `json_diff_data` value cannot be deserialized.
        TypeError: If `change_ids` is empty or contains non-integer values, potentially
            causing a database error.

    """
    if not change_ids:
        return {}  # type:ignore
    placeholders = ','.join(['%s'] * len(change_ids))
    cursor.execute(
        "SELECT editor, json_diff_data, time_stamp "
        f"FROM caseattributes_audit WHERE id IN ({placeholders}) "
        "ORDER BY time_stamp DESC",
        change_ids
    )
    results = cursor.fetchall()
    for el in results:
        el["json_diff_data"] = json.loads(el["json_diff_data"])
    return results


def view_change(cursor, change_id: int) -> dict:
    """Queries the `caseattributes_audit` table to fetch the
    `json_diff_data` column for the given `change_id`. The JSON data
    is deserialized into a Python dictionary and returned.  If no
    record is found or the `json_diff_data` is None, an empty
    dictionary is returned.

    Args:
        cursor: A MySQLdb cursor for executing SQL queries.
        change_id (int): The ID of the change to retrieve from the
        `caseattributes_audit` table.

    Returns:
        dict: The deserialized JSON diff data as a dictionary if the
              record exists and contains valid JSON; otherwise, an
              empty dictionary.

    Raises:
        json.JSONDecodeError: If the `json_diff_data` cannot be
            deserialized due to invalid JSON.
        TypeError: If `cursor.fetchone()` returns None (e.g., no
            record found) and `json_diff_data` is accessed, though the
            function handles this by returning an empty dictionary.

    """
    cursor.execute(
        "SELECT json_diff_data "
        "FROM caseattributes_audit "
        "WHERE id = %s",
        (change_id,)
    )
    json_diff_data, _ = cursor.fetchone()
    if json_diff_data:
        json_diff_data = json.loads(json_diff_data)
        return json_diff_data
    return {}


def get_changes(cursor, change_type: EditStatus, directory: Path) -> dict:
    """Retrieves case attribute changes for given lmdb data in
    directory categorized by review status.

    Fetches change IDs from an LMDB database, categorized into the
    "data" key based on the EditStatus

    Args:
        - cursor: A MySQLdb cursor for executing SQL queries.
        - change_type (EditStatus): The status of changes to retrieve
          ('review', 'approved', or 'rejected').
        - directory (Path): The base directory path for the LMDB
          database.

    Returns:
        dict: A dictionary with two keys:
            -'count': A dictionary with counts of 'reviews',
                    'approvals' and 'rejections'.
            - 'data': contains the json diff data of the modified data

    Raises:
        json.JSONDecodeError: If any `json_diff_data` in the audit
            table cannot be deserialized by
            `__fetch_case_attrs_changes__`.
        TypeError: If `inbredset_id` is not an integer or if LMDB data
            cannot be deserialized.  Also raised when an invalid change_id
            is used.

    """
    directory.mkdir(parents=True, exist_ok=True)
    review_ids, approved_ids, rejected_ids = set(), set(), set()
    directory.mkdir(parents=True, exist_ok=True)
    env = lmdb.open(directory.as_posix(), map_size=8_000_000)  # 1 MB
    with env.begin(write=False) as txn:
        if reviews := txn.get(b"review"):
            review_ids = pickle.loads(reviews)
        if approvals := txn.get(b"approved"):
            approved_ids = pickle.loads(approvals)
        if rejections := txn.get(b"rejected"):
            rejected_ids = pickle.loads(rejections)
    changes = {}
    match change_type:
        case EditStatus.review:
            changes = dict(
                zip(review_ids,
                    __fetch_case_attrs_changes__(cursor, tuple(review_ids)))
            )
        case EditStatus.approved:
            changes = dict(
                zip(approved_ids,
                    __fetch_case_attrs_changes__(cursor, tuple(approved_ids)))
            )
        case EditStatus.rejected:
            changes = dict(zip(rejected_ids,
                               __fetch_case_attrs_changes__(cursor, tuple(rejected_ids))))
        case _:
            raise TypeError
    return {
        "change-type": str(change_type),
        "count": {
            "reviews": len(review_ids),
            "approvals": len(approved_ids),
            "rejections": len(rejected_ids)
        },
        "data": changes
    }


# pylint: disable=[too-many-locals]
def apply_change(cursor, change_type: EditStatus, change_id: int, directory: Path) -> bool:
    """Applies or rejects a case attribute change and updates its
    status in the audit table and LMDB.

    Processes a change identified by `change_id` based on the
    specified `change_type` (approved or rejected). For approved
    changes, applies modifications to the `CaseAttributeXRefNew` table
    using bulk inserts and updates the audit status. For rejected
    changes, updates the audit status only.  Manages change IDs in
    LMDB by moving them from the 'review' set to either 'approved' or
    'rejected' sets. Returns False if the `change_id` is not in the
    review set.

    Args:
        cursor: A MySQLdb cursor for executing SQL queries.
        change_type (EditStatus): The action to perform, either
            `EditStatus.approved` or `EditStatus.rejected`.
        change_id (int): The ID of the change to process,
            corresponding to a record in `caseattributes_audit`.
        directory (Path): The base directory path for the LMDB
        database.

    Returns:
        bool: True if the change was successfully applied or rejected,
            False if `change_id` is not found in the LMDB 'review'
            set.

    Notes:
        - Opens an LMDB environment in the specified `directory` with
          a map size of 8 MB.
        - For `EditStatus.approved`, fetches `json_diff_data` from
          `caseattributes_audit`, extracts modifications, and performs
          bulk inserts into `CaseAttributeXRefNew` with `ON DUPLICATE
          KEY UPDATE`.
        - For `EditStatus.rejected`, updates the
          `caseattributes_audit` status without modifying case
          attributes.
        - Uses bulk `SELECT` queries to fetch `StrainId` and
          `CaseAttributeId` values efficiently.
        - Assumes `CaseAttributeXRefNew` has a unique key on
          `(InbredSetId, StrainId, CaseAttributeId)` for `ON DUPLICATE
          KEY UPDATE`.
        - The `json_diff_data` is expected to contain valid JSON with
          an `inbredset_id` and `Modifications.Current` structure.
        - The second column from `fetchone()` is ignored (denoted by
          `_`).

    Raises:
        ValueError: If `change_type` is neither `EditStatus.approved`
            nor `EditStatus.rejected`.
        json.JSONDecodeError: If `json_diff_data` cannot be
            deserialized for approved changes.
        TypeError: If `cursor.fetchone()` returns None for
            `json_diff_data` or if `strain_id` or `caseattr_id` are
            missing during bulk insert preparation.
        pickle.UnpicklingError: If LMDB data (e.g., 'review' or
            'approved' sets) cannot be deserialized.

    """
    review_ids, approved_ids, rejected_ids = set(), set(), set()
    directory.mkdir(parents=True, exist_ok=True)
    env = lmdb.open(directory.as_posix(), map_size=8_000_000)  # 1 MB
    with env.begin(write=True) as txn:
        if reviews := txn.get(b"review"):
            review_ids = pickle.loads(reviews)
        if change_id not in review_ids:
            return False
        match change_type:
            case EditStatus.rejected:
                cursor.execute(
                    "UPDATE caseattributes_audit "
                    "SET status = %s "
                    "WHERE id = %s",
                    (str(change_type), change_id))
                if rejections := txn.get(b"rejected"):
                    rejected_ids = pickle.loads(rejections)
                rejected_ids.add(change_id)
                review_ids.discard(change_id)
                txn.put(b"review", pickle.dumps(review_ids))
                txn.put(b"rejected", pickle.dumps(rejected_ids))
                return True
            case EditStatus.approved:
                cursor.execute(
                    "SELECT json_diff_data "
                    "FROM caseattributes_audit WHERE "
                    "id = %s",
                    (change_id,)
                )
                json_diff_data, _ = cursor.fetchone()
                json_diff_data = json.loads(json_diff_data)
                inbredset_id = json_diff_data.get("inbredset_id")
                modifications = json_diff_data.get(
                    "Modifications", {}).get("Current", {})
                strains = tuple(modifications.keys())
                case_attrs = set()
                for data in modifications.values():
                    case_attrs.update(data.keys())

                # Bulk fetch strain ids
                strain_id_map = {}
                if strains:
                    cursor.execute(
                        "SELECT Name, Id FROM Strain WHERE Name IN "
                        f"({', '.join(['%s'] * len(strains))})",
                        strains
                    )
                    for name, strain_id in cursor.fetchall():
                        strain_id_map[name] = strain_id

                # Bulk fetch case attr ids
                caseattr_id_map = {}
                if case_attrs:
                    cursor.execute(
                        "SELECT Name, CaseAttributeId FROM CaseAttribute "
                        "WHERE InbredSetId = %s AND Name IN "
                        f"({', '.join(['%s'] * len(case_attrs))})",
                        (inbredset_id, *case_attrs)
                    )
                    for name, caseattr_id in cursor.fetchall():
                        caseattr_id_map[name] = caseattr_id

                # Bulk insert data
                insert_data = []
                for strain, data in modifications.items():
                    strain_id = strain_id_map.get(strain)
                    for case_attr, value in data.items():
                        insert_data.append({
                            "inbredset_id": inbredset_id,
                            "strain_id": strain_id,
                            "caseattr_id": caseattr_id_map.get(case_attr),
                            "value": value,
                        })
                if insert_data:
                    cursor.executemany(
                        "INSERT INTO CaseAttributeXRefNew "
                        "(InbredSetId, StrainId, CaseAttributeId, Value) "
                        "VALUES (%(inbredset_id)s, %(strain_id)s, %(caseattr_id)s, %(value)s) "
                        "ON DUPLICATE KEY UPDATE Value = VALUES(Value)",
                        insert_data
                    )

                # Update LMDB and audit table
                cursor.execute(
                    "UPDATE caseattributes_audit "
                    "SET status = %s "
                    "WHERE id = %s",
                    (str(change_type), change_id))
                if approvals := txn.get(b"approved"):
                    approved_ids = pickle.loads(approvals)
                approved_ids.add(change_id)
                review_ids.discard(change_id)
                txn.put(b"review", pickle.dumps(review_ids))
                txn.put(b"approved", pickle.dumps(approved_ids))
                return True
            case _:
                raise ValueError