diff --git a/mobile/src/models/VoxxrinRoomStats.ts b/mobile/src/models/VoxxrinRoomStats.ts new file mode 100644 index 00000000..bc3014d7 --- /dev/null +++ b/mobile/src/models/VoxxrinRoomStats.ts @@ -0,0 +1,29 @@ +import {Replace} from "../../../shared/type-utils"; +import {DefinedRoomStats, RoomStats, RoomStatsBase, UnknownRoomStats} from "../../../shared/event-stats"; +import {RoomId} from "@/models/VoxxrinRoom"; +import {TalkId} from "@/models/VoxxrinTalk"; +import {match} from "ts-pattern"; + +export type VoxxrinRoomStatsBase = Replace +export type VoxxrinUnknownRoomStats = VoxxrinRoomStatsBase & UnknownRoomStats; +export type VoxxrinDefinedRoomStats = VoxxrinRoomStatsBase & Replace +}> +export type VoxxrinRoomStats = VoxxrinUnknownRoomStats | VoxxrinDefinedRoomStats; + +export function createVoxxrinRoomStatsFromFirestore(firestoreRoomStats: RoomStats): VoxxrinRoomStats { + return match(firestoreRoomStats) + .with({ capacityFillingRatio: 'unknown'}, (unknownRoomStats) => ({ + ...unknownRoomStats, + roomId: new RoomId(unknownRoomStats.roomId) + })).otherwise(definedRoomStats => ({ + ...definedRoomStats, + roomId: new RoomId(definedRoomStats.roomId), + valid: { + ...definedRoomStats.valid, + forTalkId: new TalkId(definedRoomStats.valid.forTalkId) + } + })) +} diff --git a/mobile/src/state/useRoomsStats.ts b/mobile/src/state/useRoomsStats.ts new file mode 100644 index 00000000..1d462f99 --- /dev/null +++ b/mobile/src/state/useRoomsStats.ts @@ -0,0 +1,40 @@ +import {computed, Ref, toValue, unref} from "vue"; +import {EventId} from "@/models/VoxxrinEvent"; +import {PERF_LOGGER} from "@/services/Logger"; +import {deferredVuefireUseDocument} from "@/views/vue-utils"; +import {collection, doc, DocumentReference} from "firebase/firestore"; +import {db} from "@/state/firebase"; +import {RoomsStats} from "../../../shared/event-stats"; +import {unescapeFirebaseKey} from "../../../shared/utilities/firebase.utils"; +import {createVoxxrinRoomStatsFromFirestore} from "@/models/VoxxrinRoomStats"; + + + +export function useRoomsStats(eventIdRef: Ref) { + PERF_LOGGER.debug(() => `useRoomsStats(eventId=${toValue(eventIdRef)?.value})`) + + const firestoreRoomsStatsRef = deferredVuefireUseDocument([eventIdRef], + ([eventId]) => getEventRoomsStatsDoc(eventId)); + + return { + firestoreRoomsStatsRef: computed(() => { + const firestoreRoomsStats = unref(firestoreRoomsStatsRef) + + if(!firestoreRoomsStats) { + return undefined; + } + + return Object.fromEntries(Object.entries(firestoreRoomsStats).map(([escapedRoomId, roomStats]) => { + return [ unescapeFirebaseKey(escapedRoomId), createVoxxrinRoomStatsFromFirestore(roomStats) ] + })) + }) + } +} + +function getEventRoomsStatsDoc(eventId: EventId|undefined) { + if(!eventId || !eventId.value) { + return undefined; + } + + return doc(collection(doc(collection(db, 'events'), eventId.value), 'roomsStats-allInOne'), 'self') as DocumentReference; +} diff --git a/shared/event-stats.d.ts b/shared/event-stats.d.ts index 67228376..16ecf9cf 100644 --- a/shared/event-stats.d.ts +++ b/shared/event-stats.d.ts @@ -5,19 +5,22 @@ export type TalkStats = { totalFavoritesCount: number } -export type RoomStats = { +export type RoomStatsBase = { roomId: string; recordedAt: ISODatetime, persistedAt: ISODatetime, -} & ({ - capacityFillingRatio: 'unknown' -} | { +} + +export type UnknownRoomStats = { capacityFillingRatio: 'unknown' } +export type DefinedRoomStats = { capacityFillingRatio: number, valid: { forTalkId: string, until: ISODatetime } -}) +} + +export type RoomStats = RoomStatsBase & (UnknownRoomStats | DefinedRoomStats) export type RoomsStats = { [encodedRoomId: string]: RoomStats