<template>
    <div class="content-l">
        <p>{{ pod.resource.metadata.namespace }} / {{ pod.resource.metadata.name }}</p>
        <Chart :datasets="dataset" :config="MEMORY_CHART_CONFIG(new Date(period.from * 1000), new Date(period.to * 1000))"></Chart>
    </div>
</template>

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

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

const props = defineProps<{
    pod: MonitoredResource<Pod>,
    period: Period
}>();

const dataset = computed(() => {
    const result = [] as Data[];
    const max = Math.max(...props.pod.jobs.map(job => job.metrics["MEMORY"].average));
    const dimension = getDimension(max);
    for (const job of props.pod.jobs)
    {
        result.push(new Data(job.metrics["MEMORY"].average / (Math.pow(1024, dimension)), new Date(job.unix * 1000)));
    }
    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>