{
  "$schema": "./.schemas/page.schema.json",
  "title": "UniFi Network",
  "icon": "wifi.router",
  "sources": {
    "net": {
      "url": "https://192.0.2.1",
      "secret": "unifi-network",
      "auth": {
        "type": "header",
        "name": "X-API-KEY"
      },
      "every": "2m",
      "requests": {
        "info": "/proxy/network/integration/v1/info",
        "sites": "/proxy/network/integration/v1/sites?limit=200",
        "devices": {
          "path": "/proxy/network/integration/v1/sites/{id}/devices?limit=200",
          "each_row": {
            "from": "sites.data",
            "key": "id",
            "max": 4
          }
        },
        "clients": {
          "path": "/proxy/network/integration/v1/sites/{id}/clients?limit=1",
          "each_row": {
            "from": "sites.data",
            "key": "id",
            "max": 4,
            "pick": [
              "totalCount"
            ]
          }
        },
        "wifi": {
          "path": "/proxy/network/integration/v1/sites/{id}/clients?limit=1&filter=type.eq(%27WIRELESS%27)",
          "each_row": {
            "from": "sites.data",
            "key": "id",
            "max": 4,
            "pick": [
              "totalCount"
            ]
          }
        },
        "stats": {
          "path": "/proxy/network/integration/v1/sites/{parent_key}/devices/{id}/statistics/latest",
          "each_row": {
            "from": "devices.data",
            "key": "id",
            "max": 40,
            "pick": [
              "uptimeSec",
              "cpuUtilizationPct",
              "memoryUtilizationPct"
            ]
          }
        }
      },
      "derive": [
        {
          "name": "dev_flat",
          "value": {
            "each": "devices",
            "where": {
              "path": "answer",
              "exists": true
            },
            "flatten": "answer.data",
            "fields": {
              "id": "id",
              "site": "parent.key",
              "name": "name",
              "model": "model",
              "state": "state",
              "fw": "firmwareVersion",
              "upd": "firmwareUpdatable",
              "kind": {
                "if": {
                  "value": {
                    "path": "model",
                    "match": "^(UDM|UDR|UDW|UCG|UXG|UX7|UX |UX$|USG|UGW|EFG|Dream |Cloud Gateway|Express)"
                  },
                  "exists": true
                },
                "then": {
                  "template": "gateway"
                },
                "else": {
                  "if": {
                    "path": "features",
                    "contains": "accessPoint"
                  },
                  "then": {
                    "template": "ap"
                  },
                  "else": {
                    "if": {
                      "path": "features",
                      "contains": "switching"
                    },
                    "then": {
                      "template": "switch"
                    },
                    "else": {
                      "template": "other"
                    }
                  }
                }
              },
              "words": {
                "path": "state",
                "values": {
                  "ONLINE": "Online",
                  "OFFLINE": "Offline",
                  "CONNECTION_INTERRUPTED": "Connection interrupted",
                  "ISOLATED": "Isolated",
                  "UPDATING": "Updating",
                  "GETTING_READY": "Getting ready",
                  "ADOPTING": "Adopting",
                  "PENDING_ADOPTION": "Waiting to be adopted",
                  "DELETING": "Being removed"
                },
                "default": "Unknown state"
              },
              "mac_tail": {
                "path": "macAddress",
                "match": "([0-9A-Fa-f]{2}:?[0-9A-Fa-f]{2})$",
                "default": {
                  "template": ""
                }
              }
            }
          }
        },
        {
          "name": "dev_named",
          "value": {
            "each": "dev_flat",
            "lookup": {
              "from": "stats",
              "by": "key",
              "on": "id",
              "as": "st"
            },
            "fields": {
              "base": {
                "if": {
                  "all": [
                    {
                      "path": "name",
                      "exists": true
                    },
                    {
                      "path": "name",
                      "not": ""
                    }
                  ]
                },
                "then": {
                  "template": "{name|clip:24}"
                },
                "else": {
                  "if": {
                    "all": [
                      {
                        "path": "model",
                        "exists": true
                      },
                      {
                        "path": "model",
                        "not": ""
                      }
                    ]
                  },
                  "then": {
                    "template": "{model|clip:18} {mac_tail|raw}"
                  },
                  "else": {
                    "template": "Device {mac_tail|raw}"
                  }
                }
              },
              "id": "id",
              "site": "site",
              "state": "state",
              "kind": "kind",
              "words": "words",
              "model": "model",
              "fw": "fw",
              "upd": "upd",
              "mac_tail": "mac_tail",
              "up_s": "st.answer.uptimeSec",
              "cpu": "st.answer.cpuUtilizationPct",
              "mem": "st.answer.memoryUtilizationPct"
            }
          }
        },
        {
          "name": "dev_up",
          "value": {
            "each": "dev_named",
            "fields": {
              "base": "base",
              "id": "id",
              "site": "site",
              "state": "state",
              "kind": "kind",
              "words": "words",
              "model": "model",
              "fw": "fw",
              "upd": "upd",
              "mac_tail": "mac_tail",
              "cpu": "cpu",
              "mem": "mem",
              "up_s": "up_s",
              "up_days": {
                "path": "up_s",
                "scale": 1.1574074074074073e-05,
                "round": 0
              }
            }
          }
        },
        {
          "name": "dev_up_text",
          "value": {
            "each": "dev_up",
            "fields": {
              "base": "base",
              "id": "id",
              "site": "site",
              "state": "state",
              "kind": "kind",
              "words": "words",
              "model": "model",
              "fw": "fw",
              "upd": "upd",
              "mac_tail": "mac_tail",
              "cpu": "cpu",
              "mem": "mem",
              "up_s": "up_s",
              "up_text": {
                "if": {
                  "path": "up_s",
                  "gte": 129600
                },
                "then": {
                  "template": "{up_days} days"
                },
                "else": {
                  "if": {
                    "path": "up_s",
                    "gte": 64800
                  },
                  "then": {
                    "template": "1 day"
                  },
                  "else": {
                    "template": "{up_s|duration:s}"
                  }
                }
              }
            }
          }
        },
        {
          "name": "dev_up_for",
          "value": {
            "each": "dev_up_text",
            "fields": {
              "base": "base",
              "id": "id",
              "site": "site",
              "state": "state",
              "kind": "kind",
              "words": "words",
              "model": "model",
              "fw": "fw",
              "upd": "upd",
              "mac_tail": "mac_tail",
              "cpu": "cpu",
              "mem": "mem",
              "up_for": {
                "if": {
                  "path": "up_s",
                  "exists": true
                },
                "then": {
                  "template": " · up {up_text}"
                },
                "else": {
                  "template": ""
                }
              }
            }
          }
        },
        {
          "name": "dev_by_name",
          "value": {
            "each": "dev_up_for",
            "group": "base",
            "fields": {
              "key": "key",
              "count": "count",
              "rows": "rows"
            }
          }
        },
        {
          "name": "dev_rows",
          "value": {
            "each": "dev_by_name",
            "flatten": "rows",
            "fields": {
              "name": {
                "if": {
                  "path": "parent.count",
                  "gt": 1
                },
                "then": {
                  "template": "{base|clip:19} {mac_tail|raw}"
                },
                "else": {
                  "template": "{base}"
                }
              },
              "id": "id",
              "state": {
                "path": "state",
                "values": {
                  "ONLINE": "ok",
                  "OFFLINE": "critical",
                  "CONNECTION_INTERRUPTED": "warning",
                  "ISOLATED": "warning"
                },
                "default": "unknown"
              },
              "raw_state": "state",
              "detail": {
                "if": {
                  "path": "state",
                  "equals": "ONLINE"
                },
                "then": {
                  "if": {
                    "path": "upd",
                    "equals": true
                  },
                  "then": {
                    "template": "{model} · {fw} · update ready"
                  },
                  "else": {
                    "template": "{model} · {fw}{up_for}"
                  }
                },
                "else": {
                  "template": "{words} · {model}"
                }
              },
              "group": {
                "path": "kind",
                "values": {
                  "gateway": "Gateway",
                  "switch": "Switches",
                  "ap": "Access points"
                },
                "default": "Other"
              },
              "order": {
                "path": "kind",
                "values": {
                  "gateway": 0,
                  "switch": 1,
                  "ap": 2
                },
                "default": 3
              },
              "update": {
                "if": {
                  "path": "upd",
                  "equals": true
                },
                "then": {
                  "template": "yes"
                },
                "else": {
                  "template": "no"
                }
              },
              "cpu": "cpu",
              "mem": "mem",
              "rank": {
                "path": "state",
                "values": {
                  "OFFLINE": 0,
                  "CONNECTION_INTERRUPTED": 1,
                  "ISOLATED": 1,
                  "ONLINE": 3
                },
                "default": 2
              },
              "url": {
                "template": "legato://page/unifi-network-device?site={site|raw}&device={id|raw}"
              }
            }
          }
        },
        {
          "name": "dev_picked",
          "value": {
            "each": "dev_rows",
            "sort": {
              "by": [
                "rank",
                "order",
                "name"
              ]
            },
            "limit": 60,
            "fields": {
              "name": "name",
              "state": "state",
              "detail": {
                "template": "{detail|clip:80}"
              },
              "group": "group",
              "order": "order",
              "url": "url"
            }
          }
        },
        {
          "name": "dev_shown",
          "value": {
            "each": "dev_picked",
            "sort": {
              "by": [
                "order",
                "name"
              ]
            },
            "fields": {
              "name": "name",
              "state": "state",
              "detail": "detail",
              "group": "group",
              "url": "url"
            }
          }
        },
        {
          "name": "dev_total",
          "value": {
            "count": "dev_rows",
            "default": 0
          }
        },
        {
          "name": "dev_offline",
          "value": {
            "count": "dev_rows",
            "where": {
              "path": "raw_state",
              "equals": "OFFLINE"
            },
            "default": 0
          }
        },
        {
          "name": "dev_updates",
          "value": {
            "count": "dev_rows",
            "where": {
              "path": "update",
              "equals": "yes"
            },
            "default": 0
          }
        },
        {
          "name": "dev_cut",
          "value": {
            "if": {
              "path": "dev_total",
              "gt": 60
            },
            "then": {
              "template": "yes"
            },
            "else": {
              "template": "no"
            }
          }
        },
        {
          "name": "n_sites",
          "value": {
            "count": "sites.data",
            "default": 0
          }
        },
        {
          "name": "listed",
          "value": {
            "sum": "answer.totalCount",
            "of": "devices",
            "default": 0
          }
        },
        {
          "name": "more",
          "value": {
            "if": {
              "any": [
                {
                  "path": "n_sites",
                  "gt": 4
                },
                {
                  "path": "listed",
                  "gt": {
                    "path": "dev_total"
                  }
                }
              ]
            },
            "then": {
              "template": "yes"
            },
            "else": {
              "template": "no"
            }
          }
        },
        {
          "name": "n_clients",
          "value": {
            "sum": "answer.totalCount",
            "of": "clients",
            "default": 0
          }
        },
        {
          "name": "n_wifi",
          "value": {
            "sum": "answer.totalCount",
            "of": "wifi",
            "default": 0
          }
        },
        {
          "name": "site_failed",
          "value": {
            "add": [
              {
                "count": "devices",
                "where": {
                  "path": "error",
                  "exists": true
                },
                "default": 0
              },
              {
                "count": "clients",
                "where": {
                  "path": "error",
                  "exists": true
                },
                "default": 0
              },
              {
                "count": "wifi",
                "where": {
                  "path": "error",
                  "exists": true
                },
                "default": 0
              }
            ]
          }
        },
        {
          "name": "partial",
          "value": {
            "if": {
              "any": [
                {
                  "path": "more",
                  "equals": "yes"
                },
                {
                  "path": "site_failed",
                  "gt": 0
                }
              ]
            },
            "then": {
              "template": "yes"
            },
            "else": {
              "template": "no"
            }
          }
        }
      ],
      "collect": {
        "dataset": "unifi-network-devices",
        "each": "dev_rows",
        "key": "{id}",
        "sample": {
          "every": "5m"
        },
        "fields": {
          "state": "state",
          "cpu": {
            "path": "cpu",
            "round": 1
          },
          "mem": {
            "path": "mem",
            "round": 1
          }
        },
        "readers": [
          "unifi-network-device"
        ]
      }
    },
    "health": {
      "url": "https://192.0.2.1",
      "secret": "unifi-network",
      "auth": {
        "type": "header",
        "name": "X-API-KEY"
      },
      "every": "2m",
      "requests": {
        "sites": "/proxy/network/api/stat/sites"
      },
      "fail": {
        "when": {
          "any": [
            {
              "path": "sites.meta.rc",
              "exists": false
            },
            {
              "path": "sites.meta.rc",
              "not": "ok"
            }
          ]
        },
        "message": {
          "if": {
            "all": [
              {
                "path": "sites.meta.msg",
                "exists": true
              },
              {
                "path": "sites.meta.msg",
                "not": ""
              }
            ]
          },
          "then": {
            "template": "UniFi's health answer said {sites.meta.msg}"
          },
          "else": {
            "template": "UniFi's health answer has no meta.rc"
          }
        }
      },
      "derive": [
        {
          "name": "site_parts",
          "value": {
            "each": "sites.data",
            "fields": {
              "id": "external_id",
              "desc": "desc",
              "w": {
                "each": "health",
                "where": {
                  "path": "subsystem",
                  "equals": "wan"
                },
                "limit": 1,
                "fields": {
                  "status": "status",
                  "isp": "isp_name",
                  "gw_name": "gw_name",
                  "gw_version": "gw_version",
                  "cpu": "gw_system-stats.cpu",
                  "mem": "gw_system-stats.mem",
                  "up_s": "gw_system-stats.uptime",
                  "gateways": {
                    "path": "num_gw",
                    "default": 0
                  },
                  "gw_off": {
                    "path": "num_disconnected",
                    "default": 0
                  },
                  "wans": {
                    "count": "uptime_stats",
                    "entries": true,
                    "default": 0
                  },
                  "up": {
                    "count": "uptime_stats",
                    "entries": true,
                    "where": {
                      "all": [
                        {
                          "path": "value.availability",
                          "exists": true
                        },
                        {
                          "path": "value.availability",
                          "gt": 0
                        }
                      ]
                    },
                    "default": 0
                  },
                  "up_names": {
                    "join": "key",
                    "of": "uptime_stats",
                    "entries": true,
                    "where": {
                      "all": [
                        {
                          "path": "value.availability",
                          "exists": true
                        },
                        {
                          "path": "value.availability",
                          "gt": 0
                        }
                      ]
                    },
                    "separator": ", "
                  },
                  "down_names": {
                    "join": "key",
                    "of": "uptime_stats",
                    "entries": true,
                    "where": {
                      "any": [
                        {
                          "path": "value.availability",
                          "exists": false
                        },
                        {
                          "path": "value.availability",
                          "lte": 0
                        }
                      ]
                    },
                    "separator": ", "
                  }
                }
              },
              "x": {
                "each": "health",
                "where": {
                  "path": "subsystem",
                  "equals": "www"
                },
                "limit": 1,
                "fields": {
                  "latency": "latency",
                  "drops": "drops",
                  "down": "xput_down",
                  "upload": "xput_up",
                  "ping": "speedtest_ping",
                  "ran": "speedtest_lastrun",
                  "test": "speedtest_status"
                }
              }
            }
          }
        },
        {
          "name": "site_words",
          "value": {
            "each": "site_parts",
            "fields": {
              "id": "id",
              "desc": "desc",
              "w": "w",
              "x": "x",
              "not_connected": {
                "if": {
                  "all": [
                    {
                      "path": "w[0].down_names",
                      "exists": true
                    },
                    {
                      "path": "w[0].down_names",
                      "not": ""
                    }
                  ]
                },
                "then": {
                  "template": " · {w[0].down_names} not connected"
                },
                "else": {
                  "template": ""
                }
              },
              "latency": {
                "if": {
                  "path": "x[0].latency",
                  "gt": 0
                },
                "then": {
                  "template": " · {x[0].latency} ms"
                },
                "else": {
                  "template": ""
                }
              }
            }
          }
        },
        {
          "name": "site_rows",
          "value": {
            "each": "site_words",
            "fields": {
              "id": "id",
              "name": {
                "if": {
                  "path": "desc",
                  "in": [
                    "Default",
                    "default",
                    ""
                  ]
                },
                "then": {
                  "if": {
                    "all": [
                      {
                        "path": "w[0].gw_name",
                        "exists": true
                      },
                      {
                        "path": "w[0].gw_name",
                        "not": ""
                      }
                    ]
                  },
                  "then": {
                    "template": "{w[0].gw_name|clip:24}"
                  },
                  "else": {
                    "template": "Default"
                  }
                },
                "else": {
                  "template": "{desc|clip:24}"
                }
              },
              "state": {
                "if": {
                  "path": "w[0].gateways",
                  "lt": 1
                },
                "then": {
                  "template": "unknown"
                },
                "else": {
                  "if": {
                    "path": "w[0].gw_off",
                    "gt": 0
                  },
                  "then": {
                    "template": "critical"
                  },
                  "else": {
                    "if": {
                      "path": "w[0].wans",
                      "equals": 0
                    },
                    "then": {
                      "template": "unknown"
                    },
                    "else": {
                      "if": {
                        "path": "w[0].up",
                        "equals": 0
                      },
                      "then": {
                        "template": "critical"
                      },
                      "else": {
                        "template": "ok"
                      }
                    }
                  }
                }
              },
              "detail": {
                "if": {
                  "path": "w[0].gateways",
                  "lt": 1
                },
                "then": {
                  "template": "UniFi reports no gateway for this site"
                },
                "else": {
                  "if": {
                    "path": "w[0].gw_off",
                    "gt": 0
                  },
                  "then": {
                    "template": "Gateway offline"
                  },
                  "else": {
                    "if": {
                      "path": "w[0].wans",
                      "equals": 0
                    },
                    "then": {
                      "template": "UniFi hasn't reported its WANs"
                    },
                    "else": {
                      "if": {
                        "path": "w[0].up",
                        "equals": 0
                      },
                      "then": {
                        "template": "No WAN connected{not_connected}"
                      },
                      "else": {
                        "if": {
                          "all": [
                            {
                              "path": "w[0].isp",
                              "exists": true
                            },
                            {
                              "path": "w[0].isp",
                              "not": ""
                            }
                          ]
                        },
                        "then": {
                          "template": "{w[0].isp} · {w[0].up_names} up{not_connected}{latency}"
                        },
                        "else": {
                          "template": "{w[0].up_names} up{not_connected}{latency}"
                        }
                      }
                    }
                  }
                }
              },
              "line": {
                "if": {
                  "value": {
                    "if": {
                      "path": "w[0].gateways",
                      "lt": 1
                    },
                    "then": {
                      "template": "unknown"
                    },
                    "else": {
                      "if": {
                        "path": "w[0].gw_off",
                        "gt": 0
                      },
                      "then": {
                        "template": "critical"
                      },
                      "else": {
                        "if": {
                          "path": "w[0].wans",
                          "equals": 0
                        },
                        "then": {
                          "template": "unknown"
                        },
                        "else": {
                          "if": {
                            "path": "w[0].up",
                            "equals": 0
                          },
                          "then": {
                            "template": "critical"
                          },
                          "else": {
                            "template": "ok"
                          }
                        }
                      }
                    }
                  },
                  "equals": "ok"
                },
                "then": {
                  "if": {
                    "all": [
                      {
                        "path": "w[0].isp",
                        "exists": true
                      },
                      {
                        "path": "w[0].isp",
                        "not": ""
                      }
                    ]
                  },
                  "then": {
                    "template": "{w[0].isp} · {w[0].up_names} up{latency}"
                  },
                  "else": {
                    "template": "{w[0].up_names} up{latency}"
                  }
                },
                "else": {
                  "if": {
                    "path": "w[0].gateways",
                    "lt": 1
                  },
                  "then": {
                    "template": "UniFi reports no gateway for this site"
                  },
                  "else": {
                    "if": {
                      "path": "w[0].gw_off",
                      "gt": 0
                    },
                    "then": {
                      "template": "Gateway offline"
                    },
                    "else": {
                      "if": {
                        "path": "w[0].wans",
                        "equals": 0
                      },
                      "then": {
                        "template": "UniFi hasn't reported its WANs"
                      },
                      "else": {
                        "if": {
                          "path": "w[0].up",
                          "equals": 0
                        },
                        "then": {
                          "template": "No WAN connected{not_connected}"
                        },
                        "else": {
                          "if": {
                            "all": [
                              {
                                "path": "w[0].isp",
                                "exists": true
                              },
                              {
                                "path": "w[0].isp",
                                "not": ""
                              }
                            ]
                          },
                          "then": {
                            "template": "{w[0].isp} · {w[0].up_names} up{not_connected}{latency}"
                          },
                          "else": {
                            "template": "{w[0].up_names} up{not_connected}{latency}"
                          }
                        }
                      }
                    }
                  }
                }
              },
              "ms": "x[0].latency",
              "up": "w[0].up",
              "w": "w",
              "x": "x"
            }
          }
        },
        {
          "name": "n_sites",
          "value": {
            "count": "site_rows",
            "default": 0
          }
        },
        {
          "name": "n_ok",
          "value": {
            "count": "site_rows",
            "where": {
              "path": "state",
              "equals": "ok"
            },
            "default": 0
          }
        },
        {
          "name": "one",
          "value": {
            "each": "site_rows",
            "limit": 1,
            "fields": {
              "line": "line",
              "w": "w",
              "x": "x",
              "name": "name"
            }
          }
        },
        {
          "name": "site_ranked",
          "value": {
            "each": "site_rows",
            "fields": {
              "name": "name",
              "state": "state",
              "detail": "detail",
              "rank": {
                "path": "state",
                "values": {
                  "critical": 0,
                  "warning": 1,
                  "unknown": 2
                },
                "default": 3
              }
            }
          }
        },
        {
          "name": "site_shown",
          "value": {
            "each": "site_ranked",
            "sort": {
              "by": [
                "rank",
                "name"
              ]
            },
            "limit": 60,
            "fields": {
              "name": "name",
              "state": "state",
              "detail": {
                "template": "{detail|clip:80}"
              }
            }
          }
        }
      ],
      "collect": {
        "dataset": "unifi-network-latency",
        "each": "site_rows",
        "key": "{id}",
        "sample": {
          "every": "5m"
        },
        "fields": {
          "ms": "ms",
          "up": "up"
        }
      }
    },
    "latency": {
      "dataset": "unifi-network-latency",
      "derive": [
        {
          "name": "keys",
          "value": {
            "each": "kept.rows",
            "group": "key",
            "fields": {
              "key": "key"
            }
          }
        },
        {
          "name": "n_keys",
          "value": {
            "count": "keys",
            "default": 0
          }
        },
        {
          "name": "n_rows",
          "value": {
            "count": "kept.rows",
            "where": {
              "path": "row.ms",
              "exists": true
            },
            "default": 0
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "internet-health",
      "type": "fleet",
      "label": "Internet",
      "placement": "page_info",
      "items": [],
      "source": {
        "ref": "health",
        "failed": {
          "state": "unknown",
          "detail": "The console's health answer isn't coming",
          "after": 3
        },
        "empty": {
          "text": "The console lists no sites.",
          "when": {
            "path": "n_sites",
            "equals": 0
          }
        },
        "map": {
          "label": {
            "if": {
              "path": "n_sites",
              "gt": 60
            },
            "then": {
              "template": "Internet, 60 of {n_sites}"
            },
            "else": {
              "template": "Internet"
            }
          },
          "items": {
            "each": "site_shown",
            "sort": "name",
            "fields": {
              "name": "name",
              "state": "state",
              "detail": "detail"
            }
          }
        }
      }
    },
    {
      "id": "internet",
      "type": "facts",
      "label": "Internet",
      "items": [
        {
          "key": "Internet",
          "value": "—"
        },
        {
          "key": "Speed test",
          "value": "—"
        },
        {
          "key": "Gateway",
          "value": "—"
        }
      ],
      "source": {
        "ref": "health",
        "when": {
          "path": "n_sites",
          "equals": 1
        },
        "map": {
          "items[0].value": {
            "template": "{one[0].line|clip:80}"
          },
          "items[1].value": {
            "if": {
              "all": [
                {
                  "path": "one[0].x[0].ran",
                  "gt": 0
                },
                {
                  "path": "one[0].x[0].down",
                  "gt": 0
                }
              ]
            },
            "then": {
              "template": "{one[0].x[0].down|round:0} down · {one[0].x[0].upload|round:0} up Mbps · {one[0].x[0].ping} ms · {one[0].x[0].ran|ago}"
            },
            "else": {
              "template": "No test yet"
            }
          },
          "items[2].value": {
            "if": {
              "all": [
                {
                  "path": "one[0].w[0].gw_version",
                  "exists": true
                },
                {
                  "path": "one[0].w[0].gw_version",
                  "not": ""
                }
              ]
            },
            "then": {
              "template": "{one[0].w[0].gw_version} · CPU {one[0].w[0].cpu|round:0}% · memory {one[0].w[0].mem|round:0}%"
            },
            "else": {
              "template": "—"
            }
          }
        }
      }
    },
    {
      "id": "internet-sites",
      "type": "facts",
      "label": "Internet",
      "items": [
        {
          "key": "Internet",
          "value": "—"
        }
      ],
      "source": {
        "ref": "health",
        "when": {
          "path": "n_sites",
          "gt": 1
        },
        "map": {
          "items[0].value": {
            "template": "{n_ok} of {n_sites} sites connected"
          }
        }
      }
    },
    {
      "id": "network",
      "type": "facts",
      "label": "Network",
      "items": [
        {
          "key": "Clients",
          "value": "—"
        },
        {
          "key": "Devices",
          "value": "—"
        },
        {
          "key": "Network",
          "value": "—"
        }
      ],
      "source": {
        "ref": "net",
        "map": {
          "items[0].value": {
            "if": {
              "path": "n_wifi",
              "gt": 0
            },
            "then": {
              "template": "{n_clients} · {n_wifi} Wi-Fi"
            },
            "else": {
              "template": "{n_clients}"
            }
          },
          "items[1].value": {
            "if": {
              "path": "dev_offline",
              "gt": 0
            },
            "then": {
              "if": {
                "path": "dev_updates",
                "gt": 0
              },
              "then": {
                "template": "{dev_total} · {dev_offline} offline · {dev_updates} updates ready"
              },
              "else": {
                "template": "{dev_total} · {dev_offline} offline"
              }
            },
            "else": {
              "if": {
                "path": "dev_updates",
                "equals": 1
              },
              "then": {
                "template": "{dev_total} · 1 update ready"
              },
              "else": {
                "if": {
                  "path": "dev_updates",
                  "gt": 1
                },
                "then": {
                  "template": "{dev_total} · {dev_updates} updates ready"
                },
                "else": {
                  "template": "{dev_total}"
                }
              }
            }
          },
          "items[2].value": {
            "if": {
              "all": [
                {
                  "path": "info.applicationVersion",
                  "exists": true
                },
                {
                  "path": "info.applicationVersion",
                  "not": ""
                }
              ]
            },
            "then": {
              "template": "{info.applicationVersion}"
            },
            "else": {
              "template": "—"
            }
          }
        }
      }
    },
    {
      "id": "latency",
      "type": "sparkline",
      "label": "Latency",
      "unit": "ms",
      "change": false,
      "points": [],
      "source": {
        "ref": "latency",
        "when": {
          "all": [
            {
              "path": "n_keys",
              "equals": 1
            },
            {
              "path": "n_rows",
              "gte": 2
            }
          ]
        },
        "map": {
          "points": {
            "each": "kept.rows",
            "where": {
              "path": "row.ms",
              "exists": true
            },
            "buckets": 144,
            "value": "row.ms"
          },
          "times": {
            "each": "kept.rows",
            "where": {
              "path": "row.ms",
              "exists": true
            },
            "buckets": 144,
            "template": "{at}"
          },
          "labels": {
            "each": "kept.rows",
            "where": {
              "path": "row.ms",
              "exists": true
            },
            "buckets": 144,
            "template": "{at|time}"
          }
        }
      }
    },
    {
      "id": "more",
      "type": "text",
      "tone": "info",
      "body": "This console has more sites or devices than the page reads. The counts here cover part of them, and the UniFi app has the rest.",
      "source": {
        "ref": "net",
        "when": {
          "path": "partial",
          "equals": "yes"
        },
        "map": {
          "body": {
            "if": {
              "path": "site_failed",
              "gt": 0
            },
            "then": {
              "template": "The console didn't send every site's devices or clients just now, so the counts here cover part of them."
            },
            "else": {
              "template": "This console has more sites or devices than the page reads. The counts here cover part of them, and the UniFi app has the rest."
            }
          }
        }
      }
    },
    {
      "id": "devices",
      "type": "fleet",
      "label": "Devices",
      "max_items": 24,
      "items": [],
      "source": {
        "ref": "net",
        "failed": {
          "state": "critical",
          "detail": "The console isn't answering",
          "after": 3
        },
        "empty": {
          "text": "The console lists no devices.",
          "when": {
            "all": [
              {
                "path": "dev_total",
                "equals": 0
              },
              {
                "path": "site_failed",
                "equals": 0
              }
            ]
          }
        },
        "map": {
          "label": {
            "if": {
              "path": "dev_cut",
              "equals": "yes"
            },
            "then": {
              "template": "Devices, 60 of {dev_total}"
            },
            "else": {
              "template": "Devices"
            }
          },
          "items": {
            "each": "dev_shown",
            "fields": {
              "name": "name",
              "state": "state",
              "detail": "detail",
              "group": "group",
              "url": "url"
            }
          }
        }
      }
    }
  ]
}
