<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">def load_sql() -&gt; str:
    """Load sql template.

    Sessions table structure:
    session_id int,
    project_id int,
    task_id int,
    user_id int,
    action varchar,
    start_time timestamp,
    end_time timestamp

    """

    sql = """
        select
          project_id
          ,task_id
          ,user_id
          ,Array_Agg(action) as action_list
          ,Array_Agg(action_text) as action_text_list
          ,Array_Agg(action_date) as action_data_list
        from
        (SELECT * FROM data_preparation.task_history
         WHERE
          project_id = '5871'
          AND
          (
          action_text in
              ('MAPPED',
               'VALIDATED',
               'BADIMAGERY',
               'READY',
               'INVALIDATED',
               'SPLIT')
          OR
          action in
              ('LOCKED_FOR_VALIDATION',
               'LOCKED_FOR_MAPPING',
               'AUTO_UNLOCKED_FOR_MAPPING',
               'AUTO_UNLOCKED_FOR_VALIDATION')
          )
         ORDER BY project_id, task_id, action_date
        ) as foo
        GROUP BY
          project_id
          ,task_id
          ,user_id
        ORDER BY project_id, task_id
        limit 1000
    """

    """

    general approach:

    * check for action, action text
    * is this the start of an action?
        * if yes:
            * set session_id, project_id, task_id, user_id, start_time
            * check next entry:
                * if this the end of an action:
                    * if yes:
                        * set end_time using action_date
                        * skip next entry (or remove both entries from list)
                    * if no: set end_time using
                        (a) action date of the next entry for this task or
                        (b) start_time + 2 hours if not conflicting with action date of next entry for this task

        * if no:
            * is this the end of an action?
                * if yes:
                    * set session_id, project_id, task_id, user_id, end_time
                    * check previous entry:
                        * set start_time using end_time - 2 hours if not conflicting with action date of previous entry for this task


                * if no:
                    skip



    """

    return sql
</pre></body></html>