Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utc timezones to OmenBet timestamps #440

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions prediction_market_agent_tooling/markets/omen/data_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import typing as t
from datetime import datetime

import pytz
from pydantic import BaseModel
from web3 import Web3

Expand Down Expand Up @@ -218,7 +219,7 @@ def openingTimestamp(self) -> int:

@property
def opening_datetime(self) -> datetime:
return datetime.fromtimestamp(self.openingTimestamp)
return datetime.fromtimestamp(self.openingTimestamp, tz=pytz.UTC)

@property
def close_time(self) -> datetime:
Expand Down Expand Up @@ -400,7 +401,7 @@ class OmenBet(BaseModel):

@property
def creation_datetime(self) -> datetime:
return datetime.fromtimestamp(self.creationTimestamp)
return datetime.fromtimestamp(self.creationTimestamp, tz=pytz.UTC)

@property
def boolean_outcome(self) -> bool:
Expand Down
15 changes: 14 additions & 1 deletion prediction_market_agent_tooling/tools/langfuse_client_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from langfuse.client import TraceWithDetails
from pydantic import BaseModel

from prediction_market_agent_tooling.loggers import logger
from prediction_market_agent_tooling.markets.data_models import (
PlacedTrade,
ProbabilisticAnswer,
Expand Down Expand Up @@ -146,9 +147,21 @@ def get_trace_for_bet(
else:
# In-case there are multiple traces for the same market, get the closest
# trace to the bet
bet_timestamp = add_utc_timezone_validator(bet.created_time)
closest_trace_index = get_closest_datetime_from_list(
add_utc_timezone_validator(bet.created_time),
bet_timestamp,
Comment on lines +150 to +152
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

⚠️ Potential issue

candidate_trace.timestamp is not timezone-aware

The trace.timestamp is assigned directly without applying add_utc_timezone_validator, which may lead to timezone-related comparison errors.

  • File: prediction_market_agent_tooling/tools/langfuse_client_utils.py
    • Line: 150-152
🔗 Analysis chain

Ensure candidate_trace.timestamp is timezone-aware when comparing datetimes

While bet_timestamp is made timezone-aware using add_utc_timezone_validator, it's important to ensure that candidate_trace.timestamp is also timezone-aware. This prevents potential errors or incorrect time difference calculations when comparing the two datetime objects.

Run the following script to verify that candidate_trace.timestamp is timezone-aware:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that 'trace.timestamp' used in ProcessMarketTrace is timezone-aware.

# Expectation: All instances where 'ProcessMarketTrace' is instantiated should ensure 'timestamp' is timezone-aware.

# Search for 'timestamp=trace.timestamp' assignments
rg --type py 'timestamp=trace.timestamp' -A 5 -B 5

# Check if 'add_utc_timezone_validator' is applied to 'trace.timestamp'
rg --type py 'timestamp=add_utc_timezone_validator\(trace.timestamp\)' -A 5 -B 5

Length of output: 1054


Script:

#!/bin/bash
# Description: Check if 'add_utc_timezone_validator' is imported in 'langfuse_client_utils.py'

# Search for import statements of 'add_utc_timezone_validator'
rg --type py 'from .* import add_utc_timezone_validator'

# Description: Find all usages of 'trace.timestamp' to verify if it's validated

# Search for all instances where 'trace.timestamp' is used
rg --type py 'trace\.timestamp'

# Description: Inspect the 'ProcessMarketTrace' class or function for internal timestamp handling

# Search for the definition of 'ProcessMarketTrace' and inspect its handling of 'timestamp'
rg --type py 'class ProcessMarketTrace' -A 20

Length of output: 2657

[t.timestamp for t in traces_for_bet],
)

# Sanity check: Let's say the upper bound for time between
# `agent.process_market` being called and the bet being placed is 20
# minutes
candidate_trace = traces_for_bet[closest_trace_index]
if abs(candidate_trace.timestamp - bet_timestamp).total_seconds() > 1200:
logger.info(
f"Closest trace to bet has timestamp {candidate_trace.timestamp}, "
f"but bet was created at {bet_timestamp}. Not matching"
)
return None

Comment on lines +156 to +166
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Parameterize the time threshold for better maintainability

The hard-coded time threshold of 1200 seconds (20 minutes) can be made more maintainable by defining it as a constant or configurable parameter. This approach allows for easier adjustments in the future without modifying the core logic.

Apply this diff to refactor the time threshold:

+TIME_THRESHOLD_SECONDS = 1200  # Upper bound for time difference between agent processing and bet placement

 candidate_trace = traces_for_bet[closest_trace_index]
-if abs(candidate_trace.timestamp - bet_timestamp).total_seconds() > 1200:
+if abs(candidate_trace.timestamp - bet_timestamp).total_seconds() > TIME_THRESHOLD_SECONDS:
     logger.info(
         f"Closest trace to bet has timestamp {candidate_trace.timestamp}, "
         f"but bet was created at {bet_timestamp}. Not matching"
     )
     return None
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Sanity check: Let's say the upper bound for time between
# `agent.process_market` being called and the bet being placed is 20
# minutes
candidate_trace = traces_for_bet[closest_trace_index]
if abs(candidate_trace.timestamp - bet_timestamp).total_seconds() > 1200:
logger.info(
f"Closest trace to bet has timestamp {candidate_trace.timestamp}, "
f"but bet was created at {bet_timestamp}. Not matching"
)
return None
TIME_THRESHOLD_SECONDS = 1200 # Upper bound for time difference between agent processing and bet placement
# Sanity check: Let's say the upper bound for time between
# `agent.process_market` being called and the bet being placed is 20
# minutes
candidate_trace = traces_for_bet[closest_trace_index]
if abs(candidate_trace.timestamp - bet_timestamp).total_seconds() > TIME_THRESHOLD_SECONDS:
logger.info(
f"Closest trace to bet has timestamp {candidate_trace.timestamp}, "
f"but bet was created at {bet_timestamp}. Not matching"
)
return None

return traces_for_bet[closest_trace_index]
Loading