{
  "$schema": "./.schemas/page.schema.json",
  "title": "Portainer",
  "icon": "shippingbox",
  "sources": {
    "containers": {
      "collect": {
        "dataset": "portainer-containers",
        "each": "all",
        "key": "1-{name}",
        "sample": {
          "every": "5m"
        },
        "fields": {
          "state": "state"
        },
        "readers": [
          "portainer-container"
        ]
      },
      "url": "https://portainer.example.com",
      "secret": "portainer",
      "auth": {
        "type": "header",
        "name": "X-API-Key"
      },
      "every": "1m",
      "requests": {
        "containers": "/api/endpoints/1/docker/containers/json?all=1"
      },
      "derive": [
        {
          "name": "base",
          "value": {
            "each": "containers",
            "fields": {
              "name": {
                "path": "Names[0]",
                "match": "^/(.*)$"
              },
              "project": {
                "path": "Labels[\"com.docker.compose.project\"]",
                "default": "Standalone"
              },
              "state_word": "State",
              "health": {
                "if": {
                  "path": "Health.Status",
                  "exists": true
                },
                "then": {
                  "path": "Health.Status"
                },
                "else": {
                  "path": "Status",
                  "match": "\\((?:health: )?(healthy|unhealthy|starting)\\)",
                  "default": "none"
                }
              },
              "exit_code": {
                "path": "Status",
                "match": "^Exited \\(([0-9]+)\\)",
                "default": 0
              },
              "status": "Status",
              "up_short": {
                "path": "Status",
                "match": "^(Up (?:Less than a second|About a minute|[0-9]+ seconds|[1-9] minutes))"
              },
              "age": {
                "since": "Created"
              }
            }
          }
        },
        {
          "name": "rows",
          "value": {
            "each": "base",
            "fields": {
              "name": "name",
              "project": "project",
              "state_word": "state_word",
              "health": "health",
              "exit_code": "exit_code",
              "status": "status",
              "up_short": "up_short",
              "age": "age",
              "protected": {
                "if": {
                  "path": "name",
                  "in_list": "keep-running",
                  "optional": true
                },
                "then": {
                  "template": "yes"
                },
                "else": {
                  "template": "no"
                }
              },
              "no_stop": {
                "if": {
                  "path": "name",
                  "in_list": "keep-running",
                  "optional": true
                },
                "then": {
                  "template": "yes"
                },
                "else": {
                  "template": "no"
                }
              },
              "restarted": {
                "if": {
                  "all": [
                    {
                      "path": "up_short",
                      "exists": true
                    },
                    {
                      "path": "age",
                      "gt": 900
                    }
                  ]
                },
                "then": {
                  "template": "yes"
                },
                "else": {
                  "template": "no"
                }
              }
            }
          }
        },
        {
          "name": "all",
          "value": {
            "each": "rows",
            "fields": {
              "name": "name",
              "project": "project",
              "state_word": "state_word",
              "health": "health",
              "exit_code": "exit_code",
              "status": "status",
              "up_short": "up_short",
              "age": "age",
              "protected": "protected",
              "no_stop": "no_stop",
              "restarted": "restarted",
              "state": {
                "if": {
                  "any": [
                    {
                      "path": "health",
                      "equals": "unhealthy"
                    },
                    {
                      "path": "state_word",
                      "equals": "dead"
                    },
                    {
                      "all": [
                        {
                          "path": "state_word",
                          "equals": "exited"
                        },
                        {
                          "path": "exit_code",
                          "gt": 0
                        }
                      ]
                    },
                    {
                      "all": [
                        {
                          "path": "protected",
                          "equals": "yes"
                        },
                        {
                          "path": "state_word",
                          "not": "running"
                        }
                      ]
                    }
                  ]
                },
                "then": {
                  "template": "critical"
                },
                "else": {
                  "if": {
                    "any": [
                      {
                        "path": "state_word",
                        "equals": "restarting"
                      },
                      {
                        "path": "health",
                        "equals": "starting"
                      },
                      {
                        "path": "restarted",
                        "equals": "yes"
                      }
                    ]
                  },
                  "then": {
                    "template": "warning"
                  },
                  "else": {
                    "if": {
                      "path": "state_word",
                      "equals": "running"
                    },
                    "then": {
                      "template": "ok"
                    },
                    "else": {
                      "if": {
                        "path": "state_word",
                        "in": [
                          "exited",
                          "created",
                          "paused"
                        ]
                      },
                      "then": {
                        "template": "off"
                      },
                      "else": {
                        "template": "unknown"
                      }
                    }
                  }
                }
              },
              "group": {
                "if": {
                  "path": "health",
                  "equals": "healthy"
                },
                "then": {
                  "template": "Healthy"
                },
                "else": {
                  "template": "No health check"
                }
              },
              "words": {
                "if": {
                  "path": "restarted",
                  "equals": "yes"
                },
                "then": {
                  "template": "Restarted · {status}"
                },
                "else": {
                  "template": "{status}"
                }
              },
              "restart_q": {
                "if": {
                  "path": "protected",
                  "equals": "yes"
                },
                "then": {
                  "template": "Restart {name}? It's on your keep-running list, so what depends on it stops for a moment."
                },
                "else": {
                  "template": "Restart {name}?"
                }
              }
            }
          }
        },
        {
          "name": "red",
          "value": {
            "each": "all",
            "where": {
              "path": "state",
              "equals": "critical"
            },
            "sort": "name",
            "fields": {
              "name": "name",
              "project": "project",
              "state_word": "state_word",
              "health": "health",
              "exit_code": "exit_code",
              "status": "status",
              "up_short": "up_short",
              "age": "age",
              "protected": "protected",
              "no_stop": "no_stop",
              "restarted": "restarted",
              "state": "state",
              "group": "group",
              "words": "words",
              "restart_q": "restart_q"
            }
          }
        },
        {
          "name": "orange",
          "value": {
            "each": "all",
            "where": {
              "path": "state",
              "equals": "warning"
            },
            "sort": "name",
            "fields": {
              "name": "name",
              "project": "project",
              "state_word": "state_word",
              "health": "health",
              "exit_code": "exit_code",
              "status": "status",
              "up_short": "up_short",
              "age": "age",
              "protected": "protected",
              "no_stop": "no_stop",
              "restarted": "restarted",
              "state": "state",
              "group": "group",
              "words": "words",
              "restart_q": "restart_q"
            }
          }
        },
        {
          "name": "fine",
          "value": {
            "each": "all",
            "where": {
              "path": "state",
              "equals": "ok"
            },
            "sort": "name",
            "fields": {
              "name": "name",
              "project": "project",
              "state_word": "state_word",
              "health": "health",
              "exit_code": "exit_code",
              "status": "status",
              "up_short": "up_short",
              "age": "age",
              "protected": "protected",
              "no_stop": "no_stop",
              "restarted": "restarted",
              "state": "state",
              "group": "group",
              "words": "words",
              "restart_q": "restart_q"
            }
          }
        },
        {
          "name": "stopped",
          "value": {
            "each": "all",
            "where": {
              "path": "state",
              "in": [
                "off",
                "unknown"
              ]
            },
            "sort": "name",
            "fields": {
              "name": "name",
              "project": "project",
              "state_word": "state_word",
              "health": "health",
              "exit_code": "exit_code",
              "status": "status",
              "up_short": "up_short",
              "age": "age",
              "protected": "protected",
              "no_stop": "no_stop",
              "restarted": "restarted",
              "state": "state",
              "group": "group",
              "words": "words",
              "restart_q": "restart_q"
            }
          }
        },
        {
          "name": "ordered",
          "value": {
            "each": [
              "red",
              "orange",
              "fine",
              "stopped"
            ],
            "fields": {
              "name": "name",
              "project": "project",
              "state_word": "state_word",
              "health": "health",
              "exit_code": "exit_code",
              "status": "status",
              "up_short": "up_short",
              "age": "age",
              "protected": "protected",
              "no_stop": "no_stop",
              "restarted": "restarted",
              "state": "state",
              "group": "group",
              "words": "words",
              "restart_q": "restart_q"
            }
          }
        },
        {
          "name": "attention",
          "value": {
            "each": [
              "red",
              "orange"
            ],
            "fields": {
              "name": "name",
              "project": "project",
              "state_word": "state_word",
              "health": "health",
              "exit_code": "exit_code",
              "status": "status",
              "up_short": "up_short",
              "age": "age",
              "protected": "protected",
              "no_stop": "no_stop",
              "restarted": "restarted",
              "state": "state",
              "group": "group",
              "words": "words",
              "restart_q": "restart_q"
            }
          }
        },
        {
          "name": "total",
          "value": {
            "count": "containers",
            "default": 0
          }
        },
        {
          "name": "running",
          "value": {
            "count": "containers",
            "where": {
              "path": "State",
              "equals": "running"
            },
            "default": 0
          }
        }
      ]
    },
    "docker": {
      "url": "https://portainer.example.com",
      "secret": "portainer",
      "auth": {
        "type": "header",
        "name": "X-API-Key"
      },
      "every": "15m",
      "requests": {
        "dashboard": "/api/docker/1/dashboard",
        "env": "/api/endpoints/1",
        "df": "/api/endpoints/1/docker/system/df"
      },
      "derive": [
        {
          "name": "unused_images",
          "value": {
            "each": "df.Images",
            "where": {
              "path": "Containers",
              "equals": 0
            },
            "fields": {
              "own": {
                "subtract": [
                  "Size",
                  {
                    "path": "SharedSize",
                    "default": 0
                  }
                ]
              }
            }
          }
        },
        {
          "name": "unused_gb",
          "value": {
            "sum": "own",
            "of": "unused_images",
            "scale": 1e-09,
            "round": 1,
            "default": 0
          }
        },
        {
          "name": "image_gb",
          "value": {
            "path": "dashboard.images.size",
            "scale": 1e-09,
            "round": 1
          }
        },
        {
          "name": "memory_gb",
          "value": {
            "path": "env.Snapshots[0].TotalMemory",
            "scale": 1e-09,
            "round": 1
          }
        }
      ]
    },
    "version": {
      "url": "https://portainer.example.com",
      "secret": "portainer",
      "auth": {
        "type": "header",
        "name": "X-API-Key"
      },
      "every": "1h",
      "requests": {
        "version": "/api/system/version"
      }
    }
  },
  "cards": [
    {
      "id": "docker",
      "type": "facts",
      "label": "Docker",
      "items": [
        {
          "key": "Containers",
          "value": "—"
        },
        {
          "key": "Images",
          "value": "—"
        },
        {
          "key": "Host",
          "value": "—"
        },
        {
          "key": "Volumes",
          "value": "—"
        },
        {
          "key": "Networks",
          "value": "—"
        }
      ],
      "source": {
        "ref": "docker",
        "map": {
          "items[0].value": {
            "template": "{dashboard.containers.total} in {dashboard.stacks} projects"
          },
          "items[1].value": {
            "if": {
              "path": "unused_gb",
              "gt": 0
            },
            "then": {
              "template": "{dashboard.images.total} · {image_gb} GB, {unused_gb} GB unused"
            },
            "else": {
              "template": "{dashboard.images.total} · {image_gb} GB"
            }
          },
          "items[2].value": {
            "template": "Docker {env.Snapshots[0].DockerVersion} · {env.Snapshots[0].TotalCPU} CPUs · {memory_gb} GB"
          },
          "items[3].value": "dashboard.volumes",
          "items[4].value": "dashboard.networks"
        }
      },
      "actions_placement": "corner",
      "actions": [
        {
          "label": "Remove unused images",
          "icon": "trash",
          "method": "POST",
          "path": "/api/endpoints/1/docker/images/prune?filters=%7B%22dangling%22%3A%7B%22false%22%3Atrue%7D%7D",
          "when": {
            "path": "unused_gb",
            "gt": 0
          },
          "confirm": {
            "message": "Remove every image no container uses? That frees at least {answer.unused_gb} GB; they can be pulled again.",
            "button": "Remove"
          }
        }
      ]
    },
    {
      "id": "portainer",
      "type": "text",
      "label": "Portainer",
      "body": "",
      "source": {
        "ref": "version",
        "when": {
          "path": "version.UpdateAvailable",
          "equals": true
        },
        "map": {
          "body": {
            "template": "Portainer {version.LatestVersion} is out; this server runs {version.ServerVersion}."
          }
        }
      }
    },
    {
      "id": "running",
      "type": "sparkline",
      "label": "Running",
      "unit": "containers",
      "change": false,
      "points": [],
      "source": {
        "ref": "containers",
        "map": {
          "points": {
            "history": "running",
            "every": "30m",
            "keep": 48
          }
        }
      }
    },
    {
      "id": "containers",
      "type": "fleet",
      "label": "Containers",
      "max_items": 60,
      "items": [],
      "source": {
        "ref": "containers",
        "map": {
          "items": {
            "each": "ordered",
            "limit": 60,
            "fields": {
              "name": {
                "template": "{name|clip:24}"
              },
              "state": "state",
              "detail": {
                "template": "{words}"
              },
              "url": {
                "template": "legato://page/portainer-container?env=1&id={name}"
              },
              "group": {
                "if": {
                  "path": "state",
                  "in": [
                    "critical",
                    "warning"
                  ]
                },
                "then": {
                  "template": "Needs attention"
                },
                "else": {
                  "if": {
                    "path": "state",
                    "in": [
                      "off",
                      "unknown"
                    ]
                  },
                  "then": {
                    "template": "Stopped"
                  },
                  "else": {
                    "template": "{group}"
                  }
                }
              }
            }
          }
        },
        "failed": {
          "state": "critical",
          "detail": "Can't reach Docker through Portainer",
          "after": 3
        }
      }
    },
    {
      "id": "more",
      "type": "text",
      "tone": "info",
      "body": "",
      "source": {
        "ref": "containers",
        "when": {
          "path": "total",
          "gt": 60
        },
        "map": {
          "body": {
            "template": "Showing 60 of {total} containers. Portainer lists them all."
          }
        }
      }
    }
  ]
}
