{
  "$schema": "./.schemas/page.schema.json",
  "title": "SABnzbd",
  "icon": "arrow.down.circle",
  "sources": {
    "sab": {
      "url": "http://sabnzbd.local:8080",
      "secret": "sabnzbd",
      "auth": {
        "type": "query",
        "name": "apikey"
      },
      "every": "1m",
      "requests": {
        "queue": "/api?mode=queue&limit=20&output=json",
        "history": "/api?mode=history&limit=4&output=json",
        "stats": "/api?mode=server_stats&output=json",
        "status": "/api?mode=status&skip_dashboard=1&output=json",
        "line": "/api?mode=get_config&section=misc&keyword=bandwidth_max&output=json"
      },
      "fail": {
        "when": {
          "any": [
            {
              "path": "queue.status",
              "equals": false
            },
            {
              "path": "history.status",
              "equals": false
            },
            {
              "path": "stats.status",
              "equals": false
            },
            {
              "path": "status.status",
              "equals": false
            },
            {
              "path": "line.status",
              "equals": false
            }
          ]
        },
        "message": {
          "if": {
            "path": "queue.error",
            "exists": true
          },
          "then": {
            "template": "{queue.error}"
          },
          "else": {
            "if": {
              "path": "history.error",
              "exists": true
            },
            "then": {
              "template": "{history.error}"
            },
            "else": {
              "if": {
                "path": "stats.error",
                "exists": true
              },
              "then": {
                "template": "{stats.error}"
              },
              "else": {
                "if": {
                  "path": "status.error",
                  "exists": true
                },
                "then": {
                  "template": "{status.error}"
                },
                "else": {
                  "template": "{line.error}"
                }
              }
            }
          }
        }
      },
      "derive": [
        {
          "name": "queue_eta",
          "value": {
            "each": "queue.queue.slots",
            "where": {
              "path": "mb",
              "gte": 0.01
            },
            "fields": {
              "nzo_id": "nzo_id",
              "seconds": {
                "add": [
                  {
                    "path": "timeleft",
                    "match": "^([0-9]+):",
                    "scale": 3600
                  },
                  {
                    "path": "timeleft",
                    "match": "^[0-9]+:([0-9]+):",
                    "scale": 60
                  },
                  {
                    "path": "timeleft",
                    "match": ":([0-9]+)$"
                  }
                ],
                "default": 0
              },
              "category": {
                "path": "cat",
                "values": {
                  "tv": "TV",
                  "movies": "Movies",
                  "music": "Music",
                  "audio": "Audio",
                  "software": "Software",
                  "*": "",
                  "None": ""
                },
                "default": {
                  "template": "{cat}"
                }
              }
            }
          }
        },
        {
          "name": "queue_rows",
          "value": {
            "each": "queue.queue.slots",
            "where": {
              "path": "mb",
              "gte": 0.01
            },
            "lookup": {
              "from": "queue_eta",
              "by": "nzo_id",
              "on": "nzo_id",
              "as": "eta"
            },
            "fields": {
              "nzo_id": "nzo_id",
              "status": "status",
              "filename": "filename",
              "title": {
                "template": "{filename|clip:40}"
              },
              "unit": {
                "if": {
                  "path": "mb",
                  "gte": 1024
                },
                "then": {
                  "template": "GB"
                },
                "else": {
                  "template": "MB"
                }
              },
              "done": {
                "if": {
                  "path": "mb",
                  "gte": 1024
                },
                "then": {
                  "subtract": [
                    {
                      "path": "mb",
                      "match": "^[0-9.]+"
                    },
                    {
                      "path": "mbleft",
                      "match": "^[0-9.]+"
                    }
                  ],
                  "scale": 0.0009765625,
                  "round": 1
                },
                "else": {
                  "if": {
                    "path": "mb",
                    "gte": 10
                  },
                  "then": {
                    "subtract": [
                      {
                        "path": "mb",
                        "match": "^[0-9.]+"
                      },
                      {
                        "path": "mbleft",
                        "match": "^[0-9.]+"
                      }
                    ],
                    "round": 0
                  },
                  "else": {
                    "subtract": [
                      {
                        "path": "mb",
                        "match": "^[0-9.]+"
                      },
                      {
                        "path": "mbleft",
                        "match": "^[0-9.]+"
                      }
                    ],
                    "round": 2
                  }
                }
              },
              "total": {
                "if": {
                  "path": "mb",
                  "gte": 1024
                },
                "then": {
                  "path": "mb",
                  "match": "^[0-9.]+",
                  "scale": 0.0009765625,
                  "round": 1
                },
                "else": {
                  "if": {
                    "path": "mb",
                    "gte": 10
                  },
                  "then": {
                    "path": "mb",
                    "match": "^[0-9.]+",
                    "round": 0
                  },
                  "else": {
                    "path": "mb",
                    "match": "^[0-9.]+",
                    "round": 2
                  }
                }
              },
              "state": {
                "path": "status",
                "values": {
                  "Paused": "Paused",
                  "Propagating": "Delayed",
                  "Checking": "Checking",
                  "Fetching": "Fetching repair blocks",
                  "Queued": "Queued"
                },
                "default": ""
              },
              "eta": {
                "if": {
                  "path": "eta.seconds",
                  "gt": 0
                },
                "then": {
                  "if": {
                    "path": "eta.category",
                    "not": ""
                  },
                  "then": {
                    "template": "{eta.category} · in {eta.seconds|duration}"
                  },
                  "else": {
                    "template": "in {eta.seconds|duration}"
                  }
                },
                "else": {
                  "template": "{eta.category}"
                }
              },
              "toggle": {
                "if": {
                  "path": "status",
                  "equals": "Paused"
                },
                "then": {
                  "template": "resume"
                },
                "else": {
                  "template": "pause"
                }
              }
            }
          }
        },
        {
          "name": "drawn",
          "value": {
            "count": "queue_rows",
            "default": 0
          }
        },
        {
          "name": "others",
          "value": {
            "subtract": [
              {
                "path": "queue.queue.noofslots",
                "default": 0
              },
              "drawn"
            ],
            "default": 0
          }
        },
        {
          "name": "queue_empty",
          "value": {
            "if": {
              "path": "others",
              "gt": 0
            },
            "then": {
              "if": {
                "path": "others",
                "equals": 1
              },
              "then": {
                "template": "1 job waiting for its NZB"
              },
              "else": {
                "template": "{others} jobs waiting for their NZBs"
              }
            },
            "else": {
              "template": "Nothing in the queue"
            }
          }
        },
        {
          "name": "left_b",
          "value": {
            "path": "queue.queue.mbleft",
            "match": "^[0-9.]+",
            "scale": 1048576
          }
        },
        {
          "name": "left_s",
          "value": {
            "add": [
              {
                "path": "queue.queue.timeleft",
                "match": "^([0-9]+):",
                "scale": 3600
              },
              {
                "path": "queue.queue.timeleft",
                "match": "^[0-9]+:([0-9]+):",
                "scale": 60
              },
              {
                "path": "queue.queue.timeleft",
                "match": ":([0-9]+)$"
              }
            ],
            "default": 0
          }
        },
        {
          "name": "free_b",
          "value": {
            "path": "queue.queue.diskspace2",
            "match": "^[0-9.]+",
            "scale": 1073741824
          }
        },
        {
          "name": "disk_b",
          "value": {
            "path": "queue.queue.diskspacetotal2",
            "match": "^[0-9.]+",
            "scale": 1073741824
          }
        },
        {
          "name": "temp_free_b",
          "value": {
            "path": "queue.queue.diskspace1",
            "match": "^[0-9.]+",
            "scale": 1073741824
          }
        },
        {
          "name": "temp_disk_b",
          "value": {
            "path": "queue.queue.diskspacetotal1",
            "match": "^[0-9.]+",
            "scale": 1073741824
          }
        },
        {
          "name": "temp_floor_b",
          "value": {
            "path": "queue.queue.diskspacetotal1",
            "match": "^[0-9.]+",
            "scale": 53687091.2
          }
        },
        {
          "name": "floor_b",
          "value": {
            "path": "queue.queue.diskspacetotal2",
            "match": "^[0-9.]+",
            "scale": 53687091.2
          }
        },
        {
          "name": "temp_low",
          "value": {
            "if": {
              "all": [
                {
                  "path": "temp_disk_b",
                  "gt": 0
                },
                {
                  "path": "temp_free_b",
                  "lt": {
                    "path": "temp_floor_b"
                  }
                }
              ]
            },
            "then": {
              "template": "yes"
            },
            "else": {
              "template": "no"
            }
          }
        },
        {
          "name": "newest_warning",
          "value": {
            "each": "status.status.warnings",
            "last": 1,
            "fields": {
              "origin": "origin"
            }
          }
        },
        {
          "name": "newest_origin",
          "value": {
            "first": "origin",
            "of": "newest_warning"
          }
        },
        {
          "name": "disk_pause",
          "value": {
            "if": {
              "all": [
                {
                  "path": "queue.queue.paused",
                  "equals": true
                },
                {
                  "path": "newest_origin",
                  "contains": "assembler.py"
                }
              ]
            },
            "then": {
              "template": "yes"
            },
            "else": {
              "template": "no"
            }
          }
        },
        {
          "name": "done_low",
          "value": {
            "if": {
              "all": [
                {
                  "path": "disk_b",
                  "gt": 0
                },
                {
                  "path": "free_b",
                  "lt": {
                    "path": "floor_b"
                  }
                }
              ]
            },
            "then": {
              "template": "yes"
            },
            "else": {
              "template": "no"
            }
          }
        },
        {
          "name": "speed_mbs",
          "value": {
            "path": "queue.queue.kbpersec",
            "match": "^[0-9.]+",
            "scale": 0.0009765625,
            "round": 2
          }
        },
        {
          "name": "server_errors",
          "value": {
            "count": "status.status.servers",
            "default": 0,
            "where": {
              "all": [
                {
                  "path": "servererror",
                  "exists": true
                },
                {
                  "path": "servererror",
                  "not": ""
                }
              ]
            }
          }
        },
        {
          "name": "server_error",
          "value": {
            "first": "servererror",
            "of": "status.status.servers",
            "where": {
              "all": [
                {
                  "path": "servererror",
                  "exists": true
                },
                {
                  "path": "servererror",
                  "not": ""
                }
              ]
            }
          }
        },
        {
          "name": "server_errors_more",
          "value": {
            "subtract": [
              "server_errors",
              1
            ]
          }
        },
        {
          "name": "pause_min",
          "value": {
            "path": "queue.queue.pause_int",
            "match": "^([0-9]+):"
          }
        },
        {
          "name": "history_failed",
          "value": {
            "each": "history.history.slots",
            "where": {
              "path": "status",
              "equals": "Failed"
            },
            "fields": {
              "nzo_id": "nzo_id",
              "name": "name",
              "status": "status",
              "retry": "retry",
              "fail_message": "fail_message",
              "bytes": "bytes",
              "category": "category",
              "completed": "completed",
              "shown_category": {
                "path": "category",
                "values": {
                  "tv": "TV",
                  "movies": "Movies",
                  "music": "Music",
                  "audio": "Audio",
                  "software": "Software",
                  "*": "",
                  "None": ""
                },
                "default": {
                  "template": "{category}"
                }
              },
              "reason": {
                "path": "fail_message",
                "match": "^(.*?)(?: - https?://\\S+)?$"
              }
            }
          }
        },
        {
          "name": "history_rest",
          "value": {
            "each": "history.history.slots",
            "where": {
              "path": "status",
              "not": "Failed"
            },
            "fields": {
              "nzo_id": "nzo_id",
              "name": "name",
              "status": "status",
              "retry": "retry",
              "fail_message": "fail_message",
              "bytes": "bytes",
              "category": "category",
              "completed": "completed",
              "shown_category": {
                "path": "category",
                "values": {
                  "tv": "TV",
                  "movies": "Movies",
                  "music": "Music",
                  "audio": "Audio",
                  "software": "Software",
                  "*": "",
                  "None": ""
                },
                "default": {
                  "template": "{category}"
                }
              },
              "reason": {
                "path": "fail_message",
                "match": "^(.*?)(?: - https?://\\S+)?$"
              }
            }
          }
        },
        {
          "name": "history_rows",
          "value": {
            "each": [
              "history_failed",
              "history_rest"
            ],
            "limit": 4,
            "fields": {
              "nzo_id": "nzo_id",
              "status": "status",
              "retry": "retry",
              "text": {
                "template": "{name|clip:60}"
              },
              "subtitle": {
                "if": {
                  "path": "status",
                  "equals": "Failed"
                },
                "then": {
                  "template": "{reason|clip:80}"
                },
                "else": {
                  "if": {
                    "path": "bytes",
                    "gt": 0
                  },
                  "then": {
                    "if": {
                      "path": "category",
                      "in": [
                        "*",
                        "None",
                        ""
                      ]
                    },
                    "then": {
                      "template": "{bytes|bytes}"
                    },
                    "else": {
                      "template": "{shown_category} · {bytes|bytes}"
                    }
                  },
                  "else": {
                    "template": "{shown_category}"
                  }
                }
              },
              "detail": {
                "path": "status",
                "values": {
                  "Completed": {
                    "template": "{completed|ago}"
                  },
                  "Failed": {
                    "template": "Failed · {completed|ago}"
                  }
                },
                "default": {
                  "template": "{status}"
                }
              }
            }
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "health",
      "type": "status",
      "placement": "page_info",
      "label": "SABnzbd",
      "state": "unknown",
      "width": "full",
      "source": {
        "ref": "sab",
        "map": {
          "state": {
            "if": {
              "any": [
                {
                  "path": "server_errors",
                  "gt": 0
                },
                {
                  "path": "temp_low",
                  "equals": "yes"
                },
                {
                  "path": "done_low",
                  "equals": "yes"
                },
                {
                  "path": "disk_pause",
                  "equals": "yes"
                }
              ]
            },
            "then": {
              "template": "warning"
            },
            "else": {
              "if": {
                "path": "queue.queue.paused",
                "equals": true
              },
              "then": {
                "template": "off"
              },
              "else": {
                "template": "ok"
              }
            }
          },
          "detail": {
            "if": {
              "path": "server_errors",
              "gt": 0
            },
            "then": {
              "if": {
                "path": "server_errors",
                "equals": 1
              },
              "then": {
                "template": "{server_error|clip:110}"
              },
              "else": {
                "template": "{server_error|clip:90} · and {server_errors_more} more"
              }
            },
            "else": {
              "if": {
                "path": "disk_pause",
                "equals": "yes"
              },
              "then": {
                "template": "Paused: not enough free space · {temp_free_b|bytes} free for downloads"
              },
              "else": {
                "if": {
                  "path": "temp_low",
                  "equals": "yes"
                },
                "then": {
                  "template": "Low on space for downloads: {temp_free_b|bytes} free of {temp_disk_b|bytes}"
                },
                "else": {
                  "if": {
                    "path": "done_low",
                    "equals": "yes"
                  },
                  "then": {
                    "template": "Low on space for finished downloads: {free_b|bytes} free of {disk_b|bytes}"
                  },
                  "else": {
                    "if": {
                      "path": "queue.queue.paused",
                      "equals": true
                    },
                    "then": {
                      "if": {
                        "path": "pause_min",
                        "gt": 0
                      },
                      "then": {
                        "template": "Paused · resumes in {pause_min} min"
                      },
                      "else": {
                        "if": {
                          "path": "queue.queue.pause_int",
                          "not": "0"
                        },
                        "then": {
                          "template": "Paused · resumes in under a minute"
                        },
                        "else": {
                          "template": "Paused"
                        }
                      }
                    },
                    "else": {
                      "template": "SABnzbd {queue.queue.version} · No problems"
                    }
                  }
                }
              }
            }
          }
        },
        "failed": {
          "state": "critical",
          "detail": "SABnzbd isn't answering",
          "after": 3
        }
      }
    },
    {
      "id": "controls",
      "type": "text",
      "label": "Controls",
      "actions_placement": "corner",
      "source": {
        "ref": "sab"
      },
      "actions": [
        {
          "label": "Pause",
          "icon": "pause.fill",
          "method": "POST",
          "path": "/api?mode=pause",
          "body": {},
          "success": {
            "path": "status",
            "equals": true
          },
          "error": "error",
          "when": {
            "path": "queue.queue.paused",
            "equals": false
          }
        },
        {
          "label": "Pause for",
          "icon": "timer",
          "method": "POST",
          "path": "/api?mode=config&name=set_pause&value={choice}",
          "body": {},
          "success": {
            "path": "status",
            "equals": true
          },
          "error": "error",
          "when": {
            "path": "queue.queue.paused",
            "equals": false
          },
          "choices": [
            {
              "label": "30 minutes",
              "value": 30
            },
            {
              "label": "1 hour",
              "value": 60
            },
            {
              "label": "3 hours",
              "value": 180
            }
          ]
        },
        {
          "label": "Resume",
          "icon": "play.fill",
          "method": "POST",
          "path": "/api?mode=resume",
          "body": {},
          "success": {
            "path": "status",
            "equals": true
          },
          "error": "error",
          "when": {
            "path": "queue.queue.paused",
            "equals": true
          }
        },
        "|",
        {
          "label": "Speed limit",
          "icon": "gauge.with.dots.needle.33percent",
          "method": "POST",
          "path": "/api?mode=config&name=speedlimit&value={choice}",
          "body": {},
          "success": {
            "path": "status",
            "equals": true
          },
          "error": "error",
          "when": {
            "all": [
              {
                "path": "line.config.misc.bandwidth_max",
                "exists": true
              },
              {
                "path": "line.config.misc.bandwidth_max",
                "not": ""
              }
            ]
          },
          "current": "queue.queue.speedlimit",
          "choices": [
            {
              "label": "No limit",
              "value": 100
            },
            {
              "label": "75%",
              "value": 75
            },
            {
              "label": "50%",
              "value": 50
            },
            {
              "label": "25%",
              "value": 25
            }
          ]
        }
      ]
    },
    {
      "id": "facts",
      "type": "facts",
      "label": "Downloads",
      "items": [
        {
          "key": "Left",
          "value": "—"
        },
        {
          "key": "Downloaded",
          "value": "—"
        },
        {
          "key": "Free space",
          "value": "—"
        },
        {
          "key": "Version",
          "value": "—"
        }
      ],
      "source": {
        "ref": "sab",
        "map": {
          "items[0].value": {
            "if": {
              "path": "left_b",
              "gt": 0
            },
            "then": {
              "if": {
                "path": "queue.queue.paused",
                "equals": true
              },
              "then": {
                "template": "{left_b|bytes} · paused"
              },
              "else": {
                "if": {
                  "path": "left_s",
                  "gt": 0
                },
                "then": {
                  "template": "{left_b|bytes} · {left_s|duration}"
                },
                "else": {
                  "template": "{left_b|bytes}"
                }
              }
            },
            "else": {
              "template": "Nothing queued"
            }
          },
          "items[1].value": {
            "template": "{stats.day|bytes} today · {stats.month|bytes} this month"
          },
          "items[2].value": {
            "template": "{free_b|bytes} of {disk_b|bytes}"
          },
          "items[3].value": {
            "template": "{queue.queue.version}"
          }
        }
      }
    },
    {
      "id": "speed",
      "type": "sparkline",
      "label": "Speed",
      "unit": "MB/s",
      "change": false,
      "points": [],
      "source": {
        "ref": "sab",
        "map": {
          "points": {
            "history": "speed_mbs",
            "every": "5m",
            "keep": 60
          }
        }
      }
    },
    {
      "id": "queue",
      "type": "pacing",
      "label": "Queue",
      "unit": "GB",
      "items": [],
      "source": {
        "ref": "sab",
        "empty": {
          "text": "{queue_empty}",
          "when": {
            "path": "drawn",
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "queue_rows",
            "fields": {
              "key": "nzo_id",
              "label": "title",
              "state": "state",
              "detail": "eta",
              "value": "done",
              "target": "total",
              "unit": "unit"
            }
          },
          "detail": {
            "if": {
              "path": "others",
              "gt": 0
            },
            "then": {
              "template": "{others} more in SABnzbd"
            }
          }
        }
      },
      "row_actions_shown": "always",
      "row_actions": [
        {
          "label": "Pause or resume",
          "icon": "playpause",
          "method": "POST",
          "path": "/api?mode=queue&name={toggle}&value={nzo_id}",
          "body": {},
          "success": {
            "path": "status",
            "equals": true
          },
          "error": "error"
        },
        {
          "label": "Delete",
          "icon": "trash",
          "method": "POST",
          "path": "/api?mode=queue&name=delete&value={nzo_id}&del_files=1",
          "body": {},
          "success": {
            "path": "status",
            "equals": true
          },
          "error": "error",
          "confirm": {
            "message": "Delete {filename|clip:80} and what it has downloaded so far?",
            "button": "Delete"
          }
        }
      ]
    },
    {
      "id": "history",
      "type": "list",
      "label": "History",
      "max_items": 4,
      "items": [],
      "source": {
        "ref": "sab",
        "empty": {
          "text": "Nothing finished yet",
          "when": {
            "path": "history.history.noofslots",
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "history_rows",
            "fields": {
              "key": "nzo_id",
              "text": "text",
              "subtitle": "subtitle",
              "detail": "detail"
            }
          }
        }
      },
      "row_actions": [
        {
          "label": "Retry",
          "icon": "arrow.uturn.forward",
          "method": "POST",
          "path": "/api?mode=retry&value={nzo_id}",
          "body": {},
          "success": {
            "path": "status",
            "equals": true
          },
          "error": "error",
          "when": {
            "all": [
              {
                "path": "status",
                "equals": "Failed"
              },
              {
                "path": "retry",
                "equals": true
              }
            ]
          }
        }
      ]
    }
  ]
}
