Skip to content

Commit

Permalink
Opt out working. Fixing issue #4
Browse files Browse the repository at this point in the history
  • Loading branch information
ebidel committed Jun 6, 2012
1 parent 8f31d0c commit 9ebb484
Showing 1 changed file with 36 additions and 15 deletions.
51 changes: 36 additions & 15 deletions metrics/insight.py
Expand Up @@ -15,11 +15,10 @@

import settings

NO_STATS = 'NO_STATS'
CLI_NAME = settings.APP['cli_name']
LOG_FILE = os.path.join(os.path.dirname(__file__), '.%sinsight' % CLI_NAME)
NUM_SUB_CMDS = 2 # Subcommand depth. TODO: This assumes only "cmd subcmd" format.

#NUM_SECONDS_TO_STASH_DATA = 60 * 60 * 4 # 4 hrs
NUM_SECONDS_TO_STASH_DATA = 0 # Send data as it happens.


Expand All @@ -30,26 +29,47 @@ class Analytics(object):

def __init__(self, tracking_code):
self.tracking_code = tracking_code
self.do_stats = True

f = open(LOG_FILE, 'a+') # Open file for reading and appending.

# If we're creating a new file, create a new client ID, setup the file, and
# record the download action. Otherwise, read the existing client ID saved
# on the first line of the file.
if os.path.getsize(LOG_FILE) == 0:
# Record the initial "download/install". Then have users opt-in.
self.client_id = '%s%s' % (time.time(), random.random())
self.__reset_file(f, self.client_id);
self.__reset_file(f, self.client_id)
self.record('downloaded')

self._send_all()

# Have users opt-in to sending usage stats.
cli_name = settings.APP['cli_name'].capitalize()
print """==========================================================================
We're constantly looking for ways to make %s better!
May we anonymously report usage statistics to improve the tool over time?
More info: XXX
==========================================================================""" % cli_name
ans = raw_input('[Y/n]: ')
if not (ans == '' or ans.capitalize() == 'Y'):
self.do_stats = False

else:
f.seek(0)
self.client_id = f.readline()[:-1] # Assumes the line ends with "\n".

first_entry_timestamp = float(f.readline().split(' ')[0])
time_delta = time.time() - first_entry_timestamp
try:
first_entry_timestamp = float(f.readline().split(' ')[0])
time_delta = time.time() - first_entry_timestamp

# If we have data that's too old, send it to Analytics.
if time_delta >= NUM_SECONDS_TO_STASH_DATA:
self._send_all()
# If we have data that's too old, send it to Analytics.
if time_delta >= NUM_SECONDS_TO_STASH_DATA:
self._send_all()
except ValueError:
# Error means we tried to parse a non timestamp or one wasn't present.
# That means no stats.
self.do_stats = False

# Insure we're at the EOF to start appending.
f.seek(os.SEEK_END)
Expand All @@ -63,7 +83,7 @@ def __reset_file(self, f, client_id=None):
f: A file object, assumed to be open when passed to this method.
client_id: The client ID to use for this file.
"""
f.write(self.client_id + '\n')
f.write(client_id+ '\n')
f.flush()

def _send(self, path='/', recorded_at=None):
Expand Down Expand Up @@ -108,8 +128,8 @@ def _send(self, path='/', recorded_at=None):

# Noop if we're offline. Just keep stashing entries.
try:
#response = urllib2.urlopen(url)
print url
response = urllib2.urlopen(url)
#print url
#if response.code == 200:
# return True
return True
Expand Down Expand Up @@ -149,10 +169,11 @@ def record(self, cmd_str):
cmd_str = filter(lambda x: x, cmd_str.split(CLI_NAME))[0].strip()
path = '/'.join(cmd_str.split(' ')[:NUM_SUB_CMDS])

f = open(LOG_FILE, 'a')
s = '%s /%s' % (time.time(), path)
f.write(s + '\n')
f.close()
if self.do_stats:
f = open(LOG_FILE, 'a')
s = '%s /%s' % (time.time(), path)
f.write(s + '\n')
f.close()


def main(args):
Expand Down

0 comments on commit 9ebb484

Please sign in to comment.