Conditions | 5 |
Paths | 8 |
Total Lines | 54 |
Code Lines | 31 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | const Constant = require('../../libraries/constant') |
||
8 | var statistics = async function () { |
||
9 | let accessDate = Moment().subtract(1, 'days').format('YYYY-MM-DD') |
||
10 | let statisticsDay = Moment().subtract(1, 'days').format('YYYYMMDD') |
||
11 | // let accessDate = Moment().format('YYYY-MM-DD') |
||
12 | // let statisticsDay = Moment().format('YYYYMMDD') |
||
13 | let uvCachePrefix = 'MIST:ST_UV:DATE:' + statisticsDay + ':STORE_ID:' |
||
14 | let pvCacheKey = 'MIST:ST_PV:DATE:' + statisticsDay |
||
15 | |||
16 | // 根据storeID前缀 1-9 |
||
17 | let keywords |
||
18 | let uvKeys |
||
19 | |||
20 | // insert update data |
||
21 | let insertData = [] |
||
22 | |||
23 | for (let index = 1; index < 10; index++) { |
||
24 | keywords = uvCachePrefix + index.toString() + '*' |
||
25 | uvKeys = await Redis.keys(keywords) |
||
26 | for (let i = 0; i < uvKeys.length; i++) { |
||
27 | element = uvKeys[i] |
||
28 | let storeId = element.replace(uvCachePrefix, '') |
||
29 | |||
30 | let tmpUv = await Redis.bitcount(element) |
||
31 | let tmpPv = await Redis.hget(pvCacheKey, storeId) |
||
32 | |||
33 | let tmp = { |
||
34 | store_id: storeId, |
||
35 | access_date: accessDate, |
||
36 | pv: tmpPv, |
||
37 | uv: tmpUv, |
||
38 | } |
||
39 | // console.log(tmp) |
||
40 | // console.log('---------------------------') |
||
41 | |||
42 | insertData.push(tmp) |
||
43 | // 每1000条插入一次 |
||
44 | if (insertData.length >= INSERT_PER_TIMES) { |
||
45 | console.log('---- insert DB ---- count: ' + insertData.length) |
||
46 | ModelStatisticsStore.add(insertData) |
||
47 | insertData.length = 0 |
||
48 | } |
||
49 | |||
50 | } |
||
51 | } |
||
52 | |||
53 | if (insertData.length > 0) { |
||
54 | console.log('---- last insert DB ---- count: ' + insertData.length) |
||
55 | await ModelStatisticsStore.add(insertData) |
||
56 | } |
||
57 | |||
58 | // update total |
||
59 | await ModelStatisticsStore.updateStatisticsTotal(accessDate) |
||
60 | console.log('ok statistics.js ' + Moment().format('YYYYMMDD HH:mm:ss')) |
||
61 | } |
||
62 | |||
63 | module.exports = statistics |