Passed
Push — master ( 1adf40...f067da )
by lv
01:05
created

app/schedule/mist/statistics.js   A

Complexity

Total Complexity 5
Complexity/F 5

Size

Lines of Code 63
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
wmc 5
eloc 37
c 0
b 0
f 0
nc 1
mnd 3
bc 5
fnc 1
dl 0
loc 63
rs 10
bpm 5
cpm 5
noi 3

1 Function

Rating   Name   Duplication   Size   Complexity  
B statistics.js ➔ statistics 0 54 5
1
const Constant = require('../../libraries/constant')
0 ignored issues
show
Unused Code introduced by
The constant Constant seems to be never used. Consider removing it.
Loading history...
2
const Moment = require('moment')
3
const Redis = require('../../../config/db').redis
4
const ModelStatisticsStore = require('../../../app/model/mist/statisticsStore')
5
6
var INSERT_PER_TIMES = 1000
7
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]
0 ignored issues
show
Bug introduced by
The variable element seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.element.
Loading history...
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)
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
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