{
  "$schema": "./.schemas/page.schema.json",
  "title": "Device",
  "icon": "network",
  "sidebar": false,
  "params": {
    "id": {
      "example": "nQ7exampleCNTRL",
      "pattern": "[A-Za-z0-9]{1,64}"
    }
  },
  "title_from": {
    "ref": "dev",
    "value": "d.hostname"
  },
  "sources": {
    "samples": {
      "dataset": "tailscale-devices",
      "key": "{param.id}",
      "derive": [
        {
          "name": "on",
          "value": {
            "multiply": [
              {
                "divide": [
                  {
                    "count": "kept.rows",
                    "where": {
                      "path": "row.state",
                      "equals": "ok"
                    }
                  },
                  {
                    "count": "kept.rows"
                  }
                ]
              },
              100
            ],
            "round": 0
          }
        }
      ]
    },
    "dev": {
      "url": "https://api.tailscale.com",
      "secret": "tailscale-client",
      "auth": {
        "type": "oauth_client",
        "token_url": "/api/v2/oauth/token"
      },
      "every": "1m",
      "requests": {
        "d": "/api/v2/device/{param.id}?fields=all"
      },
      "derive": [
        {
          "name": "name",
          "value": "d.hostname"
        },
        {
          "name": "keyed",
          "value": {
            "if": {
              "all": [
                {
                  "path": "d.keyExpiryDisabled",
                  "equals": false
                },
                {
                  "path": "d.isExternal",
                  "not": true
                },
                {
                  "path": "d.expires",
                  "exists": true
                }
              ]
            },
            "then": {
              "template": "yes"
            },
            "else": {
              "template": "no"
            }
          }
        },
        {
          "name": "key_state",
          "value": {
            "if": {
              "path": "keyed",
              "equals": "yes"
            },
            "then": {
              "expires": {
                "date": "d.expires"
              },
              "warning": 14,
              "as": "state"
            },
            "else": {
              "template": "ok"
            }
          }
        },
        {
          "name": "state",
          "value": {
            "if": {
              "path": "key_state",
              "equals": "critical"
            },
            "then": {
              "template": "critical"
            },
            "else": {
              "if": {
                "any": [
                  {
                    "path": "d.authorized",
                    "equals": false
                  },
                  {
                    "path": "key_state",
                    "equals": "warning"
                  }
                ]
              },
              "then": {
                "template": "warning"
              },
              "else": {
                "if": {
                  "path": "d.connectedToControl",
                  "equals": true
                },
                "then": {
                  "template": "ok"
                },
                "else": {
                  "template": "off"
                }
              }
            }
          }
        },
        {
          "name": "key_left",
          "value": {
            "expires": {
              "date": "d.expires"
            },
            "as": "text"
          }
        },
        {
          "name": "advertised",
          "value": {
            "count": "d.advertisedRoutes",
            "default": 0
          }
        },
        {
          "name": "enabled",
          "value": {
            "count": "d.enabledRoutes",
            "default": 0
          }
        },
        {
          "name": "system",
          "value": {
            "path": "d.os",
            "values": {
              "linux": "Linux",
              "windows": "Windows",
              "android": "Android",
              "macOS": "macOS",
              "iOS": "iOS",
              "freebsd": "FreeBSD",
              "openbsd": "OpenBSD",
              "": "—"
            },
            "default": {
              "template": "{d.os}"
            }
          }
        },
        {
          "name": "version",
          "value": {
            "path": "d.clientVersion",
            "match": "^([0-9]+(?:\\.[0-9]+)*)"
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "now",
      "type": "status",
      "width": "full",
      "label": "Now",
      "state": "unknown",
      "url": "https://login.tailscale.com/admin/machines",
      "source": {
        "ref": "dev",
        "map": {
          "state": "state",
          "detail": {
            "if": {
              "path": "d.authorized",
              "equals": false
            },
            "then": {
              "template": "Needs approval"
            },
            "else": {
              "if": {
                "path": "key_state",
                "in": [
                  "warning",
                  "critical"
                ]
              },
              "then": {
                "if": {
                  "path": "d.connectedToControl",
                  "equals": true
                },
                "then": {
                  "template": "Online · key expiry: {key_left}"
                },
                "else": {
                  "template": "Offline · key expiry: {key_left}"
                }
              },
              "else": {
                "if": {
                  "path": "d.connectedToControl",
                  "equals": true
                },
                "then": {
                  "template": "Online"
                },
                "else": {
                  "if": {
                    "path": "d.lastSeen",
                    "exists": true
                  },
                  "then": {
                    "template": "Last seen {d.lastSeen|ago}"
                  },
                  "else": {
                    "template": "Offline"
                  }
                }
              }
            }
          }
        },
        "failed": {
          "state": "critical",
          "detail": "Tailscale isn't answering, or refused the OAuth client",
          "after": 3
        }
      },
      "actions_placement": "corner",
      "actions": [
        {
          "label": "Approve device",
          "icon": "checkmark.circle",
          "method": "POST",
          "path": "/api/v2/device/{param.id}/authorized",
          "body": {
            "authorized": true
          },
          "when": {
            "all": [
              {
                "path": "d.authorized",
                "equals": false
              },
              {
                "path": "d.isExternal",
                "not": true
              }
            ]
          },
          "confirm": {
            "message": "Let {answer.name} join your tailnet?",
            "button": "Approve"
          }
        },
        {
          "label": "Turn off key expiry",
          "icon": "infinity",
          "method": "POST",
          "path": "/api/v2/device/{param.id}/key",
          "body": {
            "keyExpiryDisabled": true
          },
          "when": {
            "all": [
              {
                "path": "d.keyExpiryDisabled",
                "equals": false
              },
              {
                "path": "d.isExternal",
                "not": true
              }
            ]
          }
        },
        {
          "label": "Turn on key expiry",
          "icon": "clock",
          "method": "POST",
          "path": "/api/v2/device/{param.id}/key",
          "body": {
            "keyExpiryDisabled": false
          },
          "when": {
            "all": [
              {
                "path": "d.keyExpiryDisabled",
                "equals": true
              },
              {
                "path": "d.isExternal",
                "not": true
              }
            ]
          },
          "confirm": {
            "message": "Turn key expiry back on for {answer.name}? Its key keeps its original date, and if that's passed, the device has to sign in again.",
            "button": "Turn On"
          }
        }
      ]
    },
    {
      "id": "history",
      "type": "strip",
      "label": "Online, last 24 hours",
      "start_label": "24 hours ago",
      "end_label": "Now",
      "segments": [],
      "source": {
        "ref": "samples",
        "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": "{on}% online"
          }
        }
      }
    },
    {
      "id": "details",
      "type": "facts",
      "label": "Details",
      "items": [
        {
          "key": "Key expiry",
          "value": "—"
        },
        {
          "key": "Address",
          "value": "—"
        },
        {
          "key": "Owner",
          "value": "—"
        },
        {
          "key": "Client",
          "value": "—"
        },
        {
          "key": "Name",
          "value": "—"
        },
        {
          "key": "Routes",
          "value": "—"
        },
        {
          "key": "System",
          "value": "—"
        }
      ],
      "source": {
        "ref": "dev",
        "map": {
          "items[0].value": {
            "if": {
              "path": "d.isExternal",
              "equals": true
            },
            "then": {
              "template": "Set by its own tailnet"
            },
            "else": {
              "if": {
                "path": "d.keyExpiryDisabled",
                "equals": true
              },
              "then": {
                "template": "Off"
              },
              "else": "key_left"
            }
          },
          "items[1].value": "d.addresses[0]",
          "items[2].value": {
            "if": {
              "path": "d.isExternal",
              "equals": true
            },
            "then": {
              "template": "Shared with you"
            },
            "else": {
              "if": {
                "value": {
                  "count": "d.tags",
                  "default": 0
                },
                "gt": 0
              },
              "then": {
                "join": "$",
                "of": "d.tags",
                "separator": ", "
              },
              "else": "d.user"
            }
          },
          "items[3].value": {
            "if": {
              "path": "version",
              "exists": false
            },
            "then": {
              "template": "—"
            },
            "else": {
              "if": {
                "path": "d.updateAvailable",
                "equals": true
              },
              "then": {
                "template": "{version} · update available"
              },
              "else": {
                "template": "{version}"
              }
            }
          },
          "items[4].value": "d.name",
          "items[5].value": {
            "if": {
              "path": "advertised",
              "equals": 0
            },
            "then": {
              "template": "None"
            },
            "else": {
              "if": {
                "path": "d.advertisedRoutes",
                "contains": "0.0.0.0/0"
              },
              "then": {
                "template": "Exit node offered · {enabled} of {advertised} approved"
              },
              "else": {
                "template": "{enabled} of {advertised} approved"
              }
            }
          },
          "items[6].value": {
            "if": {
              "path": "d.distro.name",
              "exists": true
            },
            "then": {
              "template": "{system} · {d.distro.name} {d.distro.version}"
            },
            "else": "system"
          }
        }
      }
    }
  ]
}
