Skip to content

Commit

Permalink
Merge pull request #23 from lsst-sqre/tickets/DM-41304
Browse files Browse the repository at this point in the history
DM-41304: Add new parameters to narrative log service
  • Loading branch information
sebastian-aranda authored Oct 26, 2023
2 parents d373e35 + b350e30 commit 6aedd2b
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Change Log
==========

0.6.0
-----

* Add new parameters: ``category`` and ``time_lost_type``.

0.5.1
-----

Expand Down
55 changes: 55 additions & 0 deletions alembic/versions/54f755dbdb6f_add_category_and_time_lost_type_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""add category and time_lost_type parameters
Revision ID: 54f755dbdb6f
Revises: d9606992ad8d
Create Date: 2023-10-24 14:19:37.665359
"""
import logging

import sqlalchemy as sa
import sqlalchemy.types as saty

from alembic import op

# revision identifiers, used by Alembic.
revision = "54f755dbdb6f"
down_revision = "d9606992ad8d"
branch_labels = None
depends_on = None


MESSAGE_TABLE_NAME = "message"
CATEGORY_LEN = 50
TIME_LOST_TYPE_LEN = 50


def upgrade(log: logging.Logger, table_names: set[str]) -> None:
if MESSAGE_TABLE_NAME not in table_names:
log.info(f"No {MESSAGE_TABLE_NAME} table; nothing to do")
return
log.info("Add 'category' column and add 'time_lost_type' column")
op.add_column(
table_name=MESSAGE_TABLE_NAME,
column=sa.Column(
"category", saty.String(length=CATEGORY_LEN), nullable=True
),
)
op.add_column(
table_name=MESSAGE_TABLE_NAME,
column=sa.Column(
"time_lost_type",
saty.String(length=TIME_LOST_TYPE_LEN),
nullable=True,
),
)


def downgrade(log: logging.Logger, table_names: set[str]) -> None:
if MESSAGE_TABLE_NAME not in table_names:
log.info(f"No {MESSAGE_TABLE_NAME} table; nothing to do")
return

log.info("Drop 'category' column and drop 'time_lost_type' column")
op.drop_column(table_name=MESSAGE_TABLE_NAME, column_name="category")
op.drop_column(table_name=MESSAGE_TABLE_NAME, column_name="time_lost_type")
15 changes: 15 additions & 0 deletions src/narrativelog/create_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
# Length of the site_id field.
SITE_ID_LEN = 16

# Length of the category field.
CATEGORY_LEN = 50

# Length of the time_lost_type field.
TIME_LOST_TYPE_LEN = 50


def create_message_table(metadata: sa.MetaData) -> sa.Table:
"""Make a model of the narrativelog message table.
Expand Down Expand Up @@ -59,6 +65,13 @@ def create_message_table(metadata: sa.MetaData) -> sa.Table:
sa.Column("cscs", saty.ARRAY(sa.Text), nullable=True),
# Added 2022-07-37
sa.Column("date_end", saty.DateTime(), nullable=True),
# Added 2023-10-24
sa.Column("category", saty.String(length=CATEGORY_LEN), nullable=True),
sa.Column(
"time_lost_type",
saty.String(length=TIME_LOST_TYPE_LEN),
nullable=True,
),
# Constraints
sa.ForeignKeyConstraint(["parent_id"], ["message.id"]),
)
Expand All @@ -70,6 +83,8 @@ def create_message_table(metadata: sa.MetaData) -> sa.Table:
"user_id",
"is_valid",
"date_added",
"category",
"time_lost_type",
):
sa.Index(f"idx_{name}", table.columns[name])

Expand Down
7 changes: 7 additions & 0 deletions src/narrativelog/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ class Message(BaseModel):
title="Zero or more primary hardware component names. "
"Each entry should be a valid component name entry on the OBS jira project.",
)
# Added 2023-10-24
category: None | str = Field(
title="Category of message.",
)
time_lost_type: None | str = Field(
title="Type of time lost.",
)

