UNPKG

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