Exemple #1
0
def process_events(h, clusters):
    """
    Takes clusters of node uuids and
    builds, modifies, and deletes events out of them.
    """
    now = datetime.utcnow()

    # Get existing event clusters.
    event_map = {}
    existing  = {}
    for e in Event.all_active():
        # Map event ids to their event, for lookup later.
        event_map[e.id] = e

        # Map event ids to a list of their member node ids.
        existing[e.id]  = [a.node_id for a in e.articles]

    # Figure out which events to update, delete, and create.
    to_update, to_create, to_delete, unchanged = triage(existing, clusters)

    for a_ids in to_create:
        articles = Article.query.filter(Article.node_id.in_([id.item() for id in a_ids])).order_by(Article.created_at.desc()).all()
        e = Event(articles)

        e.created_at = articles[0].created_at
        e.updated_at = articles[-1].updated_at

        rep_article = representative_article(h, a_ids, articles)
        e.title = rep_article.title
        e.image = rep_article.image

        db.session.add(e)

    for e_id, a_ids in to_update.items():
        e = event_map[e_id]
        articles = Article.query.filter(Article.node_id.in_([id.item() for id in a_ids])).all()
        e.members = articles

        rep_article = representative_article(h, a_ids, articles)
        e.title = rep_article.title
        e.image = rep_article.image

        e.update()

    # Freeze expiring events and clean up their articles from the hierarchy.
    for e_id in unchanged:
        e = event_map[e_id]
        if (now - e.updated_at).days > 3:
            e.active = False
            nodes = [h.to_iid(a.node_id) for a in e.articles]
            h.prune(nodes)

    # Do this LAST so any of this event's associated articles
    # have a chance to be moved to their new clusters (if any).
    for e_id in to_delete:
        db.session.delete(event_map[e_id])
        # does this need to prune the articles as well?
        # i think the assumption is that a deleted event's articles have all migrated elsewhere.

    db.session.commit()