You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2020/08/25 01:35:28 UTC

[GitHub] [airflow] mik-laj commented on a change in pull request #9597: [WIP] Add read-only endpoints for task instances

mik-laj commented on a change in pull request #9597:
URL: https://github.com/apache/airflow/pull/9597#discussion_r476043263



##########
File path: airflow/api_connexion/schemas/task_instance_schema.py
##########
@@ -0,0 +1,147 @@
+# 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.
+
+from typing import List, NamedTuple
+
+from marshmallow import Schema, fields, ValidationError, post_load
+from marshmallow_sqlalchemy import SQLAlchemySchema, auto_field
+from sqlalchemy import and_
+
+from airflow.api_connexion.schemas.enum_schemas import TaskInstanceStateField
+from airflow.api_connexion.schemas.sla_miss_schema import SlaMissSchema
+from airflow.models import DagRun, TaskInstance, SlaMiss
+from airflow.utils.session import create_session
+
+
+class TaskInstanceSchema(Schema):
+    """Task instance schema"""
+
+    task_id = fields.Str()
+    dag_id = fields.Str()
+    execution_date = fields.DateTime()
+    start_date = fields.DateTime()
+    end_date = fields.DateTime()
+    duration = fields.Float()
+    state = TaskInstanceStateField()
+    _try_number = fields.Int(data_key="try_number")
+    max_tries = fields.Int()
+    hostname = fields.Str()
+    unixname = fields.Str()
+    pool = fields.Str()
+    pool_slots = fields.Int()
+    queue = fields.Str()
+    priority_weight = fields.Int()
+    operator = fields.Str()
+    queued_dttm = fields.DateTime(data_key="queued_when")
+    pid = fields.Int()
+    executor_config = fields.Str()
+    sla_miss = fields.Method("get_sla_miss")
+
+    @staticmethod
+    def get_sla_miss(obj: TaskInstance):
+        with create_session() as session:
+            sla_miss = session.query(SlaMiss).filter(

Review comment:
       Here we have an n+1 problem. I think we can fetch it much more efficiently if we use the more advanced features of SQLAlchemy.
   https://docs.sqlalchemy.org/en/13/orm/loading_relationships.html




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org