Skip to content

Commit

Permalink
fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-tkommineni committed Jan 6, 2025
1 parent 6789138 commit 14a6bf9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/snowflake/cli/_plugins/spcs/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def format_event_row(event_dict: dict) -> dict:
"DATABASE NAME": database_name,
"SCHEMA NAME": schema_name,
"SERVICE NAME": service_name,
"INSTANCE NAME": instance_name,
"INSTANCE ID": instance_name,
"CONTAINER NAME": container_name,
"SEVERITY": severity,
"EVENT NAME": event_name,
Expand Down Expand Up @@ -232,7 +232,7 @@ def format_metric_row(metric_dict: dict) -> dict:
"DATABASE NAME": database_name,
"SCHEMA NAME": schema_name,
"SERVICE NAME": service_name,
"INSTANCE NAME": instance_name,
"INSTANCE ID": instance_name,
"CONTAINER NAME": container_name,
"METRIC NAME": metric_name,
"METRIC VALUE": metric_value,
Expand Down
16 changes: 13 additions & 3 deletions src/snowflake/cli/_plugins/spcs/services/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,12 @@ def events(
help="Fetch events that are older than this time ago, in Snowflake interval syntax.",
),
first: int = typer.Option(
default=-1,
default=None,
show_default=False,
help="Fetch only the first N events. Cannot be used with --last.",
),
last: int = typer.Option(
default=-1,
default=None,
show_default=False,
help="Fetch only the last N events. Cannot be used with --first.",
),
Expand All @@ -339,13 +339,16 @@ def events(
),
**options,
):
"""
Retrieve platform events for a service container.
"""
if FeatureFlag.ENABLE_SPCS_SERVICE_EVENTS.is_disabled():
raise FeatureNotEnabledError(
"ENABLE_SPCS_SERVICE_EVENTS",
"Service events collection from SPCS event table is disabled.",
)

if first >= 0 and last >= 0:
if first is not None and last is not None:
raise IncompatibleParametersError(["--first", "--last"])

manager = ServiceManager()
Expand All @@ -359,6 +362,10 @@ def events(
last=last,
show_all_columns=show_all_columns,
)

if not events:
return MessageResult("No events found.")

return CollectionResult(events)


Expand Down Expand Up @@ -393,6 +400,9 @@ def metrics(
),
**options,
):
"""
Retrieve platform metrics for a service container.
"""
if FeatureFlag.ENABLE_SPCS_SERVICE_METRICS.is_disabled():
raise FeatureNotEnabledError(
"ENABLE_SPCS_SERVICE_METRICS",
Expand Down
8 changes: 4 additions & 4 deletions src/snowflake/cli/_plugins/spcs/services/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ def get_events(
container_name: str,
since: str | datetime | None = None,
until: str | datetime | None = None,
first: int = -1,
last: int = -1,
first: Optional[int] = None,
last: Optional[int] = None,
show_all_columns: bool = False,
):

Expand All @@ -234,8 +234,8 @@ def get_events(
)
since_clause, until_clause = build_time_clauses(since, until)

first_clause = f"limit {first}" if first >= 0 else ""
last_clause = f"limit {last}" if last >= 0 else ""
first_clause = f"limit {first}" if first is not None else ""
last_clause = f"limit {last}" if last is not None else ""

query = f"""\
select *
Expand Down
16 changes: 13 additions & 3 deletions tests/spcs/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
import itertools
import json
import re
from datetime import datetime
from pathlib import Path
from textwrap import dedent
Expand Down Expand Up @@ -997,9 +998,13 @@ def test_latest_metrics(mock_execute_query, mock_is_disabled, runner):
" "
)

assert (
actual_query == expected_query
), f"Generated query does not match expected query.\n\nActual:\n{repr(actual_query)}\n\nExpected:\n{repr(expected_query)}"
actual_normalized = normalize_query(actual_query)
expected_normalized = normalize_query(expected_query)

assert actual_normalized == expected_normalized, (
f"Generated query does not match expected query.\n\n"
f"Actual:\n{actual_query}\n\nExpected:\n{expected_query}"
)


@patch(
Expand Down Expand Up @@ -1547,3 +1552,8 @@ def test_command_aliases(mock_connector, runner, mock_ctx, command, parameters):

queries = ctx.get_queries()
assert queries[0] == queries[1]


def normalize_query(query):
"""Normalize SQL query by stripping extra whitespace and formatting."""
return re.sub(r"\s+", " ", query.strip())

0 comments on commit 14a6bf9

Please sign in to comment.