refactor(types): public types

move to shared types (#4774)
add types to ape client (#4786)
This commit is contained in:
Miodec 2024-02-07 22:29:16 +01:00
parent 65120db1f0
commit efb972a659
5 changed files with 27 additions and 35 deletions

View file

@ -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<boolean> {
await db.collection<MonkeyTypes.PublicStats>("public").updateOne(
await db.collection<PublicTypingStatsDB>("public").updateOne(
{ _id: "stats" },
{
$inc: {
@ -31,15 +38,15 @@ export async function getSpeedHistogram(
const key = `${language}_${mode}_${mode2}`;
const stats = await db
.collection<MonkeyTypes.PublicSpeedStats>("public")
.collection<PublicSpeedStatsDB>("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<MonkeyTypes.PublicStats> {
export async function getTypingStats(): Promise<PublicTypingStatsDB> {
const stats = await db
.collection<MonkeyTypes.PublicStats>("public")
.collection<PublicTypingStatsDB>("public")
.findOne({ _id: "stats" }, { projection: { _id: 0 } });
if (!stats) {
throw new MonkeyError(

View file

@ -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<string, number>;
}
interface QuoteRating {
_id: string;
average: number;

View file

@ -11,13 +11,15 @@ export default class Public {
this.httpClient = httpClient;
}
async getSpeedHistogram(searchQuery: SpeedStatsQuery): Ape.EndpointResponse {
async getSpeedHistogram(
searchQuery: SpeedStatsQuery
): Ape.EndpointResponse<SharedTypes.SpeedHistogram> {
return await this.httpClient.get(`${BASE_PATH}/speedHistogram`, {
searchQuery,
});
}
async getTypingStats(): Ape.EndpointResponse {
async getTypingStats(): Ape.EndpointResponse<SharedTypes.PublicTypingStats> {
return await this.httpClient.get(`${BASE_PATH}/typingStats`);
}
}

View file

@ -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) {

View file

@ -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;
}
}