Skip to content

Commit

Permalink
Share map count logic between worker and frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
needs committed Mar 5, 2024
1 parent 37265d2 commit f04af0a
Show file tree
Hide file tree
Showing 18 changed files with 452 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { notFound } from 'next/navigation';
import prisma from '../../../../../utils/prisma';
import { LayoutTabs } from '../../LayoutTabs';
import { paramsSchema } from './schema';
import { updateMapCounts } from "@teerank/teerank";

export default async function Index({
children,
Expand All @@ -12,7 +13,7 @@ export default async function Index({
}) {
const { gameTypeName, mapName } = paramsSchema.parse(params);

const map = await prisma.map.findUnique({
let map = await prisma.map.findUnique({
where: {
name_gameTypeName: {
name: mapName,
Expand All @@ -25,6 +26,8 @@ export default async function Index({
notFound();
}

map = await updateMapCounts(prisma, map);

return (
<div className="flex flex-col gap-4 py-8">
<LayoutTabs gameTypeName={gameTypeName} mapName={mapName} playerCount={map.playerCount} clanCount={map.clanCount} serverCount={map.gameServerCount} />
Expand Down
6 changes: 5 additions & 1 deletion apps/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@
"name": "frontend",
"version": "0.0.1",
"dependencies": {
"@next/third-parties": "14.1.0",
"@prisma/client": "5.4.2",
"base64url": "3.0.1",
"date-fns": "2.30.0",
"lodash.uniqby": "4.7.0",
"next": "14.0.4",
"react": "18.2.0",
"react-dom": "18.2.0",
"tailwind-merge": "2.0.0",
"tslib": "2.6.2",
"typescript": "5.1.6",
"zod": "3.21.4",
"typescript": "5.1.6"
"sharp": "0.32.6"
},
"scripts": {
"start": "next start"
Expand Down
6 changes: 3 additions & 3 deletions apps/worker/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { reportPerformances, monitorJobPerformance } from "./tasks/reportPerform
import { cancellableWait } from "./utils";
import { removeEmptySnapshots } from "./tasks/removeEmptySnapshots";
import { updateGameTypesCounts } from "./tasks/updateGameTypesCounts";
import { updateMapsCounts } from "./tasks/updateMapsCounts";
import { updateOutdatedMapsCounts } from "./tasks/updateOutdatedMapsCounts";

let stopGracefully = false;
const cancellableWaits = new Set<() => void>();
Expand Down Expand Up @@ -53,8 +53,8 @@ async function main() {
runJob(rankPlayers, 'rankPlayers', 0, 5000),
runJob(updatePlayTimes, 'updatePlayTimes', 0, 5000),
runJob(removeEmptySnapshots, 'removeEmptySnapshots', 0, 60 * 1000 * 5),
runJob(updateGameTypesCounts, 'updateGameTypesCounts', 0, 60 * 1000),
runJob(updateMapsCounts, 'updateMapsCounts', 0, 60 * 1000),
runJob(updateGameTypesCounts, 'updateGameTypesCounts', 0, 5 * 1000),
runJob(updateOutdatedMapsCounts, 'updateOudatedMapCounts', 0, 5 * 1000),
runJob(reportPerformances, 'reportPerformances', 60000, 60000),
]);

Expand Down
13 changes: 13 additions & 0 deletions apps/worker/src/tasks/updateOutdatedMapsCounts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { prisma } from "../prisma";
import { nextMapToCount, updateMapCounts } from "@teerank/teerank"

export async function updateOutdatedMapsCounts() {
const map = await nextMapToCount(prisma);

if (map === null) {
return false;
}

await updateMapCounts(prisma, map);
return true;
}
25 changes: 25 additions & 0 deletions libs/teerank/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/dependency-checks": "error"
}
}
]
}
11 changes: 11 additions & 0 deletions libs/teerank/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# teerank

This library was generated with [Nx](https://nx.dev).

## Building

Run `nx build teerank` to build the library.

## Running unit tests

Run `nx test teerank` to execute the unit tests via [Jest](https://jestjs.io).
11 changes: 11 additions & 0 deletions libs/teerank/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-disable */
export default {
displayName: 'teerank',
preset: '../../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/libs/teerank',
};
10 changes: 10 additions & 0 deletions libs/teerank/package-unused.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@teerank/teerank",
"version": "0.0.1",
"dependencies": {
"tslib": "^2.3.0"
},
"type": "commonjs",
"main": "./src/index.js",
"typings": "./src/index.d.ts"
}
29 changes: 29 additions & 0 deletions libs/teerank/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "teerank",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/teerank/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/libs/teerank",
"main": "libs/teerank/src/index.ts",
"tsConfig": "libs/teerank/tsconfig.lib.json",
"assets": ["libs/teerank/*.md"]
}
},
"lint": {
"executor": "@nx/eslint:lint"
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "libs/teerank/jest.config.ts"
}
}
},
"tags": []
}
1 change: 1 addition & 0 deletions libs/teerank/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/mapCounts';
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Map, PrismaClient } from '@prisma/client'
import { subMinutes } from "date-fns";
import { prisma } from "../prisma";

export async function updateMapsCounts() {
const map = await prisma.map.findFirst({
export async function nextMapToCount(prisma: PrismaClient) {
return await prisma.map.findFirst({
where: {
countedAt: {
lte: new Date(subMinutes(Date.now(), 1)),
Expand All @@ -12,14 +12,16 @@ export async function updateMapsCounts() {
countedAt: 'asc',
},
});
}

if (map === null) {
return false;
export async function updateMapCounts(prisma: PrismaClient, map: Map) {
if (map.countedAt > subMinutes(new Date(), 1)) {
return map;
}

const mapWithCounts = await prisma.map.findUnique({
where: {
id: map.id,
id: map.id
},
select: {
_count: {
Expand All @@ -37,20 +39,20 @@ export async function updateMapsCounts() {
{ lastSnapshot: { isNot: null } },
{
lastSnapshot: {
mapId: map.id,
mapId: map.id
},
},
],
},
});

if (mapWithCounts === null) {
return false;
return map;
}

await prisma.map.update({
return await prisma.map.update({
where: {
id: map.id,
id: map.id
},
data: {
playerCount: mapWithCounts._count.playerInfoMaps,
Expand All @@ -59,6 +61,4 @@ export async function updateMapsCounts() {
countedAt: new Date(),
},
});

return true;
}
22 changes: 22 additions & 0 deletions libs/teerank/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
10 changes: 10 additions & 0 deletions libs/teerank/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"declaration": true,
"types": ["node"]
},
"include": ["src/**/*.ts"],
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"]
}
14 changes: 14 additions & 0 deletions libs/teerank/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
}
5 changes: 5 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
"{workspaceRoot}/eslint.config.js"
],
"cache": true
},
"@nx/js:tsc": {
"cache": true,
"dependsOn": ["^build"],
"inputs": ["production", "^production"]
}
},
"namedInputs": {
Expand Down
Loading

0 comments on commit f04af0a

Please sign in to comment.