Skip to content

Commit

Permalink
Merge pull request #543 from DataRecce/feature/drc-954-add-telemetry-…
Browse files Browse the repository at this point in the history
…for-renamesave_as

[Chore] Add rename and saveas telemetry event
  • Loading branch information
popcornylu authored Dec 17, 2024
2 parents f0fadbc + b16225a commit 8223cd7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
17 changes: 17 additions & 0 deletions recce/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,23 @@ def verify_required_artifacts(**kwargs) -> Tuple[bool, Optional[str]]:
def support_tasks(self):
return self.adapter.support_tasks()

def state_loader_mode(self):
"""
The state loader mode is used for telemetry purpose.
"""
if os.environ.get('DEMO', False):
return 'demo'

if not self.state_loader:
return 'none'

if self.state_loader.cloud_mode:
return 'cloud'
elif self.state_loader.state_file:
return 'file'
else:
return 'none'


recce_context: Optional[RecceContext] = None

Expand Down
14 changes: 12 additions & 2 deletions recce/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from .apis.run_api import run_router
from .config import RecceConfig
from .core import load_context, default_context
from .event import log_api_event
from .exceptions import RecceException
from .run import load_preset_checks
from .state import RecceStateLoader
Expand Down Expand Up @@ -271,6 +272,7 @@ async def save_handler():
try:
# Sync the state file
context = default_context()
log_api_event('save', dict(state_loader_mode=context.state_loader_mode()))
state_loader = context.state_loader
if not state_loader.cloud_mode and state_loader.state_file is None:
raise RecceException('Not file mode or cloud mode')
Expand Down Expand Up @@ -327,7 +329,9 @@ async def save_as_handler(input: SaveAsOrRenameInput):
"""
Save the state to a new file
"""
context = default_context()
try:
log_api_event('saveas', dict(state_loader_mode=context.state_loader_mode()))
saveas_or_rename(input, rename=False)
except RecceException as e:
raise HTTPException(status_code=400, detail=e.message)
Expand All @@ -338,7 +342,9 @@ async def rename_handler(input: SaveAsOrRenameInput):
"""
Rename the state to a new file
"""
context = default_context()
try:
log_api_event('rename', dict(state_loader_mode=context.state_loader_mode()))
saveas_or_rename(input, rename=True)
except RecceException as e:
raise HTTPException(status_code=400, detail=e.message)
Expand All @@ -351,6 +357,7 @@ async def export_handler():
"""
context = default_context()
try:
log_api_event('export', dict(state_loader_mode=context.state_loader_mode()))
return context.export_state().to_json()
except RecceException as e:
raise HTTPException(status_code=400, detail=e.message)
Expand All @@ -366,9 +373,9 @@ async def import_handler(
Import the recce state from the client.
"""
from recce.state import RecceState

context = default_context()
try:
log_api_event('import', dict(state_loader_mode=context.state_loader_mode()))
content = await file.read()
state = RecceState.from_json(content)

Expand All @@ -378,7 +385,6 @@ async def import_handler(
return {"runs": 0, "checks": import_checks}

import_runs, import_checks = context.import_state(state)

return {"runs": import_runs, "checks": import_checks}
except ValidationError as e:
raise HTTPException(status_code=400, detail=str(e))
Expand All @@ -403,6 +409,10 @@ async def sync_handler(input: SyncStateInput, response: Response, background_tas
context = default_context()
state_loader = context.state_loader
method = input.method
log_api_event('sync', dict(
state_loader_mode=context.state_loader_mode(),
method=method,
))

if not method:
is_conflict = state_loader.check_conflict()
Expand Down

0 comments on commit 8223cd7

Please sign in to comment.