|
3 | 3 | import pandas as pd |
4 | 4 | from datetime import datetime |
5 | 5 | from enum import IntEnum |
| 6 | +from typing import Optional |
6 | 7 |
|
7 | 8 |
|
8 | 9 | class SqlQueryValues(IntEnum): |
@@ -569,3 +570,140 @@ def check_if_subject_has_temporary_address(nhs_no: str) -> pd.DataFrame: |
569 | 570 | bind_vars = {"nhs_no": nhs_no} |
570 | 571 | df = OracleDB().execute_query(query, bind_vars) |
571 | 572 | return df |
| 573 | + |
| 574 | + |
| 575 | +def set_org_parameter_value(param_id: int, param_value: str, org_id: str) -> None: |
| 576 | + """ |
| 577 | + Updates the value of an organisation parameter in the database. |
| 578 | +
|
| 579 | + This function first ends any existing values for the given parameter and organisation by updating their effective dates and audit information. |
| 580 | + It then inserts a new value for the parameter with the provided value and sets the audit fields accordingly. |
| 581 | +
|
| 582 | + Args: |
| 583 | + param_id (int): The ID of the parameter to update. |
| 584 | + param_value (str): The new value to set for the parameter. |
| 585 | + org_id (str): The organisation ID for which the parameter should be set. |
| 586 | + """ |
| 587 | + # End any old values |
| 588 | + sql_update = ( |
| 589 | + "UPDATE org_parameters op " |
| 590 | + "SET op.effective_from = op.effective_from - 1, " |
| 591 | + "op.effective_to = CASE WHEN op.effective_to IS NULL THEN TRUNC(SYSDATE)-1 ELSE op.effective_to - 1 END, " |
| 592 | + "op.audit_reason = 'AUTOMATED TESTING - END', " |
| 593 | + "op.datestamp = SYSTIMESTAMP " |
| 594 | + "WHERE op.org_id = :org_id " |
| 595 | + "AND param_id = :param_id" |
| 596 | + ) |
| 597 | + params = {"org_id": org_id, "param_id": param_id} |
| 598 | + logging.info(f"executing query to end any old values: {sql_update}") |
| 599 | + OracleDB().update_or_insert_data_to_table(sql_update, params) |
| 600 | + |
| 601 | + # Insert new value |
| 602 | + sql_insert = ( |
| 603 | + "INSERT INTO org_parameters (org_param_id, org_code, org_id, param_id, val, effective_from, pio_id, audit_reason, datestamp) " |
| 604 | + "VALUES (" |
| 605 | + "seq_org_param.NEXTVAL, " |
| 606 | + f"(SELECT org_code FROM org WHERE org_id = :org_id), " |
| 607 | + f"{org_id}, " |
| 608 | + f"{param_id}, " |
| 609 | + f"'{param_value}', " |
| 610 | + "TRUNC(SYSDATE), " |
| 611 | + "1, " |
| 612 | + "'AUTOMATED TESTING - ADD', " |
| 613 | + "SYSTIMESTAMP)" |
| 614 | + ) |
| 615 | + params = {"org_id": org_id} |
| 616 | + logging.info(f"executing query to set new value: {sql_insert}") |
| 617 | + OracleDB().update_or_insert_data_to_table(sql_insert, params) |
| 618 | + |
| 619 | + |
| 620 | +def get_org_parameter_value(param_id: int, org_id: str) -> pd.DataFrame: |
| 621 | + """ |
| 622 | + Retrieves the value of an organisation parameter from the database. |
| 623 | +
|
| 624 | + Args: |
| 625 | + param_id (int): The ID of the parameter to retrieve. |
| 626 | + org_id (str): The organisation ID for which the parameter value should be retrieved. |
| 627 | +
|
| 628 | + Returns: |
| 629 | + pd.DataFrame: A DataFrame containing the parameter value and its effective date. |
| 630 | + """ |
| 631 | + query = """ |
| 632 | + SELECT val, effective_from, audit_reason |
| 633 | + FROM org_parameters |
| 634 | + WHERE param_id = :param_id |
| 635 | + AND org_id = :org_id |
| 636 | + """ |
| 637 | + bind_vars = {"param_id": param_id, "org_id": org_id} |
| 638 | + df = OracleDB().execute_query(query, bind_vars) |
| 639 | + return df |
| 640 | + |
| 641 | + |
| 642 | +def get_investigation_dataset_polyp_category( |
| 643 | + dataset_id: int, polyp_number: int |
| 644 | +) -> Optional[str]: |
| 645 | + """ |
| 646 | + Retrieves the polyp category for a given dataset ID and polyp number. |
| 647 | +
|
| 648 | + Args: |
| 649 | + dataset_id (int): The ID of the dataset. |
| 650 | + polyp_number (int): The number of the polyp. |
| 651 | +
|
| 652 | + Returns: |
| 653 | + Optional[str]: The polyp category description if found, otherwise None. |
| 654 | + """ |
| 655 | + query = """ |
| 656 | + select polyp_category |
| 657 | + from ( |
| 658 | + select |
| 659 | + rank () over (order by p.polyp_id) as polyp_number, |
| 660 | + pc.description as polyp_category |
| 661 | + from ds_polyp_t p |
| 662 | + left outer join valid_values pc |
| 663 | + on pc.valid_value_id = p.polyp_category_id |
| 664 | + where ext_test_id = :ext_test_id |
| 665 | + and p.deleted_flag = 'N' |
| 666 | + ) |
| 667 | + where polyp_number = :polyp_number |
| 668 | + """ |
| 669 | + bind_vars = { |
| 670 | + "ext_test_id": dataset_id, |
| 671 | + "polyp_number": polyp_number, |
| 672 | + } |
| 673 | + df = OracleDB().execute_query(query, bind_vars) |
| 674 | + polyp_category = df["polyp_category"].iloc[0] if not df.empty else None |
| 675 | + return polyp_category |
| 676 | + |
| 677 | + |
| 678 | +def get_investigation_dataset_polyp_algorithm_size( |
| 679 | + dataset_id: int, polyp_number: int |
| 680 | +) -> Optional[str]: |
| 681 | + """ |
| 682 | + Retrieves the polyp algorithm size for a given dataset ID and polyp number. |
| 683 | +
|
| 684 | + Args: |
| 685 | + dataset_id (int): The ID of the dataset. |
| 686 | + polyp_number (int): The number of the polyp. |
| 687 | +
|
| 688 | + Returns: |
| 689 | + Optional[int]: The polyp algorithm size if found, otherwise None. |
| 690 | + """ |
| 691 | + query = """ |
| 692 | + select polyp_algorithm_size |
| 693 | + from ( |
| 694 | + select |
| 695 | + rank () over (order by p.polyp_id) as polyp_number, |
| 696 | + p.polyp_algorithm_size |
| 697 | + from ds_polyp_t p |
| 698 | + where ext_test_id = :ext_test_id |
| 699 | + and p.deleted_flag = 'N' |
| 700 | + ) |
| 701 | + where polyp_number = :polyp_number |
| 702 | + """ |
| 703 | + bind_vars = { |
| 704 | + "ext_test_id": dataset_id, |
| 705 | + "polyp_number": polyp_number, |
| 706 | + } |
| 707 | + df = OracleDB().execute_query(query, bind_vars) |
| 708 | + polyp_algorithm_size = df["polyp_algorithm_size"].iloc[0] if not df.empty else None |
| 709 | + return polyp_algorithm_size |
0 commit comments