All files / src/stores credits.ts

100% Statements 40/40
100% Branches 26/26
100% Functions 3/3
100% Lines 38/38

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                                                                                                      7x 117x 117x   117x 117x 117x 117x     117x 117x 117x               42x 37x 37x 37x     42x 2x     40x 40x 40x 40x 40x 3x     40x 31x   31x 31x   31x 27x   4x   30x 30x   30x   5x 5x 5x     34x         2x     117x                            
import { defineStore } from 'pinia'
import { ref } from 'vue'
import api from '@/services/api'
 
export interface CreditsTransaction {
  transactionId: string
  flowId: string | null
  pool: string
  operation: string
  amount: number
  referenceId: string | null
  note: string | null
  balanceAfter: number | null
  timestamp: string
}
 
/**
 * One (flow, function, via API key) line of a usage day — see flows-api's
 * docs/architecture/USAGE_METERING.md "Usage breakdown". `credits` is the
 * line's unrounded share of the day's debit.
 */
export interface CreditsUsageLine {
  flowId: string | null
  resource: string | null
  operation: string | null
  viaApiKey: boolean
  events: number
  gbSeconds: number
  readUnits: number
  credits: number
  /** The single line summing every line past the 20 busiest */
  other?: boolean
  /** How many lines an `other` line sums */
  count?: number
}
 
export interface CreditsUsageDay {
  events: number
  gbSeconds: number
  readUnits: number
  credits: number
  breakdown: CreditsUsageLine[]
}
 
export interface CreditsSummary {
  days: number
  spent: number
  /** False when `spent` is only a lower bound (too many transactions to read) */
  complete: boolean
}
 
export const useCreditsStore = defineStore('credits', () => {
  const balance = ref(0)
  const balanceLoaded = ref(false)
 
  const transactions = ref<CreditsTransaction[]>([])
  const transactionsNextToken = ref<string | null>(null)
  const transactionsHasMore = ref(true)
  const transactionsLoading = ref(false)
 
  // Only filled by the Credits page's `{ details: true }` fetches
  const summary = ref<CreditsSummary | null>(null)
  const usageDays = ref<Record<string, CreditsUsageDay>>({})
  const flowNames = ref<Record<string, string>>({})
 
  // GET /v1/credits always returns balance + a page of transactions together
  // (see docs/architecture/USAGE_METERING.md) — there's no separate
  // balance-only endpoint, so both the account menu badge and the full Credits
  // page call this same function. `details` (the Credits page only) also asks
  // for the 30-day summary, the usage days' breakdown and the flow names.
  async function fetchCredits(reset = true, { details = false } = {}) {
    if (reset) {
      transactions.value = []
      transactionsNextToken.value = null
      transactionsHasMore.value = true
    }
 
    if (transactionsLoading.value || (!reset && !transactionsHasMore.value)) {
      return
    }
 
    transactionsLoading.value = true
    try {
      const params: Record<string, string | number> = { limit: 30 }
      if (details) params.details = 'true'
      if (!reset && transactionsNextToken.value) {
        params.nextToken = transactionsNextToken.value
      }
 
      const response = await api.get('/v1/credits', { params })
      const data = response.data
 
      balance.value = data.balance
      balanceLoaded.value = true
 
      if (reset) {
        transactions.value = data.transactions.items
      } else {
        transactions.value = [...transactions.value, ...data.transactions.items]
      }
      transactionsNextToken.value = data.transactions.nextToken
      transactionsHasMore.value = data.transactions.hasMore
 
      if (details) {
        // The summary covers the account, not the page: only the first page's counts
        if (data.summary) summary.value = data.summary
        usageDays.value = reset ? { ...data.usageDays } : { ...usageDays.value, ...data.usageDays }
        flowNames.value = reset ? { ...data.flows } : { ...flowNames.value, ...data.flows }
      }
    } finally {
      transactionsLoading.value = false
    }
  }
 
  async function loadMoreTransactions() {
    await fetchCredits(false, { details: true })
  }
 
  return {
    balance,
    balanceLoaded,
    transactions,
    transactionsNextToken,
    transactionsHasMore,
    transactionsLoading,
    summary,
    usageDays,
    flowNames,
    fetchCredits,
    loadMoreTransactions
  }
})