product performance grouped kpi improvements

This commit is contained in:
M_Kececi
2026-07-03 01:53:19 +03:00
parent be6feb8aa8
commit 98e3587608
6 changed files with 794 additions and 126 deletions
+47 -9
View File
@@ -37,9 +37,10 @@ function resolveProductImageUrl (item) {
return fileName ? `/uploads/image/${fileName}` : ''
}
function sortedList (value) {
function normalizedList (value, sort = false) {
if (!Array.isArray(value)) return []
return value.map(x => String(x || '').trim()).filter(Boolean).sort()
const out = value.map(x => String(x || '').trim()).filter(Boolean)
return sort ? out.sort() : out
}
export const useProductPerformanceStore = defineStore('product-performance-store', {
@@ -64,8 +65,8 @@ export const useProductPerformanceStore = defineStore('product-performance-store
groupedCacheKey (params = {}) {
return JSON.stringify({
mode: String(params.mode || ''),
groupLevels: sortedList(params.groupLevels),
expandedKeys: sortedList(params.expandedKeys),
groupLevels: normalizedList(params.groupLevels),
expandedKeys: normalizedList(params.expandedKeys, true),
limit: Number(params.limit || 0)
})
},
@@ -75,15 +76,40 @@ export const useProductPerformanceStore = defineStore('product-performance-store
const now = Date.now()
const loadedAt = Number(this.groupedLoadedAtByKey[key] || 0)
const cached = this.groupedRowsByKey[key]
if (!options.force && Array.isArray(cached) && now - loadedAt < GROUPED_TTL_MS) return cached
if (this.groupedInFlightByKey[key]) return this.groupedInFlightByKey[key]
const logPrefix = '[ProductPerformance][grouped]'
if (!options.force && Array.isArray(cached) && now - loadedAt < GROUPED_TTL_MS) {
console.info(`${logPrefix} cache hit`, {
mode: params.mode,
rows: cached.length,
ageMs: now - loadedAt
})
return cached
}
if (this.groupedInFlightByKey[key]) {
console.info(`${logPrefix} in-flight reuse`, {
mode: params.mode,
groupLevels: normalizedList(params.groupLevels),
expandedKeys: normalizedList(params.expandedKeys, true).length
})
return this.groupedInFlightByKey[key]
}
this.groupedLoadingByKey = { ...this.groupedLoadingByKey, [key]: true }
const request = api.get('/pricing/product-performance/grouped', {
const startedAt = performance.now()
console.info(`${logPrefix} request start`, {
mode: params.mode,
groupLevels: normalizedList(params.groupLevels),
expandedKeys: normalizedList(params.expandedKeys, true).length,
limit: params.limit
})
const request = api.post('/pricing/product-performance/grouped', {
mode: params.mode,
group_levels: Array.isArray(params.groupLevels) ? params.groupLevels : String(params.groupLevels || '').split(',').filter(Boolean),
expanded_keys: Array.isArray(params.expandedKeys) ? params.expandedKeys : String(params.expandedKeys || '').split(',').filter(Boolean),
limit: params.limit
}, {
params: {
mode: params.mode,
group_levels: Array.isArray(params.groupLevels) ? params.groupLevels.join(',') : params.groupLevels,
expanded_keys: Array.isArray(params.expandedKeys) ? params.expandedKeys.join(',') : params.expandedKeys,
limit: params.limit
},
timeout: 90000
@@ -91,7 +117,19 @@ export const useProductPerformanceStore = defineStore('product-performance-store
const rows = Array.isArray(resp?.data) ? resp.data : []
this.groupedRowsByKey = { ...this.groupedRowsByKey, [key]: rows }
this.groupedLoadedAtByKey = { ...this.groupedLoadedAtByKey, [key]: Date.now() }
console.info(`${logPrefix} request done`, {
mode: params.mode,
rows: rows.length,
elapsedSec: Number(((performance.now() - startedAt) / 1000).toFixed(2))
})
return rows
}).catch(err => {
console.warn(`${logPrefix} request failed`, {
mode: params.mode,
elapsedSec: Number(((performance.now() - startedAt) / 1000).toFixed(2)),
message: err?.response?.data || err?.message || err
})
throw err
}).finally(() => {
const nextLoading = { ...this.groupedLoadingByKey }
const nextInFlight = { ...this.groupedInFlightByKey }