{
  "$schema": "./.schemas/page.schema.json",
  "title": "Site",
  "icon": "wifi.router",
  "sidebar": false,
  "params": {
    "host": {
      "example": "E4A1C0D2B3F40516273849AB5C6D7E8F90A1B2C3D4E5F60718293A4B5C6D7E8F90:1987654321",
      "pattern": "[0-9A-Fa-f]{1,100}:[0-9]{1,20}|[0-9a-fA-F-]{36}"
    },
    "site": {
      "example": "65f1c0a2b3d4e5f6a7b8c9d0",
      "pattern": "[0-9a-f]{24}"
    }
  },
  "title_from": {
    "ref": "site",
    "value": {
      "first": "name",
      "of": "site_rows",
      "where": {
        "path": "id",
        "equals": "{param.site}"
      }
    }
  },
  "period": {
    "choices": [
      {
        "id": "today",
        "label": "Today",
        "last_days": 1,
        "values": {
          "interval": "5m"
        }
      },
      {
        "id": "week",
        "label": "7 days",
        "last_days": 7,
        "values": {
          "interval": "1h"
        }
      },
      {
        "id": "month",
        "label": "30 days",
        "last_days": 30,
        "values": {
          "interval": "1h"
        }
      }
    ]
  },
  "sources": {
    "site": {
      "url": "https://api.ui.com",
      "secret": "unifi",
      "auth": {
        "type": "header",
        "name": "X-API-Key"
      },
      "every": "5m",
      "requests": {
        "sites": {
          "path": "/v1/sites?pageSize=500",
          "follow": {
            "items": "data",
            "param": "nextToken",
            "from": "nextToken",
            "max": 4
          }
        },
        "hosts": {
          "path": "/v1/hosts?pageSize=50",
          "follow": {
            "items": "data",
            "param": "nextToken",
            "from": "nextToken",
            "max": 4
          }
        },
        "devices": {
          "path": "/v1/devices?pageSize=500",
          "follow": {
            "items": "data",
            "param": "nextToken",
            "from": "nextToken",
            "max": 4
          }
        }
      },
      "derive": [
        {
          "name": "console_all",
          "value": {
            "each": "devices.data",
            "flatten": "devices",
            "fields": {
              "host": "parent.hostId",
              "status": "status",
              "console": "isConsole"
            }
          }
        },
        {
          "name": "console_devs",
          "value": {
            "each": "console_all",
            "where": {
              "path": "console",
              "equals": true
            },
            "fields": {
              "host": "host",
              "status": "status"
            }
          }
        },
        {
          "name": "site_list",
          "value": {
            "each": "sites.data",
            "lookup": {
              "from": "hosts.data",
              "by": "id",
              "on": "hostId",
              "as": "host"
            },
            "fields": {
              "host_id": "hostId",
              "id": "siteId",
              "name": {
                "if": {
                  "path": "meta.desc",
                  "in": [
                    "Default",
                    "default",
                    ""
                  ]
                },
                "then": {
                  "if": {
                    "all": [
                      {
                        "path": "host.reportedState.name",
                        "exists": true
                      },
                      {
                        "path": "host.reportedState.name",
                        "not": ""
                      }
                    ]
                  },
                  "then": {
                    "template": "{host.reportedState.name}"
                  },
                  "else": {
                    "template": "Default"
                  }
                },
                "else": {
                  "template": "{meta.desc}"
                }
              },
              "isp": "statistics.ispInfo.name",
              "wans": {
                "count": "statistics.wans",
                "entries": true,
                "default": 0
              },
              "up": {
                "count": "statistics.wans",
                "entries": true,
                "where": {
                  "path": "value.portUp",
                  "equals": true
                },
                "default": 0
              },
              "up_names": {
                "join": "key",
                "of": "statistics.wans",
                "entries": true,
                "where": {
                  "path": "value.portUp",
                  "equals": true
                },
                "separator": ", "
              },
              "down_names": {
                "join": "key",
                "of": "statistics.wans",
                "entries": true,
                "where": {
                  "path": "value.portUp",
                  "not": true
                },
                "separator": ", "
              },
              "gateway_off": {
                "path": "statistics.counts.offlineGatewayDevice",
                "default": 0
              },
              "connected": {
                "if": {
                  "path": "host.reportedState.state",
                  "equals": "connected"
                },
                "then": {
                  "template": "yes"
                },
                "else": {
                  "if": {
                    "path": "host.reportedState.state",
                    "equals": "disconnected"
                  },
                  "then": {
                    "template": "no"
                  },
                  "else": {
                    "template": "unknown"
                  }
                }
              },
              "since": "host.lastConnectionStateChange",
              "counts": "statistics.counts",
              "retry": "statistics.percentages.txRetry",
              "gateway": "statistics.gateway.shortname"
            }
          }
        },
        {
          "name": "site_words",
          "value": {
            "each": "site_list",
            "lookup": {
              "from": "console_devs",
              "by": "host",
              "on": "host_id",
              "as": "cdev"
            },
            "fields": {
              "host_id": "host_id",
              "id": "id",
              "name": "name",
              "isp": "isp",
              "wans": "wans",
              "up": "up",
              "up_names": "up_names",
              "down_names": "down_names",
              "gateway_off": "gateway_off",
              "connected": {
                "if": {
                  "path": "cdev.status",
                  "equals": "offline"
                },
                "then": {
                  "template": "no"
                },
                "else": "connected"
              },
              "since": "since",
              "counts": "counts",
              "retry": "retry",
              "gateway": "gateway",
              "not_connected": {
                "if": {
                  "all": [
                    {
                      "path": "down_names",
                      "exists": true
                    },
                    {
                      "path": "down_names",
                      "not": ""
                    }
                  ]
                },
                "then": {
                  "template": " · {down_names} not connected"
                },
                "else": {
                  "template": ""
                }
              }
            }
          }
        },
        {
          "name": "site_rows",
          "value": {
            "each": "site_words",
            "fields": {
              "host_id": "host_id",
              "id": "id",
              "name": "name",
              "isp": "isp",
              "wans": "wans",
              "up": "up",
              "up_names": "up_names",
              "down_names": "down_names",
              "gateway_off": "gateway_off",
              "connected": "connected",
              "since": "since",
              "counts": "counts",
              "retry": "retry",
              "gateway": "gateway",
              "state": {
                "if": {
                  "path": "connected",
                  "equals": "no"
                },
                "then": {
                  "template": "critical"
                },
                "else": {
                  "if": {
                    "path": "connected",
                    "equals": "unknown"
                  },
                  "then": {
                    "template": "unknown"
                  },
                  "else": {
                    "if": {
                      "path": "gateway_off",
                      "gt": 0
                    },
                    "then": {
                      "template": "critical"
                    },
                    "else": {
                      "if": {
                        "path": "wans",
                        "equals": 0
                      },
                      "then": {
                        "template": "unknown"
                      },
                      "else": {
                        "if": {
                          "path": "up",
                          "equals": 0
                        },
                        "then": {
                          "template": "critical"
                        },
                        "else": {
                          "template": "ok"
                        }
                      }
                    }
                  }
                }
              },
              "detail": {
                "if": {
                  "path": "connected",
                  "equals": "no"
                },
                "then": {
                  "if": {
                    "all": [
                      {
                        "path": "since",
                        "exists": true
                      },
                      {
                        "path": "since",
                        "not": ""
                      }
                    ]
                  },
                  "then": {
                    "template": "Not reporting to UniFi since {since|ago}"
                  },
                  "else": {
                    "template": "Not reporting to UniFi"
                  }
                },
                "else": {
                  "if": {
                    "path": "connected",
                    "equals": "unknown"
                  },
                  "then": {
                    "template": "UniFi has no report from its console"
                  },
                  "else": {
                    "if": {
                      "path": "gateway_off",
                      "gt": 0
                    },
                    "then": {
                      "template": "Gateway offline"
                    },
                    "else": {
                      "if": {
                        "path": "wans",
                        "equals": 0
                      },
                      "then": {
                        "template": "UniFi hasn't reported its WANs"
                      },
                      "else": {
                        "if": {
                          "path": "up",
                          "equals": 0
                        },
                        "then": {
                          "template": "No WAN connected{not_connected}"
                        },
                        "else": {
                          "if": {
                            "all": [
                              {
                                "path": "isp",
                                "exists": true
                              },
                              {
                                "path": "isp",
                                "not": ""
                              }
                            ]
                          },
                          "then": {
                            "template": "{isp} · {up_names} up{not_connected}"
                          },
                          "else": {
                            "template": "{up_names} up{not_connected}"
                          }
                        }
                      }
                    }
                  }
                }
              },
              "link": {
                "template": "legato://page/unifi-site?site={id|raw}&host={host_id|raw}"
              }
            }
          }
        },
        {
          "name": "found",
          "value": {
            "count": "site_rows",
            "where": {
              "path": "id",
              "equals": "{param.site}"
            },
            "default": 0
          }
        },
        {
          "name": "s",
          "value": {
            "each": "site_rows",
            "where": {
              "path": "id",
              "equals": "{param.site}"
            },
            "limit": 1,
            "fields": {
              "name": "name",
              "state": "state",
              "detail": "detail",
              "counts": "counts",
              "retry": "retry",
              "gateway": "gateway"
            }
          }
        },
        {
          "name": "site_clients",
          "value": {
            "add": [
              "s[0].counts.wifiClient",
              "s[0].counts.wiredClient"
            ]
          }
        }
      ]
    },
    "metrics": {
      "url": "https://api.ui.com",
      "secret": "unifi",
      "auth": {
        "type": "header",
        "name": "X-API-Key"
      },
      "every": "5m",
      "requests": {
        "metrics": {
          "method": "POST",
          "path": "/v1/isp-metrics/{period.interval}/query",
          "body": {
            "sites": [
              {
                "hostId": "{param.host}",
                "siteId": "{param.site}",
                "beginTimestamp": "{period.start_at}"
              }
            ]
          }
        }
      },
      "fail": {
        "when": {
          "path": "metrics.data.status",
          "equals": "partialSuccess"
        },
        "message": {
          "template": "UniFi didn't send this site's metrics"
        }
      },
      "derive": [
        {
          "name": "span",
          "value": {
            "template": "{period.id}"
          }
        },
        {
          "name": "periods",
          "value": {
            "each": "metrics.data.metrics",
            "where": {
              "path": "siteId",
              "equals": "{param.site}"
            },
            "flatten": "periods",
            "fields": {
              "t": "metricTime",
              "ms": {
                "max": "value.avgLatency",
                "of": "data",
                "entries": true,
                "where": {
                  "path": "value.uptime",
                  "gt": 0
                }
              },
              "pairs": {
                "each": "data",
                "entries": true,
                "fields": {
                  "key": "key",
                  "v": "value"
                }
              }
            }
          }
        },
        {
          "name": "lat",
          "value": {
            "each": "periods",
            "where": {
              "path": "ms",
              "exists": true
            },
            "fields": {
              "t": "t",
              "ms": "ms"
            }
          }
        },
        {
          "name": "lat_n",
          "value": {
            "count": "lat",
            "default": 0
          }
        },
        {
          "name": "wans",
          "value": {
            "each": "periods",
            "flatten": "pairs",
            "group": "key",
            "sort": "key",
            "limit": 8,
            "fields": {
              "key": {
                "template": "{key|upper}"
              },
              "live": {
                "count": "rows",
                "where": {
                  "path": "v.uptime",
                  "gt": 0
                },
                "default": 0
              },
              "uptime": {
                "avg": "v.uptime",
                "of": "rows",
                "round": 1
              },
              "isp": {
                "first": "v.ispName",
                "of": "rows",
                "where": {
                  "all": [
                    {
                      "path": "v.ispName",
                      "exists": true
                    },
                    {
                      "path": "v.ispName",
                      "not": ""
                    }
                  ]
                }
              },
              "n": {
                "count": "rows",
                "default": 0
              },
              "loss_n": {
                "count": "rows",
                "where": {
                  "path": "v.packetLoss",
                  "gt": 0
                },
                "default": 0
              }
            }
          }
        },
        {
          "name": "wan_loss",
          "value": {
            "each": "wans",
            "fields": {
              "key": "key",
              "live": "live",
              "uptime": "uptime",
              "isp": "isp",
              "loss_n": "loss_n",
              "loss_pct": {
                "multiply": [
                  {
                    "divide": [
                      "loss_n",
                      "n"
                    ]
                  },
                  100
                ]
              }
            }
          }
        },
        {
          "name": "wan_parts",
          "value": {
            "each": "wan_loss",
            "fields": {
              "key": "key",
              "live": "live",
              "uptime": "uptime",
              "isp": "isp",
              "loss": {
                "if": {
                  "path": "loss_n",
                  "gt": 0
                },
                "then": {
                  "template": " · loss {loss_pct|round:1}% of the time"
                },
                "else": {
                  "template": ""
                }
              }
            }
          }
        },
        {
          "name": "wan_facts",
          "value": {
            "each": "wan_parts",
            "fields": {
              "key": "key",
              "value": {
                "if": {
                  "path": "live",
                  "equals": 0
                },
                "then": {
                  "template": "Not connected"
                },
                "else": {
                  "if": {
                    "all": [
                      {
                        "path": "isp",
                        "exists": true
                      },
                      {
                        "path": "isp",
                        "not": ""
                      }
                    ]
                  },
                  "then": {
                    "template": "{isp|clip:24} · up {uptime}%{loss}"
                  },
                  "else": {
                    "template": "Up {uptime}%{loss}"
                  }
                }
              }
            }
          }
        },
        {
          "name": "n_wans",
          "value": {
            "count": "wan_facts",
            "default": 0
          }
        },
        {
          "name": "wan_lines",
          "value": {
            "each": "periods",
            "flatten": "pairs",
            "group": "key",
            "sort": "key",
            "limit": 2,
            "fields": {
              "key": {
                "template": "{key|upper}"
              },
              "rows": "rows",
              "live": {
                "count": "rows",
                "where": {
                  "path": "v.uptime",
                  "gt": 0
                },
                "default": 0
              }
            }
          }
        },
        {
          "name": "n_lines",
          "value": {
            "count": "wan_lines",
            "where": {
              "path": "live",
              "gt": 1
            },
            "default": 0
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "health",
      "type": "status",
      "placement": "page_info",
      "width": "full",
      "label": "Site",
      "state": "unknown",
      "source": {
        "ref": "site",
        "failed": {
          "state": "critical",
          "detail": "UniFi's Site Manager isn't answering",
          "after": 3
        },
        "map": {
          "label": {
            "if": {
              "path": "found",
              "gt": 0
            },
            "then": {
              "template": "{s[0].name|clip:60}"
            },
            "else": {
              "template": "Site"
            }
          },
          "state": {
            "if": {
              "path": "found",
              "gt": 0
            },
            "then": "s[0].state",
            "else": {
              "template": "unknown"
            }
          },
          "detail": {
            "if": {
              "path": "found",
              "gt": 0
            },
            "then": {
              "template": "{s[0].detail}"
            },
            "else": {
              "template": "UniFi doesn't list this site for this key"
            }
          }
        }
      }
    },
    {
      "id": "cut",
      "type": "text",
      "tone": "info",
      "body": "UniFi has more sites, consoles or devices than this page reads. Counts here cover part of them, and unifi.ui.com has the rest.",
      "source": {
        "ref": "site",
        "when": {
          "any": [
            {
              "all": [
                {
                  "path": "sites.nextToken",
                  "exists": true
                },
                {
                  "path": "sites.nextToken",
                  "not": ""
                }
              ]
            },
            {
              "all": [
                {
                  "path": "hosts.nextToken",
                  "exists": true
                },
                {
                  "path": "hosts.nextToken",
                  "not": ""
                }
              ]
            },
            {
              "all": [
                {
                  "path": "devices.nextToken",
                  "exists": true
                },
                {
                  "path": "devices.nextToken",
                  "not": ""
                }
              ]
            }
          ]
        }
      }
    },
    {
      "id": "latency",
      "type": "sparkline",
      "label": "Latency",
      "unit": "ms",
      "change": false,
      "points": [
        0,
        0
      ],
      "start_label": "Midnight",
      "end_label": "Now",
      "source": {
        "ref": "metrics",
        "when": {
          "all": [
            {
              "path": "lat_n",
              "gte": 2
            },
            {
              "path": "n_lines",
              "lt": 2
            }
          ]
        },
        "map": {
          "start_label": {
            "path": "span",
            "values": {
              "today": "Midnight",
              "week": "7 days ago",
              "month": "30 days ago"
            },
            "default": "Start"
          },
          "points": {
            "each": "lat",
            "value": "ms",
            "buckets": 180
          },
          "times": {
            "each": "lat",
            "template": "{t}",
            "buckets": 180
          }
        }
      }
    },
    {
      "id": "latency-wans",
      "type": "sparkline",
      "label": "Latency",
      "unit": "ms",
      "change": false,
      "series": [
        "WAN",
        "WAN2"
      ],
      "points": [
        0,
        0
      ],
      "points2": [
        0,
        0
      ],
      "start_label": "Midnight",
      "end_label": "Now",
      "source": {
        "ref": "metrics",
        "when": {
          "path": "n_lines",
          "equals": 2
        },
        "map": {
          "series[0]": {
            "template": "{wan_lines[0].key|clip:16}"
          },
          "series[1]": {
            "template": "{wan_lines[1].key|clip:16}"
          },
          "start_label": {
            "path": "span",
            "values": {
              "today": "Midnight",
              "week": "7 days ago",
              "month": "30 days ago"
            },
            "default": "Start"
          },
          "points": {
            "each": "wan_lines[0].rows",
            "where": {
              "path": "v.uptime",
              "gt": 0
            },
            "value": "v.avgLatency",
            "buckets": 180
          },
          "times": {
            "each": "wan_lines[0].rows",
            "where": {
              "path": "v.uptime",
              "gt": 0
            },
            "template": "{parent.t}",
            "buckets": 180
          },
          "points2": {
            "each": "wan_lines[1].rows",
            "where": {
              "path": "v.uptime",
              "gt": 0
            },
            "value": "v.avgLatency",
            "buckets": 180
          },
          "times2": {
            "each": "wan_lines[1].rows",
            "where": {
              "path": "v.uptime",
              "gt": 0
            },
            "template": "{parent.t}",
            "buckets": 180
          }
        }
      }
    },
    {
      "id": "internet",
      "type": "facts",
      "label": "Internet",
      "items": [
        {
          "key": "WAN",
          "value": "—"
        }
      ],
      "source": {
        "ref": "metrics",
        "when": {
          "path": "n_wans",
          "gt": 0
        },
        "map": {
          "items": {
            "each": "wan_facts",
            "fields": {
              "key": "key",
              "value": "value"
            }
          }
        }
      }
    },
    {
      "id": "site",
      "type": "facts",
      "label": "Site",
      "items": [
        {
          "key": "Clients",
          "value": "—"
        },
        {
          "key": "Devices",
          "value": "—"
        },
        {
          "key": "Wi-Fi retries",
          "value": "—"
        },
        {
          "key": "Gateway",
          "value": "—"
        }
      ],
      "source": {
        "ref": "site",
        "when": {
          "path": "found",
          "gt": 0
        },
        "map": {
          "items[0].value": {
            "template": "{site_clients} · {s[0].counts.wifiClient} Wi-Fi · {s[0].counts.wiredClient} wired"
          },
          "items[1].value": {
            "if": {
              "path": "s[0].counts.offlineDevice",
              "gt": 0
            },
            "then": {
              "template": "{s[0].counts.totalDevice} · {s[0].counts.offlineDevice} offline"
            },
            "else": {
              "if": {
                "path": "s[0].counts.pendingUpdateDevice",
                "gt": 0
              },
              "then": {
                "template": "{s[0].counts.totalDevice} · {s[0].counts.pendingUpdateDevice} updates ready"
              },
              "else": {
                "template": "{s[0].counts.totalDevice}"
              }
            }
          },
          "items[2].value": {
            "if": {
              "path": "s[0].retry",
              "exists": true
            },
            "then": {
              "template": "{s[0].retry|round:0}%"
            },
            "else": {
              "template": "—"
            }
          },
          "items[3].value": {
            "if": {
              "all": [
                {
                  "path": "s[0].gateway",
                  "exists": true
                },
                {
                  "path": "s[0].gateway",
                  "not": ""
                }
              ]
            },
            "then": {
              "template": "{s[0].gateway}"
            },
            "else": {
              "template": "—"
            }
          }
        }
      }
    }
  ]
}
