{
  "$schema": "./.schemas/page.schema.json",
  "title": "Check",
  "icon": "waveform.path.ecg",
  "sidebar": false,
  "params": {
    "id": {
      "example": "4f1c2a9e8b7d6c5b4a3f2e1d0c9b8a7f6e5d4c3b",
      "pattern": "[0-9a-f]{40}"
    }
  },
  "title_from": {
    "ref": "check",
    "value": {
      "first": "name",
      "of": "c"
    }
  },
  "sources": {
    "samples": {
      "dataset": "healthchecks-samples",
      "key": "{param.id}",
      "derive": [
        {
          "name": "up",
          "value": {
            "multiply": [
              {
                "divide": [
                  {
                    "count": "kept.rows",
                    "where": {
                      "path": "row.state",
                      "in": [
                        "ok",
                        "off"
                      ]
                    }
                  },
                  {
                    "count": "kept.rows"
                  }
                ]
              },
              100
            ],
            "round": 1
          }
        }
      ]
    },
    "check": {
      "url": "https://healthchecks.io",
      "secret": "healthchecks",
      "every": "1m",
      "auth": {
        "type": "header",
        "name": "X-Api-Key"
      },
      "requests": {
        "checks": "/api/v3/checks/"
      },
      "derive": [
        {
          "name": "found",
          "value": {
            "count": "checks.checks",
            "where": {
              "path": "unique_key",
              "equals": "{param.id}"
            }
          }
        },
        {
          "name": "c",
          "value": {
            "each": "checks.checks",
            "where": {
              "path": "unique_key",
              "equals": "{param.id}"
            },
            "limit": 1,
            "fields": {
              "name": {
                "if": {
                  "all": [
                    {
                      "path": "name",
                      "exists": true
                    },
                    {
                      "path": "name",
                      "not": ""
                    }
                  ]
                },
                "then": "name",
                "else": {
                  "if": {
                    "all": [
                      {
                        "path": "slug",
                        "exists": true
                      },
                      {
                        "path": "slug",
                        "not": ""
                      }
                    ]
                  },
                  "then": "slug",
                  "else": {
                    "template": "unnamed"
                  }
                }
              },
              "state": {
                "path": "status",
                "values": {
                  "up": "ok",
                  "grace": "warning",
                  "down": "critical",
                  "paused": "off",
                  "new": "off"
                },
                "default": "unknown"
              },
              "detail": {
                "if": {
                  "path": "status",
                  "equals": "down"
                },
                "then": {
                  "if": {
                    "path": "started",
                    "equals": true
                  },
                  "then": {
                    "template": "Down · still running past its grace time"
                  },
                  "else": {
                    "if": {
                      "path": "last_ping",
                      "exists": true
                    },
                    "then": {
                      "template": "Down · last ping {last_ping|ago}"
                    },
                    "else": {
                      "template": "Down"
                    }
                  }
                },
                "else": {
                  "if": {
                    "path": "status",
                    "equals": "grace"
                  },
                  "then": {
                    "template": "Late · due {next_ping|ago}"
                  },
                  "else": {
                    "if": {
                      "path": "status",
                      "equals": "paused"
                    },
                    "then": {
                      "template": "Paused"
                    },
                    "else": {
                      "if": {
                        "path": "started",
                        "equals": true
                      },
                      "then": {
                        "template": "Running"
                      },
                      "else": {
                        "if": {
                          "path": "status",
                          "equals": "new"
                        },
                        "then": {
                          "if": {
                            "path": "last_ping",
                            "exists": true
                          },
                          "then": {
                            "template": "Waiting for its next ping"
                          },
                          "else": {
                            "template": "Waiting for its first ping"
                          }
                        },
                        "else": {
                          "if": {
                            "path": "last_ping",
                            "exists": true
                          },
                          "then": {
                            "template": "Up · last ping {last_ping|ago}"
                          },
                          "else": {
                            "template": "Up"
                          }
                        }
                      }
                    }
                  }
                }
              },
              "status": "status",
              "started": "started",
              "kind": {
                "if": {
                  "path": "schedule",
                  "exists": true
                },
                "then": {
                  "template": "cron"
                },
                "else": {
                  "template": "simple"
                }
              },
              "timeout": "timeout",
              "schedule": "schedule",
              "tz": "tz",
              "grace": "grace",
              "last_duration": "last_duration",
              "n_pings": "n_pings",
              "tags": "tags",
              "desc": "desc",
              "manual_resume": "manual_resume",
              "next_ping": "next_ping",
              "site": {
                "template": "{badge_url|origin}"
              }
            }
          }
        }
      ]
    },
    "changes": {
      "url": "https://healthchecks.io",
      "secret": "healthchecks",
      "every": "1m",
      "auth": {
        "type": "header",
        "name": "X-Api-Key"
      },
      "requests": {
        "flips": "/api/v3/checks/{param.id}/flips/?seconds=7776000"
      },
      "derive": [
        {
          "name": "n_flips",
          "value": {
            "count": "flips.flips"
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "now",
      "type": "status",
      "width": "full",
      "label": "Now",
      "state": "unknown",
      "source": {
        "ref": "check",
        "empty": {
          "text": "Healthchecks no longer has this check.",
          "when": {
            "path": "found",
            "equals": 0
          }
        },
        "map": {
          "state": "c[0].state",
          "detail": {
            "if": {
              "all": [
                {
                  "path": "c[0].status",
                  "equals": "up"
                },
                {
                  "path": "c[0].started",
                  "equals": false
                },
                {
                  "path": "c[0].next_ping",
                  "exists": true
                }
              ]
            },
            "then": {
              "template": "{c[0].detail|clip:80} · next due {c[0].next_ping|in}"
            },
            "else": {
              "template": "{c[0].detail|clip:120}"
            }
          },
          "url": {
            "if": {
              "path": "c[0].site",
              "contains": "https://"
            },
            "then": {
              "template": "{c[0].site}"
            }
          }
        }
      }
    },
    {
      "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|time}"
              }
            }
          },
          "detail": {
            "template": "{up}% up"
          }
        }
      }
    },
    {
      "id": "details",
      "type": "facts",
      "label": "Details",
      "items": [
        {
          "key": "Schedule",
          "value": "—"
        },
        {
          "key": "Last run took",
          "value": "—"
        },
        {
          "key": "Grace time",
          "value": "—"
        },
        {
          "key": "About",
          "value": "—"
        },
        {
          "key": "Tags",
          "value": "—"
        },
        {
          "key": "Pings",
          "value": "—"
        }
      ],
      "source": {
        "ref": "check",
        "map": {
          "items[0].value": {
            "if": {
              "path": "c[0].kind",
              "equals": "cron"
            },
            "then": {
              "template": "{c[0].schedule} · {c[0].tz}"
            },
            "else": {
              "template": "Every {c[0].timeout|duration}"
            }
          },
          "items[1].value": {
            "if": {
              "path": "c[0].last_duration",
              "exists": true
            },
            "then": {
              "template": "{c[0].last_duration|duration}"
            },
            "else": {
              "template": "—"
            }
          },
          "items[2].value": {
            "template": "{c[0].grace|duration}"
          },
          "items[3].value": {
            "if": {
              "all": [
                {
                  "path": "c[0].desc",
                  "exists": true
                },
                {
                  "path": "c[0].desc",
                  "not": ""
                }
              ]
            },
            "then": {
              "template": "{c[0].desc|clip:80}"
            },
            "else": {
              "template": "—"
            }
          },
          "items[4].value": {
            "if": {
              "all": [
                {
                  "path": "c[0].tags",
                  "exists": true
                },
                {
                  "path": "c[0].tags",
                  "not": ""
                }
              ]
            },
            "then": {
              "template": "{c[0].tags|clip:80}"
            },
            "else": {
              "template": "None"
            }
          },
          "items[5].value": {
            "template": "{c[0].n_pings}"
          }
        }
      }
    },
    {
      "id": "flips",
      "type": "list",
      "label": "Status changes",
      "max_items": 4,
      "items": [],
      "source": {
        "ref": "changes",
        "empty": {
          "text": "No changes in the last 90 days.",
          "when": {
            "path": "n_flips",
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "flips.flips",
            "limit": 4,
            "fields": {
              "key": {
                "template": "{timestamp}"
              },
              "icon": {
                "if": {
                  "path": "up",
                  "equals": 1
                },
                "then": {
                  "template": "checkmark.circle"
                },
                "else": {
                  "template": "xmark.octagon.fill"
                }
              },
              "color": {
                "if": {
                  "path": "up",
                  "equals": 0
                },
                "then": {
                  "template": "red"
                }
              },
              "text": {
                "if": {
                  "path": "up",
                  "equals": 1
                },
                "then": {
                  "template": "Back up"
                },
                "else": {
                  "template": "Went down"
                }
              },
              "detail": {
                "template": "{timestamp|ago}"
              }
            }
          }
        }
      }
    }
  ]
}
