def query_most_active_pages(self, start_row=0, rows_per_page=10, order=1, sort_col=None, filters=None, **params): edit_counts = defaultdict(int) # {pagename: # of edits} last_edited_by = {} # {pagename: username} wiki_cache = self._request.environ['beaker.cache'].get_cache('wiki') changes = wiki_cache.get_value(key='recent_changes', createfunc=self._get_recent_changes, expiretime=3600) for change in changes: edit_counts[change['title']] += 1 if change['title'] not in last_edited_by.keys(): last_edited_by[change['title']] = change['user'].lower() most_active_pages = sorted(edit_counts.items(), cmp=lambda x, y : cmp(x[1], y[1]), reverse=True) page_data = [] for i, page in enumerate(most_active_pages): page_data.append({'number_of_edits': page[1], 'title': page[0], 'last_edited_by': last_edited_by[page[0]]}) return (len(page_data), page_data[start_row:start_row+rows_per_page])
def query_most_active_users(self, user_count=10, **params): users = defaultdict(list) wiki_cache = self._request.environ['beaker.cache'].get_cache('wiki') changes = wiki_cache.get_value(key='recent_changes', createfunc=self._get_recent_changes, expiretime=3600) for change in changes: users[change['user'].lower()].append(change['title']) most_active_users = sorted(users.items(), cmp=lambda x, y: cmp(len(x[1]), len(y[1])), reverse=True)[:user_count] user_data = [] user_ticks = [] for i, user in enumerate(most_active_users): user_data.append([i, len(user[1])]) user_ticks.append([i + 0.5, user[0]]) flot_data = {'data': [], 'options': {'xaxis': {}}} flot_data['options']['xaxis']['ticks'] = user_ticks flot_data['data'].append({ 'data': user_data, 'bars': {'show': True} }) return flot_data
def register_livewidgets(self, environ): """ Register the `moksha.livewidgets` dictionary. This is a per-request StackedObjectProxy that is used by the LiveWidgets to register their own topic callbacks. The Moksha Live Socket then handles subscribing widgets to their appropriate topics, decoding the incoming JSON data, and dispatching messages to them as they arrive. """ environ['paste.registry'].register(moksha.wsgi.lib.utils.livewidgets, { 'onopen': [], 'onclose': [], 'onerror': [], 'onerrorframe': [], 'onconnectedframe': [], 'onmessageframe': defaultdict(list) # {topic: [js_callback,]} })
def prepare(self): super(AbstractMokshaSocket, self).prepare() if not self.__shorthand__: raise ValueError("SocketWidget must declare __shorthand__") self.topics = [] self.onmessageframe = defaultdict(str) if self.notify: self.resources += gritter_resources self.before_open = "$(%s);" % unicode(gritter_callback( title=self.__shorthand__, text=self.notifications['before_open'], )) for callback in self.callbacks: cbs = '' if self.notify and callback in self.notifications: cbs += "$(%s);" % unicode(gritter_callback( title=self.__shorthand__, text=self.notifications[callback] )) if self.reconnect_interval and callback is 'onclose': cbs += "setTimeout(setup_moksha_socket, %i)" % \ int(self.reconnect_interval) if len(moksha.wsgi.lib.utils.livewidgets[callback]): if callback == 'onmessageframe': for topic in moksha.wsgi.lib.utils.livewidgets[callback]: self.topics.append(topic) for cb in moksha.wsgi.lib.utils.livewidgets[callback][topic]: self.onmessageframe[topic] += '%s;' % unicode(cb) else: for cb in moksha.wsgi.lib.utils.livewidgets[callback]: if isinstance(cb, (twc.js_callback, twc.js_function)): cbs += '$(%s);' % unicode(cb) else: cbs += unicode(cb) if cbs: setattr(self, callback, cbs)
def __init__(self, config, topics=None): self.config = config if not self.topics: self.topics = defaultdict(list) if topics == None: topics = {} for topic, callbacks in topics.iteritems(): if not isinstance(callbacks, list): callbacks = [callbacks] for callback in callbacks: self.topics[topic].append(callback) self.extensions = [ ext(self, config) for ext in find_hub_extensions(config) ]
def flot_wiki_edits_per_day(self, **params): stats_cache = Shove(config.get('stats_cache')) try: data = stats_cache['wiki_all_revisions'] except KeyError: return False timestamps = defaultdict(int) for rev_id in data['revs']: timestamp = data['revs'][rev_id]['time'] day_timestamp = int(mktime((timestamp[0], timestamp[1], timestamp[2], 0, 0, 0, 0, 0, 0))*1000) timestamps[day_timestamp] += 1 flot = {'data': [], 'options': {'xaxis': {'mode': 'time'}}} timestamps_sorted = timestamps.keys() timestamps_sorted.sort() flot['data'] = [[[timestamp, timestamps[timestamp]] \ for timestamp in timestamps_sorted][:-1]] return flot
def __init__(self, config, consumers=None, producers=None): log.info('Loading the Moksha Hub') self.topics = defaultdict(list) # These are used to override the entry-points behavior self._consumers = consumers self._producers = producers self.__init_consumers() super(CentralMokshaHub, self).__init__(config) # FIXME -- this needs to be reworked. # TODO -- consider moving this to the AMQP specific modules for ext in self.extensions: if AMQPHubExtension and isinstance(ext, AMQPHubExtension): self.__init_amqp() self.__run_consumers() self.__init_producers() self.__init_websocket_server()