-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(statistics): Merge import statistics into one method
Fixes #273 by removing 24h batch imports in favor of one call to /bewegungsdaten Adds convenient script for validating missed data for x days scenario
- Loading branch information
1 parent
618e163
commit 92d4eb2
Showing
2 changed files
with
79 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import sqlite3 | ||
|
||
|
||
def purge(database:str, days: int, sensor_id: str): | ||
conn = sqlite3.connect(database) | ||
cursor = conn.cursor() | ||
|
||
query = f""" | ||
DELETE FROM statistics | ||
WHERE metadata_id IN ( | ||
SELECT id FROM statistics_meta WHERE statistic_id = '{sensor_id}' | ||
) | ||
AND start_ts >= strftime('%s', 'now', '-{days} days'); | ||
""" | ||
|
||
# Execute the query | ||
cursor.execute(query) | ||
|
||
# Commit the changes and close the connection | ||
conn.commit() | ||
conn.close() | ||
|
||
if __name__ == "__main__": | ||
import argparse | ||
parser = argparse.ArgumentParser(description='Purge the last x days of data from the statistics table') | ||
parser.add_argument('-db', '--database', type=str, help='Path to the SQLite database', default="home-assistant_v2.db") | ||
parser.add_argument('-d', '--days', type=int, help='Number of days to keep', default=2) | ||
parser.add_argument('-s', '--sensor', type=str, help='Name in the statistics_meta table', default="sensor.at00100000000000000010000XXXXXXX_statistics") | ||
args = parser.parse_args() | ||
purge(args.database, args.days, args.sensor) |