{
  "$schema": "./.schemas/page.schema.json",
  "title": "Pi-hole",
  "icon": "shield.lefthalf.filled",
  "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": "rate",
          "value": {
            "path": "summary.queries.frequency",
            "round": 1
          }
        },
        {
          "name": "blocked_pct",
          "value": {
            "path": "summary.queries.percent_blocked",
            "round": 0
          }
        },
        {
          "name": "note_count",
          "value": {
            "count": "messages.messages"
          }
        },
        {
          "name": "recent",
          "value": {
            "each": "history.history",
            "fields": {
              "allowed": {
                "subtract": [
                  "total",
                  "blocked"
                ]
              },
              "blocked": "blocked",
              "hour": {
                "template": "{timestamp|hour}"
              },
              "t": "timestamp"
            },
            "sort": {
              "by": "timestamp",
              "order": "desc"
            }
          }
        },
        {
          "name": "hour_groups",
          "value": {
            "each": "recent",
            "fields": {
              "allowed": {
                "of": "rows",
                "sum": "allowed"
              },
              "blocked": {
                "of": "rows",
                "sum": "blocked"
              },
              "label": "key",
              "t": {
                "first": "t",
                "of": "rows"
              },
              "readings": {
                "count": "rows"
              }
            },
            "group": "hour"
          }
        },
        {
          "name": "hourly",
          "value": {
            "each": "hour_groups",
            "where": {
              "all": [
                {
                  "path": "label",
                  "not": "Now"
                },
                {
                  "path": "readings",
                  "equals": 6
                }
              ]
            },
            "sort": {
              "by": "t",
              "order": "desc"
            },
            "limit": 12,
            "fields": {
              "label": "label",
              "t": "t",
              "allowed": "allowed",
              "blocked": "blocked"
            }
          }
        },
        {
          "name": "hour_count",
          "value": {
            "count": "hourly"
          }
        }
      ],
      "requests": {
        "blocking": "/api/dns/blocking",
        "history": "/api/history",
        "messages": "/api/info/messages",
        "summary": "/api/stats/summary",
        "top_allowed": "/api/stats/top_domains?count=5",
        "top_blocked": "/api/stats/top_domains?blocked=true&count=5"
      }
    }
  },
  "cards": [
    {
      "id": "blocking",
      "label": "Ad blocking",
      "source": {
        "map": {
          "detail": {
            "default": {
              "template": "{blocking.blocking}"
            },
            "path": "blocking.blocking",
            "values": {
              "disabled": {
                "template": "Paused until {blocking.timer|until}"
              },
              "enabled": {
                "template": "On · {summary.clients.active} active clients"
              }
            }
          },
          "state": {
            "default": "unknown",
            "path": "blocking.blocking",
            "values": {
              "disabled": "off",
              "enabled": "ok"
            }
          }
        },
        "ref": "pihole"
      },
      "type": "status",
      "placement": "page_info",
      "width": "full"
    },
    {
      "id": "notes",
      "label": "Notes from Pi-hole",
      "source": {
        "map": {
          "detail": "messages.messages[0].plain",
          "label": {
            "else": {
              "template": "{note_count} notes from Pi-hole"
            },
            "if": {
              "equals": 1,
              "path": "note_count"
            },
            "then": {
              "template": "A note from Pi-hole"
            }
          }
        },
        "ref": "pihole",
        "when": {
          "gt": 0,
          "path": "note_count"
        }
      },
      "state": "warning",
      "type": "status",
      "placement": "page_info",
      "url": "legato://page/pihole-servers",
      "width": "full"
    },
    {
      "id": "recent",
      "label": "Last 24 hours",
      "source": {
        "map": {
          "delta": {
            "if": {
              "path": "summary.clients.active",
              "equals": 1
            },
            "then": {
              "template": "{rate} a second · 1 client"
            },
            "else": {
              "template": "{rate} a second · {summary.clients.active} clients"
            }
          },
          "value": "summary.queries.total"
        },
        "ref": "pihole"
      },
      "type": "stat",
      "value": 0,
      "actions_placement": "corner",
      "actions": [
        {
          "body": {
            "blocking": false,
            "timer": "{choice}"
          },
          "choices": [
            {
              "label": "5 minutes",
              "value": 300
            },
            {
              "label": "30 minutes",
              "value": 1800
            },
            {
              "label": "1 hour",
              "value": 3600
            },
            {
              "label": "8 hours",
              "value": 28800
            }
          ],
          "icon": "pause.fill",
          "label": "Pause blocking",
          "method": "POST",
          "path": "/api/dns/blocking",
          "when": {
            "equals": "enabled",
            "path": "blocking.blocking"
          }
        },
        {
          "body": {
            "blocking": true,
            "timer": null
          },
          "icon": "play.fill",
          "label": "Resume blocking",
          "method": "POST",
          "path": "/api/dns/blocking",
          "when": {
            "equals": "disabled",
            "path": "blocking.blocking"
          }
        }
      ]
    },
    {
      "id": "blocked",
      "label": "Blocked",
      "source": {
        "map": {
          "delta": {
            "template": "{blocked_pct}% of queries"
          },
          "value": "summary.queries.blocked"
        },
        "ref": "pihole"
      },
      "type": "stat",
      "url": "legato://page/pihole-queries",
      "value": 0
    },
    {
      "id": "hours",
      "items": [],
      "label": "Last 12 hours",
      "series": [
        "Allowed",
        "Blocked"
      ],
      "series_colors": [
        "green",
        "grey"
      ],
      "source": {
        "empty": {
          "text": "Waiting for a complete hour of history.",
          "when": {
            "equals": 0,
            "path": "hour_count"
          }
        },
        "map": {
          "items": {
            "each": "hourly",
            "fields": {
              "label": "label",
              "values[0]": "allowed",
              "values[1]": "blocked"
            },
            "sort": "t"
          },
          "label": {
            "else": {
              "template": "Last {hour_count} hours"
            },
            "if": {
              "equals": 12,
              "path": "hour_count"
            },
            "then": {
              "template": "Last 12 hours"
            }
          }
        },
        "ref": "pihole"
      },
      "stacked": true,
      "type": "columns",
      "legend_align": "trailing"
    },
    {
      "id": "top",
      "tabs": [
        {
          "cards": [
            {
              "id": "top-blocked",
              "label": "Most blocked",
              "source": {
                "map": {
                  "items": {
                    "each": "top_blocked.domains",
                    "fields": {
                      "label": "domain",
                      "value": "count"
                    },
                    "limit": 5
                  }
                },
                "ref": "pihole"
              },
              "type": "bars"
            }
          ],
          "label": "Blocked"
        },
        {
          "cards": [
            {
              "id": "top-allowed",
              "label": "Most allowed",
              "source": {
                "map": {
                  "items": {
                    "each": "top_allowed.domains",
                    "fields": {
                      "label": "domain",
                      "value": "count"
                    },
                    "limit": 5
                  }
                },
                "ref": "pihole"
              },
              "type": "bars"
            }
          ],
          "label": "Allowed"
        }
      ],
      "type": "tabs"
    },
    {
      "id": "explore",
      "items": [
        {
          "text": "Queries",
          "url": "legato://page/pihole-queries"
        },
        {
          "text": "Clients",
          "url": "legato://page/pihole-clients"
        },
        {
          "text": "Local DNS",
          "url": "legato://page/pihole-dns"
        },
        {
          "text": "Servers",
          "url": "legato://page/pihole-servers"
        },
        {
          "detail": "pi.hole",
          "text": "Open Pi-hole",
          "url": "http://pi.hole/admin/"
        }
      ],
      "label": "Explore",
      "type": "list"
    }
  ]
}
