{
  "$schema": "./.schemas/page.schema.json",
  "title": "Clients",
  "icon": "desktopcomputer",
  "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": "client_rows",
          "value": {
            "each": "clients.clients",
            "fields": {
              "allowed": {
                "subtract": [
                  "count",
                  {
                    "default": 0,
                    "path": "b.count"
                  }
                ]
              },
              "blocked": {
                "default": 0,
                "path": "b.count"
              },
              "count": "count",
              "name": {
                "else": {
                  "else": "name",
                  "if": {
                    "equals": "localhost",
                    "path": "name"
                  },
                  "then": {
                    "template": "Pi-hole"
                  }
                },
                "if": {
                  "equals": "",
                  "value": {
                    "default": "",
                    "path": "name"
                  }
                },
                "then": "ip"
              },
              "share": {
                "divide": [
                  {
                    "multiply": [
                      {
                        "default": 0,
                        "path": "b.count"
                      },
                      100
                    ]
                  },
                  "count"
                ]
              }
            },
            "lookup": {
              "as": "b",
              "by": "ip",
              "from": "clients_blocked.clients",
              "on": "ip"
            }
          }
        },
        {
          "name": "allowed_links",
          "value": {
            "each": "client_rows",
            "fields": {
              "from": "name",
              "to": {
                "template": "Allowed"
              },
              "value": "allowed"
            }
          }
        },
        {
          "name": "blocked_links",
          "value": {
            "each": "client_rows",
            "fields": {
              "from": "name",
              "to": {
                "template": "Blocked"
              },
              "value": "blocked"
            },
            "where": {
              "gt": 0,
              "path": "blocked"
            }
          }
        },
        {
          "name": "busiest_share",
          "value": {
            "divide": [
              {
                "multiply": [
                  "clients.clients[0].count",
                  100
                ]
              },
              "summary.queries.total"
            ]
          }
        }
      ],
      "requests": {
        "clients": "/api/stats/top_clients?count=6",
        "clients_blocked": "/api/stats/top_clients?blocked=true&count=50",
        "devices": "/api/network/devices?max_devices=20&max_addresses=1",
        "summary": "/api/stats/summary"
      }
    }
  },
  "cards": [
    {
      "id": "active",
      "label": "Active",
      "source": {
        "map": {
          "delta": {
            "template": "of {summary.clients.total} seen"
          },
          "value": "summary.clients.active"
        },
        "ref": "pihole"
      },
      "type": "stat",
      "value": 0
    },
    {
      "id": "busiest",
      "label": "Busiest",
      "source": {
        "map": {
          "delta": {
            "template": "{busiest_share|round}% of queries"
          },
          "value": "client_rows[0].name"
        },
        "ref": "pihole"
      },
      "type": "stat",
      "value": "—"
    },
    {
      "id": "clients",
      "tabs": [
        {
          "cards": [
            {
              "hide_amounts": true,
              "id": "flow",
              "label": "Top clients",
              "links": [
                {
                  "from": "—",
                  "to": "Allowed",
                  "value": 0
                }
              ],
              "source": {
                "empty": {
                  "text": "No client queries to show.",
                  "when": {
                    "equals": 0,
                    "value": {
                      "count": "clients.clients"
                    }
                  }
                },
                "map": {
                  "links": {
                    "each": [
                      "allowed_links",
                      "blocked_links"
                    ],
                    "fields": {
                      "from": "from",
                      "to": "to",
                      "value": "value"
                    }
                  }
                },
                "ref": "pihole"
              },
              "to": [
                {
                  "color": "green",
                  "label": "Allowed"
                },
                {
                  "color": "grey",
                  "label": "Blocked"
                }
              ],
              "type": "flow"
            },
            {
              "id": "share",
              "label": "Share blocked",
              "max": 100,
              "source": {
                "map": {
                  "items": {
                    "each": "client_rows",
                    "fields": {
                      "label": "name",
                      "value": {
                        "path": "share",
                        "round": 0
                      }
                    }
                  }
                },
                "ref": "pihole"
              },
              "type": "bars",
              "unit": "%"
            }
          ],
          "label": "Traffic"
        },
        {
          "cards": [
            {
              "id": "devices",
              "items": [],
              "label": "Seen on the network",
              "source": {
                "map": {
                  "items": {
                    "each": "devices.devices",
                    "fields": {
                      "detail": {
                        "template": "{ips[0].ip} · {lastQuery|ago}"
                      },
                      "text": {
                        "else": {
                          "else": "ips[0].name",
                          "if": {
                            "equals": "localhost",
                            "path": "ips[0].name"
                          },
                          "then": {
                            "template": "Pi-hole"
                          }
                        },
                        "if": {
                          "equals": "",
                          "value": {
                            "default": "",
                            "path": "ips[0].name"
                          }
                        },
                        "then": "ips[0].ip"
                      }
                    },
                    "limit": 20,
                    "where": {
                      "gt": 0,
                      "path": "lastQuery"
                    }
                  }
                },
                "ref": "pihole"
              },
              "type": "list"
            }
          ],
          "label": "Devices"
        }
      ],
      "type": "tabs"
    }
  ]
}
