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/28 02:28:50 UTC

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

OmairK commented on a change in pull request #9597:
URL: https://github.com/apache/airflow/pull/9597#discussion_r478796025



##########
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:
       Hey @mik-laj we are not using relationships in the model so I was planning to use explicit joins to overcome this n + 1 problem the only downside of that it is returns a tuple of object `(<task instance>,<sla miss>)` so I was planning to overwrite [`get_attribute`](https://marshmallow.readthedocs.io/en/stable/api_reference.html#marshmallow.Schema.get_attribute). Let me know if this sounds like a good idea. I will be doing something similar here 
   ```
   class TaskInstanceReferenceSchema(Schema):
       """Schema for the task instance reference schema"""
       task_id = fields.Str()
       dag_run_id = fields.Method('get_run_id')
       dag_id = fields.Str()
       execution_date = fields.DateTime()
   
       @staticmethod
       def get_run_id(obj: TaskInstance):
           with create_session() as session:
               return session.query(DagRun).filter(DagRun.dag_id==obj.dag_id, DagRun.execution_date==obj.execution_date).one_or_none().run_id
   ```




----------------------------------------------------------------
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