{
  "$schema": "./.schemas/page.schema.json",
  "title": "Servers",
  "icon": "server.rack",
  "sidebar": false,
  "sources": {
    "pihole": {
      "url": "http://pi.hole",
      "secret": "pihole",
      "every": "1m",
      "auth": {
        "logout": {
          "method": "DELETE",
          "path": "/api/auth"
        },
        "request": {
          "body": {
            "password": "{secret}"
          },
          "method": "POST",
          "path": "/api/auth"
        },
        "send": {
          "header": "X-FTL-SID"
        },
        "token": "session.sid",
        "type": "login"
      },
      "derive": [
        {
          "name": "note_count",
          "value": {
            "count": "messages.messages"
          }
        },
        {
          "name": "upstream_list",
          "value": {
            "each": "config_upstreams.config.dns.upstreams",
            "where": {
              "path": "$",
              "not": ""
            },
            "value": "$"
          }
        },
        {
          "name": "upstream_set",
          "value": {
            "join": "$",
            "of": "upstream_list",
            "separator": " "
          }
        }
      ],
      "requests": {
        "messages": "/api/info/messages",
        "upstreams": "/api/stats/upstreams",
        "config_upstreams": "/api/config/dns/upstreams"
      }
    },
    "pihole_slow": {
      "url": "http://pi.hole",
      "secret": "pihole",
      "every": "15m",
      "auth": {
        "logout": {
          "method": "DELETE",
          "path": "/api/auth"
        },
        "request": {
          "body": {
            "password": "{secret}"
          },
          "method": "POST",
          "path": "/api/auth"
        },
        "send": {
          "header": "X-FTL-SID"
        },
        "token": "session.sid",
        "type": "login"
      },
      "derive": [
        {
          "name": "uptime_days",
          "value": {
            "path": "system.system.uptime",
            "round": 0,
            "scale": 1.15740740741e-05
          }
        },
        {
          "name": "cpu",
          "value": {
            "path": "system.system.cpu[\"%cpu\"]",
            "round": 0
          }
        },
        {
          "name": "mem",
          "value": {
            "path": "system.system.memory.ram[\"%used\"]",
            "round": 0
          }
        }
      ],
      "requests": {
        "config_upstreams": "/api/config/dns/upstreams",
        "lists": "/api/lists",
        "summary": "/api/stats/summary",
        "system": "/api/info/system",
        "version": "/api/info/version"
      }
    }
  },
  "cards": [
    {
      "id": "notes",
      "items": [],
      "label": "Notes from Pi-hole",
      "source": {
        "map": {
          "items": {
            "each": "messages.messages",
            "limit": 5,
            "template": "{plain}"
          }
        },
        "ref": "pihole",
        "when": {
          "gt": 0,
          "path": "note_count"
        }
      },
      "tone": "warning",
      "type": "list"
    },
    {
      "id": "answered",
      "items": [
        {
          "color": "blue",
          "label": "Cache",
          "value": 0
        },
        {
          "color": "grey",
          "label": "Blocklist",
          "value": 0
        },
        {
          "color": "teal",
          "label": "Upstream",
          "value": 0
        }
      ],
      "label": "Where answers came from",
      "source": {
        "map": {
          "items[0].value": {
            "of": "upstreams.upstreams",
            "sum": "count",
            "where": {
              "equals": "cache",
              "path": "ip"
            }
          },
          "items[1].value": {
            "of": "upstreams.upstreams",
            "sum": "count",
            "where": {
              "equals": "blocklist",
              "path": "ip"
            }
          },
          "items[2].value": {
            "of": "upstreams.upstreams",
            "sum": "count",
            "where": {
              "not_in": [
                "cache",
                "blocklist"
              ],
              "path": "ip"
            }
          }
        },
        "ref": "pihole"
      },
      "type": "breakdown"
    },
    {
      "id": "upstreams",
      "label": "Upstream response",
      "source": {
        "map": {
          "items": {
            "each": "upstreams.upstreams",
            "fields": {
              "label": {
                "if": {
                  "path": "port",
                  "equals": 53
                },
                "then": "ip",
                "else": {
                  "template": "{ip}#{port|raw}"
                }
              },
              "value": {
                "path": "statistics.response",
                "round": 0,
                "scale": 1000
              }
            },
            "where": {
              "all": [
                {
                  "not_in": [
                    "cache",
                    "blocklist"
                  ],
                  "path": "ip"
                },
                {
                  "gt": 0,
                  "path": "statistics.response"
                }
              ]
            }
          }
        },
        "ref": "pihole"
      },
      "type": "bars",
      "unit": "ms",
      "actions_placement": "corner",
      "actions": [
        {
          "label": "Upstream DNS",
          "icon": "arrow.triangle.branch",
          "method": "PATCH",
          "path": "/api/config",
          "body": {
            "config": {
              "dns": {
                "upstreams": [
                  "{choice|first_word}",
                  "{choice|rest_words|first_word}",
                  "{choice|rest_words|rest_words}"
                ]
              }
            }
          },
          "choices": [
            {
              "label": "Cloudflare",
              "value": "1.1.1.1 1.0.0.1"
            },
            {
              "label": "Quad9",
              "value": "9.9.9.9 149.112.112.112"
            },
            {
              "label": "Google",
              "value": "8.8.8.8 8.8.4.4"
            }
          ],
          "current": "upstream_set",
          "error": "error.hint"
        },
        {
          "label": "Edit upstreams",
          "icon": "pencil",
          "method": "PATCH",
          "path": "/api/config",
          "inputs": [
            {
              "name": "first",
              "label": "Server",
              "required": true,
              "placeholder": "192.0.2.53#5335",
              "hint": "An address, with #port if it has one",
              "max": 80,
              "start": "upstream_list[0]"
            },
            {
              "name": "second",
              "label": "Second server",
              "placeholder": "Optional",
              "max": 80,
              "start": "upstream_list[1]"
            },
            {
              "name": "third",
              "label": "Third server",
              "placeholder": "Optional",
              "max": 80,
              "start": "upstream_list[2]"
            }
          ],
          "body": {
            "config": {
              "dns": {
                "upstreams": [
                  "{input.first}",
                  "{input.second}",
                  "{input.third}"
                ]
              }
            }
          },
          "submit": "Save",
          "error": "error.hint",
          "form_style": "card"
        }
      ]
    },
    {
      "collapsed": true,
      "detail": "How many domains each list gives",
      "id": "lists-head",
      "text": "Blocklists",
      "type": "heading"
    },
    {
      "id": "lists",
      "items": [],
      "label": "Blocklists",
      "source": {
        "map": {
          "items": {
            "each": "lists.lists",
            "fields": {
              "detail": {
                "template": "{number}"
              },
              "text": "address"
            },
            "limit": 12,
            "sort": {
              "by": "number",
              "order": "desc"
            }
          },
          "label": {
            "template": "{summary.gravity.domains_being_blocked} domains, updated {summary.gravity.last_update|ago}"
          }
        },
        "ref": "pihole_slow"
      },
      "type": "list"
    },
    {
      "collapsed": true,
      "detail": "Uptime, load and configured upstreams",
      "id": "host-head",
      "text": "Host",
      "type": "heading"
    },
    {
      "id": "versions",
      "items": [
        {
          "key": "Core",
          "value": "—"
        },
        {
          "key": "FTL",
          "value": "—"
        },
        {
          "key": "Web",
          "value": "—"
        },
        {
          "key": "Docker image",
          "value": "—"
        }
      ],
      "label": "Versions",
      "source": {
        "map": {
          "items[0].value": {
            "template": "{version.version.core.local.version} · latest {version.version.core.remote.version}"
          },
          "items[1].value": {
            "template": "{version.version.ftl.local.version} · latest {version.version.ftl.remote.version}"
          },
          "items[2].value": {
            "template": "{version.version.web.local.version} · latest {version.version.web.remote.version}"
          },
          "items[3].value": {
            "template": "{version.version.docker.local} · latest {version.version.docker.remote}"
          }
        },
        "ref": "pihole_slow"
      },
      "type": "facts"
    },
    {
      "id": "host",
      "items": [
        {
          "key": "Up",
          "value": "—"
        },
        {
          "key": "CPU",
          "value": "—"
        },
        {
          "key": "Memory",
          "value": "—"
        },
        {
          "key": "Upstreams",
          "value": "—"
        }
      ],
      "label": "Host",
      "source": {
        "map": {
          "items[0].value": {
            "template": "{uptime_days} days"
          },
          "items[1].value": {
            "template": "{cpu}%"
          },
          "items[2].value": {
            "template": "{mem}%"
          },
          "items[3].value": {
            "join": "$",
            "of": "config_upstreams.config.dns.upstreams",
            "separator": ", "
          }
        },
        "ref": "pihole_slow"
      },
      "type": "facts"
    },
    {
      "icon": "gearshape",
      "id": "open",
      "label": "Open Pi-hole settings",
      "type": "link",
      "url": "https://pi.hole/admin/settings/system"
    }
  ]
}
