Skip to content
This repository was archived by the owner on Dec 8, 2024. It is now read-only.

Commit

Permalink
feat: added period options for get_user_postures
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchellJC committed Aug 20, 2024
1 parent 6f421fa commit 8aa9167
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 13 deletions.
26 changes: 23 additions & 3 deletions client/data/routines.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,25 +123,45 @@ def get_postures(num: int = 10) -> list[Posture]:
return [Posture(*record) for record in result.fetchall()]


def get_user_postures(user_id: int, num: int = -1) -> list[Posture]:
def get_user_postures(
user_id: int,
num: int = -1,
period_start: Optional[datetime] = None,
period_end: Optional[datetime] = None,
) -> list[Posture]:
"""
Args:
user_id: Id of user to get postures for.
num: Number of posture records to retrieve. Set to -1 to retrieve all records.
period_start: Only posture records starting at or after this period will be retrieved.
Leave as None to set no restriction.
period_end: Only posture records ending at or before this period will be retrieved.
Leave as None to set no restriction.
Returns:
num posture records from the database for the specified user. Retrieves latest inserted
posture records first.
"""
params: tuple[int, ...] = (user_id,)
params: tuple[int | datetime, ...] = (user_id,)

# Add limit to query
limit = ""
if num != -1:
limit = " LIMIT ?"
params = (num,) + params

query = f"SELECT * FROM posture{limit} WHERE user_id=? ORDER BY id DESC"
# Add period to query
query_period = ""
if period_start is not None:
query_period += " AND DATETIME(period_start) >= DATETIME(?)"
params += (period_start,)
if period_end is not None:
query_period += " AND DATETIME(period_end) <= DATETIME(?)"
params += (period_end,)

query = (
f"SELECT * FROM posture{limit} WHERE user_id = ?{query_period} ORDER BY id DESC"
)

with _connect() as connection:
cursor = connection.cursor()
Expand Down
45 changes: 35 additions & 10 deletions notebooks/test-database-tools.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -32,7 +32,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -41,7 +41,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 9,
"metadata": {},
"outputs": [
{
Expand All @@ -57,7 +57,7 @@
" [(0, 'id', 'INTEGER', 0, None, 1)]]"
]
},
"execution_count": 5,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -88,7 +88,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 10,
"metadata": {},
"outputs": [
{
Expand All @@ -97,7 +97,7 @@
"[User(id_=1), User(id_=2), User(id_=3), User(id_=4), User(id_=5)]"
]
},
"execution_count": 7,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -121,7 +121,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 11,
"metadata": {},
"outputs": [
{
Expand All @@ -135,7 +135,7 @@
" Posture(id_=6, user_id=1, prop_good=0.8, prop_in_frame=1.0, period_start=datetime.datetime(2024, 8, 20, 11, 9, 13, 157727), period_end=datetime.datetime(2024, 8, 20, 11, 9, 14, 172205))]"
]
},
"execution_count": 9,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -146,7 +146,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 12,
"metadata": {},
"outputs": [
{
Expand All @@ -160,7 +160,7 @@
" Posture(id_=1, user_id=1, prop_good=0.8, prop_in_frame=1.0, period_start=datetime.datetime(2024, 8, 18, 17, 40, 44, 458720), period_end=datetime.datetime(2024, 8, 18, 17, 40, 45, 462898))]"
]
},
"execution_count": 13,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -169,6 +169,31 @@
"get_user_postures(1)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[Posture(id_=4, user_id=1, prop_good=0.8, prop_in_frame=1.0, period_start=datetime.datetime(2024, 8, 18, 17, 48, 16, 460303), period_end=datetime.datetime(2024, 8, 18, 17, 48, 17, 466763)),\n",
" Posture(id_=3, user_id=1, prop_good=0.8, prop_in_frame=1.0, period_start=datetime.datetime(2024, 8, 18, 17, 46, 2, 389947), period_end=datetime.datetime(2024, 8, 18, 17, 46, 3, 402225))]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_user_postures(\n",
" 1,\n",
" period_start=datetime(2024, 8, 18, 17, 46, 2, 389947),\n",
" period_end=datetime(2024, 8, 20, 11, 2, 10, 540731),\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down

0 comments on commit 8aa9167

Please sign in to comment.