{
  "$schema": "./.schemas/page.schema.json",
  "title": "Device",
  "icon": "wifi.router",
  "sidebar": false,
  "params": {
    "device": {
      "example": "a9b8c7d6-e5f4-4a3b-8c2d-1e0f9a8b7c6d",
      "pattern": "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"
    },
    "site": {
      "example": "5d3c1e2a-7b4f-4c6d-9e8a-1f2b3c4d5e6f",
      "pattern": "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"
    }
  },
  "title_from": {
    "ref": "dev",
    "value": {
      "if": {
        "all": [
          {
            "path": "device.name",
            "exists": true
          },
          {
            "path": "device.name",
            "not": ""
          }
        ]
      },
      "then": {
        "template": "{device.name}"
      },
      "else": "device.model"
    }
  },
  "sources": {
    "dev": {
      "url": "https://192.0.2.1",
      "secret": "unifi-network",
      "auth": {
        "type": "header",
        "name": "X-API-KEY"
      },
      "every": "1m",
      "requests": {
        "device": "/proxy/network/integration/v1/sites/{param.site}/devices/{param.device}",
        "stats": "/proxy/network/integration/v1/sites/{param.site}/devices/{param.device}/statistics/latest"
      },
      "derive": [
        {
          "name": "dev_label",
          "value": {
            "if": {
              "all": [
                {
                  "path": "device.name",
                  "exists": true
                },
                {
                  "path": "device.name",
                  "not": ""
                }
              ]
            },
            "then": {
              "template": "{device.name|clip:40}"
            },
            "else": {
              "template": "{device.model|clip:40}"
            }
          }
        },
        {
          "name": "kind",
          "value": {
            "if": {
              "value": {
                "path": "device.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": "device.features.accessPoint",
                "exists": true
              },
              "then": {
                "template": "ap"
              },
              "else": {
                "if": {
                  "path": "device.features.switching",
                  "exists": true
                },
                "then": {
                  "template": "switch"
                },
                "else": {
                  "template": "other"
                }
              }
            }
          }
        },
        {
          "name": "ports",
          "value": {
            "each": "device.interfaces.ports",
            "sort": "idx",
            "fields": {
              "idx": "idx",
              "state": "state",
              "speed": {
                "path": "speedMbps",
                "values": {
                  "10": "10 Mbps",
                  "100": "100 Mbps",
                  "1000": "1 Gbps",
                  "2500": "2.5 Gbps",
                  "5000": "5 Gbps",
                  "10000": "10 Gbps",
                  "25000": "25 Gbps"
                },
                "default": {
                  "template": "{speedMbps} Mbps"
                }
              },
              "poe": "poe"
            }
          }
        },
        {
          "name": "port_rows",
          "value": {
            "each": "ports",
            "fields": {
              "key": {
                "template": "p{idx}"
              },
              "text": {
                "template": "Port {idx}"
              },
              "detail": {
                "if": {
                  "path": "state",
                  "equals": "UP"
                },
                "then": {
                  "if": {
                    "path": "poe.state",
                    "equals": "UP"
                  },
                  "then": {
                    "template": "{speed} · PoE"
                  },
                  "else": {
                    "template": "{speed}"
                  }
                },
                "else": {
                  "if": {
                    "path": "state",
                    "equals": "DOWN"
                  },
                  "then": {
                    "template": "Down"
                  },
                  "else": {
                    "template": "Unknown"
                  }
                }
              },
              "icon": {
                "if": {
                  "path": "state",
                  "equals": "UP"
                },
                "then": {
                  "template": "circle.fill"
                },
                "else": {
                  "template": "circle"
                }
              }
            }
          }
        },
        {
          "name": "n_ports",
          "value": {
            "count": "ports",
            "default": 0
          }
        },
        {
          "name": "n_up",
          "value": {
            "count": "ports",
            "where": {
              "path": "state",
              "equals": "UP"
            },
            "default": 0
          }
        },
        {
          "name": "radio_rows",
          "value": {
            "each": "device.interfaces.radios",
            "sort": "frequencyGHz",
            "lookup": {
              "from": "stats.interfaces.radios",
              "by": "frequencyGHz",
              "on": "frequencyGHz",
              "as": "live"
            },
            "fields": {
              "key": {
                "template": "r{frequencyGHz}"
              },
              "text": {
                "template": "{frequencyGHz} GHz"
              },
              "detail": {
                "if": {
                  "path": "live.txRetriesPct",
                  "exists": true
                },
                "then": {
                  "template": "Channel {channel} · {channelWidthMHz} MHz · {live.txRetriesPct|round:0}% retries"
                },
                "else": {
                  "template": "Channel {channel} · {channelWidthMHz} MHz"
                }
              }
            }
          }
        },
        {
          "name": "n_radios",
          "value": {
            "count": "radio_rows",
            "default": 0
          }
        },
        {
          "name": "up_days",
          "value": {
            "path": "stats.uptimeSec",
            "scale": 1.1574074074074073e-05,
            "round": 0
          }
        },
        {
          "name": "up_text",
          "value": {
            "if": {
              "path": "stats.uptimeSec",
              "gte": 129600
            },
            "then": {
              "template": "{up_days} days"
            },
            "else": {
              "if": {
                "path": "stats.uptimeSec",
                "gte": 64800
              },
              "then": {
                "template": "1 day"
              },
              "else": {
                "template": "{stats.uptimeSec|duration:s}"
              }
            }
          }
        },
        {
          "name": "down_mbps",
          "value": {
            "path": "stats.uplink.rxRateBps",
            "scale": 1e-06,
            "round": 0
          }
        },
        {
          "name": "up_mbps",
          "value": {
            "path": "stats.uplink.txRateBps",
            "scale": 1e-06,
            "round": 0
          }
        }
      ]
    },
    "samples": {
      "dataset": "unifi-network-devices",
      "key": "{param.device}",
      "derive": [
        {
          "name": "n",
          "value": {
            "count": "kept.rows",
            "default": 0
          }
        },
        {
          "name": "n_cpu",
          "value": {
            "count": "kept.rows",
            "where": {
              "path": "row.cpu",
              "exists": true
            },
            "default": 0
          }
        },
        {
          "name": "n_mem",
          "value": {
            "count": "kept.rows",
            "where": {
              "path": "row.mem",
              "exists": true
            },
            "default": 0
          }
        },
        {
          "name": "up",
          "value": {
            "multiply": [
              {
                "divide": [
                  {
                    "count": "kept.rows",
                    "where": {
                      "path": "row.state",
                      "equals": "ok"
                    }
                  },
                  {
                    "count": "kept.rows"
                  }
                ]
              },
              100
            ],
            "round": 1
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "health",
      "type": "status",
      "placement": "page_info",
      "width": "full",
      "label": "Device",
      "state": "unknown",
      "source": {
        "ref": "dev",
        "failed": {
          "state": "critical",
          "detail": "The console isn't answering",
          "after": 3
        },
        "map": {
          "label": {
            "if": {
              "all": [
                {
                  "path": "device.name",
                  "exists": true
                },
                {
                  "path": "device.name",
                  "not": ""
                }
              ]
            },
            "then": {
              "template": "{device.name|clip:60}"
            },
            "else": {
              "template": "Device"
            }
          },
          "state": {
            "path": "device.state",
            "values": {
              "ONLINE": "ok",
              "OFFLINE": "critical",
              "CONNECTION_INTERRUPTED": "warning",
              "ISOLATED": "warning"
            },
            "default": "unknown"
          },
          "detail": {
            "if": {
              "path": "device.state",
              "equals": "ONLINE"
            },
            "then": {
              "if": {
                "path": "stats.uptimeSec",
                "exists": true
              },
              "then": {
                "template": "Online · up {up_text}"
              },
              "else": {
                "template": "Online"
              }
            },
            "else": {
              "path": "device.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"
            }
          }
        }
      }
    },
    {
      "id": "device",
      "type": "facts",
      "label": "Device",
      "actions_placement": "corner",
      "items": [
        {
          "key": "Uptime",
          "value": "—"
        },
        {
          "key": "Firmware",
          "value": "—"
        },
        {
          "key": "Uplink",
          "value": "—"
        },
        {
          "key": "Model",
          "value": "—"
        },
        {
          "key": "Address",
          "value": "—"
        }
      ],
      "actions": [
        {
          "label": "Restart",
          "icon": "arrow.trianglehead.2.clockwise.rotate.90",
          "method": "POST",
          "path": "/proxy/network/integration/v1/sites/{param.site}/devices/{param.device}/actions",
          "body": {
            "action": "RESTART"
          },
          "confirm": {
            "message": "Restart {answer.dev_label}? It goes offline for a minute or two.",
            "button": "Restart"
          },
          "when": {
            "all": [
              {
                "path": "device.state",
                "equals": "ONLINE"
              },
              {
                "path": "kind",
                "not": "gateway"
              }
            ]
          }
        },
        {
          "label": "Restart",
          "icon": "arrow.trianglehead.2.clockwise.rotate.90",
          "method": "POST",
          "path": "/proxy/network/integration/v1/sites/{param.site}/devices/{param.device}/actions",
          "body": {
            "action": "RESTART"
          },
          "confirm": {
            "message": "Restart {answer.dev_label}? The internet and this console drop until it's back, a few minutes.",
            "button": "Restart"
          },
          "when": {
            "all": [
              {
                "path": "device.state",
                "equals": "ONLINE"
              },
              {
                "path": "kind",
                "equals": "gateway"
              }
            ]
          }
        }
      ],
      "source": {
        "ref": "dev",
        "map": {
          "label": {
            "path": "kind",
            "values": {
              "gateway": "Gateway",
              "switch": "Switch",
              "ap": "Access point"
            },
            "default": "Device"
          },
          "items[0].value": {
            "if": {
              "path": "stats.uptimeSec",
              "exists": true
            },
            "then": {
              "template": "{up_text}"
            },
            "else": {
              "template": "—"
            }
          },
          "items[1].value": {
            "if": {
              "path": "device.firmwareUpdatable",
              "equals": true
            },
            "then": {
              "template": "{device.firmwareVersion} · update ready"
            },
            "else": {
              "template": "{device.firmwareVersion}"
            }
          },
          "items[2].value": {
            "if": {
              "path": "stats.uplink.rxRateBps",
              "exists": true
            },
            "then": {
              "template": "{down_mbps} Mbps down · {up_mbps} Mbps up"
            },
            "else": {
              "template": "—"
            }
          },
          "items[3].value": {
            "template": "{device.model}"
          },
          "items[4].value": {
            "if": {
              "all": [
                {
                  "path": "device.ipAddress",
                  "exists": true
                },
                {
                  "path": "device.ipAddress",
                  "not": ""
                }
              ]
            },
            "then": {
              "template": "{device.ipAddress}"
            },
            "else": {
              "template": "—"
            }
          }
        }
      }
    },
    {
      "id": "history",
      "type": "strip",
      "label": "Last 24 hours",
      "start_label": "24 hours ago",
      "end_label": "Now",
      "segments": [],
      "source": {
        "ref": "samples",
        "when": {
          "path": "n",
          "gt": 0
        },
        "map": {
          "segments": {
            "each": "kept.rows",
            "by_time": {
              "at": "at",
              "span": "kept.span"
            },
            "buckets": 96,
            "fields": {
              "state": {
                "worst": "row.state",
                "of": "rows"
              },
              "label": {
                "template": "{from|time}"
              }
            }
          },
          "detail": {
            "template": "{up}% online"
          }
        }
      }
    },
    {
      "id": "cpu",
      "type": "sparkline",
      "label": "CPU",
      "unit": "%",
      "width": "half",
      "change": false,
      "max": 100,
      "points": [],
      "source": {
        "ref": "samples",
        "when": {
          "path": "n_cpu",
          "gte": 2
        },
        "map": {
          "points": {
            "each": "kept.rows",
            "where": {
              "path": "row.cpu",
              "exists": true
            },
            "buckets": 48,
            "value": "row.cpu"
          },
          "times": {
            "each": "kept.rows",
            "where": {
              "path": "row.cpu",
              "exists": true
            },
            "buckets": 48,
            "template": "{at}"
          },
          "labels": {
            "each": "kept.rows",
            "where": {
              "path": "row.cpu",
              "exists": true
            },
            "buckets": 48,
            "template": "{at|time}"
          }
        }
      }
    },
    {
      "id": "memory",
      "type": "sparkline",
      "label": "Memory",
      "unit": "%",
      "width": "half",
      "change": false,
      "max": 100,
      "points": [],
      "source": {
        "ref": "samples",
        "when": {
          "path": "n_mem",
          "gte": 2
        },
        "map": {
          "points": {
            "each": "kept.rows",
            "where": {
              "path": "row.mem",
              "exists": true
            },
            "buckets": 48,
            "value": "row.mem"
          },
          "times": {
            "each": "kept.rows",
            "where": {
              "path": "row.mem",
              "exists": true
            },
            "buckets": 48,
            "template": "{at}"
          },
          "labels": {
            "each": "kept.rows",
            "where": {
              "path": "row.mem",
              "exists": true
            },
            "buckets": 48,
            "template": "{at|time}"
          }
        }
      }
    },
    {
      "id": "ports",
      "type": "list",
      "label": "Ports",
      "max_items": 8,
      "items": [],
      "source": {
        "ref": "dev",
        "when": {
          "path": "n_ports",
          "gt": 0
        },
        "map": {
          "label": {
            "template": "Ports, {n_up} of {n_ports} up"
          },
          "items": {
            "each": "port_rows",
            "fields": {
              "key": "key",
              "text": "text",
              "detail": "detail",
              "icon": "icon"
            }
          }
        }
      }
    },
    {
      "id": "radios",
      "type": "list",
      "label": "Radios",
      "items": [],
      "source": {
        "ref": "dev",
        "when": {
          "path": "n_radios",
          "gt": 0
        },
        "map": {
          "items": {
            "each": "radio_rows",
            "fields": {
              "key": "key",
              "text": "text",
              "detail": "detail"
            }
          }
        }
      }
    }
  ]
}
