{
  "$schema": "./.schemas/page.schema.json",
  "title": "Local DNS",
  "icon": "server.rack",
  "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"
            }
          }
        }
      ]
    },
    "adguard_setup": {
      "url": "http://192.0.2.53",
      "secret": "adguard-home",
      "auth": {
        "type": "basic"
      },
      "every": "15m",
      "requests": {
        "dns": "/control/dns_info",
        "rewrites": "/control/rewrite/list"
      },
      "derive": [
        {
          "name": "cache_mb",
          "value": {
            "path": "dns.cache_size",
            "scale": 9.5367431640625e-07,
            "round": 0
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "rewrites",
      "type": "list",
      "label": "Rewrites",
      "source": {
        "ref": "adguard_setup",
        "empty": {
          "when": {
            "value": {
              "count": "rewrites"
            },
            "equals": 0
          },
          "text": "No DNS rewrites."
        },
        "map": {
          "items": {
            "each": "rewrites",
            "sort": "domain",
            "limit": 20,
            "fields": {
              "text": "domain",
              "detail": "answer"
            }
          }
        }
      }
    },
    {
      "id": "upstreams",
      "type": "facts",
      "label": "Upstreams",
      "actions_placement": "corner",
      "actions": [
        {
          "label": "Clear DNS cache",
          "icon": "trash",
          "method": "POST",
          "path": "/control/cache_clear",
          "confirm": true
        }
      ],
      "items": [
        {
          "key": "Servers",
          "value": "—"
        },
        {
          "key": "Bootstrap",
          "value": "—"
        },
        {
          "key": "Mode",
          "value": "—"
        },
        {
          "key": "Cache",
          "value": "—"
        },
        {
          "key": "DNSSEC",
          "value": "—"
        }
      ],
      "source": {
        "ref": "adguard_setup",
        "map": {
          "items[0].value": {
            "join": "$",
            "of": "dns.upstream_dns",
            "separator": "\n"
          },
          "items[1].value": {
            "join": "$",
            "of": "dns.bootstrap_dns",
            "separator": ", "
          },
          "items[2].value": {
            "path": "dns.upstream_mode",
            "default": "Load balancing",
            "values": {
              "": "Load balancing",
              "load_balance": "Load balancing",
              "parallel": "All at once",
              "fastest_addr": "Fastest address"
            }
          },
          "items[3].value": {
            "if": {
              "path": "dns.cache_enabled",
              "equals": true
            },
            "then": {
              "template": "On · {cache_mb} MB"
            },
            "else": {
              "template": "Off"
            }
          },
          "items[4].value": {
            "path": "dns.dnssec_enabled",
            "values": {
              "true": "On",
              "false": "Off"
            },
            "default": "—"
          }
        }
      }
    },
    {
      "id": "answer-time",
      "type": "bars",
      "label": "Upstream answer time",
      "unit": "ms",
      "source": {
        "ref": "adguard",
        "map": {
          "items": {
            "each": "stats.top_upstreams_avg_time",
            "entries": true,
            "limit": 5,
            "fields": {
              "label": {
                "template": "{key|origin}"
              },
              "value": {
                "path": "value",
                "scale": 1000,
                "round": 1
              }
            }
          }
        }
      }
    }
  ]
}
