{
  "$schema": "./.schemas/page.schema.json",
  "title": "Container",
  "icon": "shippingbox",
  "sidebar": false,
  "params": {
    "name": {
      "example": "plex",
      "pattern": "[A-Za-z0-9][A-Za-z0-9_.-]{0,63}"
    }
  },
  "title_from": {
    "ref": "docker",
    "value": "c.name"
  },
  "sources": {
    "samples": {
      "dataset": "unraid-containers",
      "key": "{param.name}"
    },
    "docker": {
      "fail": {
        "when": {
          "any": [
            {
              "path": "gql.errors",
              "exists": true
            },
            {
              "path": "gql.data.docker.containers",
              "exists": false
            }
          ]
        },
        "message": {
          "path": "gql.errors[0].message",
          "default": "Unraid sent no containers"
        }
      },
      "url": "http://tower.local",
      "secret": "unraid",
      "auth": {
        "type": "header",
        "name": "x-api-key"
      },
      "every": "1m",
      "requests": {
        "gql": {
          "path": "/graphql",
          "method": "POST",
          "body": {
            "query": "{ docker { containers { id names image state status autoStart isUpdateAvailable created } } }"
          }
        }
      },
      "derive": [
        {
          "name": "all",
          "value": {
            "each": "gql.data.docker.containers",
            "fields": {
              "name": {
                "path": "names[0]",
                "match": "^/?(.*)$"
              },
              "id": "id",
              "image": "image",
              "state": "state",
              "status": "status",
              "auto": "autoStart",
              "update": "isUpdateAvailable",
              "created": "created"
            }
          }
        },
        {
          "name": "match",
          "value": {
            "each": "all",
            "where": {
              "path": "name",
              "equals": "{param.name}"
            },
            "fields": {
              "name": "name",
              "id": "id",
              "image": "image",
              "state": "state",
              "status": "status",
              "auto": "auto",
              "update": "update",
              "created": "created"
            }
          }
        },
        {
          "name": "count",
          "value": {
            "count": "match",
            "default": 0
          }
        },
        {
          "name": "c",
          "value": "match[0]"
        }
      ]
    },
    "logs": {
      "fail": {
        "when": {
          "path": "log.errors",
          "exists": true
        },
        "message": "log.errors[0].message"
      },
      "url": "http://tower.local",
      "secret": "unraid",
      "auth": {
        "type": "header",
        "name": "x-api-key"
      },
      "every": "1m",
      "requests": {
        "log": {
          "path": "/graphql",
          "method": "POST",
          "body": {
            "query": "query($name: PrefixedID!) { docker { logs(id: $name, tail: 10) { lines { timestamp message } } } }",
            "variables": {
              "name": "{param.name}"
            }
          }
        }
      }
    }
  },
  "cards": [
    {
      "id": "state",
      "type": "status",
      "width": "full",
      "label": "Now",
      "state": "unknown",
      "source": {
        "ref": "docker",
        "map": {
          "state": {
            "if": {
              "path": "count",
              "equals": 0
            },
            "then": {
              "template": "unknown"
            },
            "else": {
              "if": {
                "path": "c.state",
                "equals": "RUNNING"
              },
              "then": {
                "template": "ok"
              },
              "else": {
                "if": {
                  "all": [
                    {
                      "path": "c.auto",
                      "equals": true
                    },
                    {
                      "path": "c.state",
                      "equals": "EXITED"
                    }
                  ]
                },
                "then": {
                  "template": "warning"
                },
                "else": {
                  "template": "off"
                }
              }
            }
          },
          "detail": {
            "if": {
              "path": "count",
              "equals": 0
            },
            "then": {
              "template": "Unraid has no container by this name"
            },
            "else": {
              "if": {
                "all": [
                  {
                    "path": "c.auto",
                    "equals": true
                  },
                  {
                    "path": "c.state",
                    "equals": "EXITED"
                  }
                ]
              },
              "then": {
                "template": "{c.status} · set to autostart"
              },
              "else": {
                "template": "{c.status}"
              }
            }
          }
        },
        "failed": {
          "state": "critical",
          "detail": "Unraid isn't answering",
          "after": 3
        }
      },
      "actions_placement": "corner",
      "actions": [
        {
          "label": "Start",
          "icon": "play.fill",
          "path": "/graphql",
          "graphql": {
            "query": "mutation($id: PrefixedID!) { docker { start(id: $id) { id state } } }",
            "variables": {
              "id": "{answer.c.id}"
            }
          },
          "when": {
            "path": "c.state",
            "equals": "EXITED"
          }
        },
        {
          "label": "Resume",
          "icon": "playpause.fill",
          "path": "/graphql",
          "graphql": {
            "query": "mutation($id: PrefixedID!) { docker { unpause(id: $id) { id state } } }",
            "variables": {
              "id": "{answer.c.id}"
            }
          },
          "when": {
            "path": "c.state",
            "equals": "PAUSED"
          }
        },
        {
          "label": "Stop",
          "icon": "stop.fill",
          "path": "/graphql",
          "graphql": {
            "query": "mutation($id: PrefixedID!) { docker { stop(id: $id) { id state } } }",
            "variables": {
              "id": "{answer.c.id}"
            }
          },
          "when": {
            "path": "c.state",
            "equals": "RUNNING"
          },
          "confirm": {
            "message": "Stop {answer.c.name}? It stays stopped until you start it.",
            "button": "Stop"
          }
        },
        {
          "label": "Update",
          "icon": "arrow.down.circle",
          "path": "/graphql",
          "graphql": {
            "query": "mutation($id: PrefixedID!) { docker { updateContainer(id: $id) { id state } } }",
            "variables": {
              "id": "{answer.c.id}"
            }
          },
          "when": {
            "path": "c.update",
            "equals": true
          },
          "confirm": {
            "message": "Update {answer.c.name}? Unraid pulls the new image and recreates it from its template.",
            "button": "Update"
          },
          "timeout": 900
        }
      ]
    },
    {
      "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": "Autostart",
          "value": "—"
        },
        {
          "key": "Update",
          "value": "—"
        },
        {
          "key": "Created",
          "value": "—"
        }
      ],
      "source": {
        "ref": "docker",
        "when": {
          "path": "count",
          "gt": 0
        },
        "map": {
          "items[0].value": {
            "template": "{c.image|clip:60}"
          },
          "items[1].value": {
            "if": {
              "path": "c.auto",
              "equals": true
            },
            "then": {
              "template": "With the array"
            },
            "else": {
              "template": "Off"
            }
          },
          "items[2].value": {
            "if": {
              "path": "c.update",
              "equals": true
            },
            "then": {
              "template": "Ready"
            },
            "else": {
              "if": {
                "path": "c.update",
                "equals": false
              },
              "then": {
                "template": "Up to date"
              },
              "else": {
                "template": "Not checked"
              }
            }
          },
          "items[3].value": {
            "template": "{c.created|ago}"
          }
        }
      }
    },
    {
      "id": "logs",
      "type": "list",
      "label": "Logs",
      "max_items": 10,
      "items": [],
      "source": {
        "ref": "logs",
        "empty": {
          "text": "No log lines",
          "when": {
            "value": {
              "count": "log.data.docker.logs.lines",
              "default": 0
            },
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "log.data.docker.logs.lines",
            "last": 10,
            "fields": {
              "text": {
                "template": "{message|clip:120}"
              },
              "detail": {
                "template": "{timestamp|clock}"
              }
            }
          }
        }
      }
    }
  ]
}
