<template>
    <div class="content-l">
        <p>{{ pod.pod.metadata.namespace }} / {{ pod.pod.metadata.name }}</p>
        <Chart :datasets="dataset" :config="MEMORY_CHART_CONFIG()"></Chart>
    </div>
</template>

<script setup lang="ts">
import { Data, Dataset } from '~/components/chart/Dataset';
import { MonitoredPod } from '../MonitoredResource';
import { MEMORY_CHART_CONFIG } from './ChartConfig';

const units = new Map<number, string>([[0, "B"], [1, "Ki"], [2, "Mi"], [3, "Gi"], [4, "Ti"]]);

const props = defineProps<{
    pod: MonitoredPod
}>();

const dataset = computed(() => {
    const result = [] as Data[];
    const max = Math.max(...props.pod.jobs.map(job => job.average));
    const dimension = getDimension(max);
    for (const job of props.pod.jobs)
    {
        result.push(new Data(job.average / (Math.pow(1024, dimension)), job.timestamp));
    }
    return new Dataset(units.get(dimension)!, result);
});

function getDimension(max: number)
{
    if (max <= 1024)
    {
        return 0;
    }
    else if (max <= (1024 * 1024))
    {
        return 1;
    }
    else if (max <= (1024 * 1024 * 1024))
    {
        return 2;
    }
    else if (max <= (1024 * 1024 * 1024 * 1024))
    {
        return 3;
    }
    else if (max <= (1024 * 1024 * 1024 * 1024 * 1024))
    {
        return 4;
    }
    return -1;
}
</script>