Skip to content

Commit

Permalink
#79 implemented useRoomsStats hook
Browse files Browse the repository at this point in the history
  • Loading branch information
fcamblor committed Mar 30, 2024
1 parent adb6df7 commit 0f9023d
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 5 deletions.
29 changes: 29 additions & 0 deletions mobile/src/models/VoxxrinRoomStats.ts
Original file line number Diff line number Diff line change
@@ -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<RoomStatsBase, { roomId: RoomId }>
export type VoxxrinUnknownRoomStats = VoxxrinRoomStatsBase & UnknownRoomStats;
export type VoxxrinDefinedRoomStats = VoxxrinRoomStatsBase & Replace<DefinedRoomStats, {
valid: Replace<DefinedRoomStats['valid'], {
forTalkId: TalkId
}>
}>
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)
}
}))
}
40 changes: 40 additions & 0 deletions mobile/src/state/useRoomsStats.ts
Original file line number Diff line number Diff line change
@@ -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<EventId|undefined>) {
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<RoomsStats>;
}
13 changes: 8 additions & 5 deletions shared/event-stats.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0f9023d

Please sign in to comment.