'Returns the latest DagRun for each DAG.'

@classmethod
@provide_session
def get_latest_runs(cls, session):

    subquery = session.query(cls.dag_id, func.max(cls.execution_date).label(u'execution_date')).group_by(cls.dag_id).subquery()
    dagruns = session.query(cls).join(subquery, and_((cls.dag_id == subquery.c.dag_id), (cls.execution_date == subquery.c.execution_date))).all()
    return dagruns
