{
  "$schema": "./.schemas/page.schema.json",
  "title": "Gitea server",
  "icon": "si:gitea",
  "sources": {
    "server": {
      "url": "http://gitea.local:3000",
      "secret": "gitea-admin",
      "every": "1m",
      "requests": {
        "health": "/api/healthz",
        "version": "/api/v1/version",
        "runners": "/api/v1/admin/actions/runners?limit=50",
        "queued": "/api/v1/admin/actions/jobs?status=queued&sort=id&order=asc&limit=21",
        "running": "/api/v1/admin/actions/jobs?status=in_progress&limit=1"
      },
      "derive": [
        {
          "name": "parts",
          "value": {
            "each": "runners.runners",
            "fields": {
              "name": "name",
              "state": {
                "if": {
                  "path": "disabled",
                  "equals": true
                },
                "then": {
                  "template": "off"
                },
                "else": {
                  "path": "status",
                  "values": {
                    "online": "ok",
                    "idle": "ok",
                    "active": "ok",
                    "offline": "critical"
                  },
                  "default": "unknown"
                }
              },
              "detail": {
                "if": {
                  "path": "disabled",
                  "equals": true
                },
                "then": {
                  "template": "Disabled"
                },
                "else": {
                  "if": {
                    "path": "status",
                    "equals": "offline"
                  },
                  "then": {
                    "template": "Offline"
                  },
                  "else": {
                    "if": {
                      "path": "status",
                      "in": [
                        "online",
                        "idle",
                        "active"
                      ]
                    },
                    "then": {
                      "if": {
                        "path": "busy",
                        "equals": true
                      },
                      "then": {
                        "template": "Online · running a job"
                      },
                      "else": {
                        "template": "Online · idle"
                      }
                    },
                    "else": {
                      "template": "Gitea didn't say"
                    }
                  }
                }
              },
              "group": {
                "template": "Runners"
              },
              "url": {
                "template": "http://gitea.local:3000/-/admin/actions/runners/{id|raw}"
              },
              "key": {
                "template": "runner-{id|raw}"
              }
            }
          }
        },
        {
          "name": "listed",
          "value": {
            "count": "runners.runners"
          }
        },
        {
          "name": "server_rows",
          "value": {
            "each": "health",
            "entries": true,
            "where": {
              "path": "key",
              "equals": "status"
            },
            "fields": {
              "name": {
                "template": "Server"
              },
              "group": {
                "template": "Server"
              },
              "key": {
                "template": "server"
              },
              "state": {
                "path": "value",
                "values": {
                  "pass": "ok",
                  "warn": "warning",
                  "fail": "critical"
                },
                "default": "unknown"
              },
              "detail": {
                "path": "value",
                "values": {
                  "pass": "Database and cache answering",
                  "warn": "Gitea reports a warning",
                  "fail": "Gitea reports a failed check"
                },
                "default": "Gitea didn't say"
              }
            }
          }
        },
        {
          "name": "online",
          "value": {
            "count": "runners.runners",
            "where": {
              "all": [
                {
                  "path": "status",
                  "in": [
                    "online",
                    "idle",
                    "active"
                  ]
                },
                {
                  "any": [
                    {
                      "path": "disabled",
                      "exists": false
                    },
                    {
                      "path": "disabled",
                      "equals": false
                    }
                  ]
                }
              ]
            }
          }
        },
        {
          "name": "waiting",
          "value": {
            "each": "queued.jobs",
            "sort": "created_at",
            "fields": {
              "name": "name",
              "url": "html_url",
              "branch": "head_branch",
              "at": "created_at"
            }
          }
        },
        {
          "name": "waiting_read",
          "value": {
            "count": "queued.jobs"
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "parts",
      "type": "fleet",
      "placement": "page_info",
      "label": "Gitea",
      "max_items": 30,
      "items": [],
      "source": {
        "ref": "server",
        "map": {
          "items": {
            "each": [
              "server_rows",
              "parts"
            ],
            "fields": {
              "name": "name",
              "state": "state",
              "detail": "detail",
              "group": "group",
              "url": "url",
              "key": "key"
            }
          }
        },
        "failed": {
          "state": "critical",
          "detail": "Legato couldn't read the server and its runners",
          "after": 3
        }
      }
    },
    {
      "id": "actions",
      "type": "facts",
      "label": "Actions",
      "items": [
        {
          "key": "Jobs waiting",
          "value": "—"
        },
        {
          "key": "Runners",
          "value": "—"
        },
        {
          "key": "Running",
          "value": "—"
        },
        {
          "key": "Version",
          "value": "—"
        }
      ],
      "source": {
        "ref": "server",
        "map": {
          "items[0].value": {
            "if": {
              "value": {
                "path": "queued.total_count",
                "default": 0
              },
              "equals": 0
            },
            "then": {
              "template": "None"
            },
            "else": {
              "if": {
                "value": {
                  "path": "queued.total_count",
                  "default": 0
                },
                "gt": {
                  "path": "waiting_read"
                }
              },
              "then": {
                "template": "{queued.total_count}"
              },
              "else": {
                "template": "{queued.total_count} · oldest {waiting[0].at|ago}"
              }
            }
          },
          "items[1].value": {
            "if": {
              "value": {
                "path": "runners.total_count",
                "default": 0
              },
              "equals": 0
            },
            "then": {
              "template": "None registered"
            },
            "else": {
              "if": {
                "value": {
                  "path": "runners.total_count",
                  "default": 0
                },
                "gt": {
                  "path": "listed"
                }
              },
              "then": {
                "template": "{online} of the first {listed} online, of {runners.total_count}"
              },
              "else": {
                "template": "{online} of {runners.total_count} online"
              }
            }
          },
          "items[2].value": "running.total_count",
          "items[3].value": "version.version"
        }
      }
    },
    {
      "id": "queue",
      "type": "list",
      "label": "Waiting for a runner",
      "max_items": 4,
      "items": [],
      "source": {
        "ref": "server",
        "empty": {
          "text": "Nothing waiting.",
          "when": {
            "value": {
              "path": "queued.total_count",
              "default": 0
            },
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "waiting",
            "limit": 20,
            "fields": {
              "text": "name",
              "url": "url",
              "detail": {
                "template": "{branch} · {at|ago}"
              }
            }
          }
        }
      }
    }
  ]
}
