{
  "$schema": "./.schemas/page.schema.json",
  "title": "Healthchecks",
  "icon": "waveform.path.ecg",
  "sources": {
    "hc": {
      "collect": {
        "dataset": "healthchecks-samples",
        "each": "rows",
        "key": "{id}",
        "sample": {
          "every": "5m"
        },
        "fields": {
          "state": "state"
        },
        "readers": [
          "healthchecks-check",
          "healthchecks-check-ro"
        ]
      },
      "url": "https://healthchecks.io",
      "secret": "healthchecks",
      "every": "1m",
      "auth": {
        "type": "header",
        "name": "X-Api-Key"
      },
      "requests": {
        "checks": "/api/v3/checks/"
      },
      "derive": [
        {
          "name": "all",
          "value": {
            "each": "checks.checks",
            "fields": {
              "id": {
                "if": {
                  "path": "uuid",
                  "exists": true
                },
                "then": "uuid",
                "else": "unique_key"
              },
              "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",
              "last_ping": "last_ping",
              "next_ping": "next_ping",
              "rank": {
                "path": "status",
                "values": {
                  "down": 0,
                  "grace": 1
                },
                "default": 2
              },
              "link": {
                "if": {
                  "path": "uuid",
                  "exists": true
                },
                "then": {
                  "template": "legato://page/healthchecks-check?id={uuid}"
                },
                "else": {
                  "template": "legato://page/healthchecks-check-ro?id={unique_key}"
                }
              }
            }
          }
        },
        {
          "name": "rows",
          "value": {
            "each": "all",
            "sort": "name",
            "fields": {
              "id": "id",
              "name": "name",
              "state": "state",
              "detail": "detail",
              "status": "status",
              "started": "started",
              "last_ping": "last_ping",
              "next_ping": "next_ping",
              "rank": "rank",
              "link": "link"
            }
          }
        },
        {
          "name": "total",
          "value": {
            "count": "rows"
          }
        },
        {
          "name": "picked",
          "value": {
            "each": "rows",
            "sort": {
              "by": [
                "rank",
                "name"
              ]
            },
            "limit": 60,
            "fields": {
              "id": "id",
              "name": "name",
              "state": "state",
              "detail": "detail",
              "status": "status",
              "started": "started",
              "last_ping": "last_ping",
              "next_ping": "next_ping",
              "rank": "rank",
              "link": "link"
            }
          }
        },
        {
          "name": "shown",
          "value": {
            "each": "picked",
            "sort": "name",
            "fields": {
              "id": "id",
              "name": "name",
              "state": "state",
              "detail": "detail",
              "status": "status",
              "started": "started",
              "last_ping": "last_ping",
              "next_ping": "next_ping",
              "rank": "rank",
              "link": "link"
            }
          }
        },
        {
          "name": "due_name",
          "value": {
            "first": "name",
            "of": "rows",
            "where": {
              "all": [
                {
                  "path": "status",
                  "equals": "up"
                },
                {
                  "path": "started",
                  "equals": false
                },
                {
                  "path": "next_ping",
                  "exists": true
                }
              ]
            },
            "sort": "next_ping"
          }
        },
        {
          "name": "due_at",
          "value": {
            "first": "next_ping",
            "of": "rows",
            "where": {
              "all": [
                {
                  "path": "status",
                  "equals": "up"
                },
                {
                  "path": "started",
                  "equals": false
                },
                {
                  "path": "next_ping",
                  "exists": true
                }
              ]
            },
            "sort": "next_ping"
          }
        },
        {
          "name": "running",
          "value": {
            "count": "rows",
            "where": {
              "path": "started",
              "equals": true
            }
          }
        },
        {
          "name": "running_name",
          "value": {
            "first": "name",
            "of": "rows",
            "where": {
              "path": "started",
              "equals": true
            },
            "sort": "name"
          }
        },
        {
          "name": "running_more",
          "value": {
            "subtract": [
              "running",
              1
            ]
          }
        },
        {
          "name": "last_name",
          "value": {
            "first": "name",
            "of": "rows",
            "where": {
              "path": "last_ping",
              "exists": true
            },
            "sort": {
              "by": "last_ping",
              "order": "desc"
            }
          }
        },
        {
          "name": "last_at",
          "value": {
            "first": "last_ping",
            "of": "rows",
            "where": {
              "path": "last_ping",
              "exists": true
            },
            "sort": {
              "by": "last_ping",
              "order": "desc"
            }
          }
        },
        {
          "name": "paused",
          "value": {
            "count": "rows",
            "where": {
              "path": "status",
              "equals": "paused"
            }
          }
        },
        {
          "name": "paused_name",
          "value": {
            "first": "name",
            "of": "rows",
            "where": {
              "path": "status",
              "equals": "paused"
            },
            "sort": "name"
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "checks",
      "type": "fleet",
      "label": "Checks",
      "max_items": 40,
      "items": [],
      "source": {
        "ref": "hc",
        "empty": {
          "text": "This project has no checks yet.",
          "when": {
            "path": "total",
            "equals": 0
          }
        },
        "failed": {
          "state": "critical",
          "detail": "Healthchecks isn't answering",
          "after": 3
        },
        "map": {
          "items": {
            "each": "shown",
            "fields": {
              "name": {
                "template": "{name|clip:24}"
              },
              "state": "state",
              "detail": {
                "template": "{detail|clip:80}"
              },
              "url": "link"
            }
          }
        }
      }
    },
    {
      "id": "project",
      "type": "facts",
      "label": "Project",
      "items": [
        {
          "key": "Next due",
          "value": "—"
        },
        {
          "key": "Running",
          "value": "—"
        },
        {
          "key": "Last ping",
          "value": "—"
        },
        {
          "key": "Paused",
          "value": "—"
        }
      ],
      "source": {
        "ref": "hc",
        "map": {
          "items[0].value": {
            "if": {
              "path": "due_at",
              "exists": true
            },
            "then": {
              "template": "{due_name|clip:40} · {due_at|in}"
            },
            "else": {
              "template": "—"
            }
          },
          "items[1].value": {
            "if": {
              "path": "running",
              "equals": 0
            },
            "then": {
              "template": "None"
            },
            "else": {
              "if": {
                "path": "running",
                "equals": 1
              },
              "then": {
                "template": "{running_name|clip:48}"
              },
              "else": {
                "template": "{running_name|clip:40} and {running_more} more"
              }
            }
          },
          "items[2].value": {
            "if": {
              "path": "last_at",
              "exists": true
            },
            "then": {
              "template": "{last_name|clip:40} · {last_at|ago}"
            },
            "else": {
              "template": "None yet"
            }
          },
          "items[3].value": {
            "if": {
              "path": "paused",
              "equals": 0
            },
            "then": {
              "template": "None"
            },
            "else": {
              "if": {
                "path": "paused",
                "equals": 1
              },
              "then": {
                "template": "{paused_name|clip:48}"
              },
              "else": {
                "template": "{paused}"
              }
            }
          }
        }
      }
    }
  ]
}
