{
  "$schema": "./.schemas/page.schema.json",
  "title": "Queries",
  "icon": "list.bullet.rectangle",
  "sidebar": false,
  "sources": {
    "adguard": {
      "url": "http://192.0.2.53",
      "secret": "adguard-home",
      "auth": {
        "type": "basic"
      },
      "every": "1m",
      "requests": {
        "status": "/control/status",
        "stats": "/control/stats",
        "clients": "/control/clients",
        "filtering": "/control/filtering/status",
        "safebrowsing": "/control/safebrowsing/status",
        "parental": "/control/parental/status",
        "log": "/control/querylog?limit=25",
        "blocked_log": "/control/querylog?limit=25&response_status=blocked"
      },
      "derive": [
        {
          "name": "protection",
          "value": {
            "if": {
              "path": "status.running",
              "equals": false
            },
            "then": {
              "template": "stopped"
            },
            "else": {
              "if": {
                "path": "status.protection_enabled",
                "equals": false
              },
              "then": {
                "if": {
                  "path": "status.protection_disabled_duration",
                  "gt": 0
                },
                "then": {
                  "template": "paused"
                },
                "else": {
                  "template": "off"
                }
              },
              "else": {
                "if": {
                  "path": "filtering.enabled",
                  "equals": false
                },
                "then": {
                  "template": "unfiltered"
                },
                "else": {
                  "template": "on"
                }
              }
            }
          }
        },
        {
          "name": "paused_s",
          "value": {
            "path": "status.protection_disabled_duration",
            "scale": 0.001,
            "round": 0
          }
        },
        {
          "name": "lists_on",
          "value": {
            "count": "filtering.filters",
            "where": {
              "path": "enabled",
              "equals": true
            }
          }
        },
        {
          "name": "rules_on",
          "value": {
            "sum": "rules_count",
            "of": "filtering.filters",
            "where": {
              "path": "enabled",
              "equals": true
            }
          }
        },
        {
          "name": "blocked_pct",
          "value": {
            "divide": [
              "stats.num_blocked_filtering",
              "stats.num_dns_queries"
            ],
            "scale": 100,
            "round": 0,
            "default": 0
          }
        },
        {
          "name": "avg_ms",
          "value": {
            "path": "stats.avg_processing_time",
            "scale": 1000,
            "round": 0
          }
        },
        {
          "name": "client_count",
          "value": {
            "count": "stats.top_clients"
          }
        },
        {
          "name": "hourly",
          "value": {
            "each": "stats.dns_queries",
            "limit": 23,
            "value": "$"
          }
        },
        {
          "name": "client_rows",
          "value": {
            "each": "stats.top_clients",
            "entries": true,
            "lookup": {
              "from": "clients.clients",
              "by": "ids[0]",
              "on": "key",
              "as": "client"
            },
            "fields": {
              "name": {
                "path": "client.name",
                "default": {
                  "template": "{key}"
                }
              },
              "ip": "key",
              "count": "value"
            }
          }
        },
        {
          "name": "upstream_rows",
          "value": {
            "each": "stats.top_upstreams_responses",
            "entries": true,
            "fields": {
              "name": {
                "template": "{key|origin}"
              },
              "count": "value"
            }
          }
        },
        {
          "name": "log_rows",
          "value": {
            "each": "log.data",
            "fields": {
              "domain": "question.name",
              "who": {
                "if": {
                  "value": {
                    "path": "client_info.name",
                    "default": ""
                  },
                  "equals": ""
                },
                "then": "client",
                "else": "client_info.name"
              },
              "why": {
                "path": "reason",
                "default": {
                  "template": "{reason}"
                },
                "values": {
                  "NotFilteredNotFound": "Allowed",
                  "NotFilteredWhiteList": "Allowed by a rule",
                  "NotFilteredError": "Allowed, check failed",
                  "FilteredBlackList": "Blocked",
                  "FilteredSafeBrowsing": "Blocked by Safe Browsing",
                  "FilteredParental": "Blocked by parental control",
                  "FilteredInvalid": "Blocked, invalid",
                  "FilteredSafeSearch": "Safe Search",
                  "FilteredBlockedService": {
                    "template": "Blocked service: {service_name}"
                  },
                  "Rewrite": "Rewritten",
                  "RewriteEtcHosts": "From the hosts file",
                  "RewriteRule": "Rewritten by a rule"
                }
              },
              "at": "time"
            }
          }
        },
        {
          "name": "blocked_rows",
          "value": {
            "each": "blocked_log.data",
            "fields": {
              "domain": "question.name",
              "who": {
                "if": {
                  "value": {
                    "path": "client_info.name",
                    "default": ""
                  },
                  "equals": ""
                },
                "then": "client",
                "else": "client_info.name"
              },
              "why": {
                "path": "reason",
                "default": {
                  "template": "{reason}"
                },
                "values": {
                  "NotFilteredNotFound": "Allowed",
                  "NotFilteredWhiteList": "Allowed by a rule",
                  "NotFilteredError": "Allowed, check failed",
                  "FilteredBlackList": "Blocked",
                  "FilteredSafeBrowsing": "Blocked by Safe Browsing",
                  "FilteredParental": "Blocked by parental control",
                  "FilteredInvalid": "Blocked, invalid",
                  "FilteredSafeSearch": "Safe Search",
                  "FilteredBlockedService": {
                    "template": "Blocked service: {service_name}"
                  },
                  "Rewrite": "Rewritten",
                  "RewriteEtcHosts": "From the hosts file",
                  "RewriteRule": "Rewritten by a rule"
                }
              },
              "at": "time"
            }
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "recent",
      "type": "tabs",
      "tabs": [
        {
          "label": "All",
          "cards": [
            {
              "id": "recent-all",
              "type": "list",
              "label": "Recent queries",
              "max_items": 12,
              "source": {
                "ref": "adguard",
                "empty": {
                  "when": {
                    "value": {
                      "count": "log_rows"
                    },
                    "equals": 0
                  },
                  "text": "No queries in the log yet."
                },
                "map": {
                  "items": {
                    "each": "log_rows",
                    "limit": 20,
                    "fields": {
                      "text": {
                        "template": "{domain}\n{who} · {why}"
                      },
                      "detail": {
                        "if": {
                          "value": {
                            "template": "{at|day}"
                          },
                          "equals": "Today"
                        },
                        "then": {
                          "template": "{at|time}"
                        },
                        "else": {
                          "template": "{at|day} {at|time}"
                        }
                      }
                    }
                  }
                }
              }
            }
          ]
        },
        {
          "label": "Blocked",
          "cards": [
            {
              "id": "recent-blocked",
              "type": "list",
              "label": "Recently blocked",
              "max_items": 12,
              "source": {
                "ref": "adguard",
                "empty": {
                  "when": {
                    "value": {
                      "count": "blocked_rows"
                    },
                    "equals": 0
                  },
                  "text": "Nothing blocked recently."
                },
                "map": {
                  "items": {
                    "each": "blocked_rows",
                    "limit": 20,
                    "fields": {
                      "text": {
                        "template": "{domain}\n{who} · {why}"
                      },
                      "detail": {
                        "if": {
                          "value": {
                            "template": "{at|day}"
                          },
                          "equals": "Today"
                        },
                        "then": {
                          "template": "{at|time}"
                        },
                        "else": {
                          "template": "{at|day} {at|time}"
                        }
                      }
                    }
                  }
                }
              }
            }
          ]
        }
      ]
    }
  ]
}
