diff options
-rw-r--r-- | data/.gitignore | 1 | ||||
-rw-r--r-- | data/scripts/common.sh | 50 | ||||
-rwxr-xr-x | data/scripts/log-sync | 36 | ||||
-rw-r--r-- | data/shopno-os-log.desktop | 7 | ||||
-rw-r--r-- | debian/changelog | 163 | ||||
-rw-r--r-- | debian/control | 19 | ||||
-rw-r--r-- | debian/install | 5 | ||||
-rwxr-xr-x | debian/rules | 4 | ||||
-rw-r--r-- | debian/shopno-os-log-sync.user.service | 12 | ||||
-rw-r--r-- | debian/shopno-os-log-sync@.user.service | 6 | ||||
-rw-r--r-- | debian/shopno-os-log-sync@.user.timer | 9 | ||||
-rw-r--r-- | debian/source/format | 1 | ||||
-rwxr-xr-x | misc/export_to_2.0 | 11 | ||||
-rwxr-xr-x | src/Watcher/analysis.py | 7 | ||||
-rw-r--r--[-rwxr-xr-x] | src/Watcher/commands.py | 5 | ||||
-rw-r--r-- | src/Watcher/files.py | 11 | ||||
-rwxr-xr-x | src/Watcher/watch_log.py | 18 | ||||
-rwxr-xr-x | src/bin/watcher | 5 |
18 files changed, 347 insertions, 23 deletions
diff --git a/data/.gitignore b/data/.gitignore new file mode 100644 index 0000000..6804e7f --- /dev/null +++ b/data/.gitignore @@ -0,0 +1 @@ +!scripts/ diff --git a/data/scripts/common.sh b/data/scripts/common.sh new file mode 100644 index 0000000..0fbaa45 --- /dev/null +++ b/data/scripts/common.sh @@ -0,0 +1,50 @@ +#!/bin/bash + +SERVER_URL="https://frypan.jadupc.com" #"builder.jadupc.com" + +if [[ $DEBUG ]]; then + SERVER_URL="http://log-server.local" +fi + +function getiface +{ + printf '%s\n' "/sys/class/$1"*/ | head -n1 +} + +function getaddr +{ + if [[ -d $1 ]]; then + tr ':' '-' < "$1/address" + else + echo '00-00-00-00-00-00' + fi +} + +function communicate +{ + local endpoint type wlmac enmac year week day month + endpoint="$1" + type="$2" + wlmac="$3" + enmac="$4" + year="$5" + shift 5 + case "${type#*/}" in + weekly) + week="$1" + shift 1 + set -- -d "week=$week" "$@" + ;; + daily) + month="$1" + day="$2" + shift 2 + set -- -d "month=$month" -d "day=$day" "$@" + ;; + esac + + curl --disable -Ls \ + -d "type=${type%%/*}" -d "wlmac=$wlmac" \ + -d "enmac=$enmac" -d "year=$year" -d "user=$USER" "$@" \ + "$SERVER_URL$endpoint" +} diff --git a/data/scripts/log-sync b/data/scripts/log-sync new file mode 100755 index 0000000..7459a7b --- /dev/null +++ b/data/scripts/log-sync @@ -0,0 +1,36 @@ +#!/bin/bash +source "${BASH_ARGV0%/*}"/common.sh +RAW_DATA_PATH="${XDG_CACHE_HOME:-$HOME/.cache}/Watcher" + +enmac="$(getaddr "$(getiface "net/e")")" +wlmac="$(getaddr "$(getiface "net/w")")" + +get_logs() +{ + find "$RAW_DATA_PATH/$1" -type f -exec basename -a '{}' + \ + | grep -oE "$2" +} + +# shellcheck disable=SC2031,SC2030 +if [[ $1 == "weekly" ]]; then + watcher -ws 2> /dev/null >&2 + + get_logs Analysis '[0-9]+-[0-9]+' \ + | while IFS=- read -r week year; do + ! communicate "/whack/a/mole" user/weekly "$wlmac" "$enmac" "$year" "$week" --silent --fail || continue + + echo "Uploading log: Week $week, $year" >&2 + communicate "/puss/in/boots" user/weekly "$wlmac" "$enmac" "$year" "$week" \ + -# --data-urlencode "data@$RAW_DATA_PATH/Analysis/W$week-$year.csv" + done +else + get_logs daily_data '[0-9]{4}(-[0-9]{2}){2}' \ + | while IFS=- read -r year month day; do + ! [[ $(date --rfc-3339=date) == "$year-$month-$day" ]] || continue + ! communicate "/whack/a/mole" user/daily "$wlmac" "$enmac" "$year" "$month" "$day" --silent --fail || continue + + echo "Uploading log: $year-$month-$day" >&2 + communicate "/puss/in/boots" user/daily "$wlmac" "$enmac" "$year" "$month" "$day" \ + -# --data-urlencode "data@$RAW_DATA_PATH/daily_data/$year-$month-$day.csv" + done +fi diff --git a/data/shopno-os-log.desktop b/data/shopno-os-log.desktop new file mode 100644 index 0000000..15fafb3 --- /dev/null +++ b/data/shopno-os-log.desktop @@ -0,0 +1,7 @@ +[Desktop Entry] +Type=Application +Name=Usage Logger +Exec=watcher --start +NoDisplay=true +Comment=Log Application Usage +Categories=System diff --git a/debian/changelog b/debian/changelog new file mode 100644 index 0000000..d696c88 --- /dev/null +++ b/debian/changelog @@ -0,0 +1,163 @@ +shopno-os-log-sync (2.0.0+nmu1) shopno; urgency=medium + + * Non-maintainer upload. + * Use a dummy service to start timers + + -- Mubashshir <ahm@jadupc.com> Wed, 26 Apr 2023 16:48:47 +0600 + +shopno-os-log-sync (2.0.0) shopno; urgency=medium + + * Fix empty stat upload + * Add per-user stat sync support + + -- Mubashshir <ahm@jadupc.com> Fri, 07 Apr 2023 22:04:18 +0600 + +shopno-os-log-sync (1.99.3) shopno; urgency=medium + + * config: Update logger domain + + -- Mubashshir <ahm@jadupc.com> Wed, 05 Apr 2023 15:47:22 +0600 + +shopno-os-log-sync (1.99.2) shopno; urgency=medium + + * Implement Log uploader + + -- Mubashshir <ahm@jadupc.com> Thu, 23 Mar 2023 17:01:35 +0600 + +shopno-os-log-sync (1.99.1) shopno; urgency=medium + + * Add Autostart entry + + -- Mubashshir <ahm@jadupc.com> Wed, 22 Mar 2023 16:28:27 +0600 + +shopno-os-log-sync (1.99.0) shopno; urgency=medium + + * path: Use `xdg.BaseDirectory` to get cache path + + -- Mubashshir <ahm@jadupc.com> Wed, 22 Mar 2023 16:15:43 +0600 + +shopno-os-log-sync (1.0.1) shopno; urgency=medium + + [ Waishnav Deore ] + * Initial commit + + [ Waishnav ] + * initializing project with bare minimal features + * log_files(implementing new approach) | so some changes in report_creation + * updating executable file and added function in time_operation module + * sorting app usage on the basis of screen-time + * completed week overview feature + + [ Waishnav Deore ] + * Update README.md + * Update install.bash + + [ Waishnav ] + * fixing issues in install-systemd.bash + + [ Waishnav Deore ] + * Update README.md + + [ Parth ] + * Updated watcher + + [ Waishnav ] + * exception handling in watcher executable file + + [ Waishnav Deore ] + * Update README.md + + [ Waishnav ] + * (xprofile) | changing aproach to install/run watch_log script | adding install uninstall script + * fixing issue in install file + * fixing issue in watch_log | occurs is file doesn't exist + * sourcing .xprofile in install script + + [ Waishnav Deore ] + * Update install + + [ Waishnav ] + * resolving conflict in install file for those who still using systemd service + * issue resolved : watch_log stoped working after 24:00 + * issue resolved : watch_log stoped working after 24:00 + * changing approach to problem occured at 24:00 clock | added prompt in install script + * changing approach to problem occured at 24:00 clock + * error fixed in time_differnece + * sourcing .xprofile from .xinitrc at beginining of file + * removing conflict with previous version of watcher about sourcing xprofile from xinitrc + * improvised append_line function + + [ Ethan M. Lee ] + * switch to python3 in shebang + + [ Waishnav ] + * #Issue7 resolved time_difference error + * Ubuntu error solved of getting active window; + * updating log file algorithm | added AFK feature | changing dependency xdotool to ewmh + * updating install | updating bin according to new function names + * refactoring some function + * updating installation guide in README.md + * fixing typo | changing some time.sleep() values so that low CPU will consume + * aborting realtime-stats feature due to heavy RAM consumption so log-files get updated at event (when user changes window) + * Removing EWMH module as a dependancy (causing more RAM consumption over the time) + * fixing typo + * adding -s option to start watch_log function | active_window function updated to reduce error to get home-screen + * adding AFK condition if there is any video playback running in background (for pulseaudio) | changing value for more accuracy + * Adding option of custom date and custom week to get users usage + * added two commands -ds -y and -ws -pw + * removing error of 3 min timeout due to AFK | updating acitve_window_title function + * AFK closed_at variable error if date got change + + [ augustin64 ] + * Update get_windows.py + + [ Waishnav ] + * updated algorithm | update csv at every second + * removed outdated comment + * compensating error value changed + * changed function according to 2.0 analysis function + * export python script for watcher v1.2 version csv files + + [ Ujjwal R ] + * shebang line updated, readme updated + + [ Om Thakare ] + * Added CONTRIBUTING.md (#23) + + [ Waishnav ] + * cleaning documentation stuffs | making it pure dev-branch for v2.0 + * updating CONTRIBUTING.md adding website-info + + [ Vaishnav Deore ] + * Update Readme for hactoberfest + + [ Waishnav ] + * github-linguist update languages + * shifting website branch to its own repo + + [ Pawan Patil ] + * fix : v2.0 Issue to avoid replication of data as day changed + * fix: Change directory from 'raw_data' to 'daily_data' + + [ Waishnav ] + * making easy to installation link using curl + * changed some variable names + * Error in installtion + * Error in installtion solved + + [ Vaishnav Deore ] + * resolving conflict + + [ Waishnav ] + * unknown file added bymistaken + + [ AnilMandliya ] + * [./install] BugFix. The script was recursively calling itself + + -- Mubashshir <ahm@jadupc.com> Wed, 22 Mar 2023 16:06:19 +0600 + +shopno-os-log-sync (1.0.0) shopno; urgency=medium + + * Initial Release + + -- Mubashshir <ahm@jadupc.com> Wed, 15 Mar 2023 17:15:35 +0600 diff --git a/debian/control b/debian/control new file mode 100644 index 0000000..6b518c3 --- /dev/null +++ b/debian/control @@ -0,0 +1,19 @@ +Source: shopno-os-log-sync +Section: util +Priority: optional +Maintainer: Mubashshir <ahm@jadupc.com> +Build-Depends: debhelper-compat (= 13), dh-exec +Standards-Version: 4.5.0 +Vcs-Git: https://builder.jadupc.com/git/pkgs/shopno-os-log-sync +Vcs-Browser: https://builder.jadupc.com/git/pkgs/shopno-os-log-sync +Rules-Requires-Root: no + +Package: shopno-os-log-sync +Architecture: all +Depends: + python3, + python3-xdg, + xdotool, + xprintidle +Description: Shopno OS stats logger +# vim: ft=yaml:ts=1:et diff --git a/debian/install b/debian/install new file mode 100644 index 0000000..1e3ced0 --- /dev/null +++ b/debian/install @@ -0,0 +1,5 @@ +src/Watcher/ usr/share/ +src/bin/watcher usr/bin/ +data/shopno-os-log.desktop etc/xdg/autostart/ +data/scripts/common.sh usr/share/Watcher/ +data/scripts/log-sync usr/share/Watcher/ diff --git a/debian/rules b/debian/rules new file mode 100755 index 0000000..2d33f6a --- /dev/null +++ b/debian/rules @@ -0,0 +1,4 @@ +#!/usr/bin/make -f + +%: + dh $@ diff --git a/debian/shopno-os-log-sync.user.service b/debian/shopno-os-log-sync.user.service new file mode 100644 index 0000000..15a870f --- /dev/null +++ b/debian/shopno-os-log-sync.user.service @@ -0,0 +1,12 @@ +[Unit] +Description=Shopno OS Stats Sync +Requires=shopno-os-log-sync@daily.timer +Requires=shopno-os-log-sync@weekly.timer + +[Service] +Type=oneshot +ExecStart=/bin/true +RemainAfterExit=on + +[Install] +WantedBy=default.target diff --git a/debian/shopno-os-log-sync@.user.service b/debian/shopno-os-log-sync@.user.service new file mode 100644 index 0000000..928dd23 --- /dev/null +++ b/debian/shopno-os-log-sync@.user.service @@ -0,0 +1,6 @@ +[Unit] +Description=Sync %I logs + +[Service] +Type=oneshot +ExecStart=/usr/share/Watcher/log-sync %i diff --git a/debian/shopno-os-log-sync@.user.timer b/debian/shopno-os-log-sync@.user.timer new file mode 100644 index 0000000..ec7bbb3 --- /dev/null +++ b/debian/shopno-os-log-sync@.user.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Sync usage logs with server %i + +[Timer] +OnCalendar=%i +Persistent=true + +[Install] +WantedBy=timers.target diff --git a/debian/source/format b/debian/source/format new file mode 100644 index 0000000..9f67427 --- /dev/null +++ b/debian/source/format @@ -0,0 +1 @@ +3.0 (native)
\ No newline at end of file diff --git a/misc/export_to_2.0 b/misc/export_to_2.0 index ef30ffe..d6fe65c 100755 --- a/misc/export_to_2.0 +++ b/misc/export_to_2.0 @@ -5,12 +5,11 @@ sys.path.insert(0, "/usr/share/Watcher/") import csv from watch_log import get_date import datetime +import files import time_operations as to def extract_data(date): - user = os.getlogin() - path = "/home/" + user +"/.cache/Watcher/raw_data/" - filename = path + date + ".csv" + filename = files.raw_log(date) l = list() # l = list of (app_name, time spent on app on that session) d = dict() @@ -64,7 +63,7 @@ def final_report(window_opened, time_spent): def export_to_new(date): window_opened, time_spent = extract_data(date) sorted_report = final_report(window_opened, time_spent) - filename = "/home/"+os.getlogin()+"/.cache/Watcher/daily_data/"+date+".csv" + filename = files.daily_log(date) overwrite_Data = [] with open(filename, 'w') as csvfile: for x,y in sorted_report.items(): @@ -76,8 +75,6 @@ def export_to_new(date): del sorted_report del overwrite_Data -path = "/home/" + os.getlogin() +"/.cache/Watcher/raw_data/" +path = os.path.dirname(files.raw_log('empty')) for file in os.listdir(path): export_to_new(file[:-4]) - - diff --git a/src/Watcher/analysis.py b/src/Watcher/analysis.py index 0e917f8..d43b1b4 100755 --- a/src/Watcher/analysis.py +++ b/src/Watcher/analysis.py @@ -3,11 +3,11 @@ import csv from watch_log import get_date import datetime import time_operations as to +import files # creating dictionary to store time spend on each applicaitons on that particular day def final_report(date): - path = "/home/" + os.getlogin() +"/.cache/Watcher/daily_data/" - filename = path + date + ".csv" + filename = files.daily_log(date) report = dict() if os.path.isfile(filename): @@ -64,7 +64,7 @@ def weekday_from_date(date): def weekly_logs(week = str(os.popen('''date +"W%V-%Y"''').read()[0:-1])): user = os.getlogin() - filename = "/home/"+user+"/.cache/Watcher/Analysis/"+week+".csv" + filename = files.weekly_log(week) with open(filename, "w") as csvfile: csvwriter = csv.writer(csvfile, delimiter='\t') @@ -95,4 +95,3 @@ def weekly_logs(week = str(os.popen('''date +"W%V-%Y"''').read()[0:-1])): #testing if __name__ == "__main__": weekly_logs("W29-2022") - diff --git a/src/Watcher/commands.py b/src/Watcher/commands.py index 8805dac..7e1b948 100755..100644 --- a/src/Watcher/commands.py +++ b/src/Watcher/commands.py @@ -1,9 +1,11 @@ import os import csv +import uuid import datetime from watch_log import get_date import analysis as anls import time_operations as to +import files class Color: @@ -71,8 +73,7 @@ def daily_summary(date = get_date()): print(" " + Color.GREEN(f'{x:<22}') + '\t ',f'{to.format_time(y):>12}') def week_summary(week = os.popen('''date +"W%V-%Y"''').read()[:-1]): - user = os.getlogin() - filename = "/home/"+user+"/.cache/Watcher/Analysis/"+week+".csv" + filename = files.weekly_log(week) with open(filename, 'r') as file: csvreader = csv.reader(file, delimiter='\t') week_overview = dict() diff --git a/src/Watcher/files.py b/src/Watcher/files.py new file mode 100644 index 0000000..1f4c9e3 --- /dev/null +++ b/src/Watcher/files.py @@ -0,0 +1,11 @@ +from xdg.BaseDirectory import save_cache_path +from os import getlogin, path +def weekly_log(week): + return path.join(save_cache_path('Watcher/Analysis'), '%s.csv' % week) + +def daily_log(date): + return path.join(save_cache_path('Watcher/daily_data'), '%s.csv' % date) + +def raw_log(date): + return path.join(save_cache_path('Watcher/raw_data'), '%s.csv' % date) +user = getlogin() diff --git a/src/Watcher/watch_log.py b/src/Watcher/watch_log.py index 6a44ca5..6119e45 100755 --- a/src/Watcher/watch_log.py +++ b/src/Watcher/watch_log.py @@ -1,6 +1,7 @@ import os import csv import time +import files import get_windows as x import afk as y from time_operations import time_difference, time_addition, convert @@ -16,7 +17,7 @@ def get_date(): return d[0:-1] def update_csv(date, Data): - filename = "/home/"+os.getlogin()+"/.cache/Watcher/daily_data/"+date+".csv" + filename = files.daily_log(date) overwrite_Data = [] with open(filename, 'w') as csvfile: for x,y in Data.items(): @@ -43,9 +44,9 @@ def import_data(file): # TODO: AFK feature devlopement (it will be developed after completing alpha product (after whole project up end running) def log_creation(): - filename = "/home/"+os.getlogin()+"/.cache/Watcher/daily_data/"+get_date()+".csv" + filename = files.daily_log(get_date()) if not(os.path.isfile(filename)): - creat_file = "/home/"+os.getlogin()+"/.cache/Watcher/daily_data/"+get_date()+".csv" + creat_file = filename with open(creat_file, 'w') as fp: pass @@ -54,7 +55,7 @@ def log_creation(): data = import_data(filename) while True: date = get_date() - filename = "/home/"+os.getlogin()+"/.cache/Watcher/daily_data/"+date+".csv" + filename = files.daily_log(date) afk = y.is_afk(afkTimeout) print(data) @@ -71,18 +72,17 @@ def log_creation(): usage = time_addition("00:00:01", usage) data.update({active_window : usage}) - if os.path.isfile("/home/"+os.getlogin()+"/.cache/Watcher/daily_data/"+get_date()+".csv"): + if os.path.isfile(files.daily_log(get_date())): update_csv(get_date(), data) - elif not(os.path.isfile("/home/"+os.getlogin()+"/.cache/Watcher/daily_data/"+get_date()+".csv")): - new_filename = "/home/"+os.getlogin()+"/.cache/Watcher/daily_data/"+get_date()+".csv" + elif not(os.path.isfile(files.daily_log(get_date()))): + new_filename = files.daily_log(get_date()) with open(new_filename, 'w') as fp: pass data.clear() - + if __name__ == "__main__": log_creation() #afk_time = int(round(int(os.popen("xprintidle").read()[:-1])/1000, 0)) #print(afk_time) - diff --git a/src/bin/watcher b/src/bin/watcher index 0dde744..ff91167 100755 --- a/src/bin/watcher +++ b/src/bin/watcher @@ -44,7 +44,10 @@ if len(arg) == 2: help_option() elif arg[1] == "--start" or arg[1] == "-s": print("Log creations started... \nif you wanna stop it, use keyboard-shortcut (Ctrl+Shift+C or Ctrl+C)") - x.log_creation() + try: + x.log_creation() + except KeyboardInterrupt: + exit(0) elif arg[1] == "--version" or arg[1] == "-v": print("Version: 2.0.0") else: |