Add screen time to health stats

This commit is contained in:
Gobinath 2019-02-24 11:53:20 -05:00
parent 40aacdf642
commit b08fd0e7a8
2 changed files with 31 additions and 4 deletions

View File

@ -20,6 +20,7 @@
Show health statistics on the break screen.
"""
import datetime
import logging
context = None
@ -27,6 +28,8 @@ no_of_skipped_breaks = 0
no_of_breaks = 0
no_of_cycles = -1
session = None
safe_eyes_start_time = datetime.datetime.now()
total_idle_time = 0
def init(ctx, safeeyes_config, plugin_config):
@ -43,7 +46,8 @@ def init(ctx, safeeyes_config, plugin_config):
if session is None:
session = context['session']['plugin'].get('healthstats', None)
if session is None:
session = {'no_of_skipped_breaks': 0, 'no_of_breaks': 0, 'no_of_cycles': -1}
session = {'no_of_skipped_breaks': 0,
'no_of_breaks': 0, 'no_of_cycles': -1}
context['session']['plugin']['healthstats'] = session
no_of_skipped_breaks = session.get('no_of_skipped_breaks', 0)
no_of_breaks = session.get('no_of_breaks', 0)
@ -78,4 +82,22 @@ def get_widget_content(break_obj):
"""
Return the statistics.
"""
return 'BREAKS: {}\tSKIPPED: {}\tCYCLES: {}'.format(no_of_breaks, no_of_skipped_breaks, no_of_cycles)
screen_time = round(((datetime.datetime.now() - safe_eyes_start_time).total_seconds() - total_idle_time) / 60)
hours, minutes = divmod(screen_time, 60)
time_format = '{:02d}:{:02d}'.format(hours, minutes)
if hours > 6 or round((no_of_skipped_breaks / no_of_breaks), 1) >= 0.2:
# Unhealthy behavior -> Red broken heart
heart = '💔️'
else:
# Healthy behavior -> Green heart
heart = '💚'
return "{}\tBREAKS: {}\tSKIPPED: {}\tCYCLES: {}\tSCREEN TIME: {}".format(heart, no_of_breaks, no_of_skipped_breaks, no_of_cycles, time_format)
def on_start():
"""
Add the idle period to the total idle time.
"""
global total_idle_time
# idle_period is provided by Smart Pause plugin
total_idle_time += context.get('idle_period', 0)

View File

@ -50,7 +50,8 @@ def __system_idle_time():
Return the idle time if xprintidle is available, otherwise return 0.
"""
try:
return int(subprocess.check_output(['xprintidle']).decode('utf-8')) / 1000 # Convert to seconds
# Convert to seconds
return int(subprocess.check_output(['xprintidle']).decode('utf-8')) / 1000
except BaseException:
return 0
@ -95,7 +96,8 @@ def init(ctx, safeeyes_config, plugin_config):
idle_time = plugin_config['idle_time']
interpret_idle_as_break = plugin_config['interpret_idle_as_break']
postpone_if_active = plugin_config['postpone_if_active']
break_interval = safeeyes_config.get('short_break_interval') * 60 # Convert to seconds
break_interval = safeeyes_config.get(
'short_break_interval') * 60 # Convert to seconds
waiting_time = min(2, idle_time) # If idle time is 1 sec, wait only 1 sec
@ -124,6 +126,7 @@ def __start_idle_monitor():
smart_pause_activated = False
idle_period = (datetime.datetime.now() - idle_start_time)
idle_seconds = idle_period.total_seconds()
context['idle_period'] = idle_seconds
if interpret_idle_as_break and idle_seconds >= next_break_duration:
# User is idle for break duration and wants to consider it as a break
enable_safe_eyes()
@ -134,6 +137,8 @@ def __start_idle_monitor():
else:
# User is idle for more than the time between two breaks
enable_safe_eyes()
# Reset the idle_period in case if Smart Pause is disabled by the user
context['idle_period'] = 0
def on_start():