-
Notifications
You must be signed in to change notification settings - Fork 14.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[AIRFLOW-3561] Improve queries #4368
Changes from all commits
bed94ae
83e9b7f
8ee3210
acf194a
0d6ebc3
fb1539d
48ba0c2
77a73c7
3c04289
5189257
0b2d538
c14bf93
3579995
f3bec4f
bf4e6dd
4cffec2
bcacbf5
4db22b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
"""add fields to dag | ||
|
||
Revision ID: c8ffec048a3b | ||
Revises: 41f5f12752f8 | ||
Create Date: 2018-12-23 21:55:46.463634 | ||
|
||
""" | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = 'c8ffec048a3b' | ||
down_revision = '41f5f12752f8' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
def upgrade(): | ||
op.add_column('dag', sa.Column('description', sa.Text(), nullable=True)) | ||
op.add_column('dag', sa.Column('default_view', sa.String(25), nullable=True)) | ||
|
||
|
||
def downgrade(): | ||
op.drop_column('dag', 'description') | ||
op.drop_column('dag', 'default_view') |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2999,15 +2999,29 @@ class DagModel(Base): | |
fileloc = Column(String(2000)) | ||
# String representing the owners | ||
owners = Column(String(2000)) | ||
# Description of the dag | ||
description = Column(Text) | ||
# Default view of the inside the webserver | ||
default_view = Column(String(25)) | ||
|
||
def __repr__(self): | ||
return "<DAG: {self.dag_id}>".format(self=self) | ||
|
||
@property | ||
def timezone(self): | ||
return settings.TIMEZONE | ||
|
||
@classmethod | ||
@provide_session | ||
def get_current(cls, dag_id, session=None): | ||
return session.query(cls).filter(cls.dag_id == dag_id).first() | ||
|
||
def get_default_view(self): | ||
if self.default_view is None: | ||
return configuration.conf.get('webserver', 'dag_default_view').lower() | ||
else: | ||
return self.default_view | ||
|
||
|
||
@functools.total_ordering | ||
class DAG(BaseDag, LoggingMixin): | ||
|
@@ -3109,7 +3123,7 @@ def __init__( | |
'core', 'max_active_runs_per_dag'), | ||
dagrun_timeout=None, | ||
sla_miss_callback=None, | ||
default_view=configuration.conf.get('webserver', 'dag_default_view').lower(), | ||
default_view=None, | ||
orientation=configuration.conf.get('webserver', 'dag_orientation'), | ||
catchup=configuration.conf.getboolean('scheduler', 'catchup_by_default'), | ||
on_success_callback=None, on_failure_callback=None, | ||
|
@@ -3180,7 +3194,7 @@ def __init__( | |
self.max_active_runs = max_active_runs | ||
self.dagrun_timeout = dagrun_timeout | ||
self.sla_miss_callback = sla_miss_callback | ||
self.default_view = default_view | ||
self._default_view = default_view | ||
self.orientation = orientation | ||
self.catchup = catchup | ||
self.is_subdag = False # DagBag.bag_dag() will set this to True if appropriate | ||
|
@@ -3249,6 +3263,13 @@ def __exit__(self, _type, _value, _tb): | |
|
||
# /Context Manager ---------------------------------------------- | ||
|
||
def get_default_view(self): | ||
"""This is only there for backward compatible jinja2 templates""" | ||
if self._default_view is None: | ||
return configuration.conf.get('webserver', 'dag_default_view').lower() | ||
else: | ||
return self._default_view | ||
|
||
def date_range(self, start_date, num=None, end_date=timezone.utcnow()): | ||
if num: | ||
end_date = None | ||
|
@@ -4201,6 +4222,8 @@ def sync_to_db(self, owner=None, sync_time=None, session=None): | |
orm_dag.owners = owner | ||
orm_dag.is_active = True | ||
orm_dag.last_scheduler_run = sync_time | ||
orm_dag.default_view = self._default_view | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we test this in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good one, added a test |
||
orm_dag.description = self.description | ||
session.merge(orm_dag) | ||
session.commit() | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -550,31 +550,29 @@ def chart(self): | |
@login_required | ||
@provide_session | ||
def dag_stats(self, session=None): | ||
ds = models.DagStat | ||
dr = models.DagRun | ||
dm = models.DagModel | ||
dag_ids = session.query(dm.dag_id) | ||
|
||
ds.update( | ||
dag_ids=[dag.dag_id for dag in dagbag.dags.values() if not dag.is_subdag] | ||
) | ||
|
||
qry = ( | ||
session.query(ds.dag_id, ds.state, ds.count) | ||
) | ||
dag_state_stats = session.query(dr.dag_id, dr.state, sqla.func.count(dr.state)).group_by(dr.dag_id, dr.state) | ||
|
||
data = {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe give this a bit more meaningful name? Something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
for dag_id, state, count in qry: | ||
for (dag_id, ) in dag_ids: | ||
data[dag_id] = {} | ||
for dag_id, state, count in dag_state_stats: | ||
if dag_id not in data: | ||
data[dag_id] = {} | ||
data[dag_id][state] = count | ||
|
||
payload = {} | ||
for dag in dagbag.dags.values(): | ||
payload[dag.safe_dag_id] = [] | ||
for dag_id, d in data.items(): | ||
payload[dag_id] = [] | ||
for state in State.dag_states: | ||
count = data.get(dag.dag_id, {}).get(state, 0) | ||
payload[dag.safe_dag_id].append({ | ||
count = d.get(state, 0) | ||
payload[dag_id].append({ | ||
'state': state, | ||
'count': count, | ||
'dag_id': dag.dag_id, | ||
'dag_id': dag_id, | ||
'color': State.color(state) | ||
}) | ||
return wwwutils.json_response(payload) | ||
|
@@ -587,6 +585,8 @@ def task_stats(self, session=None): | |
DagRun = models.DagRun | ||
Dag = models.DagModel | ||
|
||
dag_ids = session.query(Dag.dag_id) | ||
|
||
LastDagRun = ( | ||
session.query(DagRun.dag_id, sqla.func.max(DagRun.execution_date).label('execution_date')) | ||
.join(Dag, Dag.dag_id == DagRun.dag_id) | ||
|
@@ -634,24 +634,25 @@ def task_stats(self, session=None): | |
session.commit() | ||
|
||
payload = {} | ||
for dag in dagbag.dags.values(): | ||
payload[dag.safe_dag_id] = [] | ||
for (dag_id, ) in dag_ids: | ||
payload[dag_id] = [] | ||
for state in State.task_states: | ||
count = data.get(dag.dag_id, {}).get(state, 0) | ||
payload[dag.safe_dag_id].append({ | ||
count = data.get(dag_id, {}).get(state, 0) | ||
payload[dag_id].append({ | ||
'state': state, | ||
'count': count, | ||
'dag_id': dag.dag_id, | ||
'dag_id': dag_id, | ||
'color': State.color(state) | ||
}) | ||
return wwwutils.json_response(payload) | ||
|
||
@expose('/code') | ||
@login_required | ||
def code(self): | ||
@provide_session | ||
def code(self, session=None): | ||
dag_id = request.args.get('dag_id') | ||
dag = dagbag.get_dag(dag_id) | ||
title = dag_id | ||
dm = models.DagModel | ||
dag = session.query(dm).filter(dm.dag_id == dag_id).first() | ||
try: | ||
with wwwutils.open_maybe_zipped(dag.fileloc, 'r') as f: | ||
code = f.read() | ||
|
@@ -661,7 +662,7 @@ def code(self): | |
html_code = str(e) | ||
|
||
return self.render( | ||
'airflow/dag_code.html', html_code=html_code, dag=dag, title=title, | ||
'airflow/dag_code.html', html_code=html_code, dag=dag, title=dag_id, | ||
root=request.args.get('root'), | ||
demo_mode=conf.getboolean('webserver', 'demo_mode')) | ||
|
||
|
@@ -941,8 +942,11 @@ def xcom(self, session=None): | |
execution_date = request.args.get('execution_date') | ||
dttm = pendulum.parse(execution_date) | ||
form = DateTimeForm(data={'execution_date': dttm}) | ||
dag = dagbag.get_dag(dag_id) | ||
if not dag or task_id not in dag.task_ids: | ||
dm_db = models.DagModel | ||
ti_db = models.TaskInstance | ||
dag = session.query(dm_db).filter(dm_db.dag_id == dag_id).first() | ||
ti = session.query(ti_db).filter(ti_db.dag_id == dag_id and ti_db.task_id == task_id).first() | ||
if not ti: | ||
flash( | ||
"Task [{}.{}] doesn't seem to exist" | ||
" at the moment".format(dag_id, task_id), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait up - this looks incorrect. Why was this removed?
The timezone of a DAG was previously defined as the timezone of the start_date. Is that still the case and if we do
self.timezone = tz
then this property accessor won't be used anymore?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't mean removed, I mean added.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh this is on the DagModel, not the DAG. Right.
In which case my question is: when/where is this used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is indeed a bit tricky. I have added this because in the html templates this variable is used. Some templates now takes a DagModel instead of a DAG. In the end I would like this to be in the complete webserver