Save spent time on activities

Use set_active and save events to keep track of the time,
in seconds, that the activity is on the main screen. The
time is calculated and stored each time an activity is
open/resumed and closed. Therefore, each activity entry
will have an array of seconds for each time it was used.

This makes a 1 to 1 relationship between the spent and
lauch times. In orders words, we can reflect not only the
"when", but also the " for how long".

Even though this time does not necessarily represents the
real time the user "used" the activity, it is a low-cost
approach for reflecting time.

This is a long requested featured from deployments that have
been using statistics tools on journal metadata, and this
patch is already being used by OLPC-AU.

Signed-off-by: Martin Abente Lahaye <tch@sugarlabs.org>
This commit is contained in:
Martin Abente Lahaye 2014-06-06 11:12:39 -04:00
parent 16617b22d7
commit 98a29ae12c

View File

@ -327,6 +327,8 @@ class Activity(Window, Gtk.Container):
self.connect('delete-event', self.__delete_event_cb) self.connect('delete-event', self.__delete_event_cb)
self._active = False self._active = False
self._active_time = None
self._spent_time = 0
self._activity_id = handle.activity_id self._activity_id = handle.activity_id
self.shared_activity = None self.shared_activity = None
self._join_id = None self._join_id = None
@ -367,6 +369,11 @@ class Activity(Window, Gtk.Container):
self._jobject.metadata['launch-times'] = \ self._jobject.metadata['launch-times'] = \
str(int(time.time())) str(int(time.time()))
if 'spent-times' in self._jobject.metadata:
self._jobject.metadata['spent-times'] += ', 0'
else:
self._jobject.metadata['spent-times'] = '0'
self.shared_activity = None self.shared_activity = None
self._join_id = None self._join_id = None
@ -420,6 +427,7 @@ class Activity(Window, Gtk.Container):
jobject.metadata['share-scope'] = SCOPE_PRIVATE jobject.metadata['share-scope'] = SCOPE_PRIVATE
jobject.metadata['icon-color'] = icon_color jobject.metadata['icon-color'] = icon_color
jobject.metadata['launch-times'] = str(int(time.time())) jobject.metadata['launch-times'] = str(int(time.time()))
jobject.metadata['spent-times'] = '0'
jobject.file_path = '' jobject.file_path = ''
# FIXME: We should be able to get an ID synchronously from the DS, # FIXME: We should be able to get an ID synchronously from the DS,
@ -485,9 +493,21 @@ class Activity(Window, Gtk.Container):
def get_active(self): def get_active(self):
return self._active return self._active
def _update_spent_time(self):
if self._active is True and self._active_time is None:
self._active_time = time.time()
elif self._active is False and self._active_time is not None:
self._spent_time += time.time() - self._active_time
self._active_time = None
elif self._active is True and self._active_time is not None:
current = time.time()
self._spent_time += current - self._active_time
self._active_time = current
def set_active(self, active): def set_active(self, active):
if self._active != active: if self._active != active:
self._active = active self._active = active
self._update_spent_time()
if not self._active and self._jobject: if not self._active and self._jobject:
self.save() self.save()
@ -778,6 +798,19 @@ class Activity(Window, Gtk.Container):
self.metadata['buddies_id'] = json.dumps(buddies_dict.keys()) self.metadata['buddies_id'] = json.dumps(buddies_dict.keys())
self.metadata['buddies'] = json.dumps(self._get_buddies()) self.metadata['buddies'] = json.dumps(self._get_buddies())
# update spent time before saving
self._update_spent_time()
def set_last_value(values_list, new_value):
if ', ' not in values_list:
return '%d' % new_value
else:
partial_list = ', '.join(values_list.split(', ')[:-1])
return partial_list + ', %d' % new_value
self.metadata['spent-times'] = set_last_value(
self.metadata['spent-times'], self._spent_time)
preview = self.get_preview() preview = self.get_preview()
if preview is not None: if preview is not None:
self.metadata['preview'] = dbus.ByteArray(preview) self.metadata['preview'] = dbus.ByteArray(preview)