{
  "$schema": "./.schemas/page.schema.json",
  "title": "Check",
  "icon": "waveform.path.ecg",
  "sidebar": false,
  "params": {
    "id": {
      "example": "0b7d4e3a-5c21-4f8e-9a6b-1d2c3e4f5a6b",
      "pattern": "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
    }
  },
  "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": "uuid",
              "equals": "{param.id}"
            }
          }
        },
        {
          "name": "c",
          "value": {
            "each": "checks.checks",
            "where": {
              "path": "uuid",
              "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"
          }
        }
      ]
    },
    "log": {
      "url": "https://healthchecks.io",
      "secret": "healthchecks",
      "every": "1m",
      "auth": {
        "type": "header",
        "name": "X-Api-Key"
      },
      "requests": {
        "pings": "/api/v3/checks/{param.id}/pings/"
      },
      "derive": [
        {
          "name": "runs",
          "value": {
            "each": "pings.pings",
            "where": {
              "all": [
                {
                  "path": "type",
                  "in": [
                    "success",
                    "fail"
                  ]
                },
                {
                  "path": "duration",
                  "exists": true
                }
              ]
            },
            "limit": 30,
            "fields": {
              "date": "date",
              "s": {
                "path": "duration",
                "round": 1
              },
              "min": {
                "path": "duration",
                "scale": 0.016666666666666666,
                "round": 1
              }
            }
          }
        },
        {
          "name": "runs_asc",
          "value": {
            "each": "runs",
            "sort": "date",
            "fields": {
              "date": "date",
              "s": "s",
              "min": "min"
            }
          }
        },
        {
          "name": "longest",
          "value": {
            "max": "s",
            "of": "runs"
          }
        },
        {
          "name": "long",
          "value": {
            "if": {
              "path": "longest",
              "gte": 120
            },
            "then": {
              "template": "yes"
            },
            "else": {
              "template": "no"
            }
          }
        },
        {
          "name": "n_runs",
          "value": {
            "count": "runs"
          }
        },
        {
          "name": "n_pings",
          "value": {
            "count": "pings.pings"
          }
        }
      ]
    }
  },
  "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}/checks/{param.id}/details/"
            }
          }
        }
      },
      "actions": [
        {
          "label": "Pause monitoring",
          "icon": "pause.fill",
          "method": "POST",
          "path": "/api/v3/checks/{param.id}/pause",
          "body": {},
          "when": {
            "all": [
              {
                "path": "found",
                "equals": 1
              },
              {
                "path": "c[0].status",
                "not": "paused"
              }
            ]
          }
        },
        {
          "label": "Resume monitoring",
          "icon": "play.fill",
          "method": "POST",
          "path": "/api/v3/checks/{param.id}/resume",
          "body": {},
          "when": {
            "path": "c[0].status",
            "equals": "paused"
          }
        }
      ],
      "actions_placement": "corner"
    },
    {
      "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": "runtime",
      "type": "sparkline",
      "label": "Run time",
      "unit": "s",
      "change": false,
      "points": [],
      "source": {
        "ref": "log",
        "when": {
          "path": "n_runs",
          "gte": 2
        },
        "map": {
          "points": {
            "if": {
              "path": "long",
              "equals": "yes"
            },
            "then": {
              "each": "runs_asc",
              "value": "min"
            },
            "else": {
              "each": "runs_asc",
              "value": "s"
            }
          },
          "times": {
            "each": "runs_asc",
            "value": "date"
          },
          "unit": {
            "if": {
              "path": "long",
              "equals": "yes"
            },
            "then": {
              "template": "min"
            },
            "else": {
              "template": "s"
            }
          }
        }
      }
    },
    {
      "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": "pings",
      "type": "list",
      "label": "Recent pings",
      "max_items": 4,
      "items": [],
      "source": {
        "ref": "log",
        "empty": {
          "text": "No pings yet.",
          "when": {
            "path": "n_pings",
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "pings.pings",
            "limit": 4,
            "fields": {
              "key": {
                "template": "{n|raw}"
              },
              "icon": {
                "path": "type",
                "values": {
                  "success": "checkmark.circle",
                  "fail": "xmark.octagon.fill",
                  "start": "play.fill"
                },
                "default": "info.circle"
              },
              "color": {
                "if": {
                  "path": "type",
                  "equals": "fail"
                },
                "then": {
                  "template": "red"
                }
              },
              "text": {
                "if": {
                  "path": "duration",
                  "exists": true
                },
                "then": {
                  "path": "type",
                  "values": {
                    "success": {
                      "template": "Success · {duration|duration}"
                    },
                    "fail": {
                      "template": "Failed · {duration|duration}"
                    }
                  },
                  "default": {
                    "template": "{type|title}"
                  }
                },
                "else": {
                  "path": "type",
                  "values": {
                    "success": "Success",
                    "fail": "Failed",
                    "start": "Started",
                    "log": "Logged",
                    "ign": "Ignored"
                  },
                  "default": {
                    "template": "{type|title}"
                  }
                }
              },
              "detail": {
                "template": "{date|ago}"
              }
            }
          }
        }
      }
    },
    {
      "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}"
              }
            }
          }
        }
      }
    }
  ]
}