class Config:
orm_mode = True
Expand Down
12 changes: 12 additions & 0 deletions src/narrativelog/routers/add_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ async def add_message(
level: int = fastapi.Body(
..., description="Message level; a python logging level."
),
category: None
| str = fastapi.Body(
default=None,
description="Message category; a string.",
),
time_lost_type: None
| str = fastapi.Body(
default=None,
description="Type of lost time.",
),
tags: list[str] = fastapi.Body(
default=[],
description="Tags describing the message. " + TAG_DESCRIPTION,
Expand Down Expand Up @@ -140,6 +150,8 @@ async def add_message(
systems=systems,
subsystems=subsystems,
cscs=cscs,
category=category,
time_lost_type=time_lost_type,
)
.returning(sa.literal_column("*"))
)
Expand Down
12 changes: 12 additions & 0 deletions src/narrativelog/routers/edit_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ async def edit_message(
default=None,
description="Message level; a python logging level.",
),
category: None
| str = fastapi.Body(
default=None,
description="Message category; a string.",
),
time_lost_type: None
| str = fastapi.Body(
default=None,
description="Type of lost time.",
),
tags: None
| list[str] = fastapi.Body(
default=None,
Expand Down Expand Up @@ -133,6 +143,8 @@ async def edit_message(
"components",
"primary_software_components",
"primary_hardware_components",
"category",
"time_lost_type",
"urls",
"time_lost",
"date_begin",
Expand Down
28 changes: 28 additions & 0 deletions src/narrativelog/routers/find_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,34 @@ async def find_messages(
description="User agents (which app created the message). "
"Repeat the parameter for each value.",
),
categories: None
| list[str] = fastapi.Query(
default=None,
description="Categories, or fragments of categories, "
"of which at least one must be present. "
"Repeat the parameter for each value.",
),
exclude_categories: None
| list[str] = fastapi.Query(
default=None,
description="Categories, or fragments of categories, "
"of which all must be absent. "
"Repeat the parameter for each value.",
),
time_lost_types: None
| list[str] = fastapi.Query(
default=None,
description="Time lost types, or fragments of time lost types, "
"of which at least one must be present. "
"Repeat the parameter for each value.",
),
exclude_time_lost_types: None
| list[str] = fastapi.Query(
default=None,
description="Time lost types, or fragments of time lost types, "
"of which all must be absent. "
"Repeat the parameter for each value.",
),
tags: None
| list[str] = fastapi.Query(
default=None,
Expand Down
9 changes: 8 additions & 1 deletion src/narrativelog/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
from sqlalchemy.ext.asyncio import create_async_engine

from . import main, shared_state
from .create_tables import create_jira_fields_table, create_message_table
from .create_tables import (
CATEGORY_LEN,
create_jira_fields_table,
create_message_table,
)
from .message import JIRA_FIELDS, MESSAGE_FIELDS

# Range of dates for random messages.
Expand Down Expand Up @@ -392,6 +396,9 @@ def random_message() -> MessageDictT:
primary_hardware_components=random_strings(
TEST_PRIMARY_HARDWARE_COMPONENTS
),
# Added 2023-10-24
category=random_str(nchar=CATEGORY_LEN),
time_lost_type=random.choice(["fault", "weather"]),
)

# Check that we have set all fields (not necessarily in order).
Expand Down
4 changes: 4 additions & 0 deletions tests/test_add_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ async def test_add_message(self) -> None:
add_args_full["primary_hardware_components"] = random_strings(
TEST_PRIMARY_HARDWARE_COMPONENTS
)
add_args_full["category"] = "test"
add_args_full["time_lost_type"] = random.choice(
["fault", "weather"]
)
response = await client.post(
"/narrativelog/messages", json=add_args_full
)
Expand Down
5 changes: 3 additions & 2 deletions tests/test_edit_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ def assert_good_edit_response(
(
"id",
"site_id",
"is_valid",
"parent_id",
"date_added",
"is_valid",
"date_added",
"date_invalidated",
)
):
Expand Down Expand Up @@ -86,6 +85,8 @@ async def test_edit_message(self) -> None:
"new primary_hardware_component 1",
"new primary_hardware_component 2",
],
category="New category",
time_lost_type=random.choice(["fault", "weather"]),
)
# Repeatedly edit the old message. Each time
# add a new version of the message with one field omitted,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_find_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ def assert_two_messages_ordered(
for key in order_by:
if key.startswith("-"):
field = key[1:]
val1 = message1[field]
val2 = message2[field]
desired_cmp_result = 1
else:
field = key
Expand Down Expand Up @@ -458,6 +456,8 @@ def is_valid_predicate(message: MessageDictT) -> bool:
"level",
"user_id",
"user_agent",
"category",
"time_lost_type",
)
)
for field, prefix in itertools.product(fields, ("", "-")):
Expand Down

0 comments on commit 6aedd2b

Please sign in to comment.