{
  "$schema": "./.schemas/page.schema.json",
  "title": "Container",
  "icon": "shippingbox",
  "sidebar": false,
  "params": {
    "env": {
      "type": "integer",
      "example": "1"
    },
    "id": {
      "example": "wiki",
      "pattern": "[A-Za-z0-9][A-Za-z0-9_.-]{0,63}"
    }
  },
  "title_from": {
    "ref": "inspect",
    "value": "name"
  },
  "sources": {
    "samples": {
      "dataset": "portainer-containers",
      "key": "{param.env}-{param.id}"
    },
    "inspect": {
      "url": "https://portainer.example.com",
      "secret": "portainer",
      "auth": {
        "type": "header",
        "name": "X-API-Key"
      },
      "every": "1m",
      "requests": {
        "c": "/api/endpoints/{param.env}/docker/containers/{param.id}/json",
        "env": "/api/endpoints/{param.env}"
      },
      "derive": [
        {
          "name": "name",
          "value": {
            "path": "c.Name",
            "match": "^/(.*)$"
          }
        },
        {
          "name": "state_word",
          "value": "c.State.Status"
        },
        {
          "name": "image",
          "value": {
            "path": "c.Config.Image",
            "match": "^([^@]+)"
          }
        },
        {
          "name": "health",
          "value": {
            "path": "c.State.Health.Status",
            "default": "none"
          }
        },
        {
          "name": "exit_code",
          "value": {
            "path": "c.State.ExitCode",
            "default": 0
          }
        },
        {
          "name": "protected",
          "value": {
            "if": {
              "path": "name",
              "in_list": "keep-running",
              "optional": true
            },
            "then": {
              "template": "yes"
            },
            "else": {
              "template": "no"
            }
          }
        },
        {
          "name": "no_stop",
          "value": {
            "if": {
              "path": "name",
              "in_list": "keep-running",
              "optional": true
            },
            "then": {
              "template": "yes"
            },
            "else": {
              "template": "no"
            }
          }
        },
        {
          "name": "restarted",
          "value": {
            "if": {
              "all": [
                {
                  "value": {
                    "since": "c.State.StartedAt"
                  },
                  "lt": 600
                },
                {
                  "value": {
                    "since": "c.Created"
                  },
                  "gt": 900
                }
              ]
            },
            "then": {
              "template": "yes"
            },
            "else": {
              "template": "no"
            }
          }
        },
        {
          "name": "state",
          "value": {
            "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"
                  }
                }
              }
            }
          }
        },
        {
          "name": "host",
          "value": {
            "each": "env",
            "entries": true,
            "where": {
              "path": "key",
              "equals": "PublicURL"
            },
            "fields": {
              "k": {
                "template": "x"
              },
              "host": "value"
            }
          }
        },
        {
          "name": "ports1",
          "value": {
            "each": "c.HostConfig.PortBindings",
            "entries": true,
            "where": {
              "path": "key",
              "contains": "/tcp"
            },
            "fields": {
              "k": {
                "template": "x"
              },
              "inner": {
                "path": "key",
                "match": "^([0-9]+)"
              },
              "port": "value[0].HostPort"
            }
          }
        },
        {
          "name": "ports",
          "value": {
            "each": "ports1",
            "where": {
              "all": [
                {
                  "path": "port",
                  "exists": true
                },
                {
                  "path": "h.host",
                  "exists": true
                }
              ]
            },
            "lookup": {
              "from": "host",
              "by": "k",
              "on": "k",
              "as": "h"
            },
            "fields": {
              "text": {
                "template": "Port {port|raw}"
              },
              "detail": {
                "template": "to {inner|raw}"
              },
              "url": {
                "template": "http://{h.host}:{port|raw}"
              }
            }
          }
        },
        {
          "name": "href",
          "value": {
            "each": "c.Config.Labels",
            "entries": true,
            "where": {
              "path": "key",
              "equals": "homepage.href"
            },
            "fields": {
              "text": {
                "template": "Open"
              },
              "detail": {
                "template": "{value|origin}"
              },
              "url": "value"
            }
          }
        },
        {
          "name": "links",
          "value": {
            "each": [
              "href",
              "ports"
            ],
            "where": {
              "path": "url",
              "contains": "://"
            },
            "fields": {
              "text": "text",
              "detail": "detail",
              "url": "url"
            }
          }
        },
        {
          "name": "link_count",
          "value": {
            "count": "links",
            "default": 0
          }
        },
        {
          "name": "restart_q",
          "value": {
            "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": "checks",
          "value": {
            "each": "c.State.Health.Log",
            "sort": {
              "by": "End",
              "order": "desc"
            },
            "limit": 4,
            "fields": {
              "ok": {
                "if": {
                  "path": "ExitCode",
                  "equals": 0
                },
                "then": {
                  "template": "Passed"
                },
                "else": {
                  "template": "Failed · exit {ExitCode|raw}"
                }
              },
              "output": {
                "template": "{Output|clip:80}"
              },
              "at": "End"
            }
          }
        }
      ]
    },
    "logs": {
      "url": "https://portainer.example.com",
      "secret": "portainer",
      "auth": {
        "type": "header",
        "name": "X-API-Key"
      },
      "every": "1m",
      "requests": {
        "log": {
          "path": "/api/endpoints/{param.env}/docker/containers/{param.id}/logs?stdout=1&stderr=1&tail=20&timestamps=1",
          "format": "docker-logs"
        }
      }
    },
    "stats": {
      "url": "https://portainer.example.com",
      "secret": "portainer",
      "auth": {
        "type": "header",
        "name": "X-API-Key"
      },
      "every": "1m",
      "requests": {
        "s": "/api/endpoints/{param.env}/docker/containers/{param.id}/stats?stream=false"
      },
      "derive": [
        {
          "name": "cpu",
          "value": {
            "multiply": [
              {
                "divide": [
                  {
                    "subtract": [
                      "s.cpu_stats.cpu_usage.total_usage",
                      "s.precpu_stats.cpu_usage.total_usage"
                    ]
                  },
                  {
                    "subtract": [
                      "s.cpu_stats.system_cpu_usage",
                      "s.precpu_stats.system_cpu_usage"
                    ]
                  }
                ]
              },
              "s.cpu_stats.online_cpus",
              100
            ],
            "round": 1
          }
        },
        {
          "name": "memory",
          "value": {
            "subtract": [
              "s.memory_stats.usage",
              {
                "path": "s.memory_stats.stats.inactive_file",
                "default": 0
              }
            ],
            "scale": 1e-06,
            "round": 0
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "state",
      "type": "status",
      "width": "full",
      "label": "Now",
      "state": "unknown",
      "source": {
        "ref": "inspect",
        "map": {
          "state": "state",
          "detail": {
            "if": {
              "path": "state_word",
              "equals": "running"
            },
            "then": {
              "if": {
                "path": "restarted",
                "equals": "yes"
              },
              "then": {
                "template": "Restarted {c.State.StartedAt|ago} · {c.RestartCount} restarts"
              },
              "else": {
                "if": {
                  "path": "health",
                  "equals": "none"
                },
                "then": {
                  "template": "Running since {c.State.StartedAt|ago}"
                },
                "else": {
                  "template": "Running · {health} · since {c.State.StartedAt|ago}"
                }
              }
            },
            "else": {
              "if": {
                "path": "state_word",
                "equals": "exited"
              },
              "then": {
                "if": {
                  "path": "c.State.OOMKilled",
                  "equals": true
                },
                "then": {
                  "template": "Out of memory · exited {c.State.FinishedAt|ago}"
                },
                "else": {
                  "template": "Exited · code {exit_code|raw}, {c.State.FinishedAt|ago}"
                }
              },
              "else": {
                "template": "{state_word|title}"
              }
            }
          }
        },
        "failed": {
          "state": "critical",
          "detail": "Can't reach Docker through Portainer",
          "after": 3
        }
      },
      "actions_placement": "corner",
      "actions": [
        {
          "label": "Start",
          "icon": "play.fill",
          "method": "POST",
          "path": "/api/endpoints/{param.env}/docker/containers/{param.id}/start",
          "when": {
            "path": "state_word",
            "in": [
              "exited",
              "created"
            ]
          },
          "confirm": {
            "message": "Start {answer.name}?",
            "button": "Start"
          }
        },
        {
          "label": "Stop",
          "icon": "stop.fill",
          "method": "POST",
          "path": "/api/endpoints/{param.env}/docker/containers/{param.id}/stop?t=10",
          "when": {
            "all": [
              {
                "path": "state_word",
                "equals": "running"
              },
              {
                "path": "no_stop",
                "equals": "no"
              }
            ]
          },
          "confirm": {
            "message": "Stop {answer.name}? It stays stopped until you start it.",
            "button": "Stop"
          }
        },
        {
          "label": "Restart",
          "icon": "arrow.trianglehead.2.clockwise.rotate.90",
          "method": "POST",
          "path": "/api/endpoints/{param.env}/docker/containers/{param.id}/restart?t=10",
          "when": {
            "path": "state_word",
            "in": [
              "running",
              "restarting"
            ]
          },
          "confirm": {
            "message": "{answer.restart_q}",
            "button": "Restart"
          }
        },
        {
          "label": "Pull and recreate",
          "icon": "arrow.down.circle",
          "method": "POST",
          "path": "/api/docker/{param.env}/containers/{answer.c.Id}/recreate",
          "body": {
            "PullImage": true
          },
          "confirm": {
            "message": "Pull the newest {answer.image} and recreate {answer.name}? It starts again with the same settings.",
            "button": "Recreate"
          }
        }
      ]
    },
    {
      "id": "cpu",
      "type": "sparkline",
      "label": "CPU",
      "unit": "%",
      "width": "half",
      "change": false,
      "points": [],
      "source": {
        "ref": "stats",
        "map": {
          "points": {
            "history": "cpu",
            "every": "1m",
            "keep": 60
          }
        }
      }
    },
    {
      "id": "memory",
      "type": "sparkline",
      "label": "Memory",
      "unit": "MB",
      "width": "half",
      "change": false,
      "points": [],
      "source": {
        "ref": "stats",
        "map": {
          "points": {
            "history": "memory",
            "every": "1m",
            "keep": 60
          }
        }
      }
    },
    {
      "id": "day",
      "type": "strip",
      "label": "Last 24 hours",
      "start_label": "24 hours ago",
      "end_label": "Now",
      "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}"
              }
            }
          }
        }
      }
    },
    {
      "id": "facts",
      "type": "facts",
      "label": "Container",
      "items": [
        {
          "key": "Image",
          "value": "—"
        },
        {
          "key": "Project",
          "value": "—"
        },
        {
          "key": "Restart policy",
          "value": "—"
        },
        {
          "key": "Restarts",
          "value": "—"
        },
        {
          "key": "Created",
          "value": "—"
        }
      ],
      "source": {
        "ref": "inspect",
        "map": {
          "items[0].value": {
            "template": "{image|clip:60}"
          },
          "items[1].value": {
            "if": {
              "path": "c.Config.Labels[\"com.docker.compose.project\"]",
              "exists": true
            },
            "then": {
              "template": "{c.Config.Labels[\"com.docker.compose.project\"]} · {c.Config.Labels[\"com.docker.compose.service\"]}"
            },
            "else": {
              "template": "Standalone"
            }
          },
          "items[2].value": {
            "path": "c.HostConfig.RestartPolicy.Name",
            "values": {
              "no": "None",
              "always": "Always",
              "unless-stopped": "Unless stopped",
              "on-failure": "On failure"
            },
            "default": "—"
          },
          "items[3].value": {
            "template": "{c.RestartCount} · last started {c.State.StartedAt|ago}"
          },
          "items[4].value": {
            "template": "{c.Created|ago}"
          }
        }
      }
    },
    {
      "id": "open",
      "type": "list",
      "label": "Open",
      "items": [],
      "source": {
        "ref": "inspect",
        "when": {
          "path": "link_count",
          "gt": 0
        },
        "map": {
          "items": {
            "each": "links",
            "fields": {
              "text": "text",
              "detail": "detail",
              "url": "url"
            }
          }
        }
      }
    },
    {
      "id": "logs",
      "type": "list",
      "label": "Logs",
      "max_items": 10,
      "items": [],
      "source": {
        "ref": "logs",
        "empty": {
          "text": "No log lines",
          "when": {
            "value": {
              "count": "log.lines",
              "default": 0
            },
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "log.lines",
            "last": 10,
            "fields": {
              "text": {
                "template": "{text|clip:120}"
              },
              "detail": {
                "template": "{time|clock}"
              },
              "icon": {
                "if": {
                  "path": "stream",
                  "equals": "stderr"
                },
                "then": {
                  "template": "exclamationmark.triangle"
                }
              }
            }
          }
        }
      }
    },
    {
      "id": "checks",
      "type": "list",
      "label": "Health checks",
      "max_items": 4,
      "items": [],
      "source": {
        "ref": "inspect",
        "when": {
          "path": "health",
          "not": "none"
        },
        "map": {
          "items": {
            "each": "checks",
            "fields": {
              "text": "ok",
              "detail": {
                "template": "{at|ago}"
              }
            }
          }
        }
      }
    },
    {
      "id": "mounts",
      "type": "list",
      "label": "Mounts",
      "max_items": 7,
      "items": [],
      "source": {
        "ref": "inspect",
        "empty": {
          "text": "No mounts",
          "when": {
            "value": {
              "count": "c.Mounts",
              "default": 0
            },
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "c.Mounts",
            "fields": {
              "text": {
                "template": "{Destination|clip:60}"
              },
              "detail": {
                "if": {
                  "path": "RW",
                  "equals": false
                },
                "then": {
                  "template": "Read only"
                },
                "else": {
                  "template": "{Type|title}"
                }
              }
            }
          }
        }
      }
    }
  ]
}
