2021-08-28 21:03:12 +00:00
|
|
|
import sqlite3
|
2021-12-03 15:22:54 +00:00
|
|
|
from datetime import datetime
|
|
|
|
from habitmove.loopdata import Habit, Repetition
|
2021-08-28 21:03:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
def migrate(db, habitlist, events):
|
|
|
|
c = db.cursor()
|
2021-08-31 16:05:59 +00:00
|
|
|
habits = habit_list_add_ids(c, habitlist)
|
2021-08-30 21:38:49 +00:00
|
|
|
repetitions = get_all_repetitions(habits, events)
|
2021-08-30 08:39:07 +00:00
|
|
|
for rep in repetitions:
|
2021-12-03 15:22:54 +00:00
|
|
|
add_to_database(c, habits, rep)
|
2021-08-30 21:00:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
LOOP_RANGE_VALUE_MULTIPLIER = 1000
|
2021-08-30 08:39:07 +00:00
|
|
|
|
|
|
|
|
2021-08-30 21:38:49 +00:00
|
|
|
def get_all_repetitions(habits, events):
|
2021-08-30 08:39:07 +00:00
|
|
|
"""Return list of all repetitions found of habits in events passed in.
|
|
|
|
Parameters:
|
|
|
|
habits (list): Collection of habits, with minimum necessary fields description and id.
|
|
|
|
events (list): Collection of events, with minimum necessary field end.
|
|
|
|
Returns:
|
|
|
|
repetitions (list): Collection of events transformed into Loop repetitions.
|
|
|
|
Contains fields id, timestamp, value (for ranges).
|
|
|
|
"""
|
|
|
|
repetitions = []
|
2021-08-28 21:03:12 +00:00
|
|
|
for event in events:
|
2021-12-03 15:22:54 +00:00
|
|
|
for activity in event.activities:
|
|
|
|
for habit in habits.values():
|
|
|
|
# TODO Fix reaching a layer too far into activity -> tracker
|
|
|
|
if habit.uuid == activity.tracker.id:
|
|
|
|
rep = Repetition(
|
|
|
|
habit_uuid=habit.uuid, timestamp=event.end, value=2
|
|
|
|
)
|
|
|
|
if habit.type == 1 and activity.value:
|
|
|
|
rep.value = activity.value * LOOP_RANGE_VALUE_MULTIPLIER
|
|
|
|
repetitions.append(rep)
|
2021-08-30 21:38:49 +00:00
|
|
|
|
2021-08-30 08:39:07 +00:00
|
|
|
return repetitions
|
2021-08-29 09:11:58 +00:00
|
|
|
|
|
|
|
|
2021-08-30 21:00:28 +00:00
|
|
|
# TODO possibly just get rid of this entirely
|
2021-08-31 16:05:59 +00:00
|
|
|
def habit_list_add_ids(c, habitlist):
|
|
|
|
"""Return the collection of habits with their sqlite id added.
|
2021-08-30 08:39:07 +00:00
|
|
|
Parameters:
|
|
|
|
c (sqlite.db.cursor): SQL cursor of database to query.
|
2021-12-02 22:08:58 +00:00
|
|
|
habitlist (list[Habit]): Full habit collection to return a simplified view of.
|
2021-08-30 08:39:07 +00:00
|
|
|
Returns:
|
2021-12-02 22:08:58 +00:00
|
|
|
habit_id_dict (dict[Habit]): The habit collection as a dict with the keys
|
|
|
|
consisting of the habit's sqlite database ID.
|
2021-08-30 08:39:07 +00:00
|
|
|
"""
|
2021-12-02 22:08:58 +00:00
|
|
|
with_id = {}
|
|
|
|
for h in habitlist:
|
|
|
|
sql_id = fetch_habit_id(c, h.uuid)
|
|
|
|
with_id[sql_id] = h
|
|
|
|
|
2021-08-31 16:05:59 +00:00
|
|
|
return with_id
|
2021-08-29 08:13:28 +00:00
|
|
|
|
|
|
|
|
2021-12-03 15:22:54 +00:00
|
|
|
def fetch_habit_id(cursor: sqlite3.Cursor, uuid: str):
|
2021-08-30 08:39:07 +00:00
|
|
|
"""Return sqlite internal id for habit with uuid.
|
|
|
|
Parameters:
|
2021-12-03 15:22:54 +00:00
|
|
|
:param c: SQL cursor of database to query.
|
|
|
|
:param uuid: Unique id of habit to query for.
|
2021-08-30 08:39:07 +00:00
|
|
|
Returns:
|
2021-12-03 15:22:54 +00:00
|
|
|
:return id: SQLite internal id for habit queried for.
|
2021-08-30 08:39:07 +00:00
|
|
|
"""
|
2021-12-03 15:22:54 +00:00
|
|
|
cursor.execute("select id from Habits where uuid = ?", ([uuid]))
|
|
|
|
id = cursor.fetchone()
|
2021-08-29 09:11:58 +00:00
|
|
|
if id is not None:
|
|
|
|
return id[0]
|
2021-08-28 21:03:12 +00:00
|
|
|
|
|
|
|
|
2021-12-03 15:22:54 +00:00
|
|
|
def add_to_database(
|
|
|
|
cursor: sqlite3.Cursor, habits: dict[int, Habit], repetition: Repetition
|
|
|
|
):
|
|
|
|
"""Insert the repetition into a sqlite3 table suitable for Loop.
|
|
|
|
Parameters:
|
|
|
|
:param c: SQL cursor of database to query.
|
|
|
|
:sql_id: Internal sqlite database id of the habit the repetition belongs to.
|
|
|
|
"""
|
|
|
|
for sql_id, habit in habits.items():
|
|
|
|
if repetition.habit_uuid == habit.uuid:
|
|
|
|
try:
|
|
|
|
cursor.execute(
|
|
|
|
"""
|
|
|
|
INSERT INTO
|
|
|
|
Repetitions(id, habit, timestamp, value)
|
|
|
|
VALUES (NULL, ?, ?, ?)
|
|
|
|
""",
|
|
|
|
(sql_id, repetition.timestamp, repetition.value),
|
|
|
|
)
|
|
|
|
except sqlite3.IntegrityError:
|
|
|
|
# TODO better error handling
|
|
|
|
print(
|
|
|
|
f"{sql_id}, {habit.name}: timestamp {datetime.fromtimestamp(repetition.timestamp/1000)} not unique, moving timestamp slightly."
|
|
|
|
)
|