{
  "$schema": "./.schemas/page.schema.json",
  "title": "Queries",
  "icon": "list.bullet",
  "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": "rate",
          "value": {
            "path": "summary.queries.frequency",
            "round": 1
          }
        },
        {
          "name": "blocked_pct",
          "value": {
            "path": "summary.queries.percent_blocked",
            "round": 0
          }
        },
        {
          "name": "blocked_rows",
          "value": {
            "each": "blocked.queries",
            "fields": {
              "domain": "domain",
              "time": "time",
              "client": "client",
              "outcome": {
                "path": "status",
                "values": {
                  "GRAVITY": "Blocked",
                  "DENYLIST": "Blocked",
                  "REGEX": "Blocked",
                  "EXTERNAL_BLOCKED_IP": "Blocked",
                  "EXTERNAL_BLOCKED_NULL": "Blocked",
                  "EXTERNAL_BLOCKED_NXRA": "Blocked",
                  "GRAVITY_CNAME": "Blocked",
                  "REGEX_CNAME": "Blocked",
                  "DENYLIST_CNAME": "Blocked",
                  "DBBUSY": "Blocked",
                  "SPECIAL_DOMAIN": "Blocked",
                  "FORWARDED": "Forwarded",
                  "CACHE": "Cached",
                  "CACHE_STALE": "Cached",
                  "IN_PROGRESS": "In progress",
                  "RETRIED": "Retried",
                  "RETRIED_DNSSEC": "Retried",
                  "UNKNOWN": "Unknown"
                },
                "default": {
                  "template": "{status|lower|title}"
                }
              }
            }
          }
        },
        {
          "name": "all_rows",
          "value": {
            "each": "all.queries",
            "fields": {
              "domain": "domain",
              "time": "time",
              "client": "client",
              "outcome": {
                "path": "status",
                "values": {
                  "GRAVITY": "Blocked",
                  "DENYLIST": "Blocked",
                  "REGEX": "Blocked",
                  "EXTERNAL_BLOCKED_IP": "Blocked",
                  "EXTERNAL_BLOCKED_NULL": "Blocked",
                  "EXTERNAL_BLOCKED_NXRA": "Blocked",
                  "GRAVITY_CNAME": "Blocked",
                  "REGEX_CNAME": "Blocked",
                  "DENYLIST_CNAME": "Blocked",
                  "DBBUSY": "Blocked",
                  "SPECIAL_DOMAIN": "Blocked",
                  "FORWARDED": "Forwarded",
                  "CACHE": "Cached",
                  "CACHE_STALE": "Cached",
                  "IN_PROGRESS": "In progress",
                  "RETRIED": "Retried",
                  "RETRIED_DNSSEC": "Retried",
                  "UNKNOWN": "Unknown"
                },
                "default": {
                  "template": "{status|lower|title}"
                }
              }
            }
          }
        },
        {
          "name": "mac_rows",
          "value": {
            "each": "mac.queries",
            "fields": {
              "domain": "domain",
              "time": "time",
              "client": "client",
              "outcome": {
                "path": "status",
                "values": {
                  "GRAVITY": "Blocked",
                  "DENYLIST": "Blocked",
                  "REGEX": "Blocked",
                  "EXTERNAL_BLOCKED_IP": "Blocked",
                  "EXTERNAL_BLOCKED_NULL": "Blocked",
                  "EXTERNAL_BLOCKED_NXRA": "Blocked",
                  "GRAVITY_CNAME": "Blocked",
                  "REGEX_CNAME": "Blocked",
                  "DENYLIST_CNAME": "Blocked",
                  "DBBUSY": "Blocked",
                  "SPECIAL_DOMAIN": "Blocked",
                  "FORWARDED": "Forwarded",
                  "CACHE": "Cached",
                  "CACHE_STALE": "Cached",
                  "IN_PROGRESS": "In progress",
                  "RETRIED": "Retried",
                  "RETRIED_DNSSEC": "Retried",
                  "UNKNOWN": "Unknown"
                },
                "default": {
                  "template": "{status|lower|title}"
                }
              }
            }
          }
        }
      ],
      "requests": {
        "all": "/api/queries?length=8",
        "blocked": "/api/queries?length=8&upstream=blocklist",
        "mac": "/api/queries?length=8&upstream=blocklist&client_ip=192.0.2.10",
        "summary": "/api/stats/summary"
      }
    }
  },
  "cards": [
    {
      "id": "rate",
      "label": "Rate",
      "source": {
        "map": {
          "value": "rate"
        },
        "ref": "pihole"
      },
      "type": "stat",
      "unit": "a second",
      "value": 0
    },
    {
      "id": "blocked",
      "label": "Blocked, 24 hours",
      "source": {
        "map": {
          "delta": {
            "template": "{blocked_pct}%"
          },
          "value": "summary.queries.blocked"
        },
        "ref": "pihole"
      },
      "type": "stat",
      "value": 0
    },
    {
      "id": "recent",
      "tabs": [
        {
          "cards": [
            {
              "id": "recent-blocked",
              "items": [],
              "label": "Recently blocked",
              "source": {
                "map": {
                  "items": {
                    "each": "blocked_rows",
                    "fields": {
                      "text": {
                        "if": {
                          "value": {
                            "path": "client.name",
                            "default": ""
                          },
                          "equals": ""
                        },
                        "then": {
                          "template": "{domain}\n{client.ip} · {outcome}"
                        },
                        "else": {
                          "template": "{domain}\n{client.name} · {outcome}"
                        }
                      },
                      "detail": {
                        "if": {
                          "value": {
                            "template": "{time|day}"
                          },
                          "equals": "Today"
                        },
                        "then": {
                          "template": "{time|time}"
                        },
                        "else": {
                          "template": "{time|day} {time|time}"
                        }
                      }
                    },
                    "limit": 8
                  }
                },
                "ref": "pihole"
              },
              "type": "list",
              "copyable": true
            }
          ],
          "label": "Blocked"
        },
        {
          "cards": [
            {
              "id": "recent-all",
              "items": [],
              "label": "Recent queries",
              "source": {
                "map": {
                  "items": {
                    "each": "all_rows",
                    "fields": {
                      "text": {
                        "if": {
                          "value": {
                            "path": "client.name",
                            "default": ""
                          },
                          "equals": ""
                        },
                        "then": {
                          "template": "{domain}\n{client.ip} · {outcome}"
                        },
                        "else": {
                          "template": "{domain}\n{client.name} · {outcome}"
                        }
                      },
                      "detail": {
                        "if": {
                          "value": {
                            "template": "{time|day}"
                          },
                          "equals": "Today"
                        },
                        "then": {
                          "template": "{time|time}"
                        },
                        "else": {
                          "template": "{time|day} {time|time}"
                        }
                      }
                    },
                    "limit": 8
                  }
                },
                "ref": "pihole"
              },
              "type": "list",
              "copyable": true
            }
          ],
          "label": "All"
        },
        {
          "cards": [
            {
              "id": "recent-mac",
              "items": [],
              "label": "Blocked for this Mac",
              "source": {
                "empty": {
                  "text": "Nothing blocked for this Mac lately.",
                  "when": {
                    "equals": 0,
                    "value": {
                      "count": "mac.queries"
                    }
                  }
                },
                "map": {
                  "items": {
                    "each": "mac_rows",
                    "fields": {
                      "text": {
                        "if": {
                          "value": {
                            "path": "client.name",
                            "default": ""
                          },
                          "equals": ""
                        },
                        "then": {
                          "template": "{domain}\n{client.ip} · {outcome}"
                        },
                        "else": {
                          "template": "{domain}\n{client.name} · {outcome}"
                        }
                      },
                      "detail": {
                        "if": {
                          "value": {
                            "template": "{time|day}"
                          },
                          "equals": "Today"
                        },
                        "then": {
                          "template": "{time|time}"
                        },
                        "else": {
                          "template": "{time|day} {time|time}"
                        }
                      }
                    },
                    "limit": 8
                  }
                },
                "ref": "pihole"
              },
              "type": "list",
              "copyable": true
            }
          ],
          "label": "This Mac"
        }
      ],
      "type": "tabs",
      "same_height": true
    },
    {
      "icon": "magnifyingglass",
      "id": "open-log",
      "label": "Search the query log",
      "type": "link",
      "url": "https://pi.hole/admin/queries"
    }
  ]
}
