1 | _ = require 'lodash'
|
2 | debug = (require 'debug')('octoblu-flow-canary:stats')
|
3 |
|
4 | class Stats
|
5 | constructor: ({@Date,@stats,@CANARY_UPDATE_INTERVAL,@CANARY_HEALTH_CHECK_MAX_DIFF} = {}) ->
|
6 | @Date ?= Date
|
7 | @stats ?=
|
8 | flows: {}
|
9 | startTime: @Date.now()
|
10 |
|
11 | setCanaryErrors: (error, trimSize) =>
|
12 | @stats.errors ?= []
|
13 | @stats.errors.unshift error
|
14 | @stats.errors = @stats.errors.slice 0, trimSize
|
15 |
|
16 | setFlowNames: (flow) =>
|
17 | @stats.flows[flow.flowId] ?= {}
|
18 | @stats.flows[flow.flowId].name = flow.name
|
19 |
|
20 | getFlowById: (flowId) =>
|
21 | @stats.flows[flowId] ?= {}
|
22 | return @stats.flows[flowId]
|
23 |
|
24 | getFlows: =>
|
25 | return @stats.flows
|
26 |
|
27 | getCurrentStats: =>
|
28 | @updateStats()
|
29 | return @stats
|
30 |
|
31 | getPassing: =>
|
32 | @updateStats()
|
33 | return {passing:true} if @stats.passing
|
34 | return @stats
|
35 |
|
36 | updateStats: =>
|
37 | _.each @stats.flows, (flowInfo) =>
|
38 | lastMessage = flowInfo.messageTime?[0] or flowInfo.startTime?[0] or 0
|
39 | flowInfo.currentTimeDiff = @Date.now() - lastMessage
|
40 | flowInfo.passing = @timeDiffLessThanMax flowInfo.currentTimeDiff
|
41 | _.each flowInfo.timeDiffs, (timeDiff) =>
|
42 | flowInfo.passing = false if !@passingTimeDiff timeDiff
|
43 |
|
44 | @stats.passing = _.keys(@stats.flows).length != 0
|
45 | [passing, failing] = _.partition @stats.flows, passing: true
|
46 | passingCount = _.size passing
|
47 | totalCount = _.size @stats.flows
|
48 | return unless totalCount > 0
|
49 | passingPercent = (passingCount / totalCount) * 100
|
50 | @stats.passing = passingPercent > 90
|
51 |
|
52 | cleanupFlowStats: (flows) =>
|
53 | debug 'cleaning up flow stats'
|
54 | flowIds = {}
|
55 | _.each flows, (flow) =>
|
56 | debug "has flow id #{flow.flowId}"
|
57 | flowIds[flow.flowId] = true
|
58 | _.each _.keys(@stats.flows), (flowUuid) =>
|
59 | if !flowIds[flowUuid]
|
60 | delete @stats.flows[flowUuid]
|
61 |
|
62 | timeDiffGreaterThanMin: (timeDiff) =>
|
63 | return timeDiff >= (@CANARY_UPDATE_INTERVAL - @CANARY_HEALTH_CHECK_MAX_DIFF)
|
64 |
|
65 | timeDiffLessThanMax: (timeDiff) =>
|
66 | return timeDiff <= (@CANARY_UPDATE_INTERVAL + @CANARY_HEALTH_CHECK_MAX_DIFF)
|
67 |
|
68 | passingTimeDiff: (timeDiff) =>
|
69 | return @timeDiffGreaterThanMin(timeDiff) and @timeDiffLessThanMax(timeDiff)
|
70 |
|
71 | module.exports = Stats
|