Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Command to import past MetricItems into MixPanel #3

Merged
merged 15 commits into from

2 participants

@fcurella

I've added a command to import past MetricItems into MixPanel.

I've also modified backends.mixpanel.mixpanel_metric_task to make use of the num parameter. I couldn't find how to specify that anywhere in the MixPanel API, so I just do num requests with the same args.

Signed-off-by: Flavio Curella flavio.curella@gmail.com

fcurella added some commits
@fcurella fcurella added a command for moving MetricItems from db to mixpanel
Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
189f811
@fcurella fcurella make use of 'num' in MixPanel backend
Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
8ae5a1d
@fcurella fcurella removed unnecessary import
Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
6d6451e
@fcurella fcurella moved mixpanel task so it gets registered by celery
Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
2dbb9b2
@fcurella fcurella forgot some imports
Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
cbf025a
@fcurella fcurella typo fix
Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
f2e0a87
@fcurella fcurella fixing import errors
Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
513bdd4
@fcurella fcurella fixing import errors
Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
dedb220
@fcurella fcurella NotRegistered
Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
11e7062
@fcurella fcurella trying to register the task
Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
78860a7
@fcurella fcurella moved tasks into tasks.py
Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
e8e5d53
@fcurella fcurella Merge branch 'stable'
Conflicts:
	app_metrics/backends/mixpanel.py
	app_metrics/tasks.py

Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
363e92b
@fcurella fcurella added MixPanelTrackError exception, so we can track unsuccessful tasks
Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
6b17779
@fcurella fcurella version bump. 0.3.0
Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
f0c183f
@fcurella fcurella fixed circular import
Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
44046d8
@frankwiles frankwiles merged commit e467332 into frankwiles:master
@frankwiles
Owner

Thanks Flavio, apologies it's taken me this long to get it merged!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 7, 2011
  1. @fcurella

    added a command for moving MetricItems from db to mixpanel

    fcurella authored
    Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
  2. @fcurella

    make use of 'num' in MixPanel backend

    fcurella authored
    Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
  3. @fcurella

    removed unnecessary import

    fcurella authored
    Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
Commits on Jul 11, 2011
  1. @fcurella

    moved mixpanel task so it gets registered by celery

    fcurella authored
    Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
  2. @fcurella

    forgot some imports

    fcurella authored
    Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
  3. @fcurella

    typo fix

    fcurella authored
    Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
  4. @fcurella

    fixing import errors

    fcurella authored
    Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
  5. @fcurella

    fixing import errors

    fcurella authored
    Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
  6. @fcurella

    NotRegistered

    fcurella authored
    Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
  7. @fcurella

    trying to register the task

    fcurella authored
    Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
  8. @fcurella

    moved tasks into tasks.py

    fcurella authored
    Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
  9. @fcurella

    Merge branch 'stable'

    fcurella authored
    Conflicts:
    	app_metrics/backends/mixpanel.py
    	app_metrics/tasks.py
    
    Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
Commits on Jul 12, 2011
  1. @fcurella

    added MixPanelTrackError exception, so we can track unsuccessful tasks

    fcurella authored
    Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
  2. @fcurella

    version bump. 0.3.0

    fcurella authored
    Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
  3. @fcurella

    fixed circular import

    fcurella authored
    Signed-off-by: Flavio Curella <flavio.curella@gmail.com>
