From eef9e87d24a80ac6fbf8e7b60356c0ebdf5ad4ee Mon Sep 17 00:00:00 2001 From: Patrick Winfield Date: Wed, 28 Feb 2024 16:21:07 -0700 Subject: [PATCH] B06021 usg stats pr fixes (#90) * refactor dashboard stats * refactor all happ stats * refactor hApp stats * bug clearing happ stats on error * move stats logic from holo store to stats store --- src/services/servicelogApi.js | 100 +++++++++++++--------------------- src/stores/useHoloStore.js | 39 +++---------- 2 files changed, 45 insertions(+), 94 deletions(-) diff --git a/src/services/servicelogApi.js b/src/services/servicelogApi.js index f624c1e..933ed84 100644 --- a/src/services/servicelogApi.js +++ b/src/services/servicelogApi.js @@ -44,74 +44,50 @@ export async function hAppServiceLogs(payload, signature, pubkey, environment, s } export async function hAppStats(payload, signature, pubkey, environment, serviceLogPort) { - try { - const result = await serviceLogApiCall({ - params: { happ_id: payload.payload.happ_id, days: payload.payload.days }, - endpoint: 'stats/happ', - }, - signature, - payload.nonce, - payload.timestamp, - pubkey, - environment, - serviceLogPort - ) + const result = await serviceLogApiCall({ + params: { happ_id: payload.payload.happ_id, days: payload.payload.days }, + endpoint: 'stats/happ', + }, + signature, + payload.nonce, + payload.timestamp, + pubkey, + environment, + serviceLogPort + ) - return result.data - } catch (e) { - if (axios.isAxiosError(e)) { - return e.message - } else { - return 'unknown error' - } - } + return result.data } export async function allHappStats(payload, signature, pubkey, environment, serviceLogPort) { - try { - const happIds = JSON.parse(payload.payload.happ_ids) - const result = await serviceLogApiCall({ - params: { happ_ids: happIds, days: payload.payload.days }, - endpoint: 'stats/happs', - }, - signature, - payload.nonce, - payload.timestamp, - pubkey, - environment, - serviceLogPort - ) + const happIds = JSON.parse(payload.payload.happ_ids) + const result = await serviceLogApiCall({ + params: { happ_ids: happIds, days: payload.payload.days }, + endpoint: 'stats/happs', + }, + signature, + payload.nonce, + payload.timestamp, + pubkey, + environment, + serviceLogPort + ) - return result.data - } catch (e) { - if (axios.isAxiosError(e)) { - return e.message - } else { - return 'unknown error' - } - } + return result.data } export async function dashboardStats(payload, signature, pubkey, environment, serviceLogPort) { - try { - const result = await serviceLogApiCall({ - params: { days: payload.payload.days }, - endpoint: 'stats/dashboard', - }, - signature, - payload.nonce, - payload.timestamp, - pubkey, - environment, - serviceLogPort - ) + const result = await serviceLogApiCall({ + params: { days: payload.payload.days }, + endpoint: 'stats/dashboard', + }, + signature, + payload.nonce, + payload.timestamp, + pubkey, + environment, + serviceLogPort + ) - return result.data - } catch (e) { - if (axios.isAxiosError(e)) { - return e.message - } else { - return 'unknown error' - } - } -} + return result.data +} \ No newline at end of file diff --git a/src/stores/useHoloStore.js b/src/stores/useHoloStore.js index 4c1af96..09e4666 100644 --- a/src/stores/useHoloStore.js +++ b/src/stores/useHoloStore.js @@ -5,6 +5,7 @@ import useSignalStore from './useSignalStore' import { fetchAgentKycLevel } from '../services/hbs' import { hAppServiceLogs, hAppStats, dashboardStats, allHappStats } from '../services/servicelogApi' import { generateServiceLogPayload } from '../utils/serviceLogPayload' +import { emptyHappStatistics } from 'src/utils/hAppStatistics' const msgpack = require('@msgpack/msgpack') @@ -19,7 +20,9 @@ const makeUseHoloStore = ({ connectionArgs, MockWebSdk }) => defineStore('holo', // These two values are subscribed to by clientStore isReady: false, appInfo: null, - kycLevel: null + kycLevel: null, + dashboardStatistics: emptyHappStatistics, + allHappStatistics: [] }), getters: { isAnonymous: state => state.agentState && state.agentState.isAnonymous, @@ -118,37 +121,9 @@ const makeUseHoloStore = ({ connectionArgs, MockWebSdk }) => defineStore('holo', this.kycLevel = kycLevel return kycLevel }, - async fetchHAppServiceLogs(happId, environment, serviceLogPort) { - const payload = generateServiceLogPayload({ "happ_id": happId }) - const { _, signature } = await client.signPayload(payload) - const encoded_service_logs = await hAppServiceLogs(payload, signature, this.agentKey, environment, serviceLogPort) - - return encoded_service_logs - }, - async fetchHAppStats(happId, days, environment, serviceLogPort) { - const payload = generateServiceLogPayload({ "days": days.toString(), "happ_id": happId }) - const { _, signature } = await client.signPayload(payload) - const hAppStatistics = await hAppStats(payload, signature, this.agentKey, environment, serviceLogPort) - - return hAppStatistics - }, - async fetchAllHAppStats(happIds, days, environment, serviceLogPort) { - // NB: The happ_ids param needs to be a string to succeed the msgpack serialization - // and validation check in the server. - const happIdsString = JSON.stringify(happIds) - const payload = generateServiceLogPayload({ "days": days.toString(), "happ_ids": happIdsString }) - const { _, signature } = await client.signPayload(payload) - const hAppStatistics = await allHappStats(payload, signature, this.agentKey, environment, serviceLogPort) - - return hAppStatistics - }, - async fetchDashboardStats(days, environment, serviceLogPort) { - const payload = generateServiceLogPayload({ "days": days.toString() }) - const { _, signature } = await client.signPayload(payload) - const dashboardStatistics = await dashboardStats(payload, signature, this.agentKey, environment, serviceLogPort) - - return dashboardStatistics - }, + async signPayload(payload) { + return client.signPayload(payload) + } } })