From efb972a659ef796adfdec1fd5eec18beb6d38876 Mon Sep 17 00:00:00 2001 From: Miodec Date: Wed, 7 Feb 2024 22:29:16 +0100 Subject: [PATCH] refactor(types): public types move to shared types (#4774) add types to ape client (#4786) --- backend/src/dal/public.ts | 15 +++++++++++---- backend/src/types/types.d.ts | 16 ---------------- frontend/src/ts/ape/endpoints/public.ts | 6 ++++-- frontend/src/ts/pages/about.ts | 15 ++------------- shared-types/types.d.ts | 10 ++++++++++ 5 files changed, 27 insertions(+), 35 deletions(-) diff --git a/backend/src/dal/public.ts b/backend/src/dal/public.ts index 6b14688ff..5927da57e 100644 --- a/backend/src/dal/public.ts +++ b/backend/src/dal/public.ts @@ -2,11 +2,18 @@ import * as db from "../init/db"; import { roundTo2 } from "../utils/misc"; import MonkeyError from "../utils/error"; +type PublicTypingStatsDB = SharedTypes.PublicTypingStats & { _id: "stats" }; +type PublicSpeedStatsDB = { + _id: "speedStatsHistogram"; + english_time_15: SharedTypes.SpeedHistogram; + english_time_60: SharedTypes.SpeedHistogram; +}; + export async function updateStats( restartCount: number, time: number ): Promise { - await db.collection("public").updateOne( + await db.collection("public").updateOne( { _id: "stats" }, { $inc: { @@ -31,15 +38,15 @@ export async function getSpeedHistogram( const key = `${language}_${mode}_${mode2}`; const stats = await db - .collection("public") + .collection("public") .findOne({ _id: "speedStatsHistogram" }, { projection: { [key]: 1 } }); return stats?.[key] ?? {}; } /** Get typing stats such as total number of tests completed on site */ -export async function getTypingStats(): Promise { +export async function getTypingStats(): Promise { const stats = await db - .collection("public") + .collection("public") .findOne({ _id: "stats" }, { projection: { _id: 0 } }); if (!stats) { throw new MonkeyError( diff --git a/backend/src/types/types.d.ts b/backend/src/types/types.d.ts index 88531ab10..82ba98c42 100644 --- a/backend/src/types/types.d.ts +++ b/backend/src/types/types.d.ts @@ -198,22 +198,6 @@ declare namespace MonkeyTypes { comment: string; } - interface PublicStats { - _id: "stats"; - testsCompleted: number; - testsStarted: number; - timeTyping: number; - } - - type PublicSpeedStats = PublicSpeedStatsMongoEntry & - PublicSpeedStatsByLanguage; - interface PublicSpeedStatsMongoEntry { - _id: "speedStatsHistogram"; - } - interface PublicSpeedStatsByLanguage { - [language_mode_mode2: string]: Record; - } - interface QuoteRating { _id: string; average: number; diff --git a/frontend/src/ts/ape/endpoints/public.ts b/frontend/src/ts/ape/endpoints/public.ts index e9fcb5f57..b0a574c6d 100644 --- a/frontend/src/ts/ape/endpoints/public.ts +++ b/frontend/src/ts/ape/endpoints/public.ts @@ -11,13 +11,15 @@ export default class Public { this.httpClient = httpClient; } - async getSpeedHistogram(searchQuery: SpeedStatsQuery): Ape.EndpointResponse { + async getSpeedHistogram( + searchQuery: SpeedStatsQuery + ): Ape.EndpointResponse { return await this.httpClient.get(`${BASE_PATH}/speedHistogram`, { searchQuery, }); } - async getTypingStats(): Ape.EndpointResponse { + async getTypingStats(): Ape.EndpointResponse { return await this.httpClient.get(`${BASE_PATH}/typingStats`); } } diff --git a/frontend/src/ts/pages/about.ts b/frontend/src/ts/pages/about.ts index bece0304f..c7de5e6dd 100644 --- a/frontend/src/ts/pages/about.ts +++ b/frontend/src/ts/pages/about.ts @@ -15,19 +15,8 @@ function reset(): void { void ChartController.globalSpeedHistogram.updateColors(); } -interface HistogramData { - [key: string]: number; -} - -interface TypingStatsData { - type: string; - timeTyping: number; - testsCompleted: number; - testsStarted: number; -} - -let speedHistogramResponseData: HistogramData | undefined; -let typingStatsResponseData: TypingStatsData | undefined; +let speedHistogramResponseData: SharedTypes.SpeedHistogram | null; +let typingStatsResponseData: SharedTypes.PublicTypingStats | null; function updateStatsAndHistogram(): void { if (speedHistogramResponseData) { diff --git a/shared-types/types.d.ts b/shared-types/types.d.ts index 5234af4c9..39a8adb4f 100644 --- a/shared-types/types.d.ts +++ b/shared-types/types.d.ts @@ -326,4 +326,14 @@ declare namespace SharedTypes { level?: number; date?: number; } + + interface SpeedHistogram { + [key: string]: number; + } + interface PublicTypingStats { + type: string; + timeTyping: number; + testsCompleted: number; + testsStarted: number; + } }