{
  "$schema": "./.schemas/page.schema.json",
  "title": "Tunnel",
  "title_from": {
    "ref": "tunnel",
    "value": "one.result.name"
  },
  "icon": "network",
  "sidebar": false,
  "params": {
    "id": {
      "example": "c3a1f7e2-5b9d-4e8a-a6c2-7f1d3b5e9a04",
      "pattern": "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
    }
  },
  "sources": {
    "samples": {
      "dataset": "cloudflare-tunnels",
      "key": "{param.id}",
      "derive": [
        {
          "name": "up",
          "value": {
            "multiply": [
              {
                "divide": [
                  {
                    "count": "kept.rows",
                    "where": {
                      "path": "row.state",
                      "in": [
                        "ok"
                      ]
                    }
                  },
                  {
                    "count": "kept.rows"
                  }
                ]
              },
              100
            ],
            "round": 1
          }
        }
      ]
    },
    "tunnel": {
      "url": "https://api.cloudflare.com",
      "secret": "cloudflare",
      "every": "1m",
      "requests": {
        "acct": "/client/v4/accounts?per_page=5",
        "one": {
          "path": "/client/v4/accounts/{account}/cfd_tunnel/{param.id}",
          "with": {
            "account": "acct.result[0].id"
          }
        },
        "conns": {
          "path": "/client/v4/accounts/{account}/cfd_tunnel/{param.id}/connections",
          "with": {
            "account": "acct.result[0].id"
          }
        },
        "release": {
          "path": "https://api.github.com/repos/cloudflare/cloudflared/releases/latest",
          "auth": {
            "type": "none"
          },
          "keep": "6h"
        }
      },
      "fail": {
        "when": {
          "any": [
            {
              "path": "one.success",
              "equals": false
            },
            {
              "path": "conns.success",
              "equals": false
            }
          ]
        },
        "message": {
          "if": {
            "path": "one.success",
            "equals": false
          },
          "then": {
            "template": "{one.errors[0].message}"
          },
          "else": {
            "template": "{conns.errors[0].message}"
          }
        }
      },
      "derive": [
        {
          "name": "connections",
          "value": {
            "each": "conns.result",
            "flatten": "conns",
            "fields": {
              "colo": "colo_name"
            }
          }
        },
        {
          "name": "conn_count",
          "value": {
            "count": "connections",
            "default": 0
          }
        },
        {
          "name": "colos",
          "value": {
            "join": "colo",
            "of": "connections",
            "sort": "colo",
            "collapse": "colo",
            "separator": ", "
          }
        },
        {
          "name": "matched",
          "value": {
            "each": "conns.result",
            "lookup": {
              "from": "release",
              "by": "tag_name",
              "on": "version",
              "as": "same"
            },
            "fields": {
              "current": {
                "if": {
                  "path": "same.tag_name",
                  "exists": true
                },
                "then": {
                  "template": "yes"
                },
                "else": {
                  "template": "no"
                }
              }
            }
          }
        },
        {
          "name": "behind",
          "value": {
            "if": {
              "path": "release.tag_name",
              "exists": true
            },
            "then": {
              "count": "matched",
              "where": {
                "path": "current",
                "equals": "no"
              },
              "default": 0
            },
            "else": {
              "add": [
                0,
                0
              ]
            }
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "state",
      "type": "status",
      "width": "full",
      "label": "Tunnel",
      "state": "unknown",
      "url": "https://one.dash.cloudflare.com/",
      "source": {
        "ref": "tunnel",
        "failed": {
          "state": "unknown",
          "detail": "Cloudflare isn't answering",
          "after": 3
        },
        "map": {
          "state": {
            "path": "one.result.status",
            "values": {
              "healthy": "ok",
              "degraded": "warning",
              "down": "critical",
              "inactive": "off"
            },
            "default": "unknown"
          },
          "detail": {
            "if": {
              "path": "one.result.status",
              "equals": "healthy"
            },
            "then": {
              "if": {
                "path": "conn_count",
                "gt": 0
              },
              "then": {
                "template": "Connected {one.result.conns_active_at|ago} · {conn_count} connections · {colos|clip:40}"
              },
              "else": {
                "template": "Connected {one.result.conns_active_at|ago}"
              }
            },
            "else": {
              "if": {
                "path": "one.result.status",
                "equals": "degraded"
              },
              "then": {
                "template": "Degraded · {conn_count} connections · {colos|clip:40}"
              },
              "else": {
                "if": {
                  "all": [
                    {
                      "path": "one.result.status",
                      "equals": "down"
                    },
                    {
                      "path": "one.result.conns_inactive_at",
                      "exists": true
                    }
                  ]
                },
                "then": {
                  "template": "No connections since {one.result.conns_inactive_at|ago}"
                },
                "else": {
                  "if": {
                    "path": "one.result.status",
                    "equals": "inactive"
                  },
                  "then": {
                    "template": "Never connected"
                  },
                  "else": {
                    "template": "{one.result.status|title}"
                  }
                }
              }
            }
          }
        }
      }
    },
    {
      "id": "history",
      "type": "strip",
      "label": "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|clock}"
              }
            }
          },
          "detail": {
            "template": "{up}% healthy"
          }
        }
      }
    },
    {
      "id": "connectors",
      "type": "list",
      "label": "Connectors",
      "max_items": 7,
      "items": [],
      "source": {
        "ref": "tunnel",
        "empty": {
          "text": "No connectors running.",
          "when": {
            "value": {
              "count": "conns.result",
              "default": 0
            },
            "equals": 0
          }
        },
        "map": {
          "label": {
            "if": {
              "path": "behind",
              "gt": 0
            },
            "then": {
              "template": "Connectors · {release.tag_name|clip:20} is out"
            },
            "else": {
              "template": "Connectors"
            }
          },
          "items": {
            "if": {
              "path": "release.tag_name",
              "exists": true
            },
            "then": {
              "each": "conns.result",
              "lookup": {
                "from": "release",
                "by": "tag_name",
                "on": "version",
                "as": "same"
              },
              "fields": {
                "key": "id",
                "text": {
                  "template": "cloudflared {version}"
                },
                "subtitle": {
                  "if": {
                    "path": "conns[0].origin_ip",
                    "exists": true
                  },
                  "then": {
                    "template": "{arch} · {conns[0].origin_ip}"
                  },
                  "else": {
                    "template": "{arch}"
                  }
                },
                "detail": {
                  "if": {
                    "path": "same.tag_name",
                    "exists": true
                  },
                  "then": {
                    "template": "Started {run_at|ago}"
                  },
                  "else": {
                    "template": "Update available"
                  }
                },
                "icon": {
                  "if": {
                    "path": "same.tag_name",
                    "exists": true
                  },
                  "then": {
                    "template": "server.rack"
                  },
                  "else": {
                    "template": "arrow.up.circle"
                  }
                },
                "color": {
                  "if": {
                    "path": "same.tag_name",
                    "exists": false
                  },
                  "then": {
                    "template": "orange"
                  }
                }
              }
            },
            "else": {
              "each": "conns.result",
              "lookup": {
                "from": "release",
                "by": "tag_name",
                "on": "version",
                "as": "same"
              },
              "fields": {
                "key": "id",
                "text": {
                  "template": "cloudflared {version}"
                },
                "subtitle": {
                  "if": {
                    "path": "conns[0].origin_ip",
                    "exists": true
                  },
                  "then": {
                    "template": "{arch} · {conns[0].origin_ip}"
                  },
                  "else": {
                    "template": "{arch}"
                  }
                },
                "detail": {
                  "template": "Started {run_at|ago}"
                },
                "icon": {
                  "template": "server.rack"
                }
              }
            }
          }
        }
      }
    }
  ]
}
