2014-05-26 00:25:24 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
2015-05-12 22:02:09 +00:00
|
|
|
wakatime.offlinequeue
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~
|
2014-05-26 00:25:24 +00:00
|
|
|
|
2015-05-12 22:02:09 +00:00
|
|
|
Queue for saving heartbeats while offline.
|
2014-05-26 00:25:24 +00:00
|
|
|
|
|
|
|
:copyright: (c) 2014 Alan Hamlett.
|
|
|
|
:license: BSD, see LICENSE for more details.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import os
|
2014-05-26 21:53:54 +00:00
|
|
|
import traceback
|
2014-05-26 00:25:24 +00:00
|
|
|
from time import sleep
|
2014-12-04 19:43:20 +00:00
|
|
|
|
2014-06-09 20:23:03 +00:00
|
|
|
try:
|
|
|
|
import sqlite3
|
|
|
|
HAS_SQL = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_SQL = False
|
2014-05-26 00:25:24 +00:00
|
|
|
|
2015-08-25 07:51:01 +00:00
|
|
|
from .compat import u
|
|
|
|
|
2014-05-26 00:25:24 +00:00
|
|
|
|
2014-07-25 09:45:35 +00:00
|
|
|
log = logging.getLogger('WakaTime')
|
2014-05-26 00:25:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Queue(object):
|
|
|
|
DB_FILE = os.path.join(os.path.expanduser('~'), '.wakatime.db')
|
|
|
|
|
|
|
|
def connect(self):
|
|
|
|
conn = sqlite3.connect(self.DB_FILE)
|
|
|
|
c = conn.cursor()
|
2014-12-22 06:55:59 +00:00
|
|
|
c.execute('''CREATE TABLE IF NOT EXISTS heartbeat (
|
2014-05-26 00:25:24 +00:00
|
|
|
file text,
|
|
|
|
time real,
|
|
|
|
project text,
|
|
|
|
branch text,
|
|
|
|
is_write integer,
|
2014-12-22 06:55:59 +00:00
|
|
|
stats text,
|
|
|
|
misc text,
|
2014-05-26 00:25:24 +00:00
|
|
|
plugin text)
|
|
|
|
''')
|
|
|
|
return (conn, c)
|
|
|
|
|
|
|
|
|
2014-12-22 06:55:59 +00:00
|
|
|
def push(self, data, stats, plugin, misc=None):
|
2014-06-09 20:23:03 +00:00
|
|
|
if not HAS_SQL:
|
|
|
|
return
|
2014-05-26 21:53:54 +00:00
|
|
|
try:
|
|
|
|
conn, c = self.connect()
|
2014-12-22 06:55:59 +00:00
|
|
|
heartbeat = {
|
2015-08-25 07:51:01 +00:00
|
|
|
'file': u(data.get('entity')),
|
2014-05-26 21:53:54 +00:00
|
|
|
'time': data.get('time'),
|
2015-08-25 07:51:01 +00:00
|
|
|
'project': u(data.get('project')),
|
|
|
|
'branch': u(data.get('branch')),
|
2014-05-26 21:53:54 +00:00
|
|
|
'is_write': 1 if data.get('is_write') else 0,
|
2015-08-25 07:51:01 +00:00
|
|
|
'stats': u(stats),
|
|
|
|
'misc': u(misc),
|
|
|
|
'plugin': u(plugin),
|
2014-05-26 21:53:54 +00:00
|
|
|
}
|
2015-08-25 18:21:56 +00:00
|
|
|
c.execute('INSERT INTO heartbeat VALUES (:file,:time,:project,:branch,:is_write,:stats,:misc,:plugin)', heartbeat)
|
2014-05-26 21:53:54 +00:00
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
2014-05-27 05:43:35 +00:00
|
|
|
except sqlite3.Error:
|
|
|
|
log.error(traceback.format_exc())
|
2014-05-26 00:25:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
def pop(self):
|
2014-06-09 20:23:03 +00:00
|
|
|
if not HAS_SQL:
|
|
|
|
return None
|
2014-05-26 00:25:24 +00:00
|
|
|
tries = 3
|
|
|
|
wait = 0.1
|
2014-12-22 06:55:59 +00:00
|
|
|
heartbeat = None
|
2014-05-26 21:53:54 +00:00
|
|
|
try:
|
|
|
|
conn, c = self.connect()
|
2014-05-27 05:43:35 +00:00
|
|
|
except sqlite3.Error:
|
2014-05-26 21:53:54 +00:00
|
|
|
log.debug(traceback.format_exc())
|
|
|
|
return None
|
2014-05-26 00:25:24 +00:00
|
|
|
loop = True
|
|
|
|
while loop and tries > -1:
|
|
|
|
try:
|
|
|
|
c.execute('BEGIN IMMEDIATE')
|
2014-12-22 06:55:59 +00:00
|
|
|
c.execute('SELECT * FROM heartbeat LIMIT 1')
|
2014-05-26 00:25:24 +00:00
|
|
|
row = c.fetchone()
|
|
|
|
if row is not None:
|
2014-05-26 21:53:54 +00:00
|
|
|
values = []
|
|
|
|
clauses = []
|
|
|
|
index = 0
|
2014-12-22 06:55:59 +00:00
|
|
|
for row_name in ['file', 'time', 'project', 'branch', 'is_write']:
|
2014-05-26 21:53:54 +00:00
|
|
|
if row[index] is not None:
|
|
|
|
clauses.append('{0}=?'.format(row_name))
|
2015-08-25 18:21:56 +00:00
|
|
|
values.append(row[index])
|
2014-05-26 21:53:54 +00:00
|
|
|
else:
|
|
|
|
clauses.append('{0} IS NULL'.format(row_name))
|
|
|
|
index += 1
|
|
|
|
if len(values) > 0:
|
2015-08-25 18:21:56 +00:00
|
|
|
c.execute('DELETE FROM heartbeat WHERE {0}'.format(' AND '.join(clauses)), values)
|
2014-05-26 21:53:54 +00:00
|
|
|
else:
|
2015-08-25 18:21:56 +00:00
|
|
|
c.execute('DELETE FROM heartbeat WHERE {0}'.format(' AND '.join(clauses)))
|
2014-05-26 00:25:24 +00:00
|
|
|
conn.commit()
|
|
|
|
if row is not None:
|
2014-12-22 06:55:59 +00:00
|
|
|
heartbeat = {
|
2014-05-26 00:25:24 +00:00
|
|
|
'file': row[0],
|
|
|
|
'time': row[1],
|
|
|
|
'project': row[2],
|
2014-12-22 06:55:59 +00:00
|
|
|
'branch': row[3],
|
|
|
|
'is_write': True if row[4] is 1 else False,
|
|
|
|
'stats': row[5],
|
|
|
|
'misc': row[6],
|
2014-05-26 00:25:24 +00:00
|
|
|
'plugin': row[7],
|
|
|
|
}
|
|
|
|
loop = False
|
2014-05-27 05:43:35 +00:00
|
|
|
except sqlite3.Error:
|
2014-05-26 21:53:54 +00:00
|
|
|
log.debug(traceback.format_exc())
|
2014-05-26 00:25:24 +00:00
|
|
|
sleep(wait)
|
|
|
|
tries -= 1
|
2014-05-26 21:53:54 +00:00
|
|
|
try:
|
|
|
|
conn.close()
|
2014-05-27 05:43:35 +00:00
|
|
|
except sqlite3.Error:
|
2014-05-26 21:53:54 +00:00
|
|
|
log.debug(traceback.format_exc())
|
2014-12-22 06:55:59 +00:00
|
|
|
return heartbeat
|