Skip to content

Commit

Permalink
B06021 usg stats pr fixes (#90)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Paterick authored Feb 28, 2024
1 parent 41875b6 commit eef9e87
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 94 deletions.
100 changes: 38 additions & 62 deletions src/services/servicelogApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
39 changes: 7 additions & 32 deletions src/stores/useHoloStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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,
Expand Down Expand Up @@ -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)
}
}
})

Expand Down

0 comments on commit eef9e87

Please sign in to comment.