forked from ppfun/pixelplanet
handle various cases of day change ordering on storing hourly country stats
This commit is contained in:
parent
c26ad00064
commit
d46f71e625
|
@ -33,7 +33,9 @@ const App = () => (
|
||||||
<WindowManager />
|
<WindowManager />
|
||||||
</IconContext.Provider>
|
</IconContext.Provider>
|
||||||
<a
|
<a
|
||||||
style={{ position: 'absolute', bottom: 4, left: '50%', marginLeft: -27 }}
|
style={{
|
||||||
|
position: 'absolute', bottom: 4, left: '50%', marginLeft: -27,
|
||||||
|
}}
|
||||||
href="https://www.bitchute.com/video/bOOoNdPdjWmt/"
|
href="https://www.bitchute.com/video/bOOoNdPdjWmt/"
|
||||||
>
|
>
|
||||||
<img style={{ width: 75 }} src="2024.gif" />
|
<img style={{ width: 75 }} src="2024.gif" />
|
||||||
|
|
|
@ -49,7 +49,7 @@ const UserArea = () => {
|
||||||
dispatch(fetchStats());
|
dispatch(fetchStats());
|
||||||
}
|
}
|
||||||
}, 300000);
|
}, 300000);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div style={{ textAlign: 'center' }}>
|
<div style={{ textAlign: 'center' }}>
|
||||||
<Tabs activeTab={activeTab} setActiveTab={setActiveTab}>
|
<Tabs activeTab={activeTab} setActiveTab={setActiveTab}>
|
||||||
|
|
|
@ -139,21 +139,82 @@ export async function storeHourlyCountryStats(start, amount) {
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (prevTs && prevTs > tsNow - 1000 * 3600 * 1.5) {
|
if (prevTs && prevTs > tsNow - 1000 * 3600 * 1.5) {
|
||||||
const curData = await client.zRangeWithScores(
|
const dataPromises = [
|
||||||
DAILY_CRANKED_KEY, start, start + amount, {
|
client.zRangeWithScores(
|
||||||
REV: true,
|
DAILY_CRANKED_KEY, start, start + amount, {
|
||||||
});
|
REV: true,
|
||||||
|
}),
|
||||||
|
];
|
||||||
|
|
||||||
|
/*
|
||||||
|
* when the day changed, we can not be sure if the
|
||||||
|
* PREV_DAILY_CRANKED_KEY already reset or not.
|
||||||
|
* Therefor we get the previous day history, which might already
|
||||||
|
* exist, and add it to the curent data if it makes sense
|
||||||
|
*/
|
||||||
|
const curDay = new Date().getDate();
|
||||||
|
const prevDay = new Date(prevTs).getDate();
|
||||||
|
if (curDay !== prevDay) {
|
||||||
|
const dateKey = getDateKeyOfTs(tsNow - 1000 * 3600 * 24);
|
||||||
|
dataPromises.push(client.zRangeWithScores(
|
||||||
|
`${CDAY_STATS_RANKS_KEY}:${dateKey}`, start, start + amount, {
|
||||||
|
REV: true,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
dataPromises.push(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
const [curData, addData] = await Promise.all(dataPromises);
|
||||||
|
|
||||||
const prevRanks = new Map();
|
const prevRanks = new Map();
|
||||||
prevData.forEach(({ value, score }) => prevRanks.set(value, score));
|
prevData.forEach(({ value, score }) => prevRanks.set(value, score));
|
||||||
|
|
||||||
|
const addRanks = new Map();
|
||||||
|
if (addData) {
|
||||||
|
addData.forEach(({ value, score }) => addRanks.set(value, score));
|
||||||
|
}
|
||||||
|
|
||||||
const addArr = [];
|
const addArr = [];
|
||||||
curData.forEach(({ value: cc, score: curPx }) => {
|
curData.forEach(({ value: cc, score: curPx }) => {
|
||||||
const prevPx = prevRanks.get(cc) || 0;
|
const prevPx = prevRanks.get(cc) || 0;
|
||||||
const px = (curPx > prevPx) ? curPx - prevPx : curPx;
|
const addPx = addRanks.get(cc) || 0;
|
||||||
|
let px;
|
||||||
|
if (curPx >= prevPx) {
|
||||||
|
px = curPx - prevPx;
|
||||||
|
} else if (addPx) {
|
||||||
|
px = curPx + addPx - prevPx;
|
||||||
|
} else {
|
||||||
|
px = curPx;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (addPx) {
|
||||||
|
addRanks.delete(cc);
|
||||||
|
}
|
||||||
|
|
||||||
addArr.push({
|
addArr.push({
|
||||||
value: cc,
|
value: cc,
|
||||||
score: px,
|
score: px,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
|
* after a day change and reset occured, many countries
|
||||||
|
* will not be present in curData, cause they didn't place
|
||||||
|
* a pixel yet.
|
||||||
|
* Therefore we check the previous day data that is left.
|
||||||
|
*/
|
||||||
|
addRanks.forEach((px, cc) => {
|
||||||
|
const prevPx = prevRanks.get(cc) || 0;
|
||||||
|
if (px <= prevPx) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
addArr.push({
|
||||||
|
value: cc,
|
||||||
|
score: px - prevPx,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
if (addArr.length) {
|
if (addArr.length) {
|
||||||
await client.zAdd(HOURLY_CRANKED_KEY, addArr);
|
await client.zAdd(HOURLY_CRANKED_KEY, addArr);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user