Browse Source

Add `run.sh` + fix small bugs

- Add a script to get the records and merge with existing ones.
- Start 2 minutes back to avoid merges accidentally including both
  a completed & uncompleted minute.
  - 1 minute could, with rounding errors, result in a duplicate, 2 could not.
- Add `MAX_HOURS * 60` minutes, rather than `MAX_HOURS` hours, to avoid rounding
  issues that seemed to cause some minutes to be skipped, especially on the
  first round.
- Increased `DAYS_BACK` to 14 to ensure it gets the maximum, and just in case
  there is some data returned from > 7 days ago that might be desired.
  - For some reason my export currently includes data from a 15-minute window
    from 7.5 days ago, so it's worth trying.
Jason Tarka 11 months ago
parent
commit
913a922c89
3 changed files with 67 additions and 4 deletions
  1. 20 1
      README.md
  2. 6 3
      main.py
  3. 41 0
      run.sh

+ 20 - 1
README.md

@@ -33,4 +33,23 @@ username & password:
 ```
 
 After the first time the script is run, the file will automatically
-be updated to use access tokens, rather than your password.
+be updated to use access tokens, rather than your password.
+
+## Usage
+
+To create a `usage.tsv` file, and keep it updated with new, non-duplicated,
+results, just run the script:
+
+```sh
+./run.sh
+```
+
+To run the code manually:
+
+```sh
+# Activate the environment:
+source env/bin/activate
+
+# Run the script & store the results:
+python3 main.py >> output.tsv
+```

+ 6 - 3
main.py

@@ -5,6 +5,9 @@
 # To change the timing, update the DAYS_BACK variable below.
 #
 # Reads credentials from `keys.json`
+#
+# To ensure data is not read for a minute that has not yet ended,
+# this will read up to 2 minutes ago.
 
 import pyemvue
 import json
@@ -14,8 +17,8 @@ from pyemvue import PyEmVue
 from pprint import pprint
 from datetime import datetime, timezone, timedelta
 
-NOW = datetime.now(timezone.utc)
-DAYS_BACK = 7 # Number of days in the past to get per-minute data for.
+NOW = datetime.now(timezone.utc) - timedelta(minutes=2)
+DAYS_BACK = 14 # Number of days in the past to get per-minute data for.
 MAX_HOURS = 12 # The maxinum number of hours that can be retrieved at once.
 
 # Get & display minutely data for all devices.
@@ -52,7 +55,7 @@ def channelUsageOverTime(vue: PyEmVue, c):
 		
 		# Jump to the next start time.
 		start_time = end_time
-		end_time += timedelta(hours=MAX_HOURS)
+		end_time += timedelta(minutes=(MAX_HOURS*60))
 
 if __name__ == '__main__':
 	with open('keys.json') as f:

+ 41 - 0
run.sh

@@ -0,0 +1,41 @@
+#!/bin/bash
+
+# Execute `main.py` to get the data, output it to a temp file, then merge the
+# results into an existing `usage.tsv`, removing duplicate rows.
+#
+# If `usage.tsv` doesn't exist, it will be created.
+
+set -e
+
+NEW=new.tsv
+USAGE=usage.tsv
+TEMP=temp.tsv
+
+echo "Activating venv"
+source ./env/bin/activate
+
+echo "Fetching new records into ${NEW}"
+python3 main.py > ${NEW}
+
+echo "Retrieved $(wc -l ${NEW}) records"
+
+if [[ ! -f ${USAGE} ]]; then
+	echo "${USAGE} does not exist. Renaming $NEW to $USAGE."
+	mv ${NEW} ${USAGE}
+else
+	echo "${USAGE} exists, with $(cat ${USAGE} | wc -l) lines."
+	echo "Sorting & adding new records from ${NEW}."
+	header="$(head -n1 ${USAGE})"
+	echo "${header}" > ${TEMP}
+
+	cat <(tail -n +2 ${USAGE}) <(echo) <(tail -n +2 ${NEW}) \
+		| sort | uniq \
+		>> ${TEMP}
+	
+	echo "$(wc -l ${TEMP})"
+	echo "Replacing ${USAGE} with contents of ${TEMP}"
+	rm ${USAGE}
+	mv ${TEMP} ${USAGE}
+fi
+
+rm ${NEW}