{
  "$schema": "./.schemas/page.schema.json",
  "title": "Sonarr",
  "icon": "tv",
  "sources": {
    "health": {
      "url": "http://sonarr.local:8989",
      "secret": "sonarr",
      "auth": {
        "type": "header",
        "name": "X-Api-Key"
      },
      "every": "5m",
      "requests": {
        "status": "/api/v3/system/status",
        "health": "/api/v3/health"
      },
      "derive": [
        {
          "name": "errors",
          "value": {
            "count": "health",
            "where": {
              "path": "type",
              "equals": "error"
            },
            "default": 0
          }
        },
        {
          "name": "warnings",
          "value": {
            "count": "health",
            "where": {
              "path": "type",
              "equals": "warning"
            },
            "default": 0
          }
        },
        {
          "name": "problems",
          "value": {
            "count": "health",
            "where": {
              "path": "type",
              "in": [
                "error",
                "warning"
              ]
            },
            "default": 0
          }
        },
        {
          "name": "worst",
          "value": {
            "if": {
              "path": "errors",
              "gt": 0
            },
            "then": {
              "first": "message",
              "of": "health",
              "where": {
                "path": "type",
                "equals": "error"
              }
            },
            "else": {
              "first": "message",
              "of": "health",
              "where": {
                "path": "type",
                "equals": "warning"
              }
            }
          }
        },
        {
          "name": "problems_more",
          "value": {
            "subtract": [
              "problems",
              1
            ]
          }
        }
      ]
    },
    "queue": {
      "url": "http://sonarr.local:8989",
      "secret": "sonarr",
      "auth": {
        "type": "header",
        "name": "X-Api-Key"
      },
      "every": "1m",
      "requests": {
        "queue": "/api/v3/queue?page=1&pageSize=20&includeSeries=true&includeEpisode=true"
      },
      "derive": [
        {
          "name": "queue_first",
          "value": {
            "each": "queue.records",
            "fields": {
              "id": "id",
              "title": {
                "if": {
                  "path": "series.title",
                  "exists": true
                },
                "then": {
                  "template": "{series.title}"
                },
                "else": {
                  "template": "{title|clip:60}"
                }
              },
              "subtitle": {
                "if": {
                  "path": "episode",
                  "exists": true
                },
                "then": {
                  "template": "S{episode.seasonNumber|pad:2}E{episode.episodeNumber|pad:2} · {episode.title|clip:50}"
                },
                "else": {
                  "template": ""
                }
              },
              "client": {
                "if": {
                  "path": "downloadClient",
                  "exists": true
                },
                "then": {
                  "template": "{downloadClient}"
                },
                "else": {
                  "template": "{protocol|title}"
                }
              },
              "done": {
                "subtract": [
                  "size",
                  "sizeleft"
                ],
                "scale": 1e-09,
                "round": 1
              },
              "total": {
                "path": "size",
                "scale": 1e-09,
                "round": 1
              },
              "state": {
                "if": {
                  "path": "trackedDownloadState",
                  "equals": "importBlocked"
                },
                "then": {
                  "template": "Import blocked"
                },
                "else": {
                  "if": {
                    "path": "trackedDownloadState",
                    "equals": "importPending"
                  },
                  "then": {
                    "template": "Waiting to import"
                  },
                  "else": {
                    "if": {
                      "path": "trackedDownloadStatus",
                      "equals": "error"
                    },
                    "then": {
                      "template": "Failed"
                    },
                    "else": {
                      "path": "status",
                      "values": {
                        "paused": "Paused",
                        "queued": "Queued",
                        "delay": "Delayed",
                        "warning": "Needs attention"
                      },
                      "default": ""
                    }
                  }
                }
              },
              "eta": {
                "if": {
                  "path": "estimatedCompletionTime",
                  "exists": true
                },
                "then": {
                  "template": " · {estimatedCompletionTime|in}"
                },
                "else": {
                  "template": ""
                }
              },
              "image": {
                "template": "/api/v3/mediacover/{seriesId|raw}/poster-250.jpg"
              }
            },
            "where": {
              "all": [
                {
                  "path": "size",
                  "gt": 0
                },
                {
                  "path": "trackedDownloadStatus",
                  "in": [
                    "warning",
                    "error"
                  ]
                }
              ]
            }
          }
        },
        {
          "name": "queue_rest",
          "value": {
            "each": "queue.records",
            "fields": {
              "id": "id",
              "title": {
                "if": {
                  "path": "series.title",
                  "exists": true
                },
                "then": {
                  "template": "{series.title}"
                },
                "else": {
                  "template": "{title|clip:60}"
                }
              },
              "subtitle": {
                "if": {
                  "path": "episode",
                  "exists": true
                },
                "then": {
                  "template": "S{episode.seasonNumber|pad:2}E{episode.episodeNumber|pad:2} · {episode.title|clip:50}"
                },
                "else": {
                  "template": ""
                }
              },
              "client": {
                "if": {
                  "path": "downloadClient",
                  "exists": true
                },
                "then": {
                  "template": "{downloadClient}"
                },
                "else": {
                  "template": "{protocol|title}"
                }
              },
              "done": {
                "subtract": [
                  "size",
                  "sizeleft"
                ],
                "scale": 1e-09,
                "round": 1
              },
              "total": {
                "path": "size",
                "scale": 1e-09,
                "round": 1
              },
              "state": {
                "if": {
                  "path": "trackedDownloadState",
                  "equals": "importBlocked"
                },
                "then": {
                  "template": "Import blocked"
                },
                "else": {
                  "if": {
                    "path": "trackedDownloadState",
                    "equals": "importPending"
                  },
                  "then": {
                    "template": "Waiting to import"
                  },
                  "else": {
                    "if": {
                      "path": "trackedDownloadStatus",
                      "equals": "error"
                    },
                    "then": {
                      "template": "Failed"
                    },
                    "else": {
                      "path": "status",
                      "values": {
                        "paused": "Paused",
                        "queued": "Queued",
                        "delay": "Delayed",
                        "warning": "Needs attention"
                      },
                      "default": ""
                    }
                  }
                }
              },
              "eta": {
                "if": {
                  "path": "estimatedCompletionTime",
                  "exists": true
                },
                "then": {
                  "template": " · {estimatedCompletionTime|in}"
                },
                "else": {
                  "template": ""
                }
              },
              "image": {
                "template": "/api/v3/mediacover/{seriesId|raw}/poster-250.jpg"
              }
            },
            "where": {
              "all": [
                {
                  "path": "size",
                  "gt": 0
                },
                {
                  "path": "trackedDownloadStatus",
                  "not": "warning"
                },
                {
                  "path": "trackedDownloadStatus",
                  "not": "error"
                }
              ]
            }
          }
        },
        {
          "name": "queue_rows",
          "value": {
            "each": [
              "queue_first",
              "queue_rest"
            ],
            "fields": {
              "id": "id",
              "title": "title",
              "subtitle": "subtitle",
              "client": "client",
              "done": "done",
              "total": "total",
              "state": "state",
              "eta": "eta",
              "image": "image"
            }
          }
        },
        {
          "name": "drawn",
          "value": {
            "count": "queue_rows",
            "default": 0
          }
        },
        {
          "name": "others",
          "value": {
            "subtract": [
              {
                "path": "queue.totalRecords",
                "default": 0
              },
              "drawn"
            ],
            "default": 0
          }
        },
        {
          "name": "sizeless",
          "value": {
            "count": "queue.records",
            "where": {
              "path": "size",
              "lte": 0
            },
            "default": 0
          }
        },
        {
          "name": "empty_text",
          "value": {
            "if": {
              "path": "sizeless",
              "gt": 0
            },
            "then": {
              "template": "{sizeless} with no size yet"
            },
            "else": {
              "template": "Nothing in the queue"
            }
          }
        }
      ]
    },
    "library": {
      "url": "http://sonarr.local:8989",
      "secret": "sonarr",
      "auth": {
        "type": "header",
        "name": "X-Api-Key"
      },
      "every": "15m",
      "requests": {
        "calendar": "/api/v3/calendar?start={date.today}&end={date.days_ahead_7}&includeSeries=true",
        "missing": "/api/v3/wanted/missing?page=1&pageSize=4&sortKey=episodes.airDateUtc&sortDirection=descending&includeSeries=true",
        "series": "/api/v3/series",
        "roots": "/api/v3/rootfolder"
      },
      "derive": [
        {
          "name": "free_gb",
          "value": {
            "first": "freeSpace",
            "of": "roots",
            "scale": 1e-09,
            "round": 0
          }
        },
        {
          "name": "root_path",
          "value": {
            "first": "path",
            "of": "roots"
          }
        },
        {
          "name": "missing_count",
          "value": {
            "count": "missing.records",
            "default": 0
          }
        },
        {
          "name": "series_count",
          "value": {
            "count": "series",
            "default": 0
          }
        },
        {
          "name": "files",
          "value": {
            "sum": "statistics.episodeFileCount",
            "of": "series",
            "default": 0
          }
        },
        {
          "name": "episodes",
          "value": {
            "sum": "statistics.episodeCount",
            "of": "series",
            "default": 0
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "health",
      "type": "status",
      "placement": "page_info",
      "label": "Sonarr",
      "state": "unknown",
      "width": "full",
      "source": {
        "ref": "health",
        "map": {
          "label": {
            "if": {
              "path": "status.instanceName",
              "exists": true
            },
            "then": {
              "template": "{status.instanceName}"
            },
            "else": {
              "template": "Sonarr"
            }
          },
          "state": {
            "if": {
              "path": "errors",
              "gt": 0
            },
            "then": {
              "template": "critical"
            },
            "else": {
              "if": {
                "path": "warnings",
                "gt": 0
              },
              "then": {
                "template": "warning"
              },
              "else": {
                "template": "ok"
              }
            }
          },
          "detail": {
            "if": {
              "path": "problems",
              "equals": 0
            },
            "then": {
              "template": "Sonarr {status.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": "The server isn't answering",
          "after": 3
        }
      }
    },
    {
      "id": "queue",
      "type": "pacing",
      "label": "Queue",
      "unit": "GB",
      "items": [],
      "source": {
        "ref": "queue",
        "empty": {
          "text": "{empty_text}",
          "when": {
            "path": "drawn",
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "queue_rows",
            "fields": {
              "key": "id",
              "label": "title",
              "subtitle": "subtitle",
              "state": "state",
              "detail": {
                "template": "{client}{eta}"
              },
              "value": "done",
              "target": "total",
              "image": "image",
              "image_shape": {
                "template": "poster"
              }
            }
          },
          "detail": {
            "if": {
              "path": "others",
              "gt": 0
            },
            "then": {
              "template": "{others} more in Sonarr"
            }
          }
        }
      },
      "row_actions": [
        {
          "label": "Remove",
          "icon": "xmark",
          "method": "DELETE",
          "path": "/api/v3/queue/{id}?removeFromClient=true&blocklist=true",
          "confirm": {
            "message": "Remove {title} from {client} and block this release? Sonarr will look for another.",
            "button": "Remove"
          }
        }
      ]
    },
    {
      "id": "upcoming",
      "type": "agenda",
      "label": "Upcoming",
      "days": 7,
      "max_events": 7,
      "hide_empty_days": true,
      "items": [],
      "source": {
        "ref": "library",
        "map": {
          "items": {
            "each": "calendar",
            "fields": {
              "title": {
                "template": "{series.title}"
              },
              "start": "airDateUtc",
              "detail": {
                "template": "S{seasonNumber|pad:2}E{episodeNumber|pad:2} · {title|clip:70}"
              }
            }
          }
        }
      }
    },
    {
      "id": "missing",
      "type": "list",
      "label": "Recently missing",
      "max_items": 4,
      "items": [],
      "source": {
        "ref": "library",
        "empty": {
          "text": "Nothing missing",
          "when": {
            "path": "missing_count",
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "missing.records",
            "fields": {
              "key": "id",
              "text": {
                "template": "{series.title} · S{seasonNumber|pad:2}E{episodeNumber|pad:2}"
              },
              "detail": {
                "template": "Aired {airDateUtc|ago}"
              }
            }
          }
        }
      },
      "row_actions": [
        {
          "label": "Search",
          "icon": "magnifyingglass",
          "method": "POST",
          "path": "/api/v3/command",
          "body": {
            "name": "EpisodeSearch",
            "episodeIds": [
              "{id}"
            ]
          }
        }
      ]
    },
    {
      "id": "library",
      "type": "facts",
      "label": "Library",
      "items": [
        {
          "key": "Missing",
          "value": "—"
        },
        {
          "key": "Episodes on disk",
          "value": "—"
        },
        {
          "key": "Series",
          "value": "—"
        },
        {
          "key": "Free space",
          "value": "—"
        }
      ],
      "source": {
        "ref": "library",
        "map": {
          "items[0].value": {
            "template": "{missing.totalRecords} episodes"
          },
          "items[1].value": {
            "template": "{files} of {episodes}"
          },
          "items[2].value": "series_count",
          "items[3].value": {
            "template": "{free_gb} GB on {root_path}"
          }
        }
      }
    }
  ]
}
