# Exporting tasks

> Export tasks and assessments from the Tidal Control API with activities_paged and an activityTypes filter, using inline fragments for assessment-only fields.

Source: https://tidalcontrol.com/docs/api/tasks

Tasks (executions) and assessments both use the `activities_paged` query with an `activityTypes` filter. Both types share the `Activity` interface, which provides all common fields. Use an inline fragment (`... on Assessment`) to access assessment-specific fields like `effectiveness`.

## Basic query: tasks (executions)

```graphql
query ExportTasks($first: Int, $after: String, $filter: ActivityFilter) {
  activities_paged(first: $first, after: $after, filter: $filter) {
    edges {
      node {
        id
        sequenceId
        name
        description
        notBefore
        expires
        closedDate
        reviewedDate
        createdDate
        plan {
          id
          name
        }
        controls {
          id
          sequenceId
          name
        }
        assets {
          id
          name
        }
        assignments {
          assignmentType
          user {
            name
            email
          }
        }
      }
    }
    pageInfo {
      hasNextPage
      endCursor
    }
  }
}
```

Variables to fetch the first page of tasks:

```json
{ "first": 50, "after": null, "filter": { "activityTypes": ["EXECUTE"] } }
```

## Key fields

- `sequenceId`: the human-readable number shown in the UI
- `closedDate`: set when the task is completed; `null` if still open
- `reviewedDate`: set when the task has been reviewed by a reviewer
- `plan`: the Plan that generated this task

## Complete export script (Python)

This script authenticates, pages through every task record, and writes the result to a CSV file:

```python

GRAPHQL_URL = "https://portal.tidalcontrol.com/graphql"

QUERY = """
query ExportTasks($first: Int, $after: String, $filter: ActivityFilter) {
  activities_paged(first: $first, after: $after, filter: $filter) {
    edges {
      node {
        id
        sequenceId
        name
        description
        notBefore
        expires
        closedDate
        reviewedDate
        createdDate
        plan { id name }
        controls { id sequenceId name }
        assets { id name }
        assignments { assignmentType user { name email } }
      }
    }
    pageInfo { hasNextPage endCursor }
  }
}
"""

def graphql(query, variables, access_token):
    r = requests.post(
        GRAPHQL_URL,
        json={"query": query, "variables": variables},
        headers={"Authorization": f"Bearer {access_token}"},
    )
    r.raise_for_status()
    result = r.json()
    if "errors" in result:
        raise RuntimeError(result["errors"])
    return result["data"]

def export_tasks(access_token, closed=None, assignee_ids=None):
    tasks = []
    cursor = None
    task_filter = {"activityTypes": ["EXECUTE"]}
    if closed is not None:
        task_filter["closed"] = closed
    if assignee_ids:
        task_filter["assignees"] = assignee_ids

    while True:
        data = graphql(
            QUERY,
            {"first": 50, "after": cursor, "filter": task_filter},
            access_token,
        )
        page = data["activities_paged"]
        tasks.extend(edge["node"] for edge in page["edges"])

        if not page["pageInfo"]["hasNextPage"]:
            break
        cursor = page["pageInfo"]["endCursor"]

    return tasks

def to_csv(tasks, output_path):
    if not tasks:
        print("No tasks found.")
        return

    with open(output_path, "w", newline="", encoding="utf-8") as f:
        writer = csv.writer(f)
        writer.writerow([
            "ID", "Sequence ID", "Name", "Description",
            "Not Before", "Expires", "Created Date",
            "Closed Date", "Reviewed Date",
            "Plan", "Linked Controls", "Linked Assets",
            "Owners", "Executors",
        ])
        for t in tasks:
            owners = [
                a["user"]["email"]
                for a in t["assignments"]
                if a["assignmentType"] == "OWNER"
            ]
            executors = [
                a["user"]["email"]
                for a in t["assignments"]
                if a["assignmentType"] == "EXECUTOR"
            ]
            writer.writerow([
                t["id"],
                t["sequenceId"],
                t["name"],
                t.get("description", ""),
                t.get("notBefore", ""),
                t.get("expires", ""),
                t.get("createdDate", ""),
                t.get("closedDate", "") or "",
                t.get("reviewedDate", "") or "",
                t["plan"]["name"] if t.get("plan") else "",
                ", ".join(c["name"] for c in t["controls"]),
                ", ".join(a["name"] for a in t["assets"]),
                ", ".join(owners),
                ", ".join(executors),
            ])

    print(f"Exported {len(tasks)} tasks to {output_path}")

if __name__ == "__main__":
    token = get_token()
    access_token = token["access_token"]

    tasks = export_tasks(access_token, closed=False)
    to_csv(tasks, "tasks_export.csv")
```

## Filtering examples

Pass a `filter` argument to narrow the export. These are the combinations asked for most often:

**Only open tasks:**

```json
{ "filter": { "activityTypes": ["EXECUTE"], "closed": false } }
```

**Overdue tasks:**

```json
{ "filter": { "activityTypes": ["EXECUTE"], "status": ["OVERDUE"] } }
```

**Tasks due soon:**

```json
{ "filter": { "activityTypes": ["EXECUTE"], "status": ["DUE_SOON"] } }
```

**Tasks linked to a specific control:**

```json
{ "filter": { "activityTypes": ["EXECUTE"], "controls": ["control-uuid-here"] } }
```

**Tasks assigned to a specific user:**

```json
{ "filter": { "activityTypes": ["EXECUTE"], "assignees": ["user-uuid-here"] } }
```

## Exporting assessments

Assessments use the same `activities_paged` query with `activityTypes: [ASSESS]`. Use the `... on Assessment` inline fragment to access the `effectiveness` field, which is specific to assessments.

```graphql
query ExportAssessments($first: Int, $after: String, $filter: ActivityFilter) {
  activities_paged(first: $first, after: $after, filter: $filter) {
    edges {
      node {
        id
        sequenceId
        name
        closedDate
        createdDate
        controls { id name }
        assets { id name }
        assignments { assignmentType user { name email } }
        ... on Assessment {
          effectiveness
        }
      }
    }
    pageInfo {
      hasNextPage
      endCursor
    }
  }
}
```

Variables:

```json
{ "first": 50, "after": null, "filter": { "activityTypes": ["ASSESS"] } }
```

The `effectiveness` field on assessments is: `UNDETERMINED`, `INEFFECTIVE`, or `EFFECTIVE`.

## Count only

To check how many tasks exist before exporting the full set:

```graphql
query {
  tasks_open: activities_count(filter: { closed: false, activityTypes: [EXECUTE] })
  tasks_closed: activities_count(filter: { closed: true, activityTypes: [EXECUTE] })
  assessments_open: activities_count(filter: { closed: false, activityTypes: [ASSESS] })
  assessments_closed: activities_count(filter: { closed: true, activityTypes: [ASSESS] })
}
```
