Skip to content
This repository has been archived by the owner on Mar 22, 2018. It is now read-only.

Commit

Permalink
adding rows to the history view in smaller batches.
Browse files Browse the repository at this point in the history
this will not freeze your windows, but it will take a lot longer before all is loaded in the view.
hopefully you just need the top rows so you will not even notice the program is still busy loading data.
  • Loading branch information
mfrasca committed Dec 31, 2015
1 parent e2fa4ee commit 646f7c7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 31 deletions.
10 changes: 9 additions & 1 deletion bauble/pluginmgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,9 +495,17 @@ def __init__(self, *args, **kwargs):
widget = root_widget.get_children()[0]
self.view.widgets.remove_parent(widget)
self.add(widget)
self.running_threads = []

def cancel_threads(self):
pass
for k in self.running_threads:
k.cancel()
self.running_threads = []

def start_thread(self, thread):
self.running_threads.append(thread)
thread.start()
return thread

def update(self):
pass
Expand Down
79 changes: 49 additions & 30 deletions bauble/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,16 +748,6 @@ def _impl(*args):
logger.warning(
'Could not parse accelerator: %s' % (action.accelerator))

def cancel_threads(self):
for k in self.running_threads:
k.cancel()
self.running_threads = []

def start_thread(self, thread):
self.running_threads.append(thread)
thread.start()
return thread

nresults_statusbar_context = 'searchview.nresults'

def search(self, text):
Expand Down Expand Up @@ -1260,6 +1250,35 @@ def attached_to(cls, obj):
return []


class AppendThousandRows(threading.Thread):

def callback(self, rows):
for row in rows:
self.view.add_row(row)

def __init__(self, view, group=None, verbose=None, **kwargs):
super(AppendThousandRows, self).__init__(
group=group, target=None, name=None, verbose=verbose)
self.__stopped = threading.Event()
self.view = view

def cancel(self):
self.__stopped.set()

def run(self):
session = db.Session()
q = session.query(db.History).order_by(db.History.timestamp.desc())
# add rows in small batches
offset = 0
step = 200
count = q.count()
while offset < count and not self.__stopped.isSet():
rows = q.offset(offset).limit(step).all()
gobject.idle_add(self.callback, rows)
offset += step
session.close()


class HistoryView(pluginmgr.View):
"""Show the tables row in the order they were last updated
"""
Expand All @@ -1273,26 +1292,6 @@ def __init__(self):
self.liststore = self.view.widgets.history_ls
self.update()

def update(self):
"""
Add the history items to the view.
"""
session = db.Session()
self.liststore.clear()
for item in session.query(db.History).\
order_by(db.History.timestamp.desc()).all():
d = eval(item.values)
del d['_created']
del d['_last_updated']
friendly = ', '.join(u"%s: %s" % (k, self.show_typed_value(v))
for k, v in sorted(d.items(), self.cmp_items)
)
self.liststore.append([
("%s" % item.timestamp)[:19], item.operation, item.user,
item.table_name, friendly, item.values
])
session.close()

@staticmethod
def cmp_items(a, b):
ka, va = a
Expand All @@ -1319,6 +1318,26 @@ def show_typed_value(v):
except:
return u"»%s«" % v

def add_row(self, item):
d = eval(item.values)
del d['_created']
del d['_last_updated']
friendly = ', '.join(u"%s: %s" % (k, self.show_typed_value(v))
for k, v in sorted(d.items(), self.cmp_items)
)
self.liststore.append([
("%s" % item.timestamp)[:19], item.operation, item.user,
item.table_name, friendly, item.values
])

def update(self):
"""
Add the history items to the view.
"""
self.liststore.clear()
self.start_thread(AppendThousandRows(self))


class HistoryCommandHandler(pluginmgr.CommandHandler):

command = 'history'
Expand Down

0 comments on commit 646f7c7

Please sign in to comment.