Skip to content

Commit

Permalink
set end timestamp if not set before record (#179)
Browse files Browse the repository at this point in the history
* send end timestamp if not set before record

* tests

* token cost print fix

* test fix
  • Loading branch information
bboynton97 authored May 3, 2024
1 parent 139488a commit 2a0ee73
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
10 changes: 6 additions & 4 deletions agentops/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def __init__(self,

self._session = None
self._worker = None
self._tags_for_future_session = None

self._env_data_opt_out = os.getenv('AGENTOPS_ENV_DATA_OPT_OUT') and os.getenv('AGENTOPS_ENV_DATA_OPT_OUT').lower() == 'true'

Expand Down Expand Up @@ -135,7 +136,8 @@ def record(self, event: Event | ErrorEvent):
Args:
event (Event): The event to record.
"""

if not event.end_timestamp or event.init_timestamp == event.end_timestamp:
event.end_timestamp = get_ISO_time()
if self._session is not None and not self._session.has_ended:
if isinstance(event, ErrorEvent):
if event.trigger_event:
Expand Down Expand Up @@ -270,13 +272,13 @@ def end_session(self,

self._session.video = video
self._session.end_session(end_state, end_state_reason)
token_cost = Decimal(self._worker.end_session(self._session))
token_cost = self._worker.end_session(self._session)
if token_cost == 'unknown':
print('🖇 AgentOps: Could not determine cost of run.')
else:

token_cost_d = Decimal(token_cost)
print('🖇 AgentOps: This run cost ${}'.format('{:.2f}'.format(
token_cost) if token_cost == 0 else '{:.6f}'.format(token_cost)))
token_cost_d) if token_cost_d == 0 else '{:.6f}'.format(token_cost_d)))
self._session = None
self._worker = None

Expand Down
29 changes: 29 additions & 0 deletions tests/test_events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import time
import requests_mock
import pytest
import agentops
from agentops import ActionEvent


@pytest.fixture
def mock_req():
with requests_mock.Mocker() as m:
url = 'https://api.agentops.ai'
m.post(url + '/events', text='ok')
m.post(url + '/sessions', json={'status': 'success', 'token_cost': 5})
yield m

class TestEvents:
def setup_method(self):
self.api_key = "random_api_key"
self.event_type = 'test_event_type'
self.config = agentops.Configuration(api_key=self.api_key, max_wait_time=50, max_queue_size=1)

def test_record_timestamp(self, mock_req):
agentops.init(api_key=self.api_key)

event = ActionEvent()
time.sleep(0.15)
agentops.record(event)

assert event.init_timestamp != event.end_timestamp

0 comments on commit 2a0ee73

Please sign in to comment.