All files / src/components AdminCostsPanel.vue

95.74% Statements 45/47
90.9% Branches 30/33
87.5% Functions 14/16
95.55% Lines 43/45

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192  19x 2x                       1x                                           2x           1x       2x 24x                             2x 2x 2x     2x 2x   2x       2x   2x   2x   2x                                     5x   5x 5x   5x 5x 5x   9x     10x           5x 4x 4x 2x       2x             5x 4x 4x 2x 3x     4x 2x 1x     5x             1x 1x       7x             5x                                                          
<template>
  <div class="admin-costs-panel">
    <div class="d-flex flex-wrap gap-3 align-items-center mb-4">
      <div class="btn-group btn-group-sm" role="group">
        <button
          type="button"
          :class="['btn', granularity === 'DAILY' ? 'btn-primary' : 'btn-outline-secondary']"
          @click="setGranularity('DAILY')"
        >
          {{ t('admin.costGranularityDaily') }}
        </button>
        <button
          type="button"
          :class="['btn', granularity === 'MONTHLY' ? 'btn-primary' : 'btn-outline-secondary']"
          @click="setGranularity('MONTHLY')"
        >
          {{ t('admin.costGranularityMonthly') }}
        </button>
      </div>
 
      <div v-if="granularity === 'MONTHLY'" class="d-flex align-items-center gap-2">
        <label for="cost-months-select" class="form-label mb-0 small">
          {{ t('admin.costMonthsLabel') }}
        </label>
        <select
          id="cost-months-select"
          v-model.number="months"
          class="form-select form-select-sm w-auto"
          @change="refetch"
        >
          <option :value="3">3</option>
          <option :value="6">6</option>
          <option :value="12">12</option>
        </select>
      </div>
 
      <div class="d-flex align-items-center gap-2">
        <label for="cost-environment-select" class="form-label mb-0 small">
          {{ t('admin.costEnvironmentLabel') }}
        </label>
        <select
          id="cost-environment-select"
          v-model="environment"
          class="form-select form-select-sm w-auto"
          @change="refetch"
        >
          <option value="">{{ t('admin.costAllEnvironments') }}</option>
          <option v-for="env in availableEnvironments" :key="env" :value="env">
            {{ env }}
          </option>
        </select>
      </div>
    </div>
 
    <ErrorAlert
      v-if="adminStore.costsError"
      :title="t('common.error')"
      :message="adminStore.costsError"
    />
    <LoadingSpinner v-if="adminStore.costsLoading && !adminStore.costs" />
 
    <template v-else-if="adminStore.costs">
      <div class="stat-card mb-4">
        <div class="stat-value">{{ formatCurrency(adminStore.costs.totalCost) }}</div>
        <div class="stat-label">{{ t('admin.totalCost') }}</div>
      </div>
 
      <div class="row g-4">
        <div class="col-12 col-lg-7">
          <h5>{{ t('admin.costTrend') }}</h5>
          <div class="chart-wrapper">
            <Bar :data="costChartData" :options="chartOptions" />
          </div>
        </div>
        <div class="col-12 col-lg-5">
          <h5>{{ t('admin.costByService') }}</h5>
          <table class="table table-sm">
            <tbody>
              <tr v-for="row in serviceBreakdown" :key="row.service">
                <td>{{ row.service }}</td>
                <td class="text-end">{{ formatCurrency(row.amount) }}</td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </template>
  </div>
</template>
 
<script setup lang="ts">
import { computed, onMounted, ref } from 'vue'
import { Bar } from 'vue-chartjs'
import { Chart as ChartJS, CategoryScale, LinearScale, BarElement, Tooltip, Legend } from 'chart.js'
import { useAdminStore } from '@/stores/admin'
import { useUILanguage } from '@/composables/useUILanguage'
import ErrorAlert from '@/components/ErrorAlert.vue'
import LoadingSpinner from '@/components/LoadingSpinner.vue'
 
ChartJS.register(CategoryScale, LinearScale, BarElement, Tooltip, Legend)
 
const adminStore = useAdminStore()
const { t } = useUILanguage()
 
const granularity = ref<'DAILY' | 'MONTHLY'>('DAILY')
const months = ref(6)
const environment = ref('')
 
const availableEnvironments = computed(() => adminStore.costs?.availableEnvironments ?? [])
 
function formatCurrency(value: number): string {
  return new Intl.NumberFormat(undefined, {
    style: 'currency',
    currency: adminStore.costs?.currency ?? 'USD'
  }).format(value)
}
 
const costChartData = computed(() => {
  const series = adminStore.costs?.series ?? []
  return {
    labels: series.map((point) => point.period),
    datasets: [
      {
        label: t('admin.costTrend'),
        data: series.map((point) => point.total),
        backgroundColor: '#689909'
      }
    ]
  }
})
 
const serviceBreakdown = computed(() => {
  const totals: Record<string, number> = {}
  for (const point of adminStore.costs?.series ?? []) {
    for (const entry of point.byService) {
      totals[entry.service] = (totals[entry.service] ?? 0) + entry.amount
    }
  }
  return Object.entries(totals)
    .map(([service, amount]) => ({ service, amount }))
    .sort((a, b) => b.amount - a.amount)
})
 
const chartOptions = {
  responsive: true,
  maintainAspectRatio: false,
  scales: { y: { beginAtZero: true } }
}
 
function setGranularity(value: 'DAILY' | 'MONTHLY') {
  granularity.value = value
  refetch()
}
 
function refetch() {
  adminStore.fetchCostAnalytics({
    granularity: granularity.value,
    months: granularity.value === 'MONTHLY' ? months.value : undefined,
    environment: environment.value || undefined
  })
}
 
onMounted(refetch)
</script>
 
<style scoped>
.stat-card {
  background: var(--bs-body-bg);
  border: 1px solid var(--bs-border-color);
  border-radius: 0.5rem;
  padding: 1rem;
  text-align: center;
}
 
.stat-value {
  font-size: 1.75rem;
  font-weight: 700;
  color: #689909;
}
 
.stat-label {
  font-size: 0.85rem;
  color: var(--bs-secondary-color);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
 
.chart-wrapper {
  height: 260px;
}
</style>