package containers

import (
	"encoding/json"
	"fmt"
	"github.com/coroot/coroot-node-agent/utils"
	"github.com/coroot/coroot-node-agent/utils/modelse"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/sirupsen/logrus"
	"os"
	"path"
)

const (
	Namespace   = "ns"
	PodName     = "pod_name"
	Workload    = "workload"
	ProcessName = "process_name"
	ContainerId = "container_id"
)

func setLabels(container_id, ns, workload, pod_name, process_name, pid string) prometheus.Labels {
	return map[string]string{
		ContainerId: container_id,
		Namespace:   ns,
		Workload:    workload,
		PodName:     pod_name,
		ProcessName: process_name,
		"pid":       pid,
	}
}

func setK8sTag(c *Container, tag map[string]string, pid uint32) {
	sPid := fmt.Sprintf("%d", pid)
	c.K8sContainer.ns = tag[Namespace]
	c.K8sContainer.podName = tag[PodName]
	c.K8sContainer.containerName = tag[ProcessName]
	c.K8sContainer.workload = tag[Workload]
	c.K8sContainer.pid = sPid
}

func snapAppInfo(runtimeApps map[uint32]modelse.AppStatusInfo) {
	appStr, _ := json.Marshal(runtimeApps)
	dumpPath := path.Join(utils.GetDefaultRuntimePath(), "memdump")
	err := os.MkdirAll(dumpPath, 0755)
	if err != nil {
		logrus.Error(err)
	}
	fileName := fmt.Sprintf("%s.snap", "app")
	metricsFileName := path.Join(dumpPath, fileName)
	err = os.WriteFile(metricsFileName, appStr, 0644)
	if err != nil {
		logrus.Error(err)
	}
}
