Skip to content

Comments

Comments(perfdb)

Class used for handling Comments. Can be accessed via perfdb.comments.

Parameters:

  • perfdb

    (PerfDB) –

    Top level object carrying all functionality and the connection handler.

Source code in echo_postgres/comments.py
def __init__(self, perfdb: e_pg.PerfDB) -> None:
    """Class used for handling comments. Can be accessed via `perfdb.comments`.

    Parameters
    ----------
    perfdb : PerfDB
        Top level object carrying all functionality and the connection handler.
    """
    super().__init__(perfdb)

    from .comments_events import CommentsEvents
    from .comments_ivcurve import CommentsIVCurve

    # * subclasses

    self.events = CommentsEvents(perfdb)
    self.ivcurve = CommentsIVCurve(perfdb)

delete(comment_ids)

Deletes comments.

Parameters:

  • comment_ids

    (list[int]) –

    List of comment ids to delete.

Source code in echo_postgres/comments.py
@validate_call
def delete(
    self,
    comment_ids: list[int],
) -> None:
    """Deletes comments.

    Parameters
    ----------
    comment_ids : list[int]
        List of comment ids to delete.
    """
    query = sql.SQL("DELETE FROM performance.comments WHERE id = ANY({ids})").format(
        ids=sql.Literal(comment_ids),
    )

    with self._perfdb.conn.reconnect() as conn:
        # deleting
        result = conn.execute(query)

    logger.debug(f"Deleted {result.rowcount} rows from comments table")

get(comment_regex=None, bazefield_users=None, users=None, period=None, event_ids=None, iv_curve_ids=None, output_type='DataFrame')

Gets all comments with detailed information.

The most useful keys/columns returned are:

  • id
  • comment
  • bazefield_user_id
  • bazefield_user_name
  • modified_date

Parameters:

  • comment_regex

    (str | None, default: None ) –

    String to filter the comments by regex. By default None.

  • bazefield_users

    (list[str] | None, default: None ) –

    Bazefield user names (authors of the comments). By default None.

    This parameter will be removed in future versions. Please use users instead.

  • users

    (list[str] | None, default: None ) –

    User names (authors of the comments). By default None.

  • period

    (DateTimeRange | None, default: None ) –

    Period to filter the comments. By default None.

  • event_ids

    (list | None, default: None ) –

    List of event ids to filter the comments. By default None.

  • iv_curve_ids

    (list | None, default: None ) –

    List of iv curve ids to filter the comments. By default None.

  • output_type

    (Literal['dict', 'DataFrame', 'pl.DataFrame'], default: 'DataFrame' ) –

    Output type of the data. Can be one of ["dict", "DataFrame", "pl.DataFrame"]. By default "dict"

Returns:

  • dict[int, dict[str, Any]]

    In case output_type is "dict", returns a dictionary in the format {id: {column: value, ...}}

  • DataFrame

    In case output_type is "DataFrame", returns a DataFrame where the index is the comment id.

Source code in echo_postgres/comments.py
@validate_call
def get(
    self,
    comment_regex: str | None = None,
    bazefield_users: list[str] | None = None,
    users: list[str] | None = None,
    period: DateTimeRange | None = None,
    event_ids: list | None = None,
    iv_curve_ids: list | None = None,
    output_type: Literal["dict", "DataFrame", "pl.DataFrame"] = "DataFrame",
) -> dict[int, dict[str, Any]] | pd.DataFrame | pl.DataFrame:
    """Gets all comments with detailed information.

    The most useful keys/columns returned are:

    - id
    - comment
    - bazefield_user_id
    - bazefield_user_name
    - modified_date

    Parameters
    ----------
    comment_regex : str | None, optional
        String to filter the comments by regex. By default None.
    bazefield_users : list[str] | None, optional
        Bazefield user names (authors of the comments). By default None.

        This parameter will be removed in future versions. Please use `users` instead.
    users : list[str] | None, optional
        User names (authors of the comments). By default None.
    period : DateTimeRange | None, optional
        Period to filter the comments. By default None.
    event_ids : list | None, optional
        List of event ids to filter the comments. By default None.
    iv_curve_ids : list | None, optional
        List of iv curve ids to filter the comments. By default None.
    output_type : Literal["dict", "DataFrame", "pl.DataFrame"], optional
        Output type of the data. Can be one of ["dict", "DataFrame", "pl.DataFrame"]. By default "dict"

    Returns
    -------
    dict[int, dict[str, Any]]
        In case output_type is "dict", returns a dictionary in the format {id: {column: value, ...}}
    DataFrame
        In case output_type is "DataFrame", returns a DataFrame where the index is the comment id.
    """
    # checking arguments
    where = self._check_get_args(
        comment_regex=comment_regex,
        bazefield_users=bazefield_users,
        users=users,
        period=period,
        event_ids=event_ids,
        iv_curve_ids=iv_curve_ids,
    )

    query = [sql.SQL("SELECT * FROM performance.v_comments "), where, sql.SQL("ORDER BY modified_date")]
    query = sql.Composed(query)

    with self._perfdb.conn.reconnect() as conn:
        df = conn.read_to_polars(query)

    if output_type == "pl.DataFrame":
        return df

    df = df.to_pandas(use_pyarrow_extension_array=True)
    df = df.set_index("id")

    return df.to_dict(orient="index") if output_type == "dict" else df

