vim-rana/plugin/packages/wakatime/offlinequeue.py

129 lines
3.9 KiB
Python
Raw Normal View History

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