{
  "$schema": "./.schemas/page.schema.json",
  "title": "Cloudflare",
  "icon": "cloud",
  "sources": {
    "inventory": {
      "url": "https://api.cloudflare.com",
      "secret": "cloudflare",
      "secret_expires": "token_expires",
      "every": "5m",
      "requests": {
        "acct": "/client/v4/accounts?per_page=5",
        "list": {
          "path": "/client/v4/zones?per_page=50&account.id={account}",
          "with": {
            "account": "acct.result[0].id"
          }
        }
      },
      "fail": {
        "when": {
          "path": "list.success",
          "equals": false
        },
        "message": {
          "template": "{list.errors[0].message}"
        }
      },
      "derive": [
        {
          "name": "listed",
          "value": {
            "count": "list.result",
            "default": 0
          }
        },
        {
          "name": "attention",
          "value": {
            "count": "list.result",
            "where": {
              "path": "status",
              "in": [
                "pending",
                "initializing",
                "moved"
              ]
            },
            "default": 0
          }
        },
        {
          "name": "first_attention",
          "value": {
            "first": "name",
            "of": "list.result",
            "where": {
              "path": "status",
              "in": [
                "pending",
                "initializing",
                "moved"
              ]
            },
            "sort": "name"
          }
        },
        {
          "name": "first_attention_status",
          "value": {
            "first": "status",
            "of": "list.result",
            "where": {
              "path": "status",
              "in": [
                "pending",
                "initializing",
                "moved"
              ]
            },
            "sort": "name"
          }
        },
        {
          "name": "active",
          "value": {
            "count": "list.result",
            "where": {
              "path": "status",
              "equals": "active"
            },
            "default": 0
          }
        }
      ]
    },
    "zones": {
      "url": "https://api.cloudflare.com",
      "secret": "cloudflare",
      "every": "5m",
      "requests": {
        "acct": "/client/v4/accounts?per_page=5",
        "list": {
          "path": "/client/v4/zones?per_page=50&account.id={account}",
          "with": {
            "account": "acct.result[0].id"
          }
        },
        "gql": {
          "path": "/client/v4/graphql",
          "method": "POST",
          "body": {
            "query": "query ($account: string!) { viewer { accounts(filter: { accountTag: $account }) { total: httpRequestsOverviewAdaptiveGroups(limit: 1, filter: {datetime_geq: \"{date.hours_ago_24_at}\", datetime_lt: \"{date.now}\"}) { sum { requests cachedRequests bytes } ratio { status5xx } } hours: httpRequestsOverviewAdaptiveGroups(limit: 100, filter: {datetime_geq: \"{date.hours_ago_24_at}\", datetime_lt: \"{date.now}\"}, orderBy: [datetimeHour_ASC]) { sum { requests } dimensions { datetimeHour } } zones: httpRequestsOverviewAdaptiveGroups(limit: 100, filter: {datetime_geq: \"{date.hours_ago_24_at}\", datetime_lt: \"{date.now}\"}, orderBy: [sum_requests_DESC]) { sum { requests bytes } dimensions { zoneTag } } } } }",
            "variables": {
              "account": "{account}"
            }
          },
          "with": {
            "account": "acct.result[0].id"
          }
        }
      },
      "fail": {
        "when": {
          "any": [
            {
              "path": "list.success",
              "equals": false
            },
            {
              "path": "gql.errors",
              "exists": true
            }
          ]
        },
        "message": {
          "if": {
            "path": "gql.errors",
            "exists": true
          },
          "then": {
            "if": {
              "path": "gql.errors[0].extensions.code",
              "equals": "authz"
            },
            "then": {
              "template": "Cloudflare turned down the traffic figures: the token needs Read on Account › Account Analytics"
            },
            "else": {
              "template": "{gql.errors[0].message|clip:200}"
            }
          },
          "else": {
            "template": "{list.errors[0].message}"
          }
        }
      },
      "derive": [
        {
          "name": "requests",
          "value": {
            "path": "gql.data.viewer.accounts[0].total[0].sum.requests",
            "default": 0
          }
        },
        {
          "name": "cached",
          "value": {
            "path": "gql.data.viewer.accounts[0].total[0].sum.cachedRequests",
            "default": 0
          }
        },
        {
          "name": "bytes",
          "value": {
            "path": "gql.data.viewer.accounts[0].total[0].sum.bytes",
            "default": 0
          }
        },
        {
          "name": "error_share",
          "value": {
            "path": "gql.data.viewer.accounts[0].total[0].ratio.status5xx",
            "default": 0
          }
        },
        {
          "name": "hours",
          "value": {
            "each": "gql.data.viewer.accounts[0].hours",
            "where": {
              "all": [
                {
                  "value": {
                    "since": "dimensions.datetimeHour"
                  },
                  "gte": 3600
                },
                {
                  "value": {
                    "since": "dimensions.datetimeHour"
                  },
                  "lt": 86400
                }
              ]
            },
            "fields": {
              "at": "dimensions.datetimeHour",
              "n": "sum.requests"
            }
          }
        },
        {
          "name": "per_zone",
          "value": {
            "each": "gql.data.viewer.accounts[0].zones",
            "fields": {
              "zone": "dimensions.zoneTag",
              "n": "sum.requests",
              "bytes": "sum.bytes"
            }
          }
        },
        {
          "name": "read_accounts",
          "value": {
            "count": "gql.data.viewer.accounts",
            "default": 0
          }
        },
        {
          "name": "zones_cut",
          "value": {
            "if": {
              "value": {
                "count": "gql.data.viewer.accounts[0].zones",
                "default": 0
              },
              "gte": 100
            },
            "then": {
              "template": "yes"
            },
            "else": {
              "template": "no"
            }
          }
        },
        {
          "name": "hour_count",
          "value": {
            "count": "hours",
            "default": 0
          }
        },
        {
          "name": "all_zones",
          "value": {
            "path": "list.result_info.total_count",
            "default": 0
          }
        },
        {
          "name": "listed",
          "value": {
            "count": "list.result",
            "default": 0
          }
        },
        {
          "name": "rows",
          "value": {
            "each": "list.result",
            "lookup": {
              "from": "per_zone",
              "by": "zone",
              "on": "id",
              "as": "traffic"
            },
            "fields": {
              "id": "id",
              "name": "name",
              "status": "status",
              "paused": {
                "path": "paused",
                "default": false
              },
              "plan": {
                "path": "plan.name",
                "default": ""
              },
              "n": {
                "path": "traffic.n",
                "default": 0
              },
              "read": {
                "if": {
                  "path": "traffic.zone",
                  "exists": true
                },
                "then": {
                  "template": "yes"
                },
                "else": {
                  "template": "no"
                }
              },
              "bytes": {
                "path": "traffic.bytes",
                "default": 0
              },
              "score": {
                "add": [
                  {
                    "path": "traffic.n",
                    "default": 0
                  },
                  {
                    "if": {
                      "path": "status",
                      "in": [
                        "pending",
                        "initializing",
                        "moved"
                      ]
                    },
                    "then": {
                      "add": [
                        1000000000000000,
                        0
                      ]
                    },
                    "else": {
                      "add": [
                        0,
                        0
                      ]
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "name": "attention",
          "value": {
            "count": "list.result",
            "where": {
              "path": "status",
              "in": [
                "pending",
                "initializing",
                "moved"
              ]
            },
            "default": 0
          }
        },
        {
          "name": "first_attention",
          "value": {
            "first": "name",
            "of": "list.result",
            "where": {
              "path": "status",
              "in": [
                "pending",
                "initializing",
                "moved"
              ]
            },
            "sort": "name"
          }
        },
        {
          "name": "first_attention_status",
          "value": {
            "first": "status",
            "of": "list.result",
            "where": {
              "path": "status",
              "in": [
                "pending",
                "initializing",
                "moved"
              ]
            },
            "sort": "name"
          }
        },
        {
          "name": "active",
          "value": {
            "count": "list.result",
            "where": {
              "path": "status",
              "equals": "active"
            },
            "default": 0
          }
        },
        {
          "name": "cached_share",
          "value": {
            "if": {
              "path": "requests",
              "gt": 0
            },
            "then": {
              "divide": [
                "cached",
                "requests"
              ]
            },
            "else": {
              "add": [
                0,
                0
              ]
            }
          }
        }
      ]
    },
    "tunnels": {
      "url": "https://api.cloudflare.com",
      "secret": "cloudflare",
      "every": "1m",
      "requests": {
        "acct": "/client/v4/accounts?per_page=5",
        "list": {
          "path": "/client/v4/accounts/{account}/cfd_tunnel?is_deleted=false&per_page=100",
          "with": {
            "account": "acct.result[0].id"
          }
        }
      },
      "fail": {
        "when": {
          "path": "list.success",
          "equals": false
        },
        "message": {
          "template": "{list.errors[0].message}"
        }
      },
      "derive": [
        {
          "name": "rows",
          "value": {
            "each": "list.result",
            "sort": "name",
            "fields": {
              "id": "id",
              "name": "name",
              "state": {
                "path": "status",
                "values": {
                  "healthy": "ok",
                  "degraded": "warning",
                  "down": "critical",
                  "inactive": "off"
                },
                "default": "unknown"
              },
              "detail": {
                "if": {
                  "all": [
                    {
                      "path": "status",
                      "equals": "down"
                    },
                    {
                      "path": "conns_inactive_at",
                      "exists": false
                    }
                  ]
                },
                "then": {
                  "template": "Down · no connections"
                },
                "else": {
                  "path": "status",
                  "values": {
                    "healthy": {
                      "template": "Healthy · connected {conns_active_at|ago}"
                    },
                    "degraded": {
                      "template": "Degraded · some connections lost"
                    },
                    "down": {
                      "template": "Down · no connections since {conns_inactive_at|ago}"
                    },
                    "inactive": {
                      "template": "Never connected"
                    }
                  },
                  "default": {
                    "template": "{status|title}"
                  }
                }
              }
            }
          }
        },
        {
          "name": "count",
          "value": {
            "count": "list.result",
            "default": 0
          }
        },
        {
          "name": "total",
          "value": {
            "path": "list.result_info.total_count",
            "default": 0
          }
        }
      ],
      "collect": {
        "dataset": "cloudflare-tunnels",
        "each": "rows",
        "key": "{id}",
        "sample": {
          "every": "5m"
        },
        "fields": {
          "state": "state"
        },
        "readers": [
          "cloudflare-tunnel"
        ]
      }
    },
    "registrar": {
      "url": "https://api.cloudflare.com",
      "secret": "cloudflare",
      "every": "6h",
      "requests": {
        "acct": "/client/v4/accounts?per_page=5",
        "reg": {
          "path": "/client/v4/accounts/{account}/registrar/registrations?per_page=50&sort_by=registry_expires_at&direction=asc",
          "with": {
            "account": "acct.result[0].id"
          }
        }
      },
      "fail": {
        "when": {
          "path": "reg.success",
          "equals": false
        },
        "message": {
          "template": "{reg.errors[0].message}"
        }
      },
      "derive": [
        {
          "name": "domains",
          "value": {
            "each": "reg.result",
            "where": {
              "path": "expires_at",
              "exists": true
            },
            "sort": {
              "by": "expires_at",
              "order": "asc"
            },
            "fields": {
              "name": "domain_name",
              "at": "expires_at",
              "auto": {
                "path": "auto_renew",
                "default": false
              }
            }
          }
        },
        {
          "name": "domain_count",
          "value": {
            "count": "reg.result",
            "default": 0
          }
        },
        {
          "name": "lapsed",
          "value": {
            "count": "reg.result",
            "where": {
              "path": "status",
              "in": [
                "expired",
                "redemption_period",
                "pending_delete"
              ]
            },
            "default": 0
          }
        },
        {
          "name": "lapsed_name",
          "value": {
            "first": "domain_name",
            "of": "reg.result",
            "where": {
              "path": "status",
              "in": [
                "expired",
                "redemption_period",
                "pending_delete"
              ]
            }
          }
        },
        {
          "name": "lapsed_status",
          "value": {
            "first": "status",
            "of": "reg.result",
            "where": {
              "path": "status",
              "in": [
                "expired",
                "redemption_period",
                "pending_delete"
              ]
            }
          }
        },
        {
          "name": "more",
          "value": {
            "if": {
              "all": [
                {
                  "path": "reg.result_info.cursor",
                  "exists": true
                },
                {
                  "path": "reg.result_info.cursor",
                  "not": ""
                }
              ]
            },
            "then": {
              "template": "yes"
            },
            "else": {
              "template": "no"
            }
          }
        },
        {
          "name": "last_at",
          "value": {
            "max": "at",
            "of": "domains"
          }
        },
        {
          "name": "next_manual",
          "value": {
            "each": "domains",
            "where": {
              "path": "auto",
              "equals": false
            },
            "limit": 1,
            "fields": {
              "name": "name",
              "at": "at"
            }
          }
        },
        {
          "name": "next_name",
          "value": {
            "if": {
              "value": {
                "count": "next_manual",
                "default": 0
              },
              "gt": 0
            },
            "then": {
              "path": "next_manual[0].name"
            },
            "else": {
              "path": "domains[0].name"
            }
          }
        },
        {
          "name": "next_at",
          "value": {
            "if": {
              "value": {
                "count": "next_manual",
                "default": 0
              },
              "gt": 0
            },
            "then": {
              "path": "next_manual[0].at"
            },
            "else": {
              "path": "domains[0].at"
            }
          }
        },
        {
          "name": "next_auto",
          "value": {
            "if": {
              "value": {
                "count": "next_manual",
                "default": 0
              },
              "gt": 0
            },
            "then": {
              "template": "no"
            },
            "else": {
              "template": "yes"
            }
          }
        }
      ]
    },
    "billing": {
      "url": "https://api.cloudflare.com",
      "secret": "cloudflare",
      "every": "1h",
      "requests": {
        "acct": "/client/v4/accounts?per_page=5",
        "subs": {
          "path": "/client/v4/accounts/{account}/subscriptions",
          "with": {
            "account": "acct.result[0].id"
          }
        },
        "history": {
          "path": "/client/v4/accounts/{account}/billing/history?per_page=20",
          "with": {
            "account": "acct.result[0].id"
          }
        }
      },
      "fail": {
        "when": {
          "any": [
            {
              "path": "subs.success",
              "equals": false
            },
            {
              "path": "history.success",
              "equals": false
            }
          ]
        },
        "message": {
          "if": {
            "path": "subs.success",
            "equals": false
          },
          "then": {
            "template": "{subs.errors[0].message}"
          },
          "else": {
            "template": "{history.errors[0].message}"
          }
        }
      },
      "derive": [
        {
          "name": "plans",
          "value": {
            "each": "subs.result",
            "where": {
              "path": "state",
              "not_in": [
                "Cancelled",
                "Expired"
              ]
            },
            "sort": "rate_plan.public_name",
            "fields": {
              "name": {
                "path": "rate_plan.public_name",
                "default": "Plan"
              },
              "words": {
                "if": {
                  "path": "price",
                  "gt": 0
                },
                "then": {
                  "if": {
                    "path": "currency",
                    "equals": "USD"
                  },
                  "then": {
                    "template": "{rate_plan.public_name} ${price|round:2}"
                  },
                  "else": {
                    "template": "{rate_plan.public_name} {price|round:2} {currency}"
                  }
                },
                "else": {
                  "template": "{rate_plan.public_name}"
                }
              }
            }
          }
        },
        {
          "name": "plan_words",
          "value": {
            "join": "words",
            "of": "plans",
            "separator": " · "
          }
        },
        {
          "name": "plan_count",
          "value": {
            "count": "plans",
            "default": 0
          }
        },
        {
          "name": "renewals",
          "value": {
            "each": "subs.result",
            "where": {
              "all": [
                {
                  "path": "state",
                  "not_in": [
                    "Cancelled",
                    "Expired"
                  ]
                },
                {
                  "path": "price",
                  "gt": 0
                },
                {
                  "path": "current_period_end",
                  "exists": true
                }
              ]
            },
            "sort": {
              "by": "current_period_end",
              "order": "asc"
            },
            "limit": 1,
            "fields": {
              "name": {
                "path": "rate_plan.public_name",
                "default": "Plan"
              },
              "at": "current_period_end",
              "cost": {
                "if": {
                  "path": "currency",
                  "equals": "USD"
                },
                "then": {
                  "template": "${price|round:2}"
                },
                "else": {
                  "template": "{price|round:2} {currency}"
                }
              },
              "per": {
                "path": "frequency",
                "values": {
                  "monthly": {
                    "template": "/mo"
                  },
                  "yearly": {
                    "template": "/yr"
                  },
                  "quarterly": {
                    "template": "/qtr"
                  },
                  "weekly": {
                    "template": "/wk"
                  }
                },
                "default": {
                  "template": ""
                }
              }
            }
          }
        },
        {
          "name": "renewal_count",
          "value": {
            "count": "renewals",
            "default": 0
          }
        },
        {
          "name": "charges",
          "value": {
            "each": "history.result",
            "where": {
              "path": "type",
              "in": [
                "charge",
                "Charge"
              ]
            },
            "sort": {
              "by": "occurred_at",
              "order": "desc"
            },
            "limit": 1,
            "fields": {
              "amount": {
                "if": {
                  "path": "currency",
                  "equals": "USD"
                },
                "then": {
                  "template": "${amount|round:2}"
                },
                "else": {
                  "template": "{amount|round:2} {currency}"
                }
              },
              "at": "occurred_at",
              "status": {
                "path": "status",
                "default": ""
              },
              "url": {
                "path": "hosted_invoice_url",
                "default": "https://dash.cloudflare.com/"
              }
            }
          }
        },
        {
          "name": "charge_count",
          "value": {
            "count": "history.result",
            "where": {
              "path": "type",
              "in": [
                "charge",
                "Charge"
              ]
            },
            "default": 0
          }
        },
        {
          "name": "history_read",
          "value": {
            "count": "history.result",
            "default": 0
          }
        },
        {
          "name": "history_more",
          "value": {
            "if": {
              "path": "history.result_info.total_count",
              "gt": {
                "path": "history_read"
              }
            },
            "then": {
              "template": "yes"
            },
            "else": {
              "template": "no"
            }
          }
        }
      ]
    },
    "usage": {
      "url": "https://api.cloudflare.com",
      "secret": "cloudflare",
      "every": "1h",
      "requests": {
        "acct": "/client/v4/accounts?per_page=5",
        "usage": {
          "path": "/client/v4/graphql",
          "method": "POST",
          "body": {
            "query": "query ($account: string!) { viewer { accounts(filter: { accountTag: $account }) { r2s: r2StorageAdaptiveGroups(limit: 100, filter: {datetime_geq: \"{date.month_start_at}\", datetime_lt: \"{date.now}\"}) { max { payloadSize metadataSize } dimensions { bucketName } } r2ops: r2OperationsAdaptiveGroups(limit: 100, filter: {datetime_geq: \"{date.month_start_at}\", datetime_lt: \"{date.now}\"}) { sum { requests } dimensions { actionType } } workers: workersInvocationsAdaptive(limit: 1, filter: {datetime_geq: \"{date.hours_ago_24_at}\", datetime_lt: \"{date.now}\"}) { sum { requests } } } } }",
            "variables": {
              "account": "{account}"
            }
          },
          "with": {
            "account": "acct.result[0].id"
          }
        }
      },
      "fail": {
        "when": {
          "path": "usage.errors",
          "exists": true
        },
        "message": {
          "if": {
            "path": "usage.errors[0].extensions.code",
            "equals": "authz"
          },
          "then": {
            "template": "Cloudflare turned down the use figures: the token needs Read on Account › Account Analytics"
          },
          "else": {
            "template": "{usage.errors[0].message|clip:200}"
          }
        }
      },
      "derive": [
        {
          "name": "u_storage",
          "value": {
            "each": "usage.data.viewer.accounts",
            "limit": 1,
            "fields": {
              "label": {
                "if": {
                  "value": {
                    "count": "r2s",
                    "default": 0
                  },
                  "gte": 100
                },
                "then": {
                  "template": "R2 storage, 100 buckets"
                },
                "else": {
                  "template": "R2 storage"
                }
              },
              "value": {
                "divide": [
                  {
                    "add": [
                      {
                        "sum": "max.payloadSize",
                        "of": "r2s",
                        "default": 0
                      },
                      {
                        "sum": "max.metadataSize",
                        "of": "r2s",
                        "default": 0
                      }
                    ]
                  },
                  1000000000
                ],
                "round": 2
              },
              "target": {
                "add": [
                  10,
                  0
                ]
              },
              "unit": {
                "template": "GB"
              }
            }
          }
        },
        {
          "name": "u_class_a",
          "value": {
            "each": "usage.data.viewer.accounts",
            "limit": 1,
            "fields": {
              "label": {
                "template": "R2 Class A"
              },
              "value": {
                "sum": "sum.requests",
                "of": "r2ops",
                "where": {
                  "path": "dimensions.actionType",
                  "in": [
                    "ListBuckets",
                    "PutBucket",
                    "ListObjects",
                    "PutObject",
                    "CopyObject",
                    "CompleteMultipartUpload",
                    "CreateMultipartUpload",
                    "LifecycleStorageTierTransition",
                    "ListMultipartUploads",
                    "UploadPart",
                    "UploadPartCopy",
                    "ListParts",
                    "PutBucketEncryption",
                    "PutBucketCors",
                    "PutBucketLifecycleConfiguration"
                  ]
                },
                "default": 0
              },
              "target": {
                "add": [
                  1000000,
                  0
                ]
              },
              "unit": {
                "template": ""
              }
            }
          }
        },
        {
          "name": "u_class_b",
          "value": {
            "each": "usage.data.viewer.accounts",
            "limit": 1,
            "fields": {
              "label": {
                "template": "R2 Class B"
              },
              "value": {
                "sum": "sum.requests",
                "of": "r2ops",
                "where": {
                  "path": "dimensions.actionType",
                  "in": [
                    "HeadBucket",
                    "HeadObject",
                    "GetObject",
                    "UsageSummary",
                    "GetBucketEncryption",
                    "GetBucketLocation",
                    "GetBucketCors",
                    "GetBucketLifecycleConfiguration"
                  ]
                },
                "default": 0
              },
              "target": {
                "add": [
                  10000000,
                  0
                ]
              },
              "unit": {
                "template": ""
              }
            }
          }
        },
        {
          "name": "u_workers",
          "value": {
            "each": "usage.data.viewer.accounts",
            "limit": 1,
            "fields": {
              "label": {
                "template": "Workers, 24h"
              },
              "value": {
                "sum": "sum.requests",
                "of": "workers",
                "default": 0
              },
              "target": {
                "add": [
                  100000,
                  0
                ]
              },
              "unit": {
                "template": ""
              }
            }
          }
        },
        {
          "name": "allowance",
          "value": {
            "each": [
              "u_storage",
              "u_class_a",
              "u_class_b",
              "u_workers"
            ],
            "where": {
              "path": "value",
              "gt": 0
            },
            "fields": {
              "label": "label",
              "value": "value",
              "target": "target",
              "unit": "unit"
            }
          }
        },
        {
          "name": "allowance_count",
          "value": {
            "count": "allowance",
            "default": 0
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "health",
      "type": "status",
      "width": "full",
      "placement": "page_info",
      "label": "Zones and token",
      "state": "unknown",
      "url": "https://dash.cloudflare.com/",
      "source": {
        "ref": "inventory",
        "map": {
          "state": {
            "if": {
              "value": {
                "expires": {
                  "date": "token_expires"
                },
                "as": "state",
                "default": "ok"
              },
              "equals": "critical"
            },
            "then": {
              "template": "critical"
            },
            "else": {
              "if": {
                "any": [
                  {
                    "value": {
                      "expires": {
                        "date": "token_expires"
                      },
                      "as": "state",
                      "default": "ok"
                    },
                    "equals": "warning"
                  },
                  {
                    "path": "attention",
                    "gt": 0
                  }
                ]
              },
              "then": {
                "template": "warning"
              },
              "else": {
                "if": {
                  "path": "listed",
                  "gt": 0
                },
                "then": {
                  "template": "ok"
                },
                "else": {
                  "template": "off"
                }
              }
            }
          },
          "detail": {
            "if": {
              "value": {
                "expires": {
                  "date": "token_expires"
                },
                "as": "state",
                "default": "ok"
              },
              "equals": "critical"
            },
            "then": {
              "template": "API token expired {token_expires|day}"
            },
            "else": {
              "if": {
                "path": "attention",
                "gt": 1
              },
              "then": {
                "template": "{attention} zones need a look, first {first_attention|clip:50}"
              },
              "else": {
                "if": {
                  "path": "attention",
                  "equals": 1
                },
                "then": {
                  "path": "first_attention_status",
                  "values": {
                    "pending": {
                      "template": "{first_attention|clip:60}: waiting for Cloudflare's nameservers"
                    },
                    "initializing": {
                      "template": "{first_attention|clip:60}: being set up"
                    },
                    "moved": {
                      "template": "{first_attention|clip:60}: nameservers moved away"
                    }
                  }
                },
                "else": {
                  "if": {
                    "value": {
                      "expires": {
                        "date": "token_expires"
                      },
                      "as": "state",
                      "default": "ok"
                    },
                    "equals": "warning"
                  },
                  "then": {
                    "template": "API token expires {token_expires|day}"
                  },
                  "else": {
                    "if": {
                      "path": "listed",
                      "equals": 0
                    },
                    "then": {
                      "template": "No zones on this token"
                    },
                    "else": {
                      "if": {
                        "path": "token_expires",
                        "exists": true
                      },
                      "then": {
                        "if": {
                          "path": "active",
                          "equals": 1
                        },
                        "then": {
                          "template": "1 zone active · token expires {token_expires|day}"
                        },
                        "else": {
                          "template": "{active} zones active · token expires {token_expires|day}"
                        }
                      },
                      "else": {
                        "if": {
                          "path": "active",
                          "equals": 1
                        },
                        "then": {
                          "template": "1 zone active"
                        },
                        "else": {
                          "template": "{active} zones active"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    {
      "id": "domains",
      "type": "status",
      "width": "full",
      "placement": "page_info",
      "label": "Domains",
      "state": "unknown",
      "url": "https://dash.cloudflare.com/",
      "source": {
        "ref": "registrar",
        "when": {
          "path": "domain_count",
          "gt": 0
        },
        "map": {
          "state": {
            "if": {
              "value": {
                "if": {
                  "path": "lapsed",
                  "gt": 0
                },
                "then": {
                  "template": "critical"
                },
                "else": {
                  "if": {
                    "path": "next_auto",
                    "equals": "yes"
                  },
                  "then": {
                    "template": "ok"
                  },
                  "else": {
                    "expires": {
                      "date": "next_at"
                    },
                    "as": "state",
                    "warning": 30,
                    "critical": 7,
                    "default": "ok"
                  }
                }
              },
              "equals": "ok"
            },
            "then": {
              "if": {
                "all": [
                  {
                    "path": "more",
                    "equals": "yes"
                  },
                  {
                    "value": {
                      "expires": {
                        "date": "last_at"
                      },
                      "as": "state",
                      "warning": 30,
                      "default": "ok"
                    },
                    "in": [
                      "warning",
                      "critical"
                    ]
                  }
                ]
              },
              "then": {
                "template": "warning"
              },
              "else": {
                "template": "ok"
              }
            },
            "else": {
              "if": {
                "path": "lapsed",
                "gt": 0
              },
              "then": {
                "template": "critical"
              },
              "else": {
                "if": {
                  "path": "next_auto",
                  "equals": "yes"
                },
                "then": {
                  "template": "ok"
                },
                "else": {
                  "expires": {
                    "date": "next_at"
                  },
                  "as": "state",
                  "warning": 30,
                  "critical": 7,
                  "default": "ok"
                }
              }
            }
          },
          "detail": {
            "if": {
              "all": [
                {
                  "value": {
                    "if": {
                      "path": "lapsed",
                      "gt": 0
                    },
                    "then": {
                      "template": "critical"
                    },
                    "else": {
                      "if": {
                        "path": "next_auto",
                        "equals": "yes"
                      },
                      "then": {
                        "template": "ok"
                      },
                      "else": {
                        "expires": {
                          "date": "next_at"
                        },
                        "as": "state",
                        "warning": 30,
                        "critical": 7,
                        "default": "ok"
                      }
                    }
                  },
                  "equals": "ok"
                },
                {
                  "all": [
                    {
                      "path": "more",
                      "equals": "yes"
                    },
                    {
                      "value": {
                        "expires": {
                          "date": "last_at"
                        },
                        "as": "state",
                        "warning": 30,
                        "default": "ok"
                      },
                      "in": [
                        "warning",
                        "critical"
                      ]
                    }
                  ]
                }
              ]
            },
            "then": {
              "template": "More than 50 domains expire within 30 days; the first 50 were read"
            },
            "else": {
              "if": {
                "path": "lapsed",
                "gt": 0
              },
              "then": {
                "template": "{lapsed_name|clip:50} has expired · {lapsed_status|title}"
              },
              "else": {
                "if": {
                  "path": "next_auto",
                  "equals": "yes"
                },
                "then": {
                  "if": {
                    "path": "domain_count",
                    "equals": 1
                  },
                  "then": {
                    "template": "{next_name|clip:50} renews {next_at|day}"
                  },
                  "else": {
                    "if": {
                      "path": "more",
                      "equals": "yes"
                    },
                    "then": {
                      "template": "{next_name|clip:50} renews {next_at|day} · 50+ domains"
                    },
                    "else": {
                      "template": "{next_name|clip:50} renews {next_at|day} · {domain_count} domains"
                    }
                  }
                },
                "else": {
                  "template": "{next_name|clip:50} expires {next_at|day}, auto-renew off"
                }
              }
            }
          }
        }
      }
    },
    {
      "id": "no-analytics",
      "type": "text",
      "tone": "info",
      "body": "Cloudflare sent no traffic figures. Add Read on Account Analytics to the token at [dash.cloudflare.com/profile/api-tokens](https://dash.cloudflare.com/profile/api-tokens).",
      "source": {
        "ref": "zones",
        "when": {
          "path": "read_accounts",
          "equals": 0
        }
      }
    },
    {
      "id": "day",
      "type": "facts",
      "label": "Last 24 hours",
      "items": [
        {
          "key": "Requests",
          "value": "—"
        },
        {
          "key": "Data served",
          "value": "—"
        },
        {
          "key": "Cached",
          "value": "—"
        },
        {
          "key": "Server errors",
          "value": "—"
        }
      ],
      "source": {
        "ref": "zones",
        "when": {
          "path": "read_accounts",
          "gt": 0
        },
        "map": {
          "items[0].value": {
            "template": "{requests|round:0}"
          },
          "items[1].value": {
            "template": "{bytes|bytes}"
          },
          "items[2].value": {
            "if": {
              "path": "requests",
              "gt": 0
            },
            "then": {
              "template": "{cached_share|percent}"
            },
            "else": {
              "template": "None"
            }
          },
          "items[3].value": {
            "if": {
              "path": "requests",
              "gt": 0
            },
            "then": {
              "template": "{error_share|percent:1}"
            },
            "else": {
              "template": "None"
            }
          }
        }
      }
    },
    {
      "id": "per-hour",
      "type": "sparkline",
      "label": "Requests an hour",
      "points": [],
      "change": false,
      "source": {
        "ref": "zones",
        "when": {
          "path": "hour_count",
          "gte": 2
        },
        "map": {
          "points": {
            "each": "hours",
            "value": "n"
          },
          "times": {
            "each": "hours",
            "value": "at"
          },
          "labels": {
            "each": "hours",
            "template": "{at|clock_hour}"
          },
          "start_label": {
            "template": "24h ago"
          },
          "end_label": {
            "template": "Now"
          }
        }
      }
    },
    {
      "id": "tunnels",
      "type": "fleet",
      "label": "Tunnels",
      "max_items": 24,
      "items": [],
      "source": {
        "ref": "tunnels",
        "when": {
          "path": "count",
          "gt": 0
        },
        "failed": {
          "state": "unknown",
          "detail": "Cloudflare isn't answering for the tunnels",
          "after": 3
        },
        "map": {
          "label": {
            "if": {
              "path": "total",
              "gt": {
                "path": "count"
              }
            },
            "then": {
              "template": "Tunnels · first {count} of {total}"
            },
            "else": {
              "template": "Tunnels"
            }
          },
          "items": {
            "each": "rows",
            "fields": {
              "name": "name",
              "state": "state",
              "detail": "detail",
              "url": {
                "template": "legato://page/cloudflare-tunnel?id={id}"
              }
            }
          }
        }
      }
    },
    {
      "id": "zones",
      "type": "list",
      "label": "Zones",
      "max_items": 7,
      "items": [],
      "source": {
        "ref": "zones",
        "when": {
          "path": "listed",
          "gt": 0
        },
        "map": {
          "label": {
            "if": {
              "path": "all_zones",
              "gt": {
                "path": "listed"
              }
            },
            "then": {
              "template": "Zones · first {listed} of {all_zones}"
            },
            "else": {
              "template": "Zones"
            }
          },
          "items": {
            "if": {
              "path": "read_accounts",
              "equals": 0
            },
            "then": {
              "each": "rows",
              "sort": {
                "by": "score",
                "order": "desc"
              },
              "fields": {
                "key": "id",
                "text": "name",
                "detail": {
                  "if": {
                    "path": "status",
                    "in": [
                      "pending",
                      "initializing",
                      "moved"
                    ]
                  },
                  "then": {
                    "path": "status",
                    "values": {
                      "pending": {
                        "template": "Waiting for nameservers"
                      },
                      "initializing": {
                        "template": "Being set up"
                      },
                      "moved": {
                        "template": "Nameservers moved away"
                      },
                      "active": {
                        "template": "Active"
                      }
                    }
                  },
                  "else": {
                    "if": {
                      "path": "paused",
                      "equals": true
                    },
                    "then": {
                      "template": "Paused · DNS only"
                    },
                    "else": {
                      "template": "Active"
                    }
                  }
                },
                "icon": {
                  "if": {
                    "path": "status",
                    "in": [
                      "pending",
                      "initializing",
                      "moved"
                    ]
                  },
                  "then": {
                    "template": "exclamationmark.triangle.fill"
                  },
                  "else": {
                    "template": "globe"
                  }
                },
                "color": {
                  "if": {
                    "path": "status",
                    "in": [
                      "pending",
                      "initializing",
                      "moved"
                    ]
                  },
                  "then": {
                    "template": "orange"
                  }
                },
                "url": {
                  "template": "legato://page/cloudflare-zone?zone={id}"
                }
              }
            },
            "else": {
              "if": {
                "path": "zones_cut",
                "equals": "yes"
              },
              "then": {
                "each": "rows",
                "sort": {
                  "by": "score",
                  "order": "desc"
                },
                "fields": {
                  "key": "id",
                  "text": "name",
                  "detail": {
                    "if": {
                      "path": "status",
                      "in": [
                        "pending",
                        "initializing",
                        "moved"
                      ]
                    },
                    "then": {
                      "path": "status",
                      "values": {
                        "pending": {
                          "template": "Waiting for nameservers"
                        },
                        "initializing": {
                          "template": "Being set up"
                        },
                        "moved": {
                          "template": "Nameservers moved away"
                        },
                        "active": {
                          "template": "Active"
                        }
                      }
                    },
                    "else": {
                      "if": {
                        "path": "paused",
                        "equals": true
                      },
                      "then": {
                        "template": "Paused · DNS only"
                      },
                      "else": {
                        "if": {
                          "path": "n",
                          "equals": 0
                        },
                        "then": {
                          "if": {
                            "path": "read",
                            "equals": "no"
                          },
                          "then": {
                            "template": "Not among the 100 busiest"
                          },
                          "else": {
                            "template": "No requests"
                          }
                        },
                        "else": {
                          "template": "{n|round:0} requests · {bytes|bytes}"
                        }
                      }
                    }
                  },
                  "icon": {
                    "if": {
                      "path": "status",
                      "in": [
                        "pending",
                        "initializing",
                        "moved"
                      ]
                    },
                    "then": {
                      "template": "exclamationmark.triangle.fill"
                    },
                    "else": {
                      "template": "globe"
                    }
                  },
                  "color": {
                    "if": {
                      "path": "status",
                      "in": [
                        "pending",
                        "initializing",
                        "moved"
                      ]
                    },
                    "then": {
                      "template": "orange"
                    }
                  },
                  "url": {
                    "template": "legato://page/cloudflare-zone?zone={id}"
                  }
                }
              },
              "else": {
                "each": "rows",
                "sort": {
                  "by": "score",
                  "order": "desc"
                },
                "fields": {
                  "key": "id",
                  "text": "name",
                  "detail": {
                    "if": {
                      "path": "status",
                      "in": [
                        "pending",
                        "initializing",
                        "moved"
                      ]
                    },
                    "then": {
                      "path": "status",
                      "values": {
                        "pending": {
                          "template": "Waiting for nameservers"
                        },
                        "initializing": {
                          "template": "Being set up"
                        },
                        "moved": {
                          "template": "Nameservers moved away"
                        },
                        "active": {
                          "template": "Active"
                        }
                      }
                    },
                    "else": {
                      "if": {
                        "path": "paused",
                        "equals": true
                      },
                      "then": {
                        "template": "Paused · DNS only"
                      },
                      "else": {
                        "if": {
                          "path": "n",
                          "equals": 0
                        },
                        "then": {
                          "template": "No requests"
                        },
                        "else": {
                          "template": "{n|round:0} requests · {bytes|bytes}"
                        }
                      }
                    }
                  },
                  "icon": {
                    "if": {
                      "path": "status",
                      "in": [
                        "pending",
                        "initializing",
                        "moved"
                      ]
                    },
                    "then": {
                      "template": "exclamationmark.triangle.fill"
                    },
                    "else": {
                      "template": "globe"
                    }
                  },
                  "color": {
                    "if": {
                      "path": "status",
                      "in": [
                        "pending",
                        "initializing",
                        "moved"
                      ]
                    },
                    "then": {
                      "template": "orange"
                    }
                  },
                  "url": {
                    "template": "legato://page/cloudflare-zone?zone={id}"
                  }
                }
              }
            }
          }
        }
      }
    },
    {
      "id": "billing",
      "type": "facts",
      "label": "Billing",
      "items": [
        {
          "key": "Plans",
          "value": "—"
        },
        {
          "key": "Next renewal",
          "value": "—"
        },
        {
          "key": "Last charge",
          "value": "—"
        }
      ],
      "source": {
        "ref": "billing",
        "map": {
          "items[0].value": {
            "if": {
              "path": "plan_count",
              "gt": 0
            },
            "then": {
              "template": "{plan_words|clip:80}"
            },
            "else": {
              "template": "—"
            }
          },
          "items[1].value": {
            "if": {
              "path": "renewal_count",
              "gt": 0
            },
            "then": {
              "template": "{renewals[0].name|clip:30} {renewals[0].cost}{renewals[0].per} · {renewals[0].at|day}"
            },
            "else": {
              "template": "—"
            }
          },
          "items[2].value": {
            "if": {
              "path": "charge_count",
              "gt": 0
            },
            "then": {
              "if": {
                "path": "charges[0].status",
                "exists": true
              },
              "then": {
                "template": "{charges[0].amount} · {charges[0].at|day} · {charges[0].status|title}"
              },
              "else": {
                "template": "{charges[0].amount} · {charges[0].at|day}"
              }
            },
            "else": {
              "if": {
                "path": "history_more",
                "equals": "yes"
              },
              "then": {
                "template": "None in the newest 20 entries"
              },
              "else": {
                "template": "None yet"
              }
            }
          },
          "items[2].url": {
            "if": {
              "path": "charge_count",
              "gt": 0
            },
            "then": {
              "template": "{charges[0].url}"
            },
            "else": {
              "template": "https://dash.cloudflare.com/"
            }
          }
        }
      }
    },
    {
      "id": "allowance",
      "type": "pacing",
      "label": "Free allowance",
      "items": [],
      "source": {
        "ref": "usage",
        "when": {
          "path": "allowance_count",
          "gt": 0
        },
        "map": {
          "items": {
            "each": "allowance",
            "fields": {
              "label": "label",
              "value": "value",
              "target": "target",
              "unit": "unit"
            }
          }
        }
      }
    }
  ]
}
