{
  "$schema": "./.schemas/page.schema.json",
  "title": "Guest",
  "icon": "desktopcomputer",
  "sidebar": false,
  "period": {
    "choices": [
      {
        "id": "hour",
        "label": "Hour",
        "last_days": 1
      },
      {
        "id": "day",
        "label": "Day",
        "last_days": 1
      },
      {
        "id": "week",
        "label": "Week",
        "last_days": 7
      },
      {
        "id": "month",
        "label": "Month",
        "last_days": 30
      }
    ]
  },
  "params": {
    "name": {
      "example": "guest",
      "pattern": "[A-Za-z0-9][A-Za-z0-9.-]{0,62}"
    },
    "vmid": {
      "example": "100",
      "type": "integer"
    }
  },
  "sources": {
    "backups": {
      "auth": {
        "name": "Authorization",
        "type": "header"
      },
      "derive": [
        {
          "name": "created",
          "value": {
            "default": 0,
            "match": "ctime=([0-9]+)",
            "path": "config.data.meta"
          }
        },
        {
          "name": "archives",
          "value": {
            "each": "data.data",
            "fields": {
              "ctime": "ctime",
              "gb": {
                "path": "size",
                "round": 1,
                "scale": 1e-09
              }
            },
            "where": {
              "gte": {
                "answer": "created"
              },
              "path": "ctime"
            }
          }
        },
        {
          "name": "archive_count",
          "value": {
            "count": "archives"
          }
        },
        {
          "name": "none_yet",
          "value": {
            "path": "archive_count",
            "values": {
              "0": [
                {
                  "text": "None since this guest was created"
                }
              ]
            }
          }
        }
      ],
      "every": "1m",
      "requests": {
        "config": "/api2/json/nodes/pve1/qemu/{param.vmid}/config",
        "data": "/api2/json/nodes/pve1/storage/backup-nas/content?content=backup&vmid={param.vmid}"
      },
      "secret": "proxmox",
      "url": "https://proxmox.example.com:8006"
    },
    "guest": {
      "auth": {
        "name": "Authorization",
        "type": "header"
      },
      "derive": [
        {
          "name": "mem_gb",
          "value": {
            "path": "status.data.mem",
            "round": 1,
            "scale": 1e-09
          }
        },
        {
          "name": "up_days",
          "value": {
            "divide": [
              "status.data.uptime",
              86400
            ],
            "round": 0
          }
        }
      ],
      "every": "1m",
      "requests": {
        "status": "/api2/json/nodes/pve1/qemu/{param.vmid}/status/current",
        "tasks": "/api2/json/nodes/pve1/tasks?vmid={param.vmid}&source=all&limit=12"
      },
      "secret": "proxmox",
      "url": "https://proxmox.example.com:8006"
    },
    "history": {
      "auth": {
        "name": "Authorization",
        "type": "header"
      },
      "derive": [
        {
          "name": "readings",
          "value": {
            "count": "data.data",
            "where": {
              "gte": 0,
              "path": "cpu"
            }
          }
        },
        {
          "name": "shown",
          "value": {
            "template": "{period.id}"
          }
        }
      ],
      "every": "1m",
      "requests": {
        "data": "/api2/json/nodes/pve1/qemu/{param.vmid}/rrddata?timeframe={period.id}&cf=AVERAGE"
      },
      "secret": "proxmox",
      "url": "https://proxmox.example.com:8006"
    },
    "samples": {
      "dataset": "proxmox-guest-samples",
      "key": "{param.vmid}",
      "derive": [
        {
          "name": "up",
          "value": {
            "multiply": [
              {
                "divide": [
                  {
                    "count": "kept.rows",
                    "where": {
                      "path": "row.state",
                      "in": [
                        "ok",
                        "off"
                      ]
                    }
                  },
                  {
                    "count": "kept.rows"
                  }
                ]
              },
              100
            ],
            "round": 1
          }
        }
      ]
    }
  },
  "cards": [
    {
      "actions": [
        {
          "icon": "terminal",
          "kind": "open",
          "label": "Open console",
          "url": "https://proxmox.example.com:8006/?console=kvm&novnc=1&vmid={param.vmid}&node=pve1&resize=off"
        },
        "|",
        {
          "completion": {
            "done": {
              "equals": "stopped",
              "path": "data.status"
            },
            "interval": 2,
            "path": "/api2/json/nodes/pve1/tasks/{data}/status",
            "success": {
              "equals": "OK",
              "path": "data.exitstatus"
            },
            "timeout": 300
          },
          "icon": "play.fill",
          "label": "Start",
          "path": "/api2/json/nodes/pve1/qemu/{param.vmid}/status/start",
          "when": {
            "equals": "stopped",
            "path": "status.data.status"
          }
        },
        {
          "completion": {
            "done": {
              "equals": "stopped",
              "path": "data.status"
            },
            "interval": 2,
            "path": "/api2/json/nodes/pve1/tasks/{data}/status",
            "success": {
              "equals": "OK",
              "path": "data.exitstatus"
            },
            "timeout": 300
          },
          "confirm": true,
          "icon": "stop.fill",
          "label": "Force stop",
          "path": "/api2/json/nodes/pve1/qemu/{param.vmid}/status/stop",
          "when": {
            "equals": "running",
            "path": "status.data.status"
          }
        },
        {
          "body": {
            "timeout": 120
          },
          "completion": {
            "done": {
              "equals": "stopped",
              "path": "data.status"
            },
            "interval": 2,
            "path": "/api2/json/nodes/pve1/tasks/{data}/status",
            "success": {
              "equals": "OK",
              "path": "data.exitstatus"
            },
            "timeout": 300
          },
          "confirm": true,
          "icon": "arrow.clockwise",
          "label": "Reboot",
          "path": "/api2/json/nodes/pve1/qemu/{param.vmid}/status/reboot",
          "when": {
            "all": [
              {
                "equals": "running",
                "path": "status.data.status"
              },
              {
                "equals": "running",
                "path": "status.data.qmpstatus"
              }
            ]
          }
        },
        {
          "body": {
            "timeout": 120
          },
          "completion": {
            "done": {
              "equals": "stopped",
              "path": "data.status"
            },
            "interval": 2,
            "path": "/api2/json/nodes/pve1/tasks/{data}/status",
            "success": {
              "equals": "OK",
              "path": "data.exitstatus"
            },
            "timeout": 300
          },
          "confirm": true,
          "icon": "power",
          "label": "Shut down",
          "path": "/api2/json/nodes/pve1/qemu/{param.vmid}/status/shutdown",
          "when": {
            "all": [
              {
                "equals": "running",
                "path": "status.data.status"
              },
              {
                "equals": "running",
                "path": "status.data.qmpstatus"
              }
            ]
          }
        },
        {
          "completion": {
            "done": {
              "equals": "stopped",
              "path": "data.status"
            },
            "interval": 2,
            "path": "/api2/json/nodes/pve1/tasks/{data}/status",
            "success": {
              "equals": "OK",
              "path": "data.exitstatus"
            },
            "timeout": 300
          },
          "icon": "playpause.fill",
          "label": "Resume",
          "path": "/api2/json/nodes/pve1/qemu/{param.vmid}/status/resume",
          "when": {
            "in": [
              "paused",
              "suspended"
            ],
            "path": "status.data.qmpstatus"
          }
        },
        "|",
        {
          "body": {
            "compress": "zstd",
            "mode": "snapshot",
            "storage": "backup-nas",
            "vmid": "{param.vmid}"
          },
          "completion": {
            "done": {
              "equals": "stopped",
              "path": "data.status"
            },
            "interval": 2,
            "path": "/api2/json/nodes/pve1/tasks/{data}/status",
            "success": {
              "equals": "OK",
              "path": "data.exitstatus"
            },
            "timeout": 3600
          },
          "confirm": true,
          "icon": "archivebox",
          "label": "Back up now",
          "path": "/api2/json/nodes/pve1/vzdump"
        }
      ],
      "actions_placement": "corner",
      "id": "controls",
      "label": "Controls",
      "source": {
        "ref": "guest"
      },
      "type": "text"
    },
    {
      "id": "running",
      "type": "strip",
      "label": "Running",
      "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}% running"
          }
        }
      }
    },
    {
      "change": false,
      "id": "cpu-history",
      "label": "CPU",
      "points": [
        0,
        0
      ],
      "source": {
        "empty": {
          "text": "No readings in this period",
          "when": {
            "lt": 2,
            "path": "readings"
          }
        },
        "map": {
          "axis_labels": {
            "else": {
              "else": {
                "else": {
                  "buckets": 240,
                  "each": "data.data",
                  "template": "{time|date}",
                  "where": {
                    "gte": 0,
                    "path": "cpu"
                  }
                },
                "if": {
                  "equals": "week",
                  "path": "shown"
                },
                "then": {
                  "buckets": 240,
                  "each": "data.data",
                  "template": "{time|weekday}",
                  "where": {
                    "gte": 0,
                    "path": "cpu"
                  }
                }
              },
              "if": {
                "equals": "day",
                "path": "shown"
              },
              "then": {
                "buckets": 240,
                "each": "data.data",
                "template": "{time|clock_hour}",
                "where": {
                  "gte": 0,
                  "path": "cpu"
                }
              }
            },
            "if": {
              "equals": "hour",
              "path": "shown"
            },
            "then": {
              "buckets": 240,
              "each": "data.data",
              "template": "{time|clock}",
              "where": {
                "gte": 0,
                "path": "cpu"
              }
            }
          },
          "labels": {
            "else": {
              "else": {
                "else": {
                  "buckets": 240,
                  "each": "data.data",
                  "template": "{time|date}, {time|clock_hour}",
                  "where": {
                    "gte": 0,
                    "path": "cpu"
                  }
                },
                "if": {
                  "equals": "week",
                  "path": "shown"
                },
                "then": {
                  "buckets": 240,
                  "each": "data.data",
                  "template": "{time|weekday} {time|clock_hour}",
                  "where": {
                    "gte": 0,
                    "path": "cpu"
                  }
                }
              },
              "if": {
                "equals": "day",
                "path": "shown"
              },
              "then": {
                "buckets": 240,
                "each": "data.data",
                "template": "{time|clock}",
                "where": {
                  "gte": 0,
                  "path": "cpu"
                }
              }
            },
            "if": {
              "equals": "hour",
              "path": "shown"
            },
            "then": {
              "buckets": 240,
              "each": "data.data",
              "template": "{time|clock}",
              "where": {
                "gte": 0,
                "path": "cpu"
              }
            }
          },
          "points": {
            "buckets": 240,
            "each": "data.data",
            "value": {
              "path": "cpu",
              "round": 1,
              "scale": 100
            },
            "where": {
              "gte": 0,
              "path": "cpu"
            }
          }
        },
        "ref": "history"
      },
      "type": "sparkline",
      "unit": "%",
      "width": "half"
    },
    {
      "change": false,
      "id": "memory-history",
      "label": "Memory",
      "points": [
        0,
        0
      ],
      "source": {
        "empty": {
          "text": "No readings in this period",
          "when": {
            "lt": 2,
            "path": "readings"
          }
        },
        "map": {
          "axis_labels": {
            "else": {
              "else": {
                "else": {
                  "buckets": 240,
                  "each": "data.data",
                  "template": "{time|date}",
                  "where": {
                    "gte": 0,
                    "path": "cpu"
                  }
                },
                "if": {
                  "equals": "week",
                  "path": "shown"
                },
                "then": {
                  "buckets": 240,
                  "each": "data.data",
                  "template": "{time|weekday}",
                  "where": {
                    "gte": 0,
                    "path": "cpu"
                  }
                }
              },
              "if": {
                "equals": "day",
                "path": "shown"
              },
              "then": {
                "buckets": 240,
                "each": "data.data",
                "template": "{time|clock_hour}",
                "where": {
                  "gte": 0,
                  "path": "cpu"
                }
              }
            },
            "if": {
              "equals": "hour",
              "path": "shown"
            },
            "then": {
              "buckets": 240,
              "each": "data.data",
              "template": "{time|clock}",
              "where": {
                "gte": 0,
                "path": "cpu"
              }
            }
          },
          "labels": {
            "else": {
              "else": {
                "else": {
                  "buckets": 240,
                  "each": "data.data",
                  "template": "{time|date}, {time|clock_hour}",
                  "where": {
                    "gte": 0,
                    "path": "cpu"
                  }
                },
                "if": {
                  "equals": "week",
                  "path": "shown"
                },
                "then": {
                  "buckets": 240,
                  "each": "data.data",
                  "template": "{time|weekday} {time|clock_hour}",
                  "where": {
                    "gte": 0,
                    "path": "cpu"
                  }
                }
              },
              "if": {
                "equals": "day",
                "path": "shown"
              },
              "then": {
                "buckets": 240,
                "each": "data.data",
                "template": "{time|clock}",
                "where": {
                  "gte": 0,
                  "path": "cpu"
                }
              }
            },
            "if": {
              "equals": "hour",
              "path": "shown"
            },
            "then": {
              "buckets": 240,
              "each": "data.data",
              "template": "{time|clock}",
              "where": {
                "gte": 0,
                "path": "cpu"
              }
            }
          },
          "max": {
            "max": "maxmem",
            "of": "data.data",
            "round": 1,
            "scale": 1e-09
          },
          "points": {
            "buckets": 240,
            "each": "data.data",
            "value": {
              "path": "mem",
              "round": 2,
              "scale": 1e-09
            },
            "where": {
              "gte": 0,
              "path": "cpu"
            }
          }
        },
        "ref": "history"
      },
      "type": "sparkline",
      "unit": "GB",
      "width": "half"
    },
    {
      "id": "state",
      "items": [
        {
          "key": "State",
          "value": "—"
        },
        {
          "key": "VM",
          "value": "—"
        },
        {
          "key": "Memory on host",
          "value": "—"
        },
        {
          "key": "Up",
          "value": "—"
        },
        {
          "key": "Guest runtime",
          "value": "—"
        }
      ],
      "label": "Stats",
      "source": {
        "map": {
          "items[0].value": "status.data.status",
          "items[1].value": {
            "template": "{param.vmid}"
          },
          "items[2].value": {
            "template": "{mem_gb} GB"
          },
          "items[3].value": {
            "else": {
              "template": "—"
            },
            "if": {
              "equals": "running",
              "path": "status.data.status"
            },
            "then": {
              "else": {
                "template": "{up_days} days"
              },
              "if": {
                "equals": 0,
                "path": "up_days"
              },
              "then": {
                "template": "Started today"
              }
            }
          },
          "items[4].value": "status.data.qmpstatus"
        },
        "ref": "guest"
      },
      "type": "facts"
    },
    {
      "id": "tasks",
      "items": [],
      "label": "Recent tasks",
      "max_items": 4,
      "source": {
        "map": {
          "items": {
            "each": "tasks.data",
            "fields": {
              "detail": {
                "else": {
                  "template": "Running · started {starttime|ago}"
                },
                "if": {
                  "gt": 0,
                  "path": "endtime"
                },
                "then": {
                  "template": "{status} · {endtime|ago}"
                }
              },
              "text": "type"
            }
          }
        },
        "ref": "guest"
      },
      "type": "list"
    },
    {
      "id": "latest-backup",
      "items": [],
      "label": "Latest backups",
      "source": {
        "map": {
          "items": {
            "each": [
              "archives",
              "none_yet"
            ],
            "fields": {
              "detail": {
                "else": {
                  "template": ""
                },
                "if": {
                  "gt": 0,
                  "path": "ctime"
                },
                "then": {
                  "template": "{gb} GB"
                }
              },
              "text": {
                "else": {
                  "path": "text"
                },
                "if": {
                  "gt": 0,
                  "path": "ctime"
                },
                "then": {
                  "template": "{ctime|day}"
                }
              }
            },
            "limit": 3,
            "sort": {
              "by": "ctime",
              "order": "desc"
            }
          }
        },
        "ref": "backups"
      },
      "type": "list"
    }
  ],
  "title_from": {
    "ref": "guest",
    "value": "status.data.name"
  }
}
