{
  "$schema": "./.schemas/page.schema.json",
  "title": "Beszel",
  "icon": "server.rack",
  "sources": {
    "hub": {
      "url": "http://beszel.local:8090",
      "secret": "beszel",
      "every": "1m",
      "auth": {
        "type": "login",
        "request": {
          "method": "POST",
          "path": "/api/collections/users/auth-with-password",
          "body": {
            "identity": "legato-reader@example.net",
            "password": "{secret}"
          }
        },
        "token": "token",
        "send": {
          "header": "Authorization"
        }
      },
      "requests": {
        "systems": "/api/collections/systems/records?perPage=1000&sort=status,name&fields=id,name,status,updated,info.cpu,info.mp,info.dp",
        "firing": "/api/collections/alerts_history/records?perPage=1000&sort=-created&filter=%28resolved%3D%22%22%29&fields=id,name,value,created,system",
        "recent": "/api/collections/alerts_history/records?perPage=4&sort=-created&fields=id,name,value,created,resolved,system",
        "week": "/api/collections/alerts_history/records?perPage=1&fields=id&filter=%28created%3E%3D%22{date.days_ago_6}%22%29",
        "hub": "/api/beszel/info"
      },
      "derive": [
        {
          "name": "firing_rows",
          "value": {
            "each": "firing.items",
            "lookup": {
              "from": "systems.items",
              "by": "id",
              "on": "system",
              "as": "sys"
            },
            "fields": {
              "id": "id",
              "name": "name",
              "system": "system",
              "sys_name": "sys.name",
              "words": {
                "path": "name",
                "values": {
                  "Status": {
                    "template": "Down"
                  },
                  "CPU": {
                    "template": "CPU above {value}%"
                  },
                  "Memory": {
                    "template": "Memory above {value}%"
                  },
                  "Disk": {
                    "template": "Disk above {value}%"
                  },
                  "GPU": {
                    "template": "GPU above {value}%"
                  },
                  "Temperature": {
                    "template": "Temperature above {value} °C"
                  },
                  "Bandwidth": {
                    "template": "Network above {value} MB/s"
                  },
                  "LoadAvg1": {
                    "template": "Load (1 min) above {value}"
                  },
                  "LoadAvg5": {
                    "template": "Load (5 min) above {value}"
                  },
                  "LoadAvg15": {
                    "template": "Load (15 min) above {value}"
                  },
                  "Battery": {
                    "template": "Battery below {value}%"
                  },
                  "ContainerHealth": {
                    "template": "A container is unhealthy"
                  },
                  "SystemdFailed": {
                    "template": "A service failed"
                  }
                },
                "default": {
                  "template": "{name} alert"
                }
              },
              "created": "created",
              "resolved": "resolved"
            }
          }
        },
        {
          "name": "recent_rows",
          "value": {
            "each": "recent.items",
            "lookup": {
              "from": "systems.items",
              "by": "id",
              "on": "system",
              "as": "sys"
            },
            "fields": {
              "id": "id",
              "name": "name",
              "system": "system",
              "sys_name": "sys.name",
              "words": {
                "path": "name",
                "values": {
                  "Status": {
                    "template": "Down"
                  },
                  "CPU": {
                    "template": "CPU above {value}%"
                  },
                  "Memory": {
                    "template": "Memory above {value}%"
                  },
                  "Disk": {
                    "template": "Disk above {value}%"
                  },
                  "GPU": {
                    "template": "GPU above {value}%"
                  },
                  "Temperature": {
                    "template": "Temperature above {value} °C"
                  },
                  "Bandwidth": {
                    "template": "Network above {value} MB/s"
                  },
                  "LoadAvg1": {
                    "template": "Load (1 min) above {value}"
                  },
                  "LoadAvg5": {
                    "template": "Load (5 min) above {value}"
                  },
                  "LoadAvg15": {
                    "template": "Load (15 min) above {value}"
                  },
                  "Battery": {
                    "template": "Battery below {value}%"
                  },
                  "ContainerHealth": {
                    "template": "A container is unhealthy"
                  },
                  "SystemdFailed": {
                    "template": "A service failed"
                  }
                },
                "default": {
                  "template": "{name} alert"
                }
              },
              "created": "created",
              "resolved": "resolved"
            }
          }
        },
        {
          "name": "metric_alerts",
          "value": {
            "each": "firing_rows",
            "where": {
              "path": "name",
              "not": "Status"
            },
            "fields": {
              "system": "system",
              "words": "words"
            }
          }
        },
        {
          "name": "rows",
          "value": {
            "each": "systems.items",
            "lookup": {
              "from": "metric_alerts",
              "by": "system",
              "on": "id",
              "as": "alert"
            },
            "fields": {
              "name": "name",
              "state": {
                "if": {
                  "path": "status",
                  "equals": "paused"
                },
                "then": {
                  "template": "off"
                },
                "else": {
                  "if": {
                    "path": "status",
                    "equals": "down"
                  },
                  "then": {
                    "template": "critical"
                  },
                  "else": {
                    "if": {
                      "path": "status",
                      "equals": "up"
                    },
                    "then": {
                      "if": {
                        "path": "alert.words",
                        "exists": true
                      },
                      "then": {
                        "template": "warning"
                      },
                      "else": {
                        "template": "ok"
                      }
                    },
                    "else": {
                      "template": "unknown"
                    }
                  }
                }
              },
              "detail": {
                "if": {
                  "path": "status",
                  "equals": "paused"
                },
                "then": {
                  "template": "Paused"
                },
                "else": {
                  "if": {
                    "path": "status",
                    "equals": "down"
                  },
                  "then": {
                    "template": "Down since {updated|ago}"
                  },
                  "else": {
                    "if": {
                      "path": "status",
                      "equals": "pending"
                    },
                    "then": {
                      "template": "Waiting for its agent"
                    },
                    "else": {
                      "if": {
                        "path": "alert.words",
                        "exists": true
                      },
                      "then": {
                        "template": "{alert.words} · CPU {info.cpu|round:0}% · memory {info.mp|round:0}%"
                      },
                      "else": {
                        "template": "CPU {info.cpu|round:0}% · memory {info.mp|round:0}% · disk {info.dp|round:0}%"
                      }
                    }
                  }
                }
              },
              "rank": {
                "path": "status",
                "values": {
                  "down": 0,
                  "pending": 2,
                  "paused": 4
                },
                "default": 3
              },
              "link": {
                "template": "legato://page/beszel-system?id={id|raw}"
              }
            }
          }
        },
        {
          "name": "ranked",
          "value": {
            "each": "rows",
            "fields": {
              "name": "name",
              "state": "state",
              "detail": "detail",
              "link": "link",
              "rank": {
                "if": {
                  "path": "state",
                  "equals": "warning"
                },
                "then": {
                  "template": "1"
                },
                "else": {
                  "template": "{rank}"
                }
              }
            }
          }
        },
        {
          "name": "picked",
          "value": {
            "each": "ranked",
            "sort": {
              "by": [
                "rank",
                "name"
              ]
            },
            "limit": 60,
            "fields": {
              "name": "name",
              "state": "state",
              "detail": "detail",
              "link": "link"
            }
          }
        },
        {
          "name": "shown",
          "value": {
            "each": "picked",
            "sort": "name",
            "fields": {
              "name": "name",
              "state": "state",
              "detail": "detail",
              "link": "link"
            }
          }
        },
        {
          "name": "total",
          "value": {
            "path": "systems.totalItems",
            "default": 0
          }
        },
        {
          "name": "up",
          "value": {
            "count": "systems.items",
            "where": {
              "path": "status",
              "equals": "up"
            },
            "default": 0
          }
        },
        {
          "name": "groups",
          "value": {
            "each": "systems.items",
            "where": {
              "path": "status",
              "not": "up"
            },
            "group": "status",
            "fields": {
              "t": {
                "template": "{count} {key}"
              }
            }
          }
        },
        {
          "name": "others",
          "value": {
            "join": "t",
            "of": "groups",
            "separator": " · "
          }
        },
        {
          "name": "fullest",
          "value": {
            "first": "name",
            "of": "systems.items",
            "where": {
              "path": "status",
              "equals": "up"
            },
            "sort": {
              "by": "info.dp",
              "order": "desc"
            }
          }
        },
        {
          "name": "fullest_pct",
          "value": {
            "first": "info.dp",
            "of": "systems.items",
            "where": {
              "path": "status",
              "equals": "up"
            },
            "sort": {
              "by": "info.dp",
              "order": "desc"
            }
          }
        },
        {
          "name": "n_firing",
          "value": {
            "path": "firing.totalItems",
            "default": 0
          }
        },
        {
          "name": "cut",
          "value": {
            "if": {
              "path": "systems.totalItems",
              "gt": 60
            },
            "then": {
              "template": "yes"
            },
            "else": {
              "template": "no"
            }
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "systems",
      "type": "fleet",
      "label": "Systems",
      "max_items": 40,
      "items": [],
      "source": {
        "ref": "hub",
        "empty": {
          "text": "This Beszel user can't see any systems yet.",
          "when": {
            "path": "total",
            "equals": 0
          }
        },
        "failed": {
          "state": "critical",
          "detail": "Beszel 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": "busiest",
      "type": "table",
      "label": "Busiest",
      "columns": [
        "System",
        "CPU %",
        "Memory %",
        "Disk %"
      ],
      "rows": [
        [
          "—",
          "—",
          "—",
          "—"
        ]
      ],
      "source": {
        "ref": "hub",
        "when": {
          "path": "up",
          "gt": 0
        },
        "map": {
          "rows": {
            "each": "systems.items",
            "where": {
              "path": "status",
              "equals": "up"
            },
            "sort": {
              "by": "info.cpu",
              "order": "desc"
            },
            "limit": 5,
            "cells": [
              {
                "template": "{name|clip:24}"
              },
              {
                "path": "info.cpu",
                "round": 0
              },
              {
                "path": "info.mp",
                "round": 0
              },
              {
                "path": "info.dp",
                "round": 0
              }
            ]
          }
        }
      }
    },
    {
      "id": "hub",
      "type": "facts",
      "label": "Hub",
      "items": [
        {
          "key": "Systems",
          "value": "—"
        },
        {
          "key": "Fullest disk",
          "value": "—"
        },
        {
          "key": "Beszel",
          "value": "—"
        }
      ],
      "source": {
        "ref": "hub",
        "map": {
          "items[0].value": {
            "if": {
              "all": [
                {
                  "path": "others",
                  "exists": true
                },
                {
                  "path": "others",
                  "not": ""
                }
              ]
            },
            "then": {
              "template": "{total} · {others}"
            },
            "else": {
              "template": "{total}"
            }
          },
          "items[1].value": {
            "if": {
              "path": "fullest",
              "exists": true
            },
            "then": {
              "template": "{fullest|clip:32} · {fullest_pct|round:0}%"
            },
            "else": {
              "template": "—"
            }
          },
          "items[2].value": {
            "template": "{hub.v}"
          }
        }
      }
    },
    {
      "id": "more",
      "type": "text",
      "tone": "info",
      "body": "Showing 60 systems. Beszel has the rest.",
      "source": {
        "ref": "hub",
        "when": {
          "path": "cut",
          "equals": "yes"
        },
        "map": {
          "body": {
            "template": "Showing 60 of {systems.totalItems} systems, those that need you first. Beszel has the rest."
          }
        }
      }
    },
    {
      "id": "firing",
      "type": "list",
      "label": "Alerts firing",
      "max_items": 7,
      "items": [],
      "source": {
        "ref": "hub",
        "when": {
          "path": "n_firing",
          "gt": 0
        },
        "map": {
          "label": {
            "if": {
              "path": "n_firing",
              "gt": 60
            },
            "then": {
              "template": "Alerts firing, newest 60 of {n_firing}"
            },
            "else": {
              "template": "Alerts firing"
            }
          },
          "items": {
            "each": "firing_rows",
            "limit": 60,
            "fields": {
              "key": {
                "template": "{id|raw}"
              },
              "icon": {
                "if": {
                  "path": "name",
                  "equals": "Status"
                },
                "then": {
                  "template": "xmark.octagon.fill"
                },
                "else": {
                  "template": "exclamationmark.triangle.fill"
                }
              },
              "color": {
                "if": {
                  "path": "name",
                  "equals": "Status"
                },
                "then": {
                  "template": "red"
                },
                "else": {
                  "template": "orange"
                }
              },
              "text": {
                "if": {
                  "path": "sys_name",
                  "exists": true
                },
                "then": {
                  "template": "{sys_name|clip:24} · {words}"
                },
                "else": {
                  "template": "{words}"
                }
              },
              "url": {
                "if": {
                  "path": "sys_name",
                  "exists": true
                },
                "then": {
                  "template": "legato://page/beszel-system?id={system|raw}"
                }
              },
              "detail": {
                "template": "{created|ago}"
              }
            }
          }
        }
      }
    },
    {
      "id": "history",
      "type": "heading",
      "text": "Alert history",
      "collapsed": true,
      "source": {
        "ref": "hub",
        "map": {
          "detail": {
            "if": {
              "path": "week.totalItems",
              "equals": 1
            },
            "then": {
              "template": "1 in 7 days"
            },
            "else": {
              "template": "{week.totalItems} in 7 days"
            }
          }
        }
      },
      "cards": [
        "recent"
      ]
    },
    {
      "id": "recent",
      "type": "list",
      "label": "Recent",
      "max_items": 4,
      "items": [],
      "source": {
        "ref": "hub",
        "empty": {
          "text": "No alerts have fired yet.",
          "when": {
            "path": "recent.totalItems",
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "recent_rows",
            "fields": {
              "key": {
                "template": "{id|raw}"
              },
              "icon": {
                "if": {
                  "all": [
                    {
                      "path": "resolved",
                      "exists": true
                    },
                    {
                      "path": "resolved",
                      "not": ""
                    }
                  ]
                },
                "then": {
                  "template": "checkmark.circle"
                },
                "else": {
                  "if": {
                    "path": "name",
                    "equals": "Status"
                  },
                  "then": {
                    "template": "xmark.octagon.fill"
                  },
                  "else": {
                    "template": "exclamationmark.triangle.fill"
                  }
                }
              },
              "color": {
                "if": {
                  "path": "resolved",
                  "equals": ""
                },
                "then": {
                  "if": {
                    "path": "name",
                    "equals": "Status"
                  },
                  "then": {
                    "template": "red"
                  },
                  "else": {
                    "template": "orange"
                  }
                }
              },
              "text": {
                "if": {
                  "path": "sys_name",
                  "exists": true
                },
                "then": {
                  "template": "{sys_name|clip:24} · {words}"
                },
                "else": {
                  "template": "{words}"
                }
              },
              "url": {
                "if": {
                  "path": "sys_name",
                  "exists": true
                },
                "then": {
                  "template": "legato://page/beszel-system?id={system|raw}"
                }
              },
              "detail": {
                "if": {
                  "all": [
                    {
                      "path": "resolved",
                      "exists": true
                    },
                    {
                      "path": "resolved",
                      "not": ""
                    }
                  ]
                },
                "then": {
                  "template": "Cleared {resolved|ago}"
                },
                "else": {
                  "template": "Firing since {created|ago}"
                }
              }
            }
          }
        }
      }
    }
  ]
}
