{
  "$schema": "./.schemas/page.schema.json",
  "title": "Clients",
  "icon": "person.2",
  "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": "active",
      "type": "bars",
      "label": "Most active",
      "source": {
        "ref": "adguard",
        "map": {
          "items": {
            "each": "client_rows",
            "limit": 10,
            "fields": {
              "label": "name",
              "value": "count"
            }
          }
        }
      }
    },
    {
      "id": "devices",
      "type": "list",
      "label": "Named clients",
      "source": {
        "ref": "adguard",
        "empty": {
          "when": {
            "value": {
              "count": "clients.clients"
            },
            "equals": 0
          },
          "text": "No clients named in AdGuard Home yet."
        },
        "map": {
          "items": {
            "each": "clients.clients",
            "sort": "name",
            "fields": {
              "icon": {
                "path": "tags[0]",
                "default": "circle",
                "values": {
                  "device_pc": "desktopcomputer",
                  "device_laptop": "laptopcomputer",
                  "device_phone": "iphone",
                  "device_tablet": "ipad",
                  "device_tv": "tv",
                  "device_audio": "hifispeaker",
                  "device_camera": "video",
                  "device_gameconsole": "gamecontroller",
                  "device_nas": "externaldrive",
                  "device_printer": "printer",
                  "device_securityalarm": "bell",
                  "device_other": "sensor"
                }
              },
              "text": {
                "if": {
                  "path": "use_global_settings",
                  "equals": false
                },
                "then": {
                  "template": "{name}\nIts own settings"
                },
                "else": {
                  "template": "{name}"
                }
              },
              "detail": "ids[0]"
            }
          }
        }
      }
    }
  ]
}
