{
  "$schema": "./.schemas/page.schema.json",
  "title": "Paperless",
  "icon": "doc.text",
  "sources": {
    "server": {
      "url": "http://paperless.local:8000",
      "secret": "paperless",
      "auth": {
        "type": "header",
        "name": "Authorization",
        "value": "Token {secret}"
      },
      "every": "5m",
      "headers": {
        "Accept": "application/json; version=10"
      },
      "requests": {
        "status": "/api/status/",
        "stats": "/api/statistics/",
        "latest": "/api/remote_version/",
        "month": "/api/documents/?added__date__gte={date.month_start}&page_size=1&fields=id",
        "failed": "/api/tasks/?task_type=consume_file&status=failure&acknowledged=false&page_size=10"
      },
      "derive": [
        {
          "name": "correspondents",
          "value": {
            "if": {
              "path": "stats.correspondent_count",
              "equals": 1
            },
            "then": {
              "template": "1 correspondent"
            },
            "else": {
              "template": "{stats.correspondent_count} correspondents"
            }
          }
        },
        {
          "name": "tags",
          "value": {
            "if": {
              "path": "stats.tag_count",
              "equals": 1
            },
            "then": {
              "template": "1 tag"
            },
            "else": {
              "template": "{stats.tag_count} tags"
            }
          }
        },
        {
          "name": "types",
          "value": {
            "if": {
              "path": "stats.document_type_count",
              "equals": 1
            },
            "then": {
              "template": "1 type"
            },
            "else": {
              "template": "{stats.document_type_count} types"
            }
          }
        },
        {
          "name": "errors",
          "value": {
            "add": [
              {
                "path": "status.database.status",
                "values": {
                  "ERROR": 1
                },
                "default": 0
              },
              {
                "path": "status.tasks.redis_status",
                "values": {
                  "ERROR": 1
                },
                "default": 0
              },
              {
                "path": "status.tasks.celery_status",
                "values": {
                  "ERROR": 1
                },
                "default": 0
              },
              {
                "path": "status.tasks.index_status",
                "values": {
                  "ERROR": 1
                },
                "default": 0
              },
              {
                "path": "status.tasks.classifier_status",
                "values": {
                  "ERROR": 1
                },
                "default": 0
              },
              {
                "path": "status.tasks.sanity_check_status",
                "values": {
                  "ERROR": 1
                },
                "default": 0
              }
            ],
            "default": 0
          }
        },
        {
          "name": "celery_quiet",
          "value": {
            "path": "status.tasks.celery_status",
            "values": {
              "WARNING": 1
            },
            "default": 0
          }
        },
        {
          "name": "failed_count",
          "value": {
            "path": "failed.count",
            "default": 0
          }
        },
        {
          "name": "failed_any",
          "value": {
            "path": "failed_count",
            "values": {
              "0": 0
            },
            "default": 1
          }
        },
        {
          "name": "problems",
          "value": {
            "add": [
              "errors",
              "celery_quiet",
              "failed_any"
            ],
            "default": 0
          }
        },
        {
          "name": "problems_more",
          "value": {
            "subtract": [
              "problems",
              1
            ]
          }
        },
        {
          "name": "failed_words",
          "value": {
            "if": {
              "path": "failed_count",
              "equals": 1
            },
            "then": {
              "template": "1 file didn't import"
            },
            "else": {
              "template": "{failed_count} files didn't import"
            }
          }
        },
        {
          "name": "worst",
          "value": {
            "if": {
              "path": "errors",
              "gt": 0
            },
            "then": {
              "if": {
                "path": "status.database.status",
                "equals": "ERROR"
              },
              "then": {
                "if": {
                  "path": "status.database.error",
                  "exists": true
                },
                "then": {
                  "template": "Database: {status.database.error}"
                },
                "else": {
                  "template": "Database error"
                }
              },
              "else": {
                "if": {
                  "path": "status.tasks.redis_status",
                  "equals": "ERROR"
                },
                "then": {
                  "if": {
                    "path": "status.tasks.redis_error",
                    "exists": true
                  },
                  "then": {
                    "template": "Redis: {status.tasks.redis_error}"
                  },
                  "else": {
                    "template": "Redis error"
                  }
                },
                "else": {
                  "if": {
                    "path": "status.tasks.celery_status",
                    "equals": "ERROR"
                  },
                  "then": {
                    "if": {
                      "path": "status.tasks.celery_error",
                      "exists": true
                    },
                    "then": {
                      "template": "Celery: {status.tasks.celery_error}"
                    },
                    "else": {
                      "template": "Celery error"
                    }
                  },
                  "else": {
                    "if": {
                      "path": "status.tasks.index_status",
                      "equals": "ERROR"
                    },
                    "then": {
                      "if": {
                        "path": "status.tasks.index_error",
                        "exists": true
                      },
                      "then": {
                        "template": "Search index: {status.tasks.index_error}"
                      },
                      "else": {
                        "template": "Search index error"
                      }
                    },
                    "else": {
                      "if": {
                        "path": "status.tasks.classifier_status",
                        "equals": "ERROR"
                      },
                      "then": {
                        "if": {
                          "path": "status.tasks.classifier_error",
                          "exists": true
                        },
                        "then": {
                          "template": "Classifier: {status.tasks.classifier_error}"
                        },
                        "else": {
                          "template": "Classifier error"
                        }
                      },
                      "else": {
                        "if": {
                          "path": "status.tasks.sanity_check_error",
                          "exists": true
                        },
                        "then": {
                          "template": "Sanity check: {status.tasks.sanity_check_error}"
                        },
                        "else": {
                          "template": "Sanity check error"
                        }
                      }
                    }
                  }
                }
              }
            },
            "else": {
              "if": {
                "path": "celery_quiet",
                "equals": 1
              },
              "then": {
                "if": {
                  "path": "status.tasks.celery_error",
                  "exists": true
                },
                "then": {
                  "template": "Celery: {status.tasks.celery_error}"
                },
                "else": {
                  "template": "No Celery worker answered"
                }
              },
              "else": {
                "template": "{failed_words}"
              }
            }
          }
        },
        {
          "name": "failed_rows",
          "value": {
            "each": "failed.results",
            "fields": {
              "key": {
                "template": "{id|raw}"
              },
              "id": "id",
              "text": {
                "if": {
                  "path": "input_data.filename",
                  "exists": true
                },
                "then": {
                  "template": "{input_data.filename}"
                },
                "else": {
                  "template": "A file"
                }
              },
              "subtitle": {
                "if": {
                  "path": "result_data.error_message",
                  "exists": true
                },
                "then": {
                  "template": "{result_data.error_message|clip:90}"
                }
              },
              "detail": {
                "template": "{date_done|ago}"
              }
            }
          }
        },
        {
          "name": "library_words",
          "value": {
            "template": "{correspondents} · {tags} · {types}"
          }
        }
      ]
    },
    "library": {
      "url": "http://paperless.local:8000",
      "secret": "paperless",
      "auth": {
        "type": "header",
        "name": "Authorization",
        "value": "Token {secret}"
      },
      "every": "5m",
      "headers": {
        "Accept": "application/json; version=10"
      },
      "requests": {
        "stats": "/api/statistics/",
        "inbox": "/api/documents/?is_in_inbox=true&ordering=-added&page_size=25&fields=id,title,correspondent,document_type,added",
        "recent": "/api/documents/?ordering=-added&page_size=4&fields=id,title,correspondent,document_type,added",
        "names": "/api/correspondents/?ordering=-document_count&page_size=1000",
        "types": "/api/document_types/?ordering=-document_count&page_size=1000"
      },
      "derive": [
        {
          "name": "inbox_c",
          "value": {
            "each": "inbox.results",
            "lookup": {
              "from": "names.results",
              "by": "id",
              "on": "correspondent",
              "as": "c"
            },
            "fields": {
              "id": "id",
              "title": "title",
              "added": "added",
              "dt": "document_type",
              "cname": "c.name"
            }
          }
        },
        {
          "name": "inbox_n",
          "value": {
            "each": "inbox_c",
            "lookup": {
              "from": "types.results",
              "by": "id",
              "on": "dt",
              "as": "d"
            },
            "fields": {
              "id": "id",
              "title": "title",
              "added": "added",
              "cname": "cname",
              "tname": "d.name"
            }
          }
        },
        {
          "name": "inbox_rows",
          "value": {
            "each": "inbox_n",
            "fields": {
              "key": {
                "template": "{id|raw}"
              },
              "text": {
                "if": {
                  "path": "title",
                  "exists": true
                },
                "then": {
                  "template": "{title}"
                },
                "else": {
                  "template": "Untitled"
                }
              },
              "subtitle": {
                "if": {
                  "path": "cname",
                  "exists": true
                },
                "then": {
                  "if": {
                    "path": "tname",
                    "exists": true
                  },
                  "then": {
                    "template": "{cname} · {tname}"
                  },
                  "else": {
                    "template": "{cname}"
                  }
                },
                "else": {
                  "if": {
                    "path": "tname",
                    "exists": true
                  },
                  "then": {
                    "template": "{tname}"
                  }
                }
              },
              "detail": {
                "template": "{added|ago}"
              },
              "url": {
                "template": "http://paperless.local:8000/documents/{id|raw}/details"
              }
            }
          }
        },
        {
          "name": "recent_c",
          "value": {
            "each": "recent.results",
            "lookup": {
              "from": "names.results",
              "by": "id",
              "on": "correspondent",
              "as": "c"
            },
            "fields": {
              "id": "id",
              "title": "title",
              "added": "added",
              "dt": "document_type",
              "cname": "c.name"
            }
          }
        },
        {
          "name": "recent_n",
          "value": {
            "each": "recent_c",
            "lookup": {
              "from": "types.results",
              "by": "id",
              "on": "dt",
              "as": "d"
            },
            "fields": {
              "id": "id",
              "title": "title",
              "added": "added",
              "cname": "cname",
              "tname": "d.name"
            }
          }
        },
        {
          "name": "recent_rows",
          "value": {
            "each": "recent_n",
            "fields": {
              "key": {
                "template": "{id|raw}"
              },
              "text": {
                "if": {
                  "path": "title",
                  "exists": true
                },
                "then": {
                  "template": "{title}"
                },
                "else": {
                  "template": "Untitled"
                }
              },
              "subtitle": {
                "if": {
                  "path": "cname",
                  "exists": true
                },
                "then": {
                  "if": {
                    "path": "tname",
                    "exists": true
                  },
                  "then": {
                    "template": "{cname} · {tname}"
                  },
                  "else": {
                    "template": "{cname}"
                  }
                },
                "else": {
                  "if": {
                    "path": "tname",
                    "exists": true
                  },
                  "then": {
                    "template": "{tname}"
                  }
                }
              },
              "detail": {
                "template": "{added|ago}"
              },
              "url": {
                "template": "http://paperless.local:8000/documents/{id|raw}/details"
              }
            }
          }
        },
        {
          "name": "shown_rows",
          "value": {
            "if": {
              "path": "stats.inbox_tag",
              "exists": true
            },
            "then": "inbox_rows",
            "else": "recent_rows"
          }
        },
        {
          "name": "shown_count",
          "value": {
            "if": {
              "path": "stats.inbox_tag",
              "exists": true
            },
            "then": "inbox.count",
            "else": "recent.count"
          }
        },
        {
          "name": "shown_empty",
          "value": {
            "if": {
              "path": "stats.inbox_tag",
              "exists": true
            },
            "then": {
              "template": "Inbox is empty"
            },
            "else": {
              "template": "No documents yet"
            }
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "health",
      "type": "status",
      "placement": "page_info",
      "label": "Paperless-ngx",
      "state": "unknown",
      "width": "full",
      "source": {
        "ref": "server",
        "map": {
          "state": {
            "if": {
              "path": "status.pngx_version",
              "exists": false
            },
            "then": {
              "template": "unknown"
            },
            "else": {
              "if": {
                "path": "errors",
                "gt": 0
              },
              "then": {
                "template": "critical"
              },
              "else": {
                "if": {
                  "path": "problems",
                  "gt": 0
                },
                "then": {
                  "template": "warning"
                },
                "else": {
                  "template": "ok"
                }
              }
            }
          },
          "detail": {
            "if": {
              "path": "status.pngx_version",
              "exists": false
            },
            "then": {
              "template": "Paperless didn't send its status"
            },
            "else": {
              "if": {
                "path": "problems",
                "equals": 0
              },
              "then": {
                "template": "Paperless-ngx {status.pngx_version} · No problems"
              },
              "else": {
                "if": {
                  "path": "problems",
                  "equals": 1
                },
                "then": {
                  "template": "{worst|clip:110}"
                },
                "else": {
                  "template": "{worst|clip:90} · and {problems_more} more"
                }
              }
            }
          }
        },
        "failed": {
          "state": "critical",
          "detail": "Paperless isn't sending its status",
          "after": 3
        }
      }
    },
    {
      "id": "inbox-count",
      "type": "stat",
      "label": "Inbox",
      "value": "—",
      "source": {
        "ref": "server",
        "map": {
          "value": {
            "if": {
              "path": "stats.documents_inbox",
              "exists": true
            },
            "then": "stats.documents_inbox",
            "else": {
              "template": "—"
            }
          }
        }
      }
    },
    {
      "id": "month",
      "type": "stat",
      "label": "Added this month",
      "value": "—",
      "source": {
        "ref": "server",
        "map": {
          "value": "month.count",
          "delta": {
            "template": "{stats.documents_total} in all"
          }
        }
      }
    },
    {
      "id": "server",
      "type": "facts",
      "label": "Server",
      "items": [
        {
          "key": "Storage",
          "value": "—"
        },
        {
          "key": "Library",
          "value": "—"
        },
        {
          "key": "Version",
          "value": "—"
        }
      ],
      "source": {
        "ref": "server",
        "map": {
          "items[0].value": {
            "template": "{status.storage.available|bytes} free of {status.storage.total|bytes}"
          },
          "items[1].value": "library_words",
          "items[2].value": {
            "if": {
              "path": "latest.update_available",
              "equals": true
            },
            "then": {
              "template": "v{status.pngx_version} · {latest.version} available"
            },
            "else": {
              "template": "v{status.pngx_version}"
            }
          }
        }
      }
    },
    {
      "id": "failed",
      "type": "list",
      "label": "Failed imports",
      "max_items": 4,
      "items": [],
      "source": {
        "ref": "server",
        "when": {
          "path": "failed.count",
          "gt": 0
        },
        "map": {
          "label": {
            "if": {
              "path": "failed.count",
              "gt": 10
            },
            "then": {
              "template": "Failed imports · 10 of {failed.count}"
            },
            "else": {
              "template": "Failed imports"
            }
          },
          "items": {
            "each": "failed_rows",
            "fields": {
              "key": "key",
              "text": "text",
              "subtitle": "subtitle",
              "detail": "detail",
              "id": "id"
            }
          }
        }
      },
      "row_actions": [
        {
          "label": "Dismiss",
          "icon": "xmark",
          "method": "POST",
          "path": "/api/tasks/acknowledge/",
          "body": {
            "tasks": [
              "{id}"
            ]
          }
        }
      ]
    },
    {
      "id": "inbox",
      "type": "list",
      "label": "Inbox",
      "max_items": 7,
      "items": [],
      "actions": [
        {
          "kind": "open",
          "label": "Search Paperless",
          "icon": "magnifyingglass",
          "url": "legato://page/paperless-search?q={input.q}",
          "inputs": [
            {
              "name": "q",
              "label": "Search",
              "required": true,
              "max": 200,
              "placeholder": "Words, or tag:bills",
              "hint": "As Paperless’s own search takes it"
            }
          ],
          "submit": "Search"
        }
      ],
      "actions_placement": "corner",
      "source": {
        "ref": "library",
        "empty": {
          "text": "{shown_empty}",
          "when": {
            "path": "shown_count",
            "equals": 0
          }
        },
        "map": {
          "label": {
            "if": {
              "path": "stats.inbox_tag",
              "exists": true
            },
            "then": {
              "if": {
                "path": "inbox.count",
                "gt": 25
              },
              "then": {
                "template": "Inbox · 25 of {inbox.count}"
              },
              "else": {
                "template": "Inbox"
              }
            },
            "else": {
              "template": "Recently added"
            }
          },
          "items": {
            "each": "shown_rows",
            "fields": {
              "key": "key",
              "text": "text",
              "subtitle": "subtitle",
              "detail": "detail",
              "url": "url"
            }
          }
        }
      }
    }
  ]
}
