Пример #1
0
def follower_detail(request, screen_name, json=False):
    follower = Follower.all().filter("screen_name = ", screen_name.lower()).get()
    if follower:
        dp_of_follower = Datapoint.all().filter("follower = ", follower)
        if json:
            data = []
            current_value = {}
            display_metrics = set()
            recent_metrics = dp_of_follower.order("-created_at").fetch(500)
            recent_metrics.reverse()
            for datapoint in recent_metrics:
                display_metrics.add(datapoint.metric)
                current_value[datapoint.metric] = datapoint.numerical
                data.append(current_value.copy())

            description = {}
            for metric_name in display_metrics:
                description[metric_name] = ("number", metric_name)

            data_table = gviz_api.DataTable(description)
            data_table.LoadData(data)
            json = data_table.ToJSonResponse()

            return HttpResponse(json, mimetype="application/json")
        else:
            last_metrics = {}
            for metric in follower.metrics:
                last = (
                    Datapoint.all()
                    .filter("follower = ", follower)
                    .filter("metric = ", metric)
                    .order("-created_at")
                    .get()
                )
                last_metrics[metric] = {
                    "created_at": last.created_at.isoformat(),
                    "numerical": last.numerical,
                    "annotation": last.annotation,
                }

            return direct_to_template(
                request,
                "graph/follower_detail.html",
                extra_context={"object": follower, "last_metrics": simplejson.dumps(last_metrics)},
            )
    else:
        return direct_to_template(request, "graph/follower_404.html", extra_context={"screen_name": screen_name})
Пример #2
0
def follower_metric_detail(request, screen_name, metric):
    follower = Follower.all().filter("screen_name = ", screen_name.lower()).get()
    if follower:
        return direct_to_template(
            request,
            "graph/follower_metric_detail.html",
            extra_context={
                "metric": metric,
                "follower": follower,
                "not_much_data": Datapoint.all()
                .filter("follower =", follower)
                .filter("metric = ", metric.lower())
                .order("created_at")
                .count(5)
                < 3,
                "most_recent": Datapoint.all()
                .filter("follower =", follower)
                .filter("metric = ", metric.lower())
                .order("created_at")
                .get(),
            },
        )
    else:
        return direct_to_template(request, "graph/follower_404.html", extra_context={"metric": metric})
Пример #3
0
def process_messages(request):
    most_recent = Datapoint.all().order("-created_at").get()
    url = "https://twitter.com/direct_messages.json?since_id=%u" % (most_recent.message_id if most_recent else 0)
    install_opener()
    response = simplejson.load(urllib2.urlopen(url))

    for tweet in response:
        dp = dict(
            message_id=tweet["id"],
            sender_screen_name=tweet["sender_screen_name"].lower(),
            created_at=datetime.strptime(tweet["created_at"], "%a %b %d %H:%M:%S +0000 %Y"),
        )

        (metric, sep, value) = tweet["text"].partition(":")
        if sep != ":":
            logging.warn("Tweet " + str(tweet) + " improperly formatted")
            continue

        dp["metric"] = metric.lower()

        match = re.match("-?\d+(\.\d+)?", value.lstrip())
        if match:
            dp["numerical"] = float(match.group())
            dp["annotation"] = value.lstrip()[match.end() :].strip()
        else:
            logging.warn("Tweet " + str(tweet) + " improperly formatted")
            continue

        follower = Follower.get_or_insert(
            str(tweet["sender"]["id"]),
            use_id=tweet["sender"]["id"],
            screen_name=tweet["sender_screen_name"].lower(),
            metrics=[dp["metric"]],
        )
        if dp["metric"] not in follower.metrics:
            follower.metrics.append(dp["metric"])
            follower.put()
        dp["follower"] = follower

        Datapoint(**dp).put()

    return direct_to_template(request, "graph/messages.html", extra_context={"response_json": response})
Пример #4
0
def follower_metric_detail_json(request, screen_name, metric):
    follower = Follower.all().filter("screen_name = ", screen_name.lower()).get()
    if follower:
        description = {
            "created_at": ("datetime", "date"),
            "numerical": ("number", metric),
            "title1": ("string", "Annotation"),
        }
        data = []
        for datapoint in (
            Datapoint.all().filter("follower =", follower).filter("metric = ", metric.lower()).order("created_at")
        ):
            data.append(
                {"created_at": datapoint.created_at, "numerical": datapoint.numerical, "title1": datapoint.annotation}
            )

        data_table = gviz_api.DataTable(description)
        data_table.LoadData(data)
        json = data_table.ToJSonResponse(columns_order=("created_at", "numerical", "title1"))

        return HttpResponse(json, mimetype="application/json")
    else:
        raise Http404
Пример #5
0
def insert_test_messages(request, months):
    if int(months) < 0:
        logging.info("trying to delete every datapoint")
        db.delete(Datapoint.all(keys_only=True).fetch(5000))

    update = datetime.now() - timedelta(7)
    mid = 0
    follower = Follower.all().filter("screen_name = ", "jimblomo").get()
    while update > datetime.now() - timedelta(int(months) * 30):
        Datapoint(
            message_id=mid,
            sender_screen_name="jimblomo",
            follower=follower,
            metric="test",
            numerical=float(random.randrange(1, 10)),
            created_at=update,
            annotation="annotation" if random.randrange(1, 10) % 7 == 0 else "",
        ).put()
        update -= timedelta(1)
        mid += 1
    return redirect_to(
        request, reverse("graph.views.follower_metric_detail", kwargs=dict(screen_name="jimblomo", metric="test"))
    )
Пример #6
0
def show_datapoint(request, key):
    return object_detail(request, Datapoint.all(), key)