{
  "$schema": "./.schemas/page.schema.json",
  "title": "AdGuard Home",
  "icon": "checkmark.shield",
  "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": "protection",
      "type": "status",
      "placement": "page_info",
      "label": "Protection",
      "width": "full",
      "source": {
        "ref": "adguard",
        "failed": {
          "state": "critical",
          "detail": "AdGuard Home isn't answering"
        },
        "map": {
          "state": {
            "path": "protection",
            "default": "unknown",
            "values": {
              "on": "ok",
              "paused": "off",
              "off": "off",
              "unfiltered": "warning",
              "stopped": "critical"
            }
          },
          "detail": {
            "path": "protection",
            "values": {
              "on": {
                "template": "{rules_on} rules from {lists_on} lists"
              },
              "paused": {
                "template": "Paused · back on in {paused_s|duration}"
              },
              "off": "Off until turned back on",
              "unfiltered": "On, with filtering turned off",
              "stopped": "The DNS server isn't running"
            }
          }
        }
      }
    },
    {
      "id": "controls",
      "type": "text",
      "label": "Controls",
      "actions_placement": "corner",
      "source": {
        "ref": "adguard",
        "map": {},
        "when": {
          "path": "status.running",
          "equals": true
        }
      },
      "actions": [
        {
          "label": "Pause protection",
          "icon": "pause",
          "method": "POST",
          "path": "/control/protection",
          "body": {
            "enabled": false,
            "duration": "{choice}"
          },
          "choices": [
            {
              "label": "5 minutes",
              "value": 300000
            },
            {
              "label": "30 minutes",
              "value": 1800000
            },
            {
              "label": "1 hour",
              "value": 3600000
            },
            {
              "label": "8 hours",
              "value": 28800000
            }
          ],
          "when": {
            "path": "protection",
            "in": [
              "on",
              "unfiltered"
            ]
          }
        },
        {
          "label": "Resume protection",
          "icon": "play",
          "method": "POST",
          "path": "/control/protection",
          "body": {
            "enabled": true
          },
          "when": {
            "path": "protection",
            "in": [
              "paused",
              "off"
            ]
          }
        },
        "|",
        {
          "label": "Turn on Safe Browsing",
          "icon": "shield",
          "method": "POST",
          "path": "/control/safebrowsing/enable",
          "when": {
            "path": "safebrowsing.enabled",
            "equals": false
          }
        },
        {
          "label": "Turn off Safe Browsing",
          "icon": "shield.slash",
          "method": "POST",
          "path": "/control/safebrowsing/disable",
          "when": {
            "path": "safebrowsing.enabled",
            "equals": true
          }
        },
        {
          "label": "Turn on parental filter",
          "icon": "hand.raised",
          "method": "POST",
          "path": "/control/parental/enable",
          "when": {
            "path": "parental.enabled",
            "equals": false
          }
        },
        {
          "label": "Turn off parental filter",
          "icon": "hand.raised.slash",
          "method": "POST",
          "path": "/control/parental/disable",
          "when": {
            "path": "parental.enabled",
            "equals": true
          }
        },
        "|",
        {
          "label": "Clear DNS cache",
          "icon": "trash",
          "method": "POST",
          "path": "/control/cache_clear",
          "confirm": true
        }
      ]
    },
    {
      "id": "hours",
      "type": "sparkline",
      "label": "Queries per hour",
      "points": [],
      "change": false,
      "start_label": "23 hours ago",
      "end_label": "Last hour",
      "source": {
        "ref": "adguard",
        "empty": {
          "when": {
            "value": {
              "count": "hourly"
            },
            "lt": 2
          },
          "text": "Waiting for a second complete hour."
        },
        "map": {
          "points": "hourly"
        }
      }
    },
    {
      "id": "queries",
      "type": "stat",
      "label": "Last 24 hours",
      "value": 0,
      "url": "legato://page/adguard-home-queries",
      "source": {
        "ref": "adguard",
        "map": {
          "value": "stats.num_dns_queries"
        }
      }
    },
    {
      "id": "blocked",
      "type": "stat",
      "label": "Blocked",
      "value": 0,
      "url": "legato://page/adguard-home-queries#recent-blocked",
      "source": {
        "ref": "adguard",
        "map": {
          "value": "stats.num_blocked_filtering",
          "delta": {
            "template": "{blocked_pct}% of queries"
          }
        }
      }
    },
    {
      "id": "readings",
      "type": "facts",
      "label": "Server",
      "items": [
        {
          "key": "Clients",
          "value": "—"
        },
        {
          "key": "Upstream",
          "value": "—"
        },
        {
          "key": "Blocklists",
          "value": "—"
        },
        {
          "key": "Version",
          "value": "—"
        },
        {
          "key": "Answer time",
          "value": "—"
        }
      ],
      "source": {
        "ref": "adguard",
        "map": {
          "items[0].value": {
            "template": "{client_count} · busiest {client_rows[0].name}"
          },
          "items[1].value": "upstream_rows[0].name",
          "items[2].value": {
            "template": "{rules_on} rules from {lists_on} lists"
          },
          "items[3].value": "status.version",
          "items[4].value": {
            "template": "{avg_ms} ms"
          }
        }
      }
    },
    {
      "id": "top",
      "type": "tabs",
      "tabs": [
        {
          "label": "Blocked",
          "cards": [
            {
              "id": "top-blocked",
              "type": "bars",
              "label": "Most blocked",
              "source": {
                "ref": "adguard",
                "map": {
                  "items": {
                    "each": "stats.top_blocked_domains",
                    "entries": true,
                    "limit": 5,
                    "fields": {
                      "label": "key",
                      "value": "value"
                    }
                  }
                }
              }
            }
          ]
        },
        {
          "label": "Not blocked",
          "cards": [
            {
              "id": "top-queried",
              "type": "bars",
              "label": "Most queried",
              "source": {
                "ref": "adguard",
                "map": {
                  "items": {
                    "each": "stats.top_queried_domains",
                    "entries": true,
                    "limit": 5,
                    "fields": {
                      "label": "key",
                      "value": "value"
                    }
                  }
                }
              }
            }
          ]
        }
      ]
    },
    {
      "id": "explore",
      "type": "list",
      "label": "Explore",
      "items": [
        {
          "text": "Queries",
          "url": "legato://page/adguard-home-queries"
        },
        {
          "text": "Clients",
          "url": "legato://page/adguard-home-clients"
        },
        {
          "text": "Blocklists",
          "url": "legato://page/adguard-home-filtering"
        },
        {
          "text": "Local DNS",
          "url": "legato://page/adguard-home-dns"
        }
      ]
    }
  ]
}
