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()
def _create_dated_story(self): datetime_A = datetime.utcnow() - timedelta(days=1) datetime_B = datetime.utcnow() - timedelta(days=5) article_a = fac.article(title='The Illiad', text='The Illiad has Argos in it.') event_a = Event([article_a]) event_a.created_at = datetime_A article_b = fac.article(title='The Illiad', text='The Illiad has Argos in it.') event_b = Event([article_b]) event_b.created_at = datetime_B article_c = fac.article(title='The Illiad', text='The Illiad has Argos in it.') event_c = Event([article_c]) event_c.created_at = datetime_A story = Story([event_a, event_b, event_c]) self.db.session.add(story) self.db.session.commit() return story, datetime_A, datetime_B