use heartbeat name instead of action

This commit is contained in:
Alan Hamlett 2015-05-06 12:25:52 -07:00
parent b51ae5c2c4
commit 4bcddf2a98

View File

@ -25,13 +25,13 @@ from subprocess import Popen
# globals # globals
ACTION_FREQUENCY = 2 HEARTBEAT_FREQUENCY = 2
ST_VERSION = int(sublime.version()) ST_VERSION = int(sublime.version())
PLUGIN_DIR = os.path.dirname(os.path.realpath(__file__)) PLUGIN_DIR = os.path.dirname(os.path.realpath(__file__))
API_CLIENT = os.path.join(PLUGIN_DIR, 'packages', 'wakatime', 'cli.py') API_CLIENT = os.path.join(PLUGIN_DIR, 'packages', 'wakatime', 'cli.py')
SETTINGS_FILE = 'WakaTime.sublime-settings' SETTINGS_FILE = 'WakaTime.sublime-settings'
SETTINGS = {} SETTINGS = {}
LAST_ACTION = { LAST_HEARTBEAT = {
'time': 0, 'time': 0,
'file': None, 'file': None,
'is_write': False, 'is_write': False,
@ -137,7 +137,7 @@ def obfuscate_apikey(command_list):
def enough_time_passed(now, last_time, is_write): def enough_time_passed(now, last_time, is_write):
if now - last_time > ACTION_FREQUENCY * 60: if now - last_time > HEARTBEAT_FREQUENCY * 60:
return True return True
if is_write and now - last_time > 2: if is_write and now - last_time > 2:
return True return True
@ -173,17 +173,17 @@ def find_project_from_folders(folders, current_file):
return os.path.basename(folder) if folder else None return os.path.basename(folder) if folder else None
def handle_action(view, is_write=False): def handle_heartbeat(view, is_write=False):
window = view.window() window = view.window()
if window is not None: if window is not None:
target_file = view.file_name() target_file = view.file_name()
project = window.project_data() if hasattr(window, 'project_data') else None project = window.project_data() if hasattr(window, 'project_data') else None
folders = window.folders() folders = window.folders()
thread = SendActionThread(target_file, view, is_write=is_write, project=project, folders=folders) thread = SendHeartbeatThread(target_file, view, is_write=is_write, project=project, folders=folders)
thread.start() thread.start()
class SendActionThread(threading.Thread): class SendHeartbeatThread(threading.Thread):
def __init__(self, target_file, view, is_write=False, project=None, folders=None, force=False): def __init__(self, target_file, view, is_write=False, project=None, folders=None, force=False):
threading.Thread.__init__(self) threading.Thread.__init__(self)
@ -196,14 +196,14 @@ class SendActionThread(threading.Thread):
self.debug = SETTINGS.get('debug') self.debug = SETTINGS.get('debug')
self.api_key = SETTINGS.get('api_key', '') self.api_key = SETTINGS.get('api_key', '')
self.ignore = SETTINGS.get('ignore', []) self.ignore = SETTINGS.get('ignore', [])
self.last_action = LAST_ACTION.copy() self.last_heartbeat = LAST_HEARTBEAT.copy()
self.view = view self.view = view
def run(self): def run(self):
with self.lock: with self.lock:
if self.target_file: if self.target_file:
self.timestamp = time.time() self.timestamp = time.time()
if self.force or self.target_file != self.last_action['file'] or enough_time_passed(self.timestamp, self.last_action['time'], self.is_write): if self.force or self.target_file != self.last_heartbeat['file'] or enough_time_passed(self.timestamp, self.last_heartbeat['time'], self.is_write):
self.send_heartbeat() self.send_heartbeat()
def send_heartbeat(self): def send_heartbeat(self):
@ -245,15 +245,15 @@ class SendActionThread(threading.Thread):
def sent(self): def sent(self):
sublime.set_timeout(self.set_status_bar, 0) sublime.set_timeout(self.set_status_bar, 0)
sublime.set_timeout(self.set_last_action, 0) sublime.set_timeout(self.set_last_heartbeat, 0)
def set_status_bar(self): def set_status_bar(self):
if SETTINGS.get('status_bar_message'): if SETTINGS.get('status_bar_message'):
self.view.set_status('wakatime', 'WakaTime active {0}'.format(datetime.now().strftime('%I:%M %p'))) self.view.set_status('wakatime', 'WakaTime active {0}'.format(datetime.now().strftime('%I:%M %p')))
def set_last_action(self): def set_last_heartbeat(self):
global LAST_ACTION global LAST_HEARTBEAT
LAST_ACTION = { LAST_HEARTBEAT = {
'file': self.target_file, 'file': self.target_file,
'time': self.timestamp, 'time': self.timestamp,
'is_write': self.is_write, 'is_write': self.is_write,
@ -285,13 +285,13 @@ if ST_VERSION < 3000:
class WakatimeListener(sublime_plugin.EventListener): class WakatimeListener(sublime_plugin.EventListener):
def on_post_save(self, view): def on_post_save(self, view):
handle_action(view, is_write=True) handle_heartbeat(view, is_write=True)
def on_selection_modified(self, view): def on_selection_modified(self, view):
handle_action(view) handle_heartbeat(view)
def on_modified(self, view): def on_modified(self, view):
handle_action(view) handle_heartbeat(view)
class WakatimeDashboardCommand(sublime_plugin.ApplicationCommand): class WakatimeDashboardCommand(sublime_plugin.ApplicationCommand):