diff --git a/src/v2/controller/contributions.controller.ts b/src/v2/controller/contributions.controller.ts index aa7b4a6..6a257f2 100644 --- a/src/v2/controller/contributions.controller.ts +++ b/src/v2/controller/contributions.controller.ts @@ -33,7 +33,7 @@ export class ContributionsController extends Controller { * Get the raw collection of contributions */ @Get("raw") - public async getRaw(): Promise { + public async getRaw(): Promise> { return this.service.getRaw(); } diff --git a/src/v2/controller/file.controller.ts b/src/v2/controller/file.controller.ts index a2e8cea..4d4cdb7 100644 --- a/src/v2/controller/file.controller.ts +++ b/src/v2/controller/file.controller.ts @@ -1,5 +1,6 @@ import { Controller, Get, Route, Tags, Security } from "tsoa"; import { FileService } from "../service/file.service"; +import { File } from "../interfaces"; @Route("files") @Tags("Files") @@ -11,7 +12,7 @@ export class FileController extends Controller { */ @Get("raw") @Security("bot") - public async getRaw(): Promise { + public async getRaw(): Promise> { return this.service.getRaw(); } } diff --git a/src/v2/controller/modpacks.controller.ts b/src/v2/controller/modpacks.controller.ts index b72feea..23cd907 100644 --- a/src/v2/controller/modpacks.controller.ts +++ b/src/v2/controller/modpacks.controller.ts @@ -1,6 +1,6 @@ import { Controller, Get, Path, Request, Route, SuccessResponse, Tags } from "tsoa"; import { Request as ExRequest, Response as ExResponse } from "express"; -import { Modpacks } from "../interfaces"; +import { Modpack } from "../interfaces"; import ModpacksService from "../service/modpacks.service"; import cache from "../tools/cache"; @@ -13,7 +13,7 @@ export class ModpacksController extends Controller { * Get the raw collection of mods */ @Get("raw") - public async getRaw(): Promise { + public async getRaw(): Promise> { return this.service.getRaw(); } diff --git a/src/v2/controller/mods.controller.ts b/src/v2/controller/mods.controller.ts index bad08c3..65ed7c4 100644 --- a/src/v2/controller/mods.controller.ts +++ b/src/v2/controller/mods.controller.ts @@ -1,6 +1,6 @@ import { Controller, Get, Path, Request, Route, SuccessResponse, Tags, Response } from "tsoa"; import { Request as ExRequest, Response as ExResponse } from "express"; -import { Mods, PackVersions } from "../interfaces"; +import { Mod, PackVersions } from "../interfaces"; import { NotFoundError } from "../tools/ApiError"; import ModsService from "../service/mods.service"; import cache from "../tools/cache"; @@ -14,7 +14,7 @@ export class ModsController extends Controller { * Get the raw collection of mods */ @Get("raw") - public async getRaw(): Promise { + public async getRaw(): Promise> { return this.service.getRaw(); } diff --git a/src/v2/controller/path.controller.ts b/src/v2/controller/path.controller.ts index 5e2dc24..d000b94 100644 --- a/src/v2/controller/path.controller.ts +++ b/src/v2/controller/path.controller.ts @@ -10,7 +10,7 @@ import { Body, Put, } from "tsoa"; -import { Path, InputPath, Paths, PathNewVersionParam } from "../interfaces"; +import { Path, InputPath, PathNewVersionParam } from "../interfaces"; import PathService from "../service/path.service"; @Route("paths") @@ -22,7 +22,7 @@ export class PathsController extends Controller { * Get the raw collection of paths */ @Get("raw") - public async getRaw(): Promise { + public async getRaw(): Promise> { return this.service.getRaw(); } diff --git a/src/v2/controller/settings.controller.ts b/src/v2/controller/settings.controller.ts index 296b5f1..8fc493e 100644 --- a/src/v2/controller/settings.controller.ts +++ b/src/v2/controller/settings.controller.ts @@ -23,7 +23,7 @@ export class SettingsController extends Controller { */ @SuccessResponse(200) @Get("raw") - public async getRaw(): Promise { + public async getRaw(): Promise> { return this.settingsService.raw(); } diff --git a/src/v2/controller/texture.controller.ts b/src/v2/controller/texture.controller.ts index ec4bdee..a57456f 100644 --- a/src/v2/controller/texture.controller.ts +++ b/src/v2/controller/texture.controller.ts @@ -38,7 +38,7 @@ export class TextureController extends Controller { * Get the raw collection of textures */ @Get("raw") - public async getRaw(): Promise { + public async getRaw(): Promise> { return this.service.getRaw(); } diff --git a/src/v2/controller/user.controller.ts b/src/v2/controller/user.controller.ts index b108455..fc8ea87 100644 --- a/src/v2/controller/user.controller.ts +++ b/src/v2/controller/user.controller.ts @@ -54,7 +54,7 @@ export class UserController extends Controller { * Get the raw collection of users */ @Get("raw") - public async getRaw(): Promise { + public async getRaw(): Promise> { return this.userService.getRaw(); } diff --git a/src/v2/controller/uses.controller.ts b/src/v2/controller/uses.controller.ts index 4f08c1a..13c002e 100644 --- a/src/v2/controller/uses.controller.ts +++ b/src/v2/controller/uses.controller.ts @@ -12,7 +12,7 @@ export class UsesController extends Controller { * @returns {Promise} */ @Get("raw") - public async getRaw(): Promise { + public async getRaw(): Promise> { return this.service.getRaw(); } diff --git a/src/v2/firestorm/textures/index.ts b/src/v2/firestorm/textures/index.ts index dcfc8af..24c7010 100644 --- a/src/v2/firestorm/textures/index.ts +++ b/src/v2/firestorm/textures/index.ts @@ -39,7 +39,7 @@ export const textures = firestorm.collection("textures", (el) => { return settings .read_raw() - .then((settings_file: { [key: string]: any }) => { + .then((settings_file: Record) => { urls = settings_file.repositories.raw[pack]; return el.paths(); }) diff --git a/src/v2/interfaces/files.ts b/src/v2/interfaces/files.ts index 80da5ad..3739d0d 100644 --- a/src/v2/interfaces/files.ts +++ b/src/v2/interfaces/files.ts @@ -30,5 +30,5 @@ export interface FileRepository { removeFilesByParent(parent: FileParent): Promise; removeFilesByParentAndUse(parent: FileParent, use: FileUse): Promise; removeFileByPath(path: string): Promise; - getRaw(): Promise; + getRaw(): Promise>; } diff --git a/src/v2/interfaces/paths.ts b/src/v2/interfaces/paths.ts index 5a4e4b6..aa175c1 100644 --- a/src/v2/interfaces/paths.ts +++ b/src/v2/interfaces/paths.ts @@ -26,7 +26,7 @@ export interface PathRepository { modifyVersion(old_version: string, new_version: string): void | PromiseLike; removePathById(path_id: string): Promise; removePathsByBulk(path_ids: string[]): Promise; - getRaw(): Promise; + getRaw(): Promise>; } export interface PathNewVersionParam { diff --git a/src/v2/interfaces/settings.ts b/src/v2/interfaces/settings.ts index 61dd592..aa92893 100644 --- a/src/v2/interfaces/settings.ts +++ b/src/v2/interfaces/settings.ts @@ -1,4 +1,4 @@ export interface SettingsRepository { - getRaw(): Promise; + getRaw(): Promise>; update(body: any): Promise; } diff --git a/src/v2/interfaces/textures.ts b/src/v2/interfaces/textures.ts index 7829cdc..3ed16d2 100644 --- a/src/v2/interfaces/textures.ts +++ b/src/v2/interfaces/textures.ts @@ -48,7 +48,7 @@ export type TextureProperty = "uses" | "paths" | "contributions" | "mcmeta" | "a export interface TextureRepository { changeTexture(id: string, body: TextureCreationParam): Promise; - getRaw(): Promise; + getRaw(): Promise>; getByNameIdAndTag( tag: string | undefined, search: string | undefined, diff --git a/src/v2/interfaces/users.ts b/src/v2/interfaces/users.ts index 464d418..9e7eb24 100644 --- a/src/v2/interfaces/users.ts +++ b/src/v2/interfaces/users.ts @@ -60,7 +60,7 @@ export interface UserRepository { getProfileOrCreate(id: string): User | PromiseLike; getUserProfiles(authors: string[]): Promise; getNameById(id: string): Promise; - getRaw(): Promise; + getRaw(): Promise>; getNames(): Promise; getUserById(id: string): Promise; getUsersByName(name: string): Promise; diff --git a/src/v2/interfaces/uses.ts b/src/v2/interfaces/uses.ts index 533bb04..550477f 100644 --- a/src/v2/interfaces/uses.ts +++ b/src/v2/interfaces/uses.ts @@ -22,7 +22,7 @@ export interface Uses extends Array {} export interface UseRepository { getUsesByIdAndEdition(id_arr: number[], edition: string): Promise; getUsesByEdition(edition: string): Promise; - getRaw(): Promise; + getRaw(): Promise>; getUseByIdOrName(id_or_name: string): Promise; deleteUse(id: string): Promise; set(use: Use): Promise; diff --git a/src/v2/repository/firestorm/files.repository.ts b/src/v2/repository/firestorm/files.repository.ts index 3f1250e..ae04afc 100644 --- a/src/v2/repository/firestorm/files.repository.ts +++ b/src/v2/repository/firestorm/files.repository.ts @@ -72,7 +72,7 @@ export class FilesFirestormRepository implements FileRepository { return firestorm.files.delete(path).then(() => {}); } - getRaw(): Promise { + getRaw(): Promise> { return files.read_raw(); } } diff --git a/src/v2/repository/firestorm/path.repository.ts b/src/v2/repository/firestorm/path.repository.ts index 5e79b07..3d2d927 100644 --- a/src/v2/repository/firestorm/path.repository.ts +++ b/src/v2/repository/firestorm/path.repository.ts @@ -88,7 +88,7 @@ export default class PathFirestormRepository implements PathRepository { .then(() => {}); } - getRaw() { + getRaw(): Promise> { return paths.read_raw(); } } diff --git a/src/v2/repository/firestorm/settings.repository.ts b/src/v2/repository/firestorm/settings.repository.ts index 42883eb..faf4198 100644 --- a/src/v2/repository/firestorm/settings.repository.ts +++ b/src/v2/repository/firestorm/settings.repository.ts @@ -2,7 +2,7 @@ import { settings } from "../../firestorm/index"; import { SettingsRepository } from "../../interfaces"; export default class SettingsFirestormRepository implements SettingsRepository { - getRaw(): Promise { + getRaw(): Promise> { return settings.read_raw(); } diff --git a/src/v2/repository/firestorm/texture.repository.ts b/src/v2/repository/firestorm/texture.repository.ts index 8a11076..4ab3993 100644 --- a/src/v2/repository/firestorm/texture.repository.ts +++ b/src/v2/repository/firestorm/texture.repository.ts @@ -27,7 +27,7 @@ export default class TextureFirestormRepository implements TextureRepository { ): Promise { // * none, read raw if (tag === undefined && search === undefined) { - return this.getRaw(); + return this.getRaw().then((res: any) => Object.values(res)); } // * number id: get + includes tag? @@ -75,8 +75,8 @@ export default class TextureFirestormRepository implements TextureRepository { return textures.search(criterias); } - public getRaw() { - return textures.read_raw().then((res: any) => Object.values(res)); + public getRaw(): Promise> { + return textures.read_raw(); } public getURLById(id: number, pack: KnownPacks, version: string) { diff --git a/src/v2/repository/firestorm/use.repository.ts b/src/v2/repository/firestorm/use.repository.ts index 74f44ae..e8c9e9b 100644 --- a/src/v2/repository/firestorm/use.repository.ts +++ b/src/v2/repository/firestorm/use.repository.ts @@ -29,8 +29,8 @@ export default class UseFirestormRepository implements UseRepository { ]); } - getRaw(): Promise { - return uses.read_raw().then((res: any) => Object.values(res)); + getRaw(): Promise> { + return uses.read_raw(); } getUseByIdOrName(id_or_name: string): Promise { diff --git a/src/v2/repository/firestorm/user.repository.ts b/src/v2/repository/firestorm/user.repository.ts index 3024d3d..476def4 100644 --- a/src/v2/repository/firestorm/user.repository.ts +++ b/src/v2/repository/firestorm/user.repository.ts @@ -13,17 +13,15 @@ import { } from "../../interfaces"; // eslint-disable-next-line no-underscore-dangle -function __transformUser(user: any): User { +const __transformUser = (user: any): User => ({ // falsy checking and remove warns field (unused) - return { - id: user.id, - username: user.username || "", - uuid: user.uuid || "", - roles: user.roles || [], - media: user.media, - anonymous: user.anonymous || false, - }; -} + id: user.id, + username: user.username || "", + uuid: user.uuid || "", + roles: user.roles || [], + media: user.media, + anonymous: user.anonymous || false, +}); export default class UserFirestormRepository implements UserRepository { getNameById(id: string): Promise { @@ -34,11 +32,15 @@ export default class UserFirestormRepository implements UserRepository { })); } - getRaw(): Promise { - return users - .read_raw() - .then((res: any) => Object.values(res)) - .then((arr: Array) => arr.map((el) => __transformUser(el))); + getRaw(): Promise> { + return ( + users + .read_raw() + .then((res: Record) => Object.entries(res)) + // convert to entries to map, convert back to object after mapping done + .then((arr: [string, User][]) => arr.map(([key, el]) => [key, __transformUser(el)])) + .then((arr: [string, User][]) => Object.fromEntries(arr)) + ); } getNames(): Promise { diff --git a/src/v2/service/contributions.service.ts b/src/v2/service/contributions.service.ts index c3a1e03..d256c24 100644 --- a/src/v2/service/contributions.service.ts +++ b/src/v2/service/contributions.service.ts @@ -20,80 +20,82 @@ export default class ContributionService { private readonly textureService: TextureService = new TextureService(); - getRaw(): Promise { - return contributions.read_raw().then((res: any) => Object.values(res)); + getRaw(): Promise> { + return contributions.read_raw(); } getStats(): Promise { - return this.getRaw().then((cs) => { - let total_authors = 0; - let total_contributions = 0; - - const authors = {}; - - let total_last_week = 0; - let total_last_month = 0; - let total_last_day = 0; - - const last_month = startOfDay(lastMonth()).getTime(); - const last_week = startOfDay(lastWeek()).getTime(); - const last_day = startOfDay(lastDay()).getTime(); - - const aggregate: PackRecord = {} as PackRecord; - - cs.forEach((cur) => { - total_contributions += 1; - - cur.authors.forEach((a) => { - if (!authors[a]) { - authors[a] = true; - total_authors++; + return this.getRaw() + .then((res) => Object.values(res)) + .then((cs) => { + let total_authors = 0; + let total_contributions = 0; + + const authors = {}; + + let total_last_week = 0; + let total_last_month = 0; + let total_last_day = 0; + + const last_month = startOfDay(lastMonth()).getTime(); + const last_week = startOfDay(lastWeek()).getTime(); + const last_day = startOfDay(lastDay()).getTime(); + + const aggregate: PackRecord = {} as PackRecord; + + cs.forEach((cur) => { + total_contributions += 1; + + cur.authors.forEach((a) => { + if (!authors[a]) { + authors[a] = true; + total_authors++; + } + }); + + const { pack, date: timestamp } = cur; + //! Group data by the start of date if time dont coincide + const start_of_day = startOfDay(timestamp).getTime(); + + aggregate[pack] ||= {}; + aggregate[pack][start_of_day] ||= { + date: start_of_day, + count: 0, + }; + aggregate[pack][start_of_day].count++; + + if (timestamp >= last_week) { + total_last_week += 1; + } + if (timestamp >= last_month) { + total_last_month += 1; + } + if (timestamp >= last_day) { + total_last_day += 1; } }); - const { pack, date: timestamp } = cur; - //! Group data by the start of date if time dont coincide - const start_of_day = startOfDay(timestamp).getTime(); - - aggregate[pack] ||= {}; - aggregate[pack][start_of_day] ||= { - date: start_of_day, - count: 0, - }; - aggregate[pack][start_of_day].count++; - - if (timestamp >= last_week) { - total_last_week += 1; - } - if (timestamp >= last_month) { - total_last_month += 1; - } - if (timestamp >= last_day) { - total_last_day += 1; - } - }); + const final_activity = {} as PackData; + const percentiles = {} as PackPercentile; + Object.entries(aggregate).forEach(([pack, pack_aggregate]) => { + final_activity[pack] = Object.values(pack_aggregate); - const final_activity = {} as PackData; - const percentiles = {} as PackPercentile; - Object.entries(aggregate).forEach(([pack, pack_aggregate]) => { - final_activity[pack] = Object.values(pack_aggregate); + const counts = Object.values(pack_aggregate) + .map((e) => e.count) // ? No need to filter 0 as the contruction of the record makes it impossible + .sort(); + percentiles[pack] = counts[Math.round((counts.length * 95) / 100)]; + }); - const counts = Object.values(pack_aggregate) - .map((e) => e.count) // ? No need to filter 0 as the contruction of the record makes it impossible - .sort(); - percentiles[pack] = counts[Math.round((counts.length * 95) / 100)]; + return { + total_authors, + total_contributions, + total_last_day, + total_last_week, + total_last_month, + activity: final_activity, + percentiles, + }; }); - - return { - total_authors, - total_contributions, - total_last_day, - total_last_week, - total_last_month, - activity: final_activity, - percentiles, - }; - }); } getPacks(): ContributionsPacks { diff --git a/src/v2/service/modpacks.service.ts b/src/v2/service/modpacks.service.ts index b64f3d6..2e0dea0 100644 --- a/src/v2/service/modpacks.service.ts +++ b/src/v2/service/modpacks.service.ts @@ -1,11 +1,11 @@ -import { Modpacks, ModpacksRepository } from "~/v2/interfaces"; +import { Modpack, ModpacksRepository } from "~/v2/interfaces"; import { modpacks } from "../firestorm"; import ModpacksFirestormRepository from "../repository/firestorm/modpacks.repository"; export default class ModpacksService { private readonly modsRepo: ModpacksRepository = new ModpacksFirestormRepository(); - getRaw(): Promise { + getRaw(): Promise> { return modpacks.read_raw(); } diff --git a/src/v2/service/mods.service.ts b/src/v2/service/mods.service.ts index 454fac7..e55aceb 100644 --- a/src/v2/service/mods.service.ts +++ b/src/v2/service/mods.service.ts @@ -1,4 +1,4 @@ -import { Mods, ModsRepository, PackVersions } from "~/v2/interfaces"; +import { Mod, ModsRepository, PackVersions } from "~/v2/interfaces"; import { mods } from "../firestorm"; import { pack_versions } from "../firestorm/modding/pack_versions"; import ModsFirestormRepository from "../repository/firestorm/mods.repository"; @@ -6,7 +6,7 @@ import ModsFirestormRepository from "../repository/firestorm/mods.repository"; export default class ModsService { private readonly modsRepo: ModsRepository = new ModsFirestormRepository(); - getRaw(): Promise { + getRaw(): Promise> { return mods.read_raw(); } diff --git a/src/v2/service/path.service.ts b/src/v2/service/path.service.ts index 87a9f59..20ccac7 100644 --- a/src/v2/service/path.service.ts +++ b/src/v2/service/path.service.ts @@ -19,8 +19,8 @@ export default class PathService { private readonly repository: PathRepository = new PathFirestormRepository(); - getRaw(): Promise { - return this.repository.getRaw().then((res: any) => Object.values(res)); + getRaw(): Promise> { + return this.repository.getRaw(); } getPathByUseId(use_id: string): Promise { diff --git a/src/v2/service/settings.service.ts b/src/v2/service/settings.service.ts index 0dc8aa7..86f6ac9 100644 --- a/src/v2/service/settings.service.ts +++ b/src/v2/service/settings.service.ts @@ -4,7 +4,7 @@ import SettingsFirestormRepository from "../repository/firestorm/settings.reposi export class SettingsService { private readonly settingsRepository: SettingsRepository = new SettingsFirestormRepository(); - raw(): Promise { + raw(): Promise> { return this.settingsRepository.getRaw(); } diff --git a/src/v2/service/texture.service.ts b/src/v2/service/texture.service.ts index c273b51..7f05fe5 100644 --- a/src/v2/service/texture.service.ts +++ b/src/v2/service/texture.service.ts @@ -30,7 +30,7 @@ export default class TextureService { TextureService.instance = this; } - getRaw(): Promise { + getRaw(): Promise> { return this.textureRepo.getRaw(); } diff --git a/src/v2/service/use.service.ts b/src/v2/service/use.service.ts index 27407da..fb59dc2 100644 --- a/src/v2/service/use.service.ts +++ b/src/v2/service/use.service.ts @@ -24,7 +24,7 @@ export default class UseService { ); } - getRaw(): Promise { + getRaw(): Promise> { return this.useRepo.getRaw(); } diff --git a/src/v2/service/user.service.ts b/src/v2/service/user.service.ts index 33c47b0..b9c3a1f 100644 --- a/src/v2/service/user.service.ts +++ b/src/v2/service/user.service.ts @@ -15,38 +15,40 @@ import { BadRequestError } from "../tools/ApiError"; export class UserService { private repository: UserRepository = new UserFirestormRepository(); - public getRaw(): Promise { + public getRaw(): Promise> { return this.repository.getRaw(); } public getStats(): Promise { - return this.getRaw().then((users) => { - const all_roles = [] as string[]; - return users.reduce( - (acc, user) => { - acc.total++; - if (user.anonymous) acc.total_anonymous++; - - user.roles.forEach((role) => { - if (!all_roles.includes(role)) { - all_roles.push(role); - acc.total_roles++; - - acc.total_per_roles[role] = 0; - } - acc.total_per_roles[role]++; - }); - - return acc; - }, - { - total: 0, - total_anonymous: 0, - total_roles: 0, - total_per_roles: {}, - } as UserStats, - ); - }); + return this.getRaw() + .then((raw) => Object.values(raw)) + .then((users) => { + const all_roles = [] as string[]; + return users.reduce( + (acc, user) => { + acc.total++; + if (user.anonymous) acc.total_anonymous++; + + user.roles.forEach((role) => { + if (!all_roles.includes(role)) { + all_roles.push(role); + acc.total_roles++; + + acc.total_per_roles[role] = 0; + } + acc.total_per_roles[role]++; + }); + + return acc; + }, + { + total: 0, + total_anonymous: 0, + total_roles: 0, + total_per_roles: {}, + } as UserStats, + ); + }); } public getNames(): Promise {