{
  "$schema": "./.schemas/page.schema.json",
  "title": "Tailscale",
  "icon": "network",
  "sources": {
    "ts": {
      "collect": {
        "dataset": "tailscale-devices",
        "each": "d.devices",
        "key": "{nodeId}",
        "sample": {
          "every": "5m"
        },
        "fields": {
          "state": {
            "if": {
              "path": "connectedToControl",
              "equals": true
            },
            "then": {
              "template": "ok"
            },
            "else": {
              "template": "off"
            }
          }
        },
        "readers": [
          "tailscale-device"
        ]
      },
      "url": "https://api.tailscale.com",
      "secret": "tailscale-client",
      "auth": {
        "type": "oauth_client",
        "token_url": "/api/v2/oauth/token"
      },
      "every": "1m",
      "requests": {
        "d": "/api/v2/tailnet/-/devices?fields=all"
      },
      "derive": [
        {
          "name": "all",
          "value": {
            "each": "d.devices",
            "fields": {
              "name": {
                "template": "{hostname|clip:24}"
              },
              "id": "nodeId",
              "state": {
                "if": {
                  "all": [
                    {
                      "all": [
                        {
                          "path": "keyExpiryDisabled",
                          "equals": false
                        },
                        {
                          "path": "isExternal",
                          "not": true
                        },
                        {
                          "path": "expires",
                          "exists": true
                        }
                      ]
                    },
                    {
                      "value": {
                        "expires": {
                          "date": "expires"
                        },
                        "warning": 14,
                        "as": "state"
                      },
                      "equals": "critical"
                    }
                  ]
                },
                "then": {
                  "template": "critical"
                },
                "else": {
                  "if": {
                    "any": [
                      {
                        "path": "authorized",
                        "equals": false
                      },
                      {
                        "all": [
                          {
                            "all": [
                              {
                                "path": "keyExpiryDisabled",
                                "equals": false
                              },
                              {
                                "path": "isExternal",
                                "not": true
                              },
                              {
                                "path": "expires",
                                "exists": true
                              }
                            ]
                          },
                          {
                            "value": {
                              "expires": {
                                "date": "expires"
                              },
                              "warning": 14,
                              "as": "state"
                            },
                            "equals": "warning"
                          }
                        ]
                      }
                    ]
                  },
                  "then": {
                    "template": "warning"
                  },
                  "else": {
                    "if": {
                      "path": "connectedToControl",
                      "equals": true
                    },
                    "then": {
                      "template": "ok"
                    },
                    "else": {
                      "template": "off"
                    }
                  }
                }
              },
              "online": "connectedToControl",
              "seen": "lastSeen",
              "authorized": "authorized",
              "update": "updateAvailable",
              "expires": {
                "if": {
                  "all": [
                    {
                      "path": "keyExpiryDisabled",
                      "equals": false
                    },
                    {
                      "path": "isExternal",
                      "not": true
                    },
                    {
                      "path": "expires",
                      "exists": true
                    }
                  ]
                },
                "then": "expires"
              },
              "key_left": {
                "if": {
                  "all": [
                    {
                      "path": "keyExpiryDisabled",
                      "equals": false
                    },
                    {
                      "path": "isExternal",
                      "not": true
                    },
                    {
                      "path": "expires",
                      "exists": true
                    }
                  ]
                },
                "then": {
                  "expires": {
                    "date": "expires"
                  },
                  "as": "text"
                }
              },
              "group": {
                "if": {
                  "path": "isExternal",
                  "equals": true
                },
                "then": {
                  "template": "Shared with you"
                },
                "else": {
                  "if": {
                    "value": {
                      "count": "tags",
                      "default": 0
                    },
                    "gt": 0
                  },
                  "then": {
                    "template": "Tagged"
                  },
                  "else": {
                    "template": "Personal"
                  }
                }
              }
            }
          }
        },
        {
          "name": "devices",
          "value": {
            "each": "all",
            "sort": "name",
            "fields": {
              "name": "name",
              "state": "state",
              "group": "group",
              "url": {
                "template": "legato://page/tailscale-device?id={id|raw}"
              },
              "detail": {
                "if": {
                  "path": "state",
                  "equals": "critical"
                },
                "then": {
                  "template": "Key expiry: {key_left}"
                },
                "else": {
                  "if": {
                    "path": "authorized",
                    "equals": false
                  },
                  "then": {
                    "template": "Needs approval"
                  },
                  "else": {
                    "if": {
                      "path": "state",
                      "equals": "warning"
                    },
                    "then": {
                      "template": "Key expiry: {key_left}"
                    },
                    "else": {
                      "if": {
                        "path": "online",
                        "equals": true
                      },
                      "then": {
                        "template": "Online"
                      },
                      "else": {
                        "if": {
                          "path": "seen",
                          "exists": true
                        },
                        "then": {
                          "template": "Last seen {seen|ago}"
                        },
                        "else": {
                          "template": "Offline"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        },
        {
          "name": "total",
          "value": {
            "count": "all"
          }
        },
        {
          "name": "online",
          "value": {
            "count": "all",
            "where": {
              "path": "online",
              "equals": true
            }
          }
        },
        {
          "name": "updates",
          "value": {
            "count": "all",
            "where": {
              "path": "update",
              "equals": true
            }
          }
        },
        {
          "name": "expiring",
          "value": {
            "count": "all",
            "where": {
              "value": {
                "expires": {
                  "date": "expires"
                },
                "warning": 14,
                "as": "state"
              },
              "in": [
                "warning",
                "critical"
              ]
            }
          }
        },
        {
          "name": "next_name",
          "value": {
            "first": "name",
            "of": "all",
            "where": {
              "path": "expires",
              "exists": true
            },
            "sort": {
              "by": "expires",
              "as": "date"
            }
          }
        },
        {
          "name": "next_id",
          "value": {
            "first": "id",
            "of": "all",
            "where": {
              "path": "expires",
              "exists": true
            },
            "sort": {
              "by": "expires",
              "as": "date"
            }
          }
        },
        {
          "name": "next_expires",
          "value": {
            "first": "expires",
            "of": "all",
            "where": {
              "path": "expires",
              "exists": true
            },
            "sort": {
              "by": "expires",
              "as": "date"
            }
          }
        },
        {
          "name": "next_left",
          "value": {
            "expires": {
              "date": "next_expires"
            },
            "as": "text"
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "tailnet",
      "type": "facts",
      "label": "Tailnet",
      "items": [
        {
          "key": "Online",
          "value": "—"
        },
        {
          "key": "Next key expiry",
          "value": "—"
        },
        {
          "key": "Keys expiring",
          "value": "—"
        },
        {
          "key": "Updates available",
          "value": "—"
        }
      ],
      "source": {
        "ref": "ts",
        "map": {
          "items[0].value": {
            "template": "{online} of {total}"
          },
          "items[1].value": {
            "if": {
              "path": "next_expires",
              "exists": true
            },
            "then": {
              "template": "{next_name} · {next_left}"
            },
            "else": {
              "template": "None"
            }
          },
          "items[1].url": {
            "if": {
              "path": "next_id",
              "exists": true
            },
            "then": {
              "template": "legato://page/tailscale-device?id={next_id|raw}"
            }
          },
          "items[2].value": {
            "if": {
              "path": "expiring",
              "equals": 0
            },
            "then": {
              "template": "None within 14 days"
            },
            "else": {
              "template": "{expiring} within 14 days"
            }
          },
          "items[3].value": {
            "if": {
              "path": "updates",
              "equals": 0
            },
            "then": {
              "template": "None"
            },
            "else": {
              "template": "{updates}"
            }
          }
        }
      }
    },
    {
      "id": "devices",
      "type": "fleet",
      "label": "Devices",
      "max_items": 24,
      "items": [],
      "source": {
        "ref": "ts",
        "map": {
          "items": {
            "each": "devices",
            "fields": {
              "name": "name",
              "state": "state",
              "detail": "detail",
              "group": "group",
              "url": "url"
            }
          }
        },
        "failed": {
          "state": "critical",
          "detail": "Tailscale isn't answering, or refused the OAuth client",
          "after": 3
        }
      }
    },
    {
      "id": "explore",
      "type": "list",
      "label": "Explore",
      "items": [
        {
          "text": "Machines",
          "detail": "login.tailscale.com",
          "url": "https://login.tailscale.com/admin/machines"
        }
      ]
    }
  ]
}