This page is out of date. Refresh to see the latest.
View
1  AUTHORS.txt
@@ -3,4 +3,5 @@ django-app-metrics was originally created by Frank Wiles <frank@revsys.com>
Thanks to all of our contributors!
Ross Poulton <ross@rossp.org>
+Flavio Curella <flavio@storymarket.com>
View
2  app_metrics/__init__.py
@@ -1 +1 @@
-VERSION = (0, 2, 0)
+VERSION = (0, 3, 0)
View
11 app_metrics/backends/db.py
@@ -1,14 +1,5 @@
-from celery.decorators import task
-
-from app_metrics.models import Metric, MetricItem
+from app_metrics.tasks import db_metric_task
def metric(slug, num=1, **kwargs):
""" Fire a celery task to record our metric in the database """
db_metric_task.delay(slug, num, **kwargs)
-
-@task
-def db_metric_task(slug, num=1, **kwargs):
- met = Metric.objects.get(slug=slug)
-
- new_metric = MetricItem.objects.create(metric=met, num=num)
-
View
38 app_metrics/backends/mixpanel.py
@@ -1,23 +1,9 @@
# Backend to handle sending app metrics directly to mixpanel.com
# See http://mixpanel.com/api/docs/ for more information on their API
-import base64
-import json
-import urllib
-import urllib2
-
-from celery.decorators import task
-
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
-
-def _get_token():
- token = getattr(settings, 'APP_METRICS_MIXPANEL_TOKEN', None)
-
- if not token:
- raise ImproperlyConfigured("You must define APP_METRICS_MIXPANEL_TOKEN when using the mixpanel backend.")
- else:
- return token
+from app_metrics.tasks import mixpanel_metric_task
def metric(slug, num=1, properties=None):
"""
@@ -31,26 +17,4 @@ def metric(slug, num=1, properties=None):
metric("invite-friends",
properties={"method": "email", "number-friends": "12", "ip": "123.123.123.123"})
"""
- token = _get_token()
mixpanel_metric_task.delay(slug, num, properties)
-
-@task
-def mixpanel_metric_task(slug, num, properties=None, **kwargs):
-
- token = _get_token()
- if properties == None:
- properties = dict()
-
- if "token" not in properties:
- properties["token"] = token
-
- url = getattr(settings, 'APP_METRICS_MIXPANEL_API_URL', "http://api.mixpanel.com/track/")
-
- params = {"event": slug, "properties": properties}
- b64_data = base64.b64encode(json.dumps(params))
-
- data = urllib.urlencode({"data": b64_data})
- req = urllib2.Request(url, data)
- response = urllib2.urlopen(req)
-
-
View
30 app_metrics/management/commands/move_to_mixpanel.py
@@ -0,0 +1,30 @@
+from django.core.management.base import NoArgsCommand
+
+from app_metrics.models import MetricItem
+from app_metrics.backends.mixpanel import metric
+
+class Command(NoArgsCommand):
+ help = "Move MetricItems from the db backend to MixPanel"
+
+ requires_model_validation = True
+
+ def handle_noargs(self, **options):
+ """ Move MetricItems from the db backend to MixPanel" """
+
+ backend = get_backend()
+
+ # If not using Mixpanel this command is a NOOP
+ if backend != 'app_metrics.backends.mixpanel':
+ print "You need to set the backend to MixPanel"
+ return
+
+ items = MetricItem.objects.all()
+
+ for i in items:
+ properties = {
+ 'time': i.created.strftime('%s'),
+ }
+ metric(i.metric.slug, num=i.num, properties=properties)
+
+ # Kill off our items
+ items.delete()
View
47 app_metrics/tasks.py
@@ -0,0 +1,47 @@
+import base64
+import json
+import urllib
+import urllib2
+
+from django.conf import settings
+from celery.decorators import task
+from app_metrics.models import Metric, MetricItem
+
+class MixPanelTrackError(Exception):
+ pass
+
+@task
+def db_metric_task(slug, num=1, **kwargs):
+ met = Metric.objects.get(slug=slug)
+
+ new_metric = MetricItem.objects.create(metric=met, num=num)
+
+def _get_token():
+ token = getattr(settings, 'APP_METRICS_MIXPANEL_TOKEN', None)
+
+ if not token:
+ raise ImproperlyConfigured("You must define APP_METRICS_MIXPANEL_TOKEN when using the mixpanel backend.")
+ else:
+ return token
+
+@task
+def mixpanel_metric_task(slug, num, properties=None, **kwargs):
+
+ token = _get_token()
+ if properties == None:
+ properties = dict()
+
+ if "token" not in properties:
+ properties["token"] = token
+
+ url = getattr(settings, 'APP_METRICS_MIXPANEL_API_URL', "http://api.mixpanel.com/track/")
+
+ params = {"event": slug, "properties": properties}
+ b64_data = base64.b64encode(json.dumps(params))
+
+ data = urllib.urlencode({"data": b64_data})
+ req = urllib2.Request(url, data)
+ for i in range(num):
+ response = urllib2.urlopen(req)
+ if response.read() == '0':
+ raise MixPanelTrackError(u'MixPanel returned 0')
Something went wrong with that request. Please try again.