Skip to content

Commit

Permalink
source-gladly: use AwareDatetime for timestamp fields
Browse files Browse the repository at this point in the history
Type the `timestamp` field of events as an AwareDatetime, so they are validated and formatted as
`date-time`.
  • Loading branch information
williamhbaker committed Mar 5, 2024
1 parent 3e7fe2f commit 3be121a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
8 changes: 3 additions & 5 deletions source-gladly/source_gladly/api.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from datetime import datetime
from logging import Logger
from typing import Any, AsyncGenerator
from typing import AsyncGenerator

from estuary_cdk.http import HTTPSession
from estuary_cdk.capture.common import LogCursor
from pydantic import AwareDatetime


from .models import Event
Expand All @@ -28,9 +27,8 @@ async def fetch_events(
max_ts = log_cursor
async for line in http.request_lines(log, url, params=params):
event = Event.model_validate_json(line)
ts = datetime.fromisoformat(event.timestamp)
if ts > max_ts:
max_ts = ts
if event.timestamp > max_ts:
max_ts = event.timestamp
event.meta_ = Event.Meta(op="c")
yield event

Expand Down
4 changes: 2 additions & 2 deletions source-gladly/source_gladly/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from enum import StrEnum
from pydantic import BaseModel, Field
from pydantic import AwareDatetime, BaseModel, Field
from typing import TYPE_CHECKING

from estuary_cdk.capture.common import (
Expand Down Expand Up @@ -57,6 +57,6 @@ class Context(BaseDocument, extra="allow"):

id: str
type: str
timestamp: str
timestamp: AwareDatetime
initiator: Initiator
content: Content
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
"type": "string"
},
"timestamp": {
"format": "date-time",
"title": "Timestamp",
"type": "string"
},
Expand Down Expand Up @@ -288,6 +289,7 @@
"type": "string"
},
"timestamp": {
"format": "date-time",
"title": "Timestamp",
"type": "string"
},
Expand Down Expand Up @@ -445,6 +447,7 @@
"type": "string"
},
"timestamp": {
"format": "date-time",
"title": "Timestamp",
"type": "string"
},
Expand Down Expand Up @@ -602,6 +605,7 @@
"type": "string"
},
"timestamp": {
"format": "date-time",
"title": "Timestamp",
"type": "string"
},
Expand Down Expand Up @@ -759,6 +763,7 @@
"type": "string"
},
"timestamp": {
"format": "date-time",
"title": "Timestamp",
"type": "string"
},
Expand Down Expand Up @@ -916,6 +921,7 @@
"type": "string"
},
"timestamp": {
"format": "date-time",
"title": "Timestamp",
"type": "string"
},
Expand Down Expand Up @@ -1073,6 +1079,7 @@
"type": "string"
},
"timestamp": {
"format": "date-time",
"title": "Timestamp",
"type": "string"
},
Expand Down

0 comments on commit 3be121a

Please sign in to comment.