get_ids(comment_regex=None, bazefield_users=None, users=None, period=None, event_ids=None, iv_curve_ids=None)

Gets the comment ids.

Parameters:

  • comment_regex

    (str | None, default: None ) –

    String to filter the comments by regex. By default None.

  • bazefield_users

    (list[str] | None, default: None ) –

    Bazefield user names (authors of the comments). By default None.

    This parameter will be removed in future versions. Please use users instead.

  • users

    (list[str] | None, default: None ) –

    User names (authors of the comments). By default None.

  • period

    (DateTimeRange | None, default: None ) –

    Period to filter the comments. By default None.

  • event_ids

    (list | None, default: None ) –

    List of event ids to filter the comments. By default None.

  • iv_curve_ids

    (list | None, default: None ) –

    List of iv curve ids to filter the comments. By default None.

Returns:

  • list[int]

    List of comment ids.

Source code in echo_postgres/comments.py
@validate_call
def get_ids(
    self,
    comment_regex: str | None = None,
    bazefield_users: list[str] | None = None,
    users: list[str] | None = None,
    period: DateTimeRange | None = None,
    event_ids: list | None = None,
    iv_curve_ids: list | None = None,
) -> list[int]:
    """Gets the comment ids.

    Parameters
    ----------
    comment_regex : str | None, optional
        String to filter the comments by regex. By default None.
    bazefield_users : list[str] | None, optional
        Bazefield user names (authors of the comments). By default None.

        This parameter will be removed in future versions. Please use `users` instead.
    users : list[str] | None, optional
        User names (authors of the comments). By default None.
    period : DateTimeRange | None, optional
        Period to filter the comments. By default None.
    event_ids : list | None, optional
        List of event ids to filter the comments. By default None.
    iv_curve_ids : list | None, optional
        List of iv curve ids to filter the comments. By default None.

    Returns
    -------
    list[int]
        List of comment ids.
    """
    # checking arguments
    where = self._check_get_args(
        comment_regex=comment_regex,
        bazefield_users=bazefield_users,
        period=period,
        event_ids=event_ids,
        users=users,
        iv_curve_ids=iv_curve_ids,
    )

    query = [
        sql.SQL("SELECT id FROM performance.v_comments "),
        where,
    ]
    query = sql.Composed(query)

    with self._perfdb.conn.reconnect() as conn:
        df = conn.read_to_polars(query)

    return df["id"].to_list()

insert(comment, bazefield_user_name=None, user_name=None, event_ids=None, iv_curve_ids=None)

Inserts a new comment.

Parameters:

  • comment

    (str) –

    Text of the comment.

  • bazefield_user_name

    (str | None, default: None ) –

    Bazefield user name. This will be deprecated in future versions. Please use user_name instead. By default None.

    If not provided, user_name must be provided.

  • user_name

    (str | None, default: None ) –

    User name. By default None.

    If not provided, bazefield_user_name must be provided.

  • event_ids

    (list[int] | None, default: None ) –

    List of event ids to associate with the comment, by default None

  • iv_curve_ids

    (list[int] | None, default: None ) –

    List of iv curve ids to associate with the comment, by default None

Returns:

  • int

    Id of the inserted comment.

Source code in echo_postgres/comments.py
@validate_call
def insert(
    self,
    comment: str,
    bazefield_user_name: str | None = None,
    user_name: str | None = None,
    event_ids: list[int] | None = None,
    iv_curve_ids: list[int] | None = None,
) -> int:
    """Inserts a new comment.

    Parameters
    ----------
    comment : str
        Text of the comment.
    bazefield_user_name : str | None, optional
        Bazefield user name. This will be deprecated in future versions. Please use user_name instead. By default None.

        If not provided, user_name must be provided.
    user_name : str | None, optional
        User name. By default None.

        If not provided, bazefield_user_name must be provided.
    event_ids : list[int] | None, optional
        List of event ids to associate with the comment, by default None
    iv_curve_ids : list[int] | None, optional
        List of iv curve ids to associate with the comment, by default None

    Returns
    -------
    int
        Id of the inserted comment.
    """
    query = sql.SQL(
        "SELECT * FROM performance.fn_create_comment(_comment => {comment}, _bazefield_user_name => {bazefield_user_name}, _user_name => {user_name})",
    ).format(
        comment=sql.Literal(comment),
        bazefield_user_name=sql.Literal(bazefield_user_name),
        user_name=sql.Literal(user_name),
    )

    with self._perfdb.conn.reconnect() as conn:
        comm_id = conn.execute(query).fetchone()[0]

    if event_ids:
        try:
            self._perfdb.comments.events.insert(comment_id=comm_id, event_ids=event_ids)
            logger.debug(f"Events {event_ids} inserted for comment made by user {bazefield_user_name=} {user_name=}.")
        except Exception as e:
            logger.exception(
                f"Error inserting events for comment made by user {bazefield_user_name=} {user_name=}. The comment was inserted but the events were not.",
            )
            raise RuntimeError(
                f"Error inserting events for comment made by {bazefield_user_name=} {user_name=}. The comment was inserted but the events were not.",
            ) from e

    logger.debug(f"Comment inserted by user {bazefield_user_name=} {user_name=} with id {comm_id}")

    return comm_id