Skip to content
This repository has been archived by the owner on Aug 11, 2023. It is now read-only.

Commit

Permalink
Remove fractions from timestamp during response parsing to fix rare b…
Browse files Browse the repository at this point in the history
…ug caused by timestamps with no fractions
  • Loading branch information
wjh18 committed Jul 4, 2023
1 parent 3fe8d3b commit 9b1291e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/pyspeedinsights/api/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ def _get_timestamp(json_resp: dict) -> str:
A str in format year-month-day_hour.minute.second.
"""
timestamp = json_resp["analysisUTCTimestamp"]
dt_object = datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S.%fZ")
ts_no_fractions = timestamp.split(".")[0] # Remove fraction
if ts_no_fractions[-1] != "Z":
ts_no_fractions += "Z" # Add Z back after fraction removal
dt_object = datetime.strptime(ts_no_fractions, "%Y-%m-%dT%H:%M:%SZ")
date = dt_object.strftime("%Y-%m-%d_%H.%M.%S")
return date
12 changes: 9 additions & 3 deletions tests/api/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class TestProcessExcel:
def _get_json_resp(self, all_metrics, category="performance"):
return {
"analysisUTCTimestamp": "2023-02-26T17:36:18.266Z",
"analysisUTCTimestamp": "2023-02-26T17:36:18",
"lighthouseResult": {
"configSettings": {"formFactor": "desktop"},
"categories": {category: {"score": 1}},
Expand Down Expand Up @@ -88,7 +88,7 @@ def test_process_excel_non_performance_without_metrics(self, all_metrics):

def test_parse_metadata():
json_resp = {
"analysisUTCTimestamp": "2023-02-26T17:36:18.266Z",
"analysisUTCTimestamp": "2023-02-26T17:36:18Z",
"lighthouseResult": {
"configSettings": {"formFactor": "desktop"},
"categories": {"performance": {"score": 1}},
Expand Down Expand Up @@ -162,6 +162,12 @@ def test_get_audits_base():


def test_get_timestamp():
json_resp = {"analysisUTCTimestamp": "2023-02-26T17:36:18.266Z"}
json_resp = {"analysisUTCTimestamp": "2023-02-26T17:36:18Z"}
timestamp = _get_timestamp(json_resp)
assert timestamp == "2023-02-26_17.36.18"


def test_get_timestamp_fraction_removed():
json_resp = {"analysisUTCTimestamp": "2023-02-26T17:36:18.123Z"}
timestamp = _get_timestamp(json_resp)
assert timestamp == "2023-02-26_17.36.18"

0 comments on commit 9b1291e

Please sign in to comment.