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

    # * subclasses

    self.events = CommentsEvents(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 IN ({ids})").format(
        ids=sql.SQL(", ").join(map(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, period=None, event_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.

  • 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.

  • output_type

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

    Output type of the data. Can be one of ["dict", "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,
    period: DateTimeRange | None = None,
    event_ids: list | None = None,
    output_type: Literal["dict", "DataFrame"] = "DataFrame",
) -> dict[int, dict[str, Any]] | 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.
    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.
    output_type : Literal["dict", "DataFrame"], optional
        Output type of the data. Can be one of ["dict", "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, period=period, event_ids=event_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_pandas(query)
    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, period=None, event_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.

  • 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.

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,
    period: DateTimeRange | None = None,
    event_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.
    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.

    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)

    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_pandas(query)

    return df["id"].tolist()

insert(comment, bazefield_user_name, event_ids=None)

Inserts a new comment.

Parameters:

  • comment

    (str) –

    Text of the comment.

  • bazefield_user_name

    (str) –

    Bazefield user name.

  • event_ids

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

    List of event 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,
    event_ids: list[int] | None = None,
) -> int:
    """Inserts a new comment.

    Parameters
    ----------
    comment : str
        Text of the comment.
    bazefield_user_name : str
        Bazefield user name.
    event_ids : list[int] | None, optional
        List of event 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}, {bazefield_user_name})").format(
        comment=sql.Literal(comment),
        bazefield_user_name=sql.Literal(bazefield_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.info(f"Events {event_ids} inserted for comment made by user {bazefield_user_name}")
        except Exception as e:
            logger.exception(
                f"Error inserting events for comment made by user {bazefield_user_name}. The comment was inserted but the events were not.",
            )
            raise RuntimeError(
                f"Error inserting events for comment made by {bazefield_user_name}. The comment was inserted but the events were not.",
            ) from e

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

    return comm_id