fetch ranking only when needed

This commit is contained in:
HF 2022-07-09 14:49:08 +02:00
parent dc4e57ebaa
commit 35c16b1334
8 changed files with 24 additions and 15 deletions

View File

@ -8,7 +8,6 @@ import fetch from 'isomorphic-fetch'; // TODO put in the beggining with webpack!
import onKeyPress from './controls/keypress'; import onKeyPress from './controls/keypress';
import { import {
fetchMe, fetchMe,
fetchStats,
initTimer, initTimer,
urlChange, urlChange,
receiveOnline, receiveOnline,
@ -118,11 +117,6 @@ function init() {
store.dispatch(fetchMe()); store.dispatch(fetchMe());
SocketClient.connect(); SocketClient.connect();
store.dispatch(fetchStats());
// TODO: We don't have to do this this often
// the client might not even look at it
setInterval(() => { store.dispatch(fetchStats()); }, 300000);
} }
init(); init();

View File

@ -15,6 +15,7 @@ function useInterval(callback, delay) {
savedCallback.current(); savedCallback.current();
} }
if (delay !== null) { if (delay !== null) {
tick();
const id = setInterval(tick, delay); const id = setInterval(tick, delay);
return () => clearInterval(id); return () => clearInterval(id);
} }

View File

@ -4,9 +4,11 @@
*/ */
import React, { Suspense } from 'react'; import React, { Suspense } from 'react';
import { useSelector } from 'react-redux'; import { useSelector, useDispatch } from 'react-redux';
import { t } from 'ttag'; import { t } from 'ttag';
import { fetchStats } from '../../actions';
import useInterval from '../hooks/interval';
import LogInArea from '../LogInArea'; import LogInArea from '../LogInArea';
import Tabs from '../Tabs'; import Tabs from '../Tabs';
import UserAreaContent from '../UserAreaContent'; import UserAreaContent from '../UserAreaContent';
@ -20,6 +22,14 @@ const Modtools = React.lazy(() => import(/* webpackChunkName: "modtools" */ '../
const UserArea = ({ windowId }) => { const UserArea = ({ windowId }) => {
const name = useSelector((state) => state.user.name); const name = useSelector((state) => state.user.name);
const userlvl = useSelector((state) => state.user.userlvl); const userlvl = useSelector((state) => state.user.userlvl);
const lastStatsFetch = useSelector((state) => state.ranks.lastFetch);
const dispatch = useDispatch();
useInterval(() => {
if (Date.now() - 300000 > lastStatsFetch) {
dispatch(fetchStats());
}
}, 300000);
return ( return (
<div style={{ textAlign: 'center' }}> <div style={{ textAlign: 'center' }}>

View File

@ -76,13 +76,11 @@ class RedisCanvas {
j, j,
offset, offset,
) { ) {
/*
* TODO what if chunk does not exist?
*/
if (!RedisCanvas.multi) { if (!RedisCanvas.multi) {
RedisCanvas.multi = client.multi(); RedisCanvas.multi = client.multi();
setTimeout(RedisCanvas.flushPixels, 100); setTimeout(RedisCanvas.flushPixels, 100);
} }
RedisCanvas.multi.addCommand( RedisCanvas.multi.addCommand(
/* /*
* NOTE: * NOTE:
@ -99,6 +97,7 @@ class RedisCanvas {
String(color), String(color),
], ],
); );
RedisCanvas.execChunkChangeCallback(canvasId, [i, j]); RedisCanvas.execChunkChangeCallback(canvasId, [i, j]);
} }

View File

@ -19,7 +19,6 @@ const config = {
storage: localForage, storage: localForage,
blacklist: [ blacklist: [
'user', 'user',
'ranks',
'canvas', 'canvas',
'alert', 'alert',
'chat', 'chat',

View File

@ -1,5 +1,6 @@
const initialState = { const initialState = {
lastFetch: 0,
totalPixels: 0, totalPixels: 0,
dailyTotalPixels: 0, dailyTotalPixels: 0,
ranking: 1488, ranking: 1488,
@ -14,8 +15,8 @@ const initialState = {
online: { online: {
total: 0, total: 0,
}, },
totalRanking: {}, totalRanking: [],
totalDailyRanking: {}, totalDailyRanking: [],
}; };
export default function ranks( export default function ranks(
@ -62,8 +63,10 @@ export default function ranks(
case 'RECEIVE_STATS': { case 'RECEIVE_STATS': {
const { totalRanking, totalDailyRanking } = action; const { totalRanking, totalDailyRanking } = action;
const lastFetch = Date.now();
return { return {
...state, ...state,
lastFetch,
totalRanking, totalRanking,
totalDailyRanking, totalDailyRanking,
}; };

View File

@ -246,7 +246,10 @@ class SocketServer {
// eslint-disable-next-line no-underscore-dangle // eslint-disable-next-line no-underscore-dangle
ws._sender.sendFrame(frames, (err) => { ws._sender.sendFrame(frames, (err) => {
if (err) { if (err) {
logger.error(`WebSocket broadcast error: ${err.message}`); logger.error(
// eslint-disable-next-line max-len
`WebSocket broadcast error on ${ws.user && ws.user.ip} : ${err.message}`,
);
} }
}); });
} }

View File

@ -103,7 +103,7 @@ class ChunkLoader {
const px = getCellInsideChunk(historicalCanvasSize, [x, y]); const px = getCellInsideChunk(historicalCanvasSize, [x, y]);
const curTime = Date.now(); const curTime = Date.now();
if (!historicalTime || historicalTime !== '0000') { if (historicalTime && historicalTime !== '0000') {
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
const incrementialChunkKey = `${historicalDate}${historicalTime}:${cx}:${cy}`; const incrementialChunkKey = `${historicalDate}${historicalTime}:${cx}:${cy}`;
const incrementialChunk = this.chunks.get(incrementialChunkKey); const incrementialChunk = this.chunks.get(incrementialChunkKey);