apiVersion: v1
kind: ConfigMap
metadata:
  name: test-component-pod-view
  namespace: vela-system
data:
  template: |
    import (
      "vela/ql"
      "vela/op"
    )

    parameter: {
      appName:    string
      appNs:      string
      name?:      string
      cluster?:   string
      clusterNs?: string
    }

    application: ql.#ListResourcesInApp & {
      app: {
        name:      parameter.appName
        namespace: parameter.appNs
        filter: {
          if parameter.cluster != _|_ {
            cluster: parameter.cluster
          }
          if parameter.clusterNs != _|_ {
            clusterNamespace: parameter.clusterNs
          }
          if parameter.name != _|_ {
            components: [parameter.name]
          }
        }
      }
    }

    if application.err != _|_ {
      status: error: application.err
    }

    if application.err == _|_ {
      resources: application.list

      podsMap: op.#Steps & {
        for i, resource in resources {
          "\(i)": ql.#CollectPods & {
            value:   resource.object
            cluster: resource.cluster
          }
        }
      }

      podsWithCluster: [ for i, pods in podsMap for podObj in pods.list {
        cluster: pods.cluster
        obj:     podObj
      }]

      podStatus: op.#Steps & {
        for i, pod in podsWithCluster {
          "\(i)": op.#Steps & {
            name: pod.obj.metadata.name
            containers: {for container in pod.obj.status.containerStatuses {
              "\(container.name)": {
                image: container.image
                state: container.state
              }
            }}
            events: ql.#SearchEvents & {
              value:   pod.obj
              cluster: pod.cluster
            }
            metrics: ql.#Read & {
              cluster: pod.cluster
              value: {
                apiVersion: "metrics.k8s.io/v1beta1"
                kind:       "PodMetrics"
                metadata: {
                  name:      pod.obj.metadata.name
                  namespace: pod.obj.metadata.namespace
                }
              }
            }
          }
        }
      }

      status: {
        podList: [ for podInfo in podStatus {
          name: podInfo.name
          containers: [ for containerName, container in podInfo.containers {
            containerName
          }]
          events: podInfo.events.list
        }]
      }
    }
