{
  "$schema": "./.schemas/page.schema.json",
  "title": "Prowlarr",
  "icon": "magnifyingglass",
  "sources": {
    "health": {
      "url": "http://prowlarr.local:9696",
      "secret": "prowlarr",
      "auth": {
        "type": "header",
        "name": "X-Api-Key"
      },
      "every": "5m",
      "requests": {
        "status": "/api/v1/system/status",
        "health": "/api/v1/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
            ]
          }
        }
      ]
    },
    "indexers": {
      "url": "http://prowlarr.local:9696",
      "secret": "prowlarr",
      "auth": {
        "type": "header",
        "name": "X-Api-Key"
      },
      "every": "5m",
      "requests": {
        "list": "/api/v1/indexer",
        "stats": "/api/v1/indexerstats?startDate={date.hours_ago_168_at}",
        "blocked": "/api/v1/indexerstatus",
        "grabs": "/api/v1/history?page=1&pageSize=4&sortKey=date&sortDirection=descending&eventType=1"
      },
      "derive": [
        {
          "name": "own",
          "value": {
            "each": "list",
            "fields": {
              "id": "id",
              "name": "name",
              "enabled": "enable",
              "resource": "$"
            }
          }
        },
        {
          "name": "all",
          "value": {
            "each": "own",
            "lookup": {
              "from": "stats.indexers",
              "by": "indexerId",
              "on": "id",
              "as": "stat"
            },
            "fields": {
              "id": "id",
              "name": "name",
              "enabled": "enabled",
              "resource": "resource",
              "asked": "stat.indexerId",
              "q": {
                "add": [
                  "stat.numberOfQueries",
                  "stat.numberOfRssQueries"
                ],
                "default": 0
              },
              "g": {
                "path": "stat.numberOfGrabs",
                "default": 0
              },
              "fails": {
                "add": [
                  "stat.numberOfFailedQueries",
                  "stat.numberOfFailedRssQueries"
                ],
                "default": 0
              }
            }
          }
        },
        {
          "name": "marked",
          "value": {
            "each": "all",
            "lookup": {
              "from": "blocked",
              "by": "indexerId",
              "on": "id",
              "as": "block"
            },
            "fields": {
              "id": "id",
              "name": "name",
              "enabled": "enabled",
              "resource": "resource",
              "asked": "asked",
              "q": "q",
              "g": "g",
              "fails": "fails",
              "failed_share": {
                "divide": [
                  "fails",
                  "q"
                ]
              },
              "till": "block.disabledTill",
              "first": "block.initialFailure"
            }
          }
        },
        {
          "name": "blocked_rows",
          "value": {
            "each": "marked",
            "where": {
              "path": "till",
              "exists": true
            },
            "sort": "till",
            "fields": {
              "key": "id",
              "text": "name",
              "detail": {
                "template": "Unavailable"
              },
              "enabled": "enabled",
              "resource": "resource",
              "subtitle": {
                "if": {
                  "value": {
                    "since": "till"
                  },
                  "lt": 0
                },
                "then": {
                  "template": "First failed {first|ago} · retry {till|in}"
                },
                "else": {
                  "template": "First failed {first|ago}"
                }
              }
            }
          }
        },
        {
          "name": "on_rows",
          "value": {
            "each": "marked",
            "where": {
              "all": [
                {
                  "path": "till",
                  "exists": false
                },
                {
                  "path": "enabled",
                  "equals": true
                }
              ]
            },
            "sort": {
              "by": "q",
              "order": "desc"
            },
            "fields": {
              "key": "id",
              "text": "name",
              "enabled": "enabled",
              "resource": "resource",
              "detail": {
                "if": {
                  "path": "asked",
                  "exists": true
                },
                "then": {
                  "if": {
                    "path": "q",
                    "equals": 1
                  },
                  "then": {
                    "if": {
                      "path": "g",
                      "equals": 1
                    },
                    "then": {
                      "template": "1 query · 1 grab"
                    },
                    "else": {
                      "template": "1 query · {g} grabs"
                    }
                  },
                  "else": {
                    "if": {
                      "path": "g",
                      "equals": 1
                    },
                    "then": {
                      "template": "{q} queries · 1 grab"
                    },
                    "else": {
                      "template": "{q} queries · {g} grabs"
                    }
                  }
                },
                "else": {
                  "template": "No queries in 7 days"
                }
              },
              "subtitle": {
                "if": {
                  "path": "fails",
                  "gt": 0
                },
                "then": {
                  "if": {
                    "path": "failed_share",
                    "lt": 0.01
                  },
                  "then": {
                    "template": "Under 1% of queries failed"
                  },
                  "else": {
                    "template": "{failed_share|percent:0} of queries failed"
                  }
                }
              }
            }
          }
        },
        {
          "name": "off_rows",
          "value": {
            "each": "marked",
            "where": {
              "all": [
                {
                  "path": "till",
                  "exists": false
                },
                {
                  "path": "enabled",
                  "not": true
                }
              ]
            },
            "fields": {
              "key": "id",
              "text": "name",
              "detail": {
                "template": "Off"
              },
              "enabled": "enabled",
              "resource": "resource"
            }
          }
        },
        {
          "name": "indexer_rows",
          "value": {
            "each": [
              "blocked_rows",
              "on_rows",
              "off_rows"
            ],
            "fields": {
              "key": "key",
              "text": "text",
              "subtitle": "subtitle",
              "detail": "detail",
              "enabled": "enabled",
              "resource": "resource"
            }
          }
        },
        {
          "name": "indexer_count",
          "value": {
            "count": "indexer_rows",
            "default": 0
          }
        },
        {
          "name": "grab_total",
          "value": {
            "sum": "numberOfGrabs",
            "of": "stats.indexers",
            "default": 0
          }
        },
        {
          "name": "failed_grabs",
          "value": {
            "sum": "numberOfFailedGrabs",
            "of": "stats.indexers",
            "default": 0
          }
        },
        {
          "name": "searches",
          "value": {
            "sum": "numberOfQueries",
            "of": "stats.indexers",
            "default": 0
          }
        },
        {
          "name": "failed_searches",
          "value": {
            "sum": "numberOfFailedQueries",
            "of": "stats.indexers",
            "default": 0
          }
        },
        {
          "name": "rss",
          "value": {
            "sum": "numberOfRssQueries",
            "of": "stats.indexers",
            "default": 0
          }
        },
        {
          "name": "failed_rss",
          "value": {
            "sum": "numberOfFailedRssQueries",
            "of": "stats.indexers",
            "default": 0
          }
        },
        {
          "name": "apps",
          "value": {
            "each": "stats.userAgents",
            "sort": {
              "by": "numberOfQueries",
              "order": "desc"
            },
            "limit": 3,
            "where": {
              "all": [
                {
                  "path": "numberOfQueries",
                  "gt": 0
                },
                {
                  "path": "userAgent",
                  "not": ""
                }
              ]
            },
            "fields": {
              "t": {
                "template": "{userAgent|clip:16} {numberOfQueries}"
              }
            }
          }
        },
        {
          "name": "by_app",
          "value": {
            "join": "t",
            "of": "apps",
            "separator": " · "
          }
        },
        {
          "name": "app_count",
          "value": {
            "count": "apps",
            "default": 0
          }
        },
        {
          "name": "grab_rows",
          "value": {
            "each": "grabs.records",
            "lookup": {
              "from": "list",
              "by": "id",
              "on": "indexerId",
              "as": "ix"
            },
            "fields": {
              "key": "id",
              "text": {
                "if": {
                  "path": "data.grabTitle",
                  "exists": true
                },
                "then": {
                  "template": "{data.grabTitle|clip:120}"
                },
                "else": {
                  "template": "A release"
                }
              },
              "subtitle": {
                "if": {
                  "path": "ix.name",
                  "exists": true
                },
                "then": {
                  "if": {
                    "path": "data.source",
                    "exists": true
                  },
                  "then": {
                    "template": "{ix.name} · {data.source|clip:30}"
                  },
                  "else": {
                    "template": "{ix.name}"
                  }
                },
                "else": {
                  "if": {
                    "path": "data.source",
                    "exists": true
                  },
                  "then": {
                    "template": "{data.source|clip:30}"
                  }
                }
              },
              "detail": {
                "if": {
                  "path": "successful",
                  "equals": false
                },
                "then": {
                  "template": "Failed · {date|ago}"
                },
                "else": {
                  "template": "{date|ago}"
                }
              }
            }
          }
        },
        {
          "name": "grab_count",
          "value": {
            "count": "grab_rows",
            "default": 0
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "health",
      "type": "status",
      "placement": "page_info",
      "label": "Prowlarr",
      "state": "unknown",
      "width": "full",
      "source": {
        "ref": "health",
        "map": {
          "label": {
            "if": {
              "path": "status.instanceName",
              "exists": true
            },
            "then": {
              "template": "{status.instanceName}"
            },
            "else": {
              "template": "Prowlarr"
            }
          },
          "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": "Prowlarr {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": "indexers",
      "type": "list",
      "label": "Indexers",
      "max_items": 7,
      "items": [],
      "source": {
        "ref": "indexers",
        "empty": {
          "text": "No indexers in Prowlarr yet",
          "when": {
            "path": "indexer_count",
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "indexer_rows",
            "fields": {
              "key": "key",
              "text": "text",
              "subtitle": "subtitle",
              "detail": "detail"
            }
          }
        }
      },
      "row_actions": [
        {
          "label": "Test",
          "icon": "testtube.2",
          "method": "POST",
          "path": "/api/v1/indexer/test",
          "body": "{resource}",
          "error": "[0].errorMessage",
          "timeout": 120,
          "done_note": "Passed",
          "when": {
            "path": "enabled",
            "equals": true
          }
        }
      ]
    },
    {
      "id": "week",
      "type": "facts",
      "label": "Last 7 days",
      "items": [
        {
          "key": "Grabs",
          "value": "—"
        },
        {
          "key": "By app",
          "value": "—"
        },
        {
          "key": "Searches",
          "value": "—"
        },
        {
          "key": "RSS syncs",
          "value": "—"
        }
      ],
      "source": {
        "ref": "indexers",
        "map": {
          "items[0].value": {
            "if": {
              "path": "failed_grabs",
              "gt": 0
            },
            "then": {
              "template": "{grab_total} · {failed_grabs} failed"
            },
            "else": {
              "template": "{grab_total}"
            }
          },
          "items[1].value": {
            "if": {
              "path": "app_count",
              "gt": 0
            },
            "then": {
              "template": "{by_app}"
            },
            "else": {
              "template": "None"
            }
          },
          "items[2].value": {
            "if": {
              "path": "failed_searches",
              "gt": 0
            },
            "then": {
              "template": "{searches} · {failed_searches} failed"
            },
            "else": {
              "template": "{searches}"
            }
          },
          "items[3].value": {
            "if": {
              "path": "failed_rss",
              "gt": 0
            },
            "then": {
              "template": "{rss} · {failed_rss} failed"
            },
            "else": {
              "template": "{rss}"
            }
          }
        }
      }
    },
    {
      "id": "grabs",
      "type": "list",
      "label": "Recent grabs",
      "max_items": 4,
      "items": [],
      "source": {
        "ref": "indexers",
        "empty": {
          "text": "No grabs yet",
          "when": {
            "path": "grab_count",
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "grab_rows",
            "fields": {
              "key": "key",
              "text": "text",
              "subtitle": "subtitle",
              "detail": "detail"
            }
          }
        }
      }
    }
  ]
}
