home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 October / maximum-cd-2011-10.iso / DiscContents / digsby_setup.exe / lib / plugins / stats_counters / count_tracker.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-06-22  |  13.8 KB  |  301 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from stanzas.action import Action, SYNC_PROTOS
  5. from stanzas.counter import CounterQuery
  6. import stanzas.action as stanzas
  7. from peak.util.addons import AddOn
  8. from threading import RLock
  9. from util.threads.timeout_thread import ResetTimer
  10. from collections import defaultdict
  11. import warnings
  12. import traceback
  13. COUNT_TIMEOUT = 300
  14. simple_incrementers = [
  15.     ('digsby.twitter.status_updated', 'twit_post'),
  16.     ('digsby.twitter.direct_sent', 'twit_post'),
  17.     ('digsby.statistics.twitter.feed_window.shown', 'twit_feed_window_shown'),
  18.     ('digsby.statistics.twitter.feed_window.activated', 'twit_feed_window_activated'),
  19.     ('digsby.statistics.twitter.new_search', 'twit_new_search'),
  20.     ('digsby.statistics.twitter.viewed_search', 'twit_viewed_search'),
  21.     ('digsby.statistics.twitter.infobox.shown', 'twit_infobox_shown'),
  22.     ('digsby.statistics.twitter.invite.shown', 'twitter.invite.shown'),
  23.     ('digsby.statistics.twitter.invite.yes', 'twitter.invite.yes'),
  24.     ('digsby.statistics.twitter.invite.no', 'twitter.invite.no'),
  25.     ('digsby.statistics.email.inbox_opened', 'email.inbox_opened'),
  26.     ('digsby.statistics.email.delete', 'email.delete'),
  27.     ('digsby.statistics.email.archive', 'email.archive'),
  28.     ('digsby.statistics.email.mark_as_read', 'email.mark_as_read'),
  29.     ('digsby.statistics.email.spam', 'email.spam'),
  30.     ('digsby.statistics.email.email_opened', 'email.email_opened'),
  31.     ('digsby.statistics.email.compose', 'email.compose'),
  32.     ('digsby.statistics.email.sent_from_imwindow', 'email.imwin_sent'),
  33.     ('digsby.statistics.email.invite.shown', 'email.invite.shown'),
  34.     ('digsby.statistics.email.invite.yes', 'email.invite.yes'),
  35.     ('digsby.statistics.email.invite.no', 'email.invite.no'),
  36.     ('digsby.myspace.status_updated', 'mysp_post'),
  37.     ('digsby.myspace.photo_seen', 'mysp_photo'),
  38.     ('digsby.myspace.comment_added', 'mysp_comment'),
  39.     ('digsby.myspace.dislike_added', 'mysp_dislike'),
  40.     ('digsby.myspace.like_added', 'mysp_like'),
  41.     ('digsby.facebook.status_updated', 'face_post'),
  42.     ('digsby.facebook.like_added', 'face_like'),
  43.     ('digsby.facebook.dislike_added', 'face_dislike'),
  44.     ('digsby.facebook.comment_added', 'face_comment'),
  45.     ('digsby.facebook.photo_seen', 'face_photo'),
  46.     ('digsby.linkedin.status_updated', 'link_post'),
  47.     ('digsby.linkedin.comment_added', 'link_comment'),
  48.     ('digsby.linkedin.dislike_added', 'link_dislike'),
  49.     ('digsby.linkedin.like_added', 'link_like'),
  50.     ('digsby.statistics.buddylist.search', 'buddylist.search'),
  51.     ('digsby.statistics.feed_ads.impression', 'feed_ad.impression'),
  52.     ('digsby.statistics.feed_ads.click', 'feed_ad.click'),
  53.     ('digsby.statistics.feed_ads.share', 'feed_ad.share'),
  54.     ('digsby.statistics.feed_ads.citygrid.click', 'feed_ad.citygrid.click'),
  55.     ('digsby.statistics.feed_ads.citygrid.impression', 'feed_ad.citygrid.impression'),
  56.     ('digsby.statistics.feed_ads.citygrid.share', 'feed_ad.citygrid.share')]
  57. stanzas.action.WHITELISTED_TYPES.update((lambda .0: for hook, name in .0:
  58. name)(simple_incrementers))
  59.  
  60. def incrementer(what):
  61.     
  62.     def incr(self, *a, **k):
  63.         self.increment(what)
  64.  
  65.     return incr
  66.  
  67.  
  68. class CountTracker(AddOn):
  69.     
  70.     def __init__(self, subject):
  71.         self.profile = subject
  72.         self.values = defaultdict(int)
  73.         self.cached = { }
  74.         self.thresholds = { }
  75.         self.lock = RLock()
  76.         self.did_setup = False
  77.         super(CountTracker, self).__init__(subject)
  78.  
  79.     
  80.     def self_incrementer(self, what):
  81.         
  82.         def incr(*a, **k):
  83.             self.increment(what)
  84.  
  85.         return incr
  86.  
  87.     
  88.     def im(self, _conv, _msg, type):
  89.         if type == 'outgoing':
  90.             self.increment('im_sent')
  91.         
  92.         if type == 'incoming':
  93.             self.increment('im_received')
  94.         
  95.  
  96.     emoticon_box_viewed = incrementer('emoticon_box_viewed')
  97.     emoticon_chosen = incrementer('emoticon_chosen')
  98.     video_chat_requested = incrementer('video_chat_requested')
  99.     sms_sent = incrementer('sms_sent')
  100.     log_viewed = incrementer('log_viewed')
  101.     prefs_opened = incrementer('prefs.prefs_opened')
  102.     
  103.     def increment_account(self, prefix, account):
  104.         protocol = getattr(account, 'protocol', None)
  105.         if isinstance(protocol, basestring) and protocol in SYNC_PROTOS:
  106.             self.increment(prefix + '.' + protocol)
  107.         
  108.  
  109.     infobox_shown = incrementer('infobox.shown')
  110.     imwin_created = incrementer('imwin.imwin_created')
  111.     
  112.     def imwin_engage(self, seconds):
  113.         self.increment('imwin.imwin_engage', seconds)
  114.  
  115.     contact_added = incrementer('contact_added')
  116.     contact_add_dialog = incrementer('ui.dialogs.add_contact.created')
  117.     ui_select_status = incrementer('ui.select_status')
  118.     
  119.     def citygrid_click(self, cents):
  120.         self.increment('feed_ad.citygrid.click_cents', cents)
  121.  
  122.     
  123.     def setup(self):
  124.         if self.did_setup:
  125.             warnings.warn('reinitialized AddOn CountTracker')
  126.             return None
  127.         self.did_setup = True
  128.         Hook = Hook
  129.         import peak.util.plugins
  130.         Hook('digsby.im.msg.async', 'stats_counter').register(self.im)
  131.         Hook('digsby.statistics.emoticons.box_viewed', 'stats_counter').register(self.emoticon_box_viewed)
  132.         Hook('digsby.statistics.emoticons.chosen', 'stats_counter').register(self.emoticon_chosen)
  133.         Hook('digsby.video_chat.requested', 'stats_counter').register(self.video_chat_requested)
  134.         Hook('digsby.statistics.sms.sent', 'stats_counter').register(self.sms_sent)
  135.         Hook('digsby.statistics.logviewer.log_viewed', 'stats_counter').register(self.log_viewed)
  136.         Hook('digsby.statistics.prefs.prefs_opened', 'stats_counter').register(self.prefs_opened)
  137.         Hook('digsby.statistics.imwin.imwin_created', 'stats_counter').register(self.imwin_created)
  138.         Hook('digsby.statistics.imwin.imwin_engage', 'stats_counter').register(self.imwin_engage)
  139.         Hook('digsby.statistics.infobox.shown', 'stats_counter').register(self.infobox_shown)
  140.         Hook('digsby.statistics.contact_added', 'stats_counter').register(self.contact_added)
  141.         Hook('digsby.statistics.ui.select_status', 'stats_counter').register(self.ui_select_status)
  142.         Hook('digsby.statistics.ui.dialogs.add_contact.created', 'stats_counter').register(self.contact_add_dialog)
  143.         Hook('digsby.app.exit', 'stats_counter').register(self.flush)
  144.         Hook('digsby.stats_counter.important_threshold', 'stats_counter').register(self.important)
  145.         for hook_name, server_side_name in simple_incrementers:
  146.             Hook(hook_name, 'stats_counter').register(self.self_incrementer(server_side_name))
  147.         
  148.         Hook('digsby.statistics.feed_ads.citygrid.click_cents', 'stats_counter').register(self.citygrid_click)
  149.         Hook('digsby.research.run_time', 'stats_counter').register(self.research_run_time)
  150.  
  151.     
  152.     def important(self, type, value):
  153.         self.thresholds[type] = value
  154.  
  155.     
  156.     def research_run_time(self, seconds):
  157.         self.increment('research.run_time', int(seconds))
  158.  
  159.     
  160.     def increment(self, type, value = None):
  161.         self.lock.__enter__()
  162.         
  163.         try:
  164.             self.update_val(type = type, value = value)
  165.             if type in self.thresholds and type in self.cached:
  166.                 if self.values[type] + self.cached[type] >= self.thresholds[type]:
  167.                     self.flush_key(type)
  168.                 
  169.             elif type not in self.cached:
  170.                 self.flush_key(type)
  171.         finally:
  172.             pass
  173.  
  174.  
  175.     
  176.     def update_val(self, type, value = None):
  177.         if value is None:
  178.             value = 1
  179.         
  180.         self.lock.__enter__()
  181.         
  182.         try:
  183.             self.values[type] += value
  184.             if hasattr(self, 'timer'):
  185.                 if self.timer.isAlive():
  186.                     self.timer.reset()
  187.                 
  188.             else:
  189.                 self.timer = ResetTimer(COUNT_TIMEOUT, self.flush)
  190.                 self.timer.start()
  191.         finally:
  192.             pass
  193.  
  194.  
  195.     
  196.     def flush(self):
  197.         self.lock.__enter__()
  198.         
  199.         try:
  200.             values = self.values
  201.             self.values = defaultdict(int)
  202.             self.thresholds.clear()
  203.         finally:
  204.             pass
  205.  
  206.         self.send_updates_batched(values)
  207.  
  208.     
  209.     def flush_key(self, key):
  210.         self.lock.__enter__()
  211.         
  212.         try:
  213.             val = self.values.pop(key)
  214.             self.thresholds.pop(key, None)
  215.         finally:
  216.             pass
  217.  
  218.         self.send_update(key, val)
  219.  
  220.     
  221.     def send_update(self, type, value = None):
  222.         connection = None
  223.         
  224.         try:
  225.             connection = getattr(self.profile, 'connection', None)
  226.         except AttributeError:
  227.             pass
  228.  
  229.         if connection is None:
  230.             self.update_val(type, value)
  231.             return None
  232.         iq = connection is None([
  233.             CounterQuery(Action, type = 'value' if value and value > 1 else None)]).make_push(connection)
  234.         
  235.         try:
  236.             connection.send_cb(iq, success = self.handle_response)
  237.         except Exception:
  238.             traceback.print_exc()
  239.  
  240.         return None
  241.         return self.send_updates_batched(dict(type = value))
  242.  
  243.     
  244.     def send_updates_batched(self, updates):
  245.         for update in updates.items():
  246.             self.send_update(*update)
  247.         
  248.         return None
  249.         connection = None
  250.         
  251.         try:
  252.             connection = getattr(self.profile, 'connection', None)
  253.         except AttributeError:
  254.             pass
  255.  
  256.         if connection is None:
  257.             for update in updates.items():
  258.                 self.update_val(*update)
  259.             
  260.             return None
  261.         iq = []([ _[1](Action, type = 'value' if value and value > 1 else None) for type, value in updates.items() ]).make_push(connection)
  262.         
  263.         try:
  264.             connection.send_cb(iq, success = self.handle_response)
  265.         except Exception:
  266.             CounterQuery
  267.             CounterQuery
  268.             connection is None
  269.             traceback.print_exc()
  270.         except:
  271.             CounterQuery
  272.  
  273.  
  274.     
  275.     def handle_response(self, stanza):
  276.         
  277.         try:
  278.             c = CounterQuery(stanza.get_query())
  279.         except ValueError:
  280.             return None
  281.  
  282.         self.lock.__enter__()
  283.         
  284.         try:
  285.             for action in c:
  286.                 self.cached[action.type] = action.result
  287.         finally:
  288.             pass
  289.  
  290.         import hooks
  291.         actions = []
  292.         for action in c:
  293.             actions.append(dict(type = action.type, initial = action.initial, value = action.value, result = action.result))
  294.         
  295.         for action in actions:
  296.             hooks.notify('digsby.stats_counter', **action)
  297.         
  298.         hooks.notify('digsby.stats_counter.batch', actions)
  299.  
  300.  
  301.