{
  "$schema": "./.schemas/page.schema.json",
  "title": "Zone",
  "title_from": {
    "ref": "zone",
    "value": "info.result.name"
  },
  "icon": "globe",
  "sidebar": false,
  "params": {
    "zone": {
      "example": "9b2e7c4d1a8f3e6b5c0d9a7e2f4b8c1d",
      "pattern": "[0-9a-f]{32}"
    }
  },
  "sources": {
    "zone": {
      "url": "https://api.cloudflare.com",
      "secret": "cloudflare",
      "every": "5m",
      "requests": {
        "info": "/client/v4/zones/{param.zone}",
        "gql": {
          "path": "/client/v4/graphql",
          "method": "POST",
          "body": {
            "query": "{ viewer { zones(filter: { zoneTag: \"{param.zone}\" }) { days: httpRequests1dGroups(limit: 31, filter: { date_geq: \"{date.days_ago_29}\" }, orderBy: [date_ASC]) { dimensions { date } sum { requests cachedRequests bytes } } codes: httpRequestsAdaptiveGroups(limit: 40, filter: {datetime_geq: \"{date.hours_ago_24_at}\", datetime_lt: \"{date.now}\", requestSource: \"eyeball\"}, orderBy: [count_DESC]) { count dimensions { edgeResponseStatus } } countries: httpRequestsAdaptiveGroups(limit: 6, filter: {datetime_geq: \"{date.hours_ago_24_at}\", datetime_lt: \"{date.now}\", requestSource: \"eyeball\"}, orderBy: [count_DESC]) { count dimensions { clientCountryName } } } } }"
          }
        }
      },
      "fail": {
        "when": {
          "any": [
            {
              "path": "info.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 Zone › Analytics"
            },
            "else": {
              "template": "{gql.errors[0].message|clip:200}"
            }
          },
          "else": {
            "template": "{info.errors[0].message}"
          }
        }
      },
      "derive": [
        {
          "name": "days",
          "value": {
            "each": "gql.data.viewer.zones[0].days",
            "fields": {
              "day": "dimensions.date",
              "n": "sum.requests",
              "cached": "sum.cachedRequests",
              "bytes": "sum.bytes"
            }
          }
        },
        {
          "name": "month_requests",
          "value": {
            "sum": "n",
            "of": "days",
            "default": 0
          }
        },
        {
          "name": "day_count",
          "value": {
            "count": "days",
            "default": 0
          }
        },
        {
          "name": "month_cached",
          "value": {
            "sum": "cached",
            "of": "days",
            "default": 0
          }
        },
        {
          "name": "month_bytes",
          "value": {
            "sum": "bytes",
            "of": "days",
            "default": 0
          }
        },
        {
          "name": "month_cached_share",
          "value": {
            "if": {
              "path": "month_requests",
              "gt": 0
            },
            "then": {
              "divide": [
                "month_cached",
                "month_requests"
              ]
            },
            "else": {
              "add": [
                0,
                0
              ]
            }
          }
        },
        {
          "name": "classes",
          "value": {
            "each": "gql.data.viewer.zones[0].codes",
            "fields": {
              "class": {
                "path": "dimensions.edgeResponseStatus",
                "match": "^([1-5])"
              },
              "n": "count"
            }
          }
        },
        {
          "name": "day_requests",
          "value": {
            "sum": "n",
            "of": "classes",
            "default": 0
          }
        },
        {
          "name": "c2",
          "value": {
            "sum": "n",
            "of": "classes",
            "where": {
              "path": "class",
              "equals": "2"
            },
            "default": 0
          }
        },
        {
          "name": "c3",
          "value": {
            "sum": "n",
            "of": "classes",
            "where": {
              "path": "class",
              "equals": "3"
            },
            "default": 0
          }
        },
        {
          "name": "c4",
          "value": {
            "sum": "n",
            "of": "classes",
            "where": {
              "path": "class",
              "equals": "4"
            },
            "default": 0
          }
        },
        {
          "name": "c5",
          "value": {
            "sum": "n",
            "of": "classes",
            "where": {
              "path": "class",
              "equals": "5"
            },
            "default": 0
          }
        }
      ]
    },
    "certs": {
      "url": "https://api.cloudflare.com",
      "secret": "cloudflare",
      "every": "6h",
      "requests": {
        "packs": "/client/v4/zones/{param.zone}/ssl/certificate_packs?status=all&per_page=50"
      },
      "fail": {
        "when": {
          "path": "packs.success",
          "equals": false
        },
        "message": {
          "template": "{packs.errors[0].message}"
        }
      },
      "derive": [
        {
          "name": "live",
          "value": {
            "each": "packs.result",
            "where": {
              "path": "status",
              "not_in": [
                "deleted",
                "inactive"
              ]
            },
            "fields": {
              "type": "type",
              "status": "status",
              "hosts": {
                "join": "$",
                "of": "hosts",
                "separator": ", "
              }
            }
          }
        },
        {
          "name": "certs",
          "value": {
            "each": "packs.result",
            "where": {
              "path": "status",
              "in": [
                "active",
                "backup_issued",
                "pending_expiration"
              ]
            },
            "flatten": "certificates",
            "fields": {
              "at": "expires_on"
            }
          }
        },
        {
          "name": "soonest",
          "value": {
            "min": "at",
            "of": "certs"
          }
        },
        {
          "name": "pack_count",
          "value": {
            "count": "live",
            "default": 0
          }
        },
        {
          "name": "packs_read",
          "value": {
            "count": "packs.result",
            "default": 0
          }
        },
        {
          "name": "packs_cut",
          "value": {
            "if": {
              "path": "packs.result_info.total_count",
              "gt": {
                "path": "packs_read"
              }
            },
            "then": {
              "template": "yes"
            },
            "else": {
              "template": "no"
            }
          }
        },
        {
          "name": "expired_packs",
          "value": {
            "count": "live",
            "where": {
              "path": "status",
              "equals": "expired"
            },
            "default": 0
          }
        },
        {
          "name": "stalled",
          "value": {
            "count": "live",
            "where": {
              "path": "status",
              "in": [
                "validation_timed_out",
                "issuance_timed_out",
                "deployment_timed_out",
                "pending_validation"
              ]
            },
            "default": 0
          }
        },
        {
          "name": "first_stalled",
          "value": {
            "first": "status",
            "of": "live",
            "where": {
              "path": "status",
              "in": [
                "validation_timed_out",
                "issuance_timed_out",
                "deployment_timed_out",
                "pending_validation"
              ]
            }
          }
        }
      ]
    },
    "settings": {
      "url": "https://api.cloudflare.com",
      "secret": "cloudflare",
      "every": "1m",
      "requests": {
        "level": "/client/v4/zones/{param.zone}/settings/security_level",
        "dev": "/client/v4/zones/{param.zone}/settings/development_mode"
      },
      "fail": {
        "when": {
          "any": [
            {
              "path": "level.success",
              "equals": false
            },
            {
              "path": "dev.success",
              "equals": false
            }
          ]
        },
        "message": {
          "if": {
            "path": "level.success",
            "equals": false
          },
          "then": {
            "template": "{level.errors[0].message}"
          },
          "else": {
            "template": "{dev.errors[0].message}"
          }
        }
      }
    }
  },
  "cards": [
    {
      "id": "certificates",
      "type": "status",
      "width": "full",
      "placement": "page_info",
      "label": "Certificates",
      "state": "unknown",
      "source": {
        "ref": "certs",
        "map": {
          "state": {
            "if": {
              "path": "pack_count",
              "equals": 0
            },
            "then": {
              "template": "off"
            },
            "else": {
              "if": {
                "any": [
                  {
                    "path": "expired_packs",
                    "gt": 0
                  },
                  {
                    "value": {
                      "expires": {
                        "date": "soonest"
                      },
                      "as": "state",
                      "warning": 7,
                      "critical": 2,
                      "default": "ok"
                    },
                    "equals": "critical"
                  }
                ]
              },
              "then": {
                "template": "critical"
              },
              "else": {
                "if": {
                  "any": [
                    {
                      "path": "stalled",
                      "gt": 0
                    },
                    {
                      "value": {
                        "expires": {
                          "date": "soonest"
                        },
                        "as": "state",
                        "warning": 7,
                        "critical": 2,
                        "default": "ok"
                      },
                      "equals": "warning"
                    },
                    {
                      "path": "packs_cut",
                      "equals": "yes"
                    }
                  ]
                },
                "then": {
                  "template": "warning"
                },
                "else": {
                  "template": "ok"
                }
              }
            }
          },
          "detail": {
            "if": {
              "path": "pack_count",
              "equals": 0
            },
            "then": {
              "template": "No edge certificates"
            },
            "else": {
              "if": {
                "path": "expired_packs",
                "gt": 0
              },
              "then": {
                "template": "A certificate has expired"
              },
              "else": {
                "if": {
                  "value": {
                    "expires": {
                      "date": "soonest"
                    },
                    "as": "state",
                    "warning": 7,
                    "critical": 2,
                    "default": "ok"
                  },
                  "equals": "critical"
                },
                "then": {
                  "template": "A certificate expires {soonest|day} and hasn't renewed"
                },
                "else": {
                  "if": {
                    "path": "stalled",
                    "gt": 0
                  },
                  "then": {
                    "path": "first_stalled",
                    "values": {
                      "expired": {
                        "template": "A certificate has expired"
                      },
                      "validation_timed_out": {
                        "template": "A certificate couldn't be validated"
                      },
                      "issuance_timed_out": {
                        "template": "A certificate couldn't be issued"
                      },
                      "deployment_timed_out": {
                        "template": "A certificate couldn't be deployed"
                      },
                      "pending_validation": {
                        "template": "A certificate is waiting for validation"
                      }
                    }
                  },
                  "else": {
                    "if": {
                      "value": {
                        "expires": {
                          "date": "soonest"
                        },
                        "as": "state",
                        "warning": 7,
                        "critical": 2,
                        "default": "ok"
                      },
                      "equals": "warning"
                    },
                    "then": {
                      "template": "A certificate expires {soonest|day} and hasn't renewed"
                    },
                    "else": {
                      "if": {
                        "path": "packs_cut",
                        "equals": "yes"
                      },
                      "then": {
                        "template": "More than 50 certificate packs; the first 50 were read"
                      },
                      "else": {
                        "if": {
                          "path": "pack_count",
                          "equals": 1
                        },
                        "then": {
                          "template": "1 certificate pack · next renews by {soonest|day}"
                        },
                        "else": {
                          "template": "{pack_count} certificate packs · next renews by {soonest|day}"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    {
      "id": "controls",
      "type": "text",
      "label": "Controls",
      "actions_placement": "corner",
      "source": {
        "ref": "settings"
      },
      "actions": [
        {
          "label": "Security level",
          "icon": "shield.lefthalf.filled",
          "method": "PATCH",
          "path": "/client/v4/zones/{param.zone}/settings/security_level",
          "body": {
            "value": "{choice}"
          },
          "current": "level.result.value",
          "error": "errors[0].message",
          "choices": [
            {
              "label": "Essentially off",
              "value": "essentially_off"
            },
            {
              "label": "Low",
              "value": "low"
            },
            {
              "label": "Medium",
              "value": "medium"
            },
            {
              "label": "High",
              "value": "high"
            },
            {
              "label": "I'm Under Attack",
              "value": "under_attack"
            }
          ]
        },
        "|",
        {
          "label": "Development mode on",
          "icon": "hammer",
          "method": "PATCH",
          "path": "/client/v4/zones/{param.zone}/settings/development_mode",
          "body": {
            "value": "on"
          },
          "error": "errors[0].message",
          "when": {
            "path": "dev.result.value",
            "equals": "off"
          }
        },
        {
          "label": "Development mode off",
          "icon": "stop.fill",
          "method": "PATCH",
          "path": "/client/v4/zones/{param.zone}/settings/development_mode",
          "body": {
            "value": "off"
          },
          "error": "errors[0].message",
          "when": {
            "path": "dev.result.value",
            "equals": "on"
          }
        },
        "|",
        {
          "label": "Purge cache",
          "icon": "trash",
          "method": "POST",
          "path": "/client/v4/zones/{param.zone}/purge_cache",
          "body": {
            "purge_everything": true
          },
          "error": "errors[0].message",
          "confirm": {
            "message": "Remove every file Cloudflare has cached for this zone? The next visits go to your server.",
            "button": "Purge"
          }
        }
      ]
    },
    {
      "id": "per-day",
      "type": "sparkline",
      "label": "Requests a day",
      "points": [],
      "change": false,
      "source": {
        "ref": "zone",
        "when": {
          "path": "day_count",
          "gte": 2
        },
        "map": {
          "points": {
            "each": "days",
            "value": "n"
          },
          "labels": {
            "each": "days",
            "template": "{day|date}"
          }
        }
      }
    },
    {
      "id": "facts",
      "type": "facts",
      "label": "Last 30 days",
      "items": [
        {
          "key": "Requests",
          "value": "—"
        },
        {
          "key": "Cached",
          "value": "—"
        },
        {
          "key": "Data served",
          "value": "—"
        },
        {
          "key": "Status",
          "value": "—"
        },
        {
          "key": "Plan",
          "value": "—"
        }
      ],
      "source": {
        "ref": "zone",
        "map": {
          "items[0].value": {
            "template": "{month_requests|round:0}"
          },
          "items[1].value": {
            "if": {
              "path": "month_requests",
              "gt": 0
            },
            "then": {
              "template": "{month_cached_share|percent}"
            },
            "else": {
              "template": "None"
            }
          },
          "items[2].value": {
            "template": "{month_bytes|bytes}"
          },
          "items[3].value": {
            "if": {
              "path": "info.result.status",
              "in": [
                "pending",
                "initializing",
                "moved"
              ]
            },
            "then": {
              "path": "info.result.status",
              "values": {
                "pending": {
                  "template": "Waiting for nameservers"
                },
                "initializing": {
                  "template": "Being set up"
                },
                "moved": {
                  "template": "Nameservers moved away"
                },
                "active": {
                  "template": "Active"
                }
              }
            },
            "else": {
              "if": {
                "path": "info.result.paused",
                "equals": true
              },
              "then": {
                "template": "Paused · DNS only"
              },
              "else": {
                "template": "Active"
              }
            }
          },
          "items[4].value": {
            "path": "info.result.plan.name",
            "default": "Not shown"
          }
        }
      }
    },
    {
      "id": "codes",
      "type": "breakdown",
      "label": "Responses · 24h",
      "items": [
        {
          "label": "2xx",
          "color": "green",
          "value": 0
        },
        {
          "label": "3xx",
          "color": "blue",
          "value": 0
        },
        {
          "label": "4xx",
          "color": "orange",
          "value": 0
        },
        {
          "label": "5xx",
          "color": "red",
          "value": 0
        }
      ],
      "source": {
        "ref": "zone",
        "when": {
          "path": "day_requests",
          "gt": 0
        },
        "map": {
          "items[0].value": {
            "path": "c2"
          },
          "items[1].value": {
            "path": "c3"
          },
          "items[2].value": {
            "path": "c4"
          },
          "items[3].value": {
            "path": "c5"
          }
        }
      }
    },
    {
      "id": "countries",
      "type": "bars",
      "label": "Top countries · 24h",
      "items": [],
      "source": {
        "ref": "zone",
        "when": {
          "path": "day_requests",
          "gt": 0
        },
        "map": {
          "items": {
            "each": "gql.data.viewer.zones[0].countries",
            "fields": {
              "label": {
                "path": "dimensions.clientCountryName",
                "default": "Unknown"
              },
              "value": "count"
            }
          }
        }
      }
    }
  ]
}
