Permalink
Browse files

Merge branch 'release/0.1.4'

  • Loading branch information...
2 parents e92d6d9 + 8fc292a commit af778447f46c1cf62a7600d45dcd855c75dce2fa @dcloud dcloud committed Jan 3, 2012
Showing with 41 additions and 23 deletions.
  1. +4 −0 config/ga.json
  2. +1 −1 dashboard/models.py
  3. +21 −17 loading/management/commands/loadfacebook.py
  4. +15 −5 loading/management/commands/makeratios.py
View
@@ -60,6 +60,10 @@
{
"slug": "politiwidgets",
"profile_id": "34075661"
+ },
+ {
+ "slug": "capitol-words",
+ "profile_id": "9437033"
}
View
@@ -20,7 +20,7 @@ def __unicode__(self):
return "List: {name}".format(name=self.name)
def save(self):
- if self.default:
+ if hasattr(self, 'default'):
try:
obj = self.__class__.objects.get(default=True)
if self != obj:
@@ -50,25 +50,29 @@ def handle(self, *args, **options):
fbid = urlunsplit( (spliturl.scheme, spliturl.netloc, cleanpath, spliturl.query, spliturl.fragment) )
proj_by_fbid[fbid] = p
# Make a single request for all of the data points
- url_str = ','.join([key for key in proj_by_fbid.iterkeys()])
+ # url_str = ','.join([key for key in proj_by_fbid.iterkeys()])
+ url_list = ','.join(['"{0}"'.format(proj.url) for proj in projects])
+ fql_query = 'SELECT url, normalized_url, total_count FROM link_stat WHERE url IN ({0})'.format(url_list)
if verbosity >= 2:
- self.stdout.write('url_str: {url_str}\n'.format(url_str=url_str))
- params = urlencode({'ids': url_str})
- fp = urlopen("{base_url}?{params}".format(base_url=FACEBOOK_GRAPH_URL, params=params))
+ self.stdout.write('fql_query: {fql_query}\n'.format(fql_query=fql_query))
+ params = urlencode({'q': fql_query})
+ fp = urlopen("{base_url}fql/?{params}".format(base_url=FACEBOOK_GRAPH_URL, params=params))
results = json.load(fp)
# Process results back out to their respective projects
- for r in results.itervalues():
- proj = proj_by_fbid[r['id']]
+ for result in results['data']:
+ # result = results[res_key]
+ proj = projects.get(url=result['url'])
(metric, metric_created) = Metric.objects.get_or_create(unit=unit, project=proj, is_cumulative=FACEBOOK_METRIC["cumulative"])
- (observation, observation_created) = observation_class.objects.get_or_create( metric=metric,
- from_datetime=datetime.datetime.now(),
- to_datetime=datetime.datetime.now(),
- value=r['shares']
- )
- self.stdout.write("{project}'s {unit} for {datetime}: {obs}\n".format(
- project=proj.name,
- unit=unit.name,
- datetime=observation.from_datetime,
- obs=repr(observation)
- ))
+ if result.get('total_count'):
+ (observation, observation_created) = observation_class.objects.get_or_create( metric=metric,
+ from_datetime=datetime.datetime.now(),
+ to_datetime=datetime.datetime.now(),
+ value=result['total_count']
+ )
+ self.stdout.write("{project}'s {unit} for {datetime}: {obs}\n".format(
+ project=proj.name,
+ unit=unit.name,
+ datetime=observation.from_datetime,
+ obs=repr(observation)
+ ))
@@ -7,7 +7,9 @@
# from django.contrib.contenttypes.models import ContentType
from googleanalytics import Connection
from lapidus.metrics.models import Project, Metric, Unit, CountObservation, RatioObservation, UNIT_TYPES, CATEGORIES, PERIODS
-# import datetime
+
+from datetime import timedelta
+
import json
# Unit.name, slug, category, period, observation_type, observation_unit (UNIT_TYPES)
@@ -41,6 +43,10 @@ def _make_missing_metrics(self, u):
for proj in applicable_projs:
m = Metric.objects.create(project=proj, unit=u)
+ def _find_missing_dates(self, input_dates):
+ input_dates = list(input_dates)
+ date_set = set(input_dates[0]+timedelta(x) for x in range((input_dates[-1]-input_dates[0]).days))
+ return sorted(date_set-set(input_dates))
def handle(self, *args, **options):
verbosity = int(options.get('verbosity'))
@@ -80,11 +86,15 @@ def handle(self, *args, **options):
self.stderr.write("Skipping '{metric}' on {project}\n".format(metric=ratio['antecedent'], project=project))
continue
ratio_metric = Metric.objects.get(project=project, unit=u) # We already searched for it then created it
- antecedent_class = antecedent_metric.unit.observation_type.model_class()
- consequent_class = consequent_metric.unit.observation_type.model_class()
+ # antecedent_class = antecedent_metric.unit.observation_type.model_class()
+ # consequent_class = consequent_metric.unit.observation_type.model_class()
- antecedent_observations = antecedent_class.objects.filter(metric=antecedent_metric).order_by('from_datetime', 'to_datetime')
- consequent_observations = consequent_class.objects.filter(metric=consequent_metric).order_by('from_datetime', 'to_datetime')
+ existing_ratio_obs_dates = ratio_metric.related_observations.order_by('from_datetime', 'to_datetime').dates('to_datetime', 'day')
+ antecedent_observations = antecedent_metric.related_observations.order_by('from_datetime', 'to_datetime')
+ antecedent_obs_dates = antecedent_observations.dates('to_datetime', 'day')
+ missing_dates = list(set(ao for ao in antecedent_obs_dates).difference(ro for ro in existing_ratio_obs_dates))
+ antecedent_observations = antecedent_metric.related_observations.filter(from_datetime__in=missing_dates)
+ consequent_observations = consequent_metric.related_observations.filter(from_datetime__in=missing_dates)
# FIXME This doesn't calculate new ratioobservations for each time frame...
for a_obs in antecedent_observations:
if verbosity >= 2:

0 comments on commit af77844

Please sign in to comment.