def get_formatted_cache_stats(self): """ returns a string the shows # of objects retrieved from cache and # of objects loaded from ZODB since the last time this function was called. set VERBOSE_CACHE_LOGGING=True if you want to see the actual objects (tons of output!) """ from qon.util import get_oid # create the verbose version while tallying up data for the summary version verbose_accessed = '' verbose_added = '' accessed_tally = {} added_tally = {} self.accessed_oids.sort(lambda x, y: x[0]-y[0]) self.added_oids.sort(lambda x, y: x[0]-y[0]) for size, oid in self.accessed_oids: if qon.local.VERBOSE_CACHE_LOGGING: verbose_accessed += "\n --(%d bytes) %s" % (size, str(get_oid(oid))) self._tally(accessed_tally, type(get_oid(oid)), size) for size, oid in self.added_oids: if qon.local.VERBOSE_CACHE_LOGGING: verbose_added += "\n --(%d bytes) %s" % (size, str(get_oid(oid))) self._tally(added_tally, type(get_oid(oid)), size) if qon.local.VERBOSE_CACHE_LOGGING: verbose_combined = '\n%d OBJECTS RETRIEVED FROM CACHE:\n%s\n\n%d OBJECTS LOADED FROM ZODB:\n%s\n====================================================\n' \ % (len(self.accessed_oids), verbose_accessed, len(self.added_oids), verbose_added) # ok, now let's finish off the summary version accessed = '' added = '' accessed_keys = accessed_tally.keys() accessed_keys.sort(lambda x, y: accessed_tally[x][1]-accessed_tally[y][1]) added_keys = added_tally.keys() added_keys.sort(lambda x, y: added_tally[x][1]-added_tally[y][1]) for t in accessed_keys: accessed += "\n --(%d bytes) (%d) %s" % (accessed_tally[t][1], accessed_tally[t][0], t) for t in added_keys: added += "\n --(%d bytes) (%d) %s" % (added_tally[t][1], added_tally[t][0], t) combined = '\n%d OBJECTS RETRIEVED FROM CACHE:\n%s\n\n%d OBJECTS LOADED FROM ZODB:\n%s\n====================================================\n' \ % (len(self.accessed_oids), accessed, len(self.added_oids), added) # reset oid lists to prepare for next call self.clear_oid_lists() if qon.local.VERBOSE_CACHE_LOGGING: return verbose_combined else: return combined
def get_pageview_counts_per_user(self): """return a list of (user, counts)""" return_list = [] for user_oid, (dt, count) in self.__user_access.iteritems(): if count > 0: return_list.append((get_oid(pack_oid(user_oid)), count)) return return_list
def index_all_tagged (): tidb = get_tagged_item_database() i = 1 for oid in tidb: item = get_oid(oid) index_tagged_item(item) _commit(i) i += 1 _commit(0)
def _get_items(self, oid_list): """Return a list of valid watchable items from oid_list""" items = [] for k in oid_list: try: obj = get_oid(k) obj_name = obj.watchable_name() except: self._lost_oid(k) else: items.append(obj) return items
def get_hits(self, time_delta, prune=1): """Returns unsorted list of tuples of hits not older than time_delta. Tuples returned: (datetime, obj)... If prune is set, deletes items older than time_delta. """ cutoff = datetime.utcnow() - time_delta l = [] for k, v in self.root.items(): if v > cutoff: l.append((v, get_oid(k))) else: if prune: del self.root[k] return l
def most_watched(self): """Return list of most-watched objects: [(count, object), ...]""" if self.__most_watched: return self.__most_watched from qon.util import get_oid # collect all watched objects oids = {} for user_id, user in get_user_database().root.iteritems(): for oid in user.get_watch_list().watched_items_oids(): oids[oid] = oids.get(oid, 0) + 1 get_connection().cacheGC() # sort them bycount = [] for oid, count in oids.iteritems(): bycount.append((count, oid)) del oids bycount.sort() bycount = bycount[-self._watched_items_count:] bycount.reverse() # get objects watched = [] for count, oid in bycount: try: watched.append((count, get_oid(oid))) except KeyError: # invalid oid pass # save and return self.__most_watched = watched return self.__most_watched