Skip to content

Commit 234ff50

Browse files
committed
change new pending_claim_reward reader
1 parent e412539 commit 234ff50

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

src/db/cache/index.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,71 @@ function* clearRecordCache2(model, conditions = {}) {
302302
}
303303
}
304304

305+
function* getPendingClaimReward(model, username) {
306+
if (!model) {
307+
return null;
308+
}
309+
if (!username) {
310+
return null;
311+
}
312+
313+
const keyPrefix = model.getCachePrefix();
314+
const cacheKey = `${keyPrefix}${username}`;
315+
316+
let vestsTotal = 0,
317+
t1,
318+
t2,
319+
t3;
320+
try {
321+
t1 = process.uptime();
322+
vestsTotal =
323+
env === 'production'
324+
? yield getAsync(cacheKey)
325+
: log('getPendingClaimReward', { msg: 'none_production' });
326+
t2 = process.uptime();
327+
log('[timer] tron_user getPendingClaimReward redis getAsync', {
328+
t: (t2 - t1) * 1000,
329+
});
330+
if (!vestsTotal) {
331+
// not hit cache
332+
log('getPendingClaimReward', {
333+
msg: 'not_hit_cache',
334+
cacheKey,
335+
});
336+
const dbOptions = {
337+
where: {
338+
tron_addr: null,
339+
reward_type: 0,
340+
username,
341+
},
342+
};
343+
t1 = process.uptime();
344+
vestsTotal = yield model.sum('reward_vests', dbOptions);
345+
t2 = process.uptime();
346+
log('[timer] tron_user getPendingClaimReward db execute', {
347+
t: (t2 - t1) * 1000,
348+
vestsTotal,
349+
});
350+
if (env === 'production') {
351+
t1 = process.uptime();
352+
yield setAsync(cacheKey, vestsTotal);
353+
t2 = process.uptime();
354+
yield expireAsync([cacheKey, EXPIRED_TIME]);
355+
t3 = process.uptime();
356+
log(
357+
'[timer] tron_user getPendingClaimReward redis setAsync, expireAsync:',
358+
{ t1: (t2 - t1) * 1000, t2: (t3 - t2) * 1000 }
359+
);
360+
}
361+
return vestsTotal;
362+
}
363+
return vestsTotal;
364+
} catch (e) {
365+
log('getPendingClaimReward', { msg: e.message, cacheKey });
366+
return null;
367+
}
368+
}
369+
305370
module.exports = {
306371
getRecordCache,
307372
updateRecordCache,
@@ -310,4 +375,5 @@ module.exports = {
310375
getRecordCache2,
311376
updateRecordCache2,
312377
clearRecordCache2,
378+
getPendingClaimReward,
313379
};

src/db/models/tron_reward.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,6 @@ module.exports = (sequelize, DataTypes) => {
8383
underscored: true,
8484
}
8585
);
86+
tronReward.getCachePrefix = () => 'tron_reward_pending_';
8687
return tronReward;
8788
};

src/server/api/tron_reward.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import config from 'config';
77
import {
88
clearRecordCache2,
99
getRecordCache2,
10+
getPendingClaimReward,
1011
updateRecordCache2,
1112
} from 'db/cache';
1213
import models from 'db/models';
@@ -110,10 +111,27 @@ export default function useTronRewardApi(app) {
110111
}
111112
}
112113

114+
let pending_trx;
115+
116+
try {
117+
const vestsPerTrx = config.get('tron_reward.vests_per_trx');
118+
const pendingReward = yield getPendingClaimReward(
119+
models.TronReward,
120+
username
121+
);
122+
pending_trx = pendingReward / 1000000 / vestsPerTrx;
123+
} catch (e) {
124+
this.body = JSON.stringify({ error: e.message });
125+
log('[timer] get /tron_user all', {
126+
t: process.uptime() * 1000 - t1,
127+
});
128+
return;
129+
}
130+
113131
const result = {
114132
username: tronUser.username,
115133
tron_addr: tronUser.tron_addr,
116-
pending_claim_tron_reward: tronUser.pending_claim_tron_reward,
134+
pending_claim_tron_reward: `${pending_trx.toFixed(6)} TRX`,
117135
tip_count: tronUser.tip_count,
118136
};
119137

0 commit comments

Comments
 (0)