Skip to content

Commit

Permalink
Merge branch 'main' into modify-install-script
Browse files Browse the repository at this point in the history
  • Loading branch information
abrichr authored Aug 29, 2023
2 parents d668901 + e5d4955 commit 705a2f0
Show file tree
Hide file tree
Showing 18 changed files with 154 additions and 49 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@



## v0.12.0 (2023-08-29)

### Feature

* feat: manage recording database (#451)

* implemented db module
* use click + loguru ([`5b25b90`](https://github.com/OpenAdaptAI/OpenAdapt/commit/5b25b90fc4b0a7221a062170c3cb38c520afc718))


## v0.11.0 (2023-08-29)

### Feature
Expand Down
4 changes: 2 additions & 2 deletions alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from alembic import context
from openadapt.config import DB_URL
from openadapt.db import Base
from openadapt.db import db

# This is the Alembic Config object, which provides
# access to the values within the .ini file in use.
Expand All @@ -24,7 +24,7 @@
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = Base.metadata
target_metadata = db.Base.metadata


def get_url() -> str:
Expand Down
1 change: 1 addition & 0 deletions openadapt/db/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Package for interacting with the OpenAdapt database."""
8 changes: 7 additions & 1 deletion openadapt/crud.py → openadapt/db/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import sqlalchemy as sa

from openadapt import config
from openadapt.db import BaseModel, Session
from openadapt.db.db import BaseModel, Session
from openadapt.models import (
ActionEvent,
MemoryStat,
Expand Down Expand Up @@ -196,6 +196,12 @@ def insert_recording(recording_data: Recording) -> Recording:
return db_obj


def delete_recording(recording_timestamp: int) -> None:
"""Remove the recording from the db."""
db.query(Recording).filter(Recording.timestamp == recording_timestamp).delete()
db.commit()


def get_all_recordings() -> list[Recording]:
"""Get all recordings.
Expand Down
File renamed without changes.
34 changes: 34 additions & 0 deletions openadapt/db/list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""Lists all recordings in the database.
Usage: python -m openadapt.db.list
"""

from sys import stdout

from loguru import logger

from openadapt.db.crud import get_all_recordings


def main() -> None:
"""Prints all recordings in the database."""
logger.remove()
logger.add(
stdout,
colorize=True,
format="<blue>[DB] </blue><yellow>{message}</yellow>",
)
print() # newline

if not get_all_recordings():
logger.info("No recordings found.")

for idx, recording in enumerate(get_all_recordings()[::-1], start=1):
logger.info(
f"[{idx}]: {recording.task_description} | {recording.timestamp}"
+ (" [latest]" if idx == len(get_all_recordings()) else "")
)


if __name__ == "__main__":
main()
69 changes: 69 additions & 0 deletions openadapt/db/remove.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""Removes recordings from the database.
Usage: python -m openadapt.db.remove [--all | --latest | --id <recording_id>]
"""
from sys import stdout

from loguru import logger
import click

from openadapt.db.crud import delete_recording, get_all_recordings

print() # newline


@click.command()
@click.option("--all", is_flag=True, help="Remove all recordings.")
@click.option("--latest", is_flag=True, help="Remove the latest recording.")
@click.option("--id", "recording_id", type=int, help="Remove recording by ID.")
def remove(all: str, latest: str, recording_id: int) -> int:
"""Removes a recording from the database."""
recordings = get_all_recordings()[::-1]

logger.remove()
logger.add(
stdout,
colorize=True,
format="<blue>[DB] </blue><yellow>{message}</yellow>",
)

if sum([all, latest, recording_id is not None]) > 1:
logger.error("Invalid usage.")
logger.error("Use --help for more information.")
return 1

if not recordings:
logger.error("No recordings found.")
return 1

if all:
if click.confirm("Are you sure you want to delete all recordings?"):
for r in recordings:
logger.info(f"Deleting {r.task_description} | {r.timestamp}...")
delete_recording(r.timestamp)
logger.info("All recordings deleted.")
else:
logger.info("Aborting...")
return 0

if latest:
recording_id = len(recordings)

elif recording_id is None or not (1 <= recording_id <= len(recordings)):
logger.error("Invalid recording ID.")
return 1
recording_to_delete = recordings[recording_id - 1]

if click.confirm(
"Are you sure you want to delete recording"
f" {recording_to_delete.task_description} | {recording_to_delete.timestamp}?"
):
delete_recording(recording_to_delete.timestamp)
logger.info("Recording deleted.")
else:
logger.info("Aborting...")
return 0


if __name__ == "__main__":
remove()
3 changes: 2 additions & 1 deletion openadapt/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from scipy.spatial import distance
import numpy as np

from openadapt import common, crud, models, utils
from openadapt import common, models, utils
from openadapt.db import crud

MAX_PROCESS_ITERS = 1
MOUSE_MOVE_EVENT_MERGE_DISTANCE_THRESHOLD = 1
Expand Down
3 changes: 2 additions & 1 deletion openadapt/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import numpy as np
import sqlalchemy as sa

from openadapt import config, db, window
from openadapt import config, window
from openadapt.db import db


# https://groups.google.com/g/sqlalchemy/c/wlr7sShU6-k
Expand Down
2 changes: 1 addition & 1 deletion openadapt/productivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from bokeh.models.widgets import Div
from loguru import logger

from openadapt.crud import get_latest_recording, get_window_events
from openadapt.db.crud import get_latest_recording, get_window_events
from openadapt.events import get_events
from openadapt.models import ActionEvent, WindowEvent
from openadapt.utils import (
Expand Down
3 changes: 2 additions & 1 deletion openadapt/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
import mss.tools
import psutil

from openadapt import config, crud, utils, window
from openadapt import config, utils, window
from openadapt.db import crud
from openadapt.extensions import synchronized_queue as sq
from openadapt.models import ActionEvent

Expand Down
3 changes: 2 additions & 1 deletion openadapt/replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
from loguru import logger
import fire

from openadapt import capture, crud, utils
from openadapt import capture, utils
from openadapt.db import crud
from openadapt.models import Recording

LOG_LEVEL = "INFO"
Expand Down
2 changes: 1 addition & 1 deletion openadapt/strategies/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from loguru import logger

from openadapt.crud import get_screenshots
from openadapt.db.crud import get_screenshots
from openadapt.models import Recording, Screenshot, WindowEvent
from openadapt.strategies.base import BaseReplayStrategy
from openadapt.strategies.mixins.ascii import ASCIIReplayStrategyMixin
Expand Down
6 changes: 3 additions & 3 deletions openadapt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import numpy as np

from openadapt import common, config
from openadapt.db import BaseModel
from openadapt.db import db
from openadapt.logging import filter_log_messages
from openadapt.models import ActionEvent

Expand Down Expand Up @@ -65,7 +65,7 @@ def configure_logging(logger: logger, log_level: str) -> None:
logger.debug(f"{log_level=}")


def row2dict(row: Union[dict, BaseModel], follow: bool = True) -> dict:
def row2dict(row: Union[dict, db.BaseModel], follow: bool = True) -> dict:
"""Convert a row object to a dictionary.
Args:
Expand Down Expand Up @@ -680,7 +680,7 @@ def plot_performance(
plt.style.use("dark_background")

# avoid circular import
from openadapt import crud
from openadapt.db import crud

if not recording_timestamp:
recording_timestamp = crud.get_latest_recording().timestamp
Expand Down
2 changes: 1 addition & 1 deletion openadapt/visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import click

from openadapt import config
from openadapt.crud import get_latest_recording, get_recording
from openadapt.db.crud import get_latest_recording, get_recording
from openadapt.events import get_events
from openadapt.utils import (
EMPTY,
Expand Down
Loading

0 comments on commit 705a2f0

Please sign in to comment.