mirror of
https://github.com/atom/atom.git
synced 2026-04-28 03:01:47 -04:00
Modernize editor-stats package with package.cson
This commit is contained in:
committed by
Corey Johnson & Kevin Sawicki
parent
a70f5ea1f7
commit
34c570bed5
103
src/packages/editor-stats/lib/editor-stats-view.coffee
Normal file
103
src/packages/editor-stats/lib/editor-stats-view.coffee
Normal file
@@ -0,0 +1,103 @@
|
||||
ScrollView = require 'scroll-view'
|
||||
d3 = require 'd3.v3'
|
||||
_ = require 'underscore'
|
||||
$ = require 'jquery'
|
||||
|
||||
module.exports =
|
||||
class EditorStatsView extends ScrollView
|
||||
@activate: ->
|
||||
new EditorStatsView
|
||||
|
||||
@content: ->
|
||||
@div class: 'editor-stats-wrapper', tabindex: -1, =>
|
||||
@div class: 'editor-stats', outlet: 'editorStats'
|
||||
|
||||
pt: 15
|
||||
pl: 10
|
||||
pb: 3
|
||||
pr: 25
|
||||
|
||||
initialize: ->
|
||||
super
|
||||
|
||||
resizer = =>
|
||||
return unless @isOnDom()
|
||||
@draw()
|
||||
@update()
|
||||
@subscribe $(window), 'resize', _.debounce(resizer, 300)
|
||||
|
||||
draw: ->
|
||||
@editorStats.empty()
|
||||
@x ?= d3.scale.ordinal().domain d3.range(@stats.hours * 60)
|
||||
@y ?= d3.scale.linear()
|
||||
w = rootView.vertical.width()
|
||||
h = @height()
|
||||
data = d3.entries @stats.eventLog
|
||||
max = d3.max data, (d) -> d.value
|
||||
|
||||
@x.rangeBands [0, w - @pl - @pr], 0.2
|
||||
@y.domain([0, max]).range [h - @pt - @pb, 0]
|
||||
|
||||
@xaxis ?= d3.svg.axis().scale(@x).orient('top')
|
||||
.tickSize(-h + @pt + @pb)
|
||||
.tickFormat (d) =>
|
||||
d = new Date(@stats.startDate.getTime() + (d * 6e4))
|
||||
mins = d.getMinutes()
|
||||
mins = "0#{mins}" if mins <= 9
|
||||
"#{d.getHours()}:#{mins}"
|
||||
|
||||
vis = d3.select(@editorStats.get(0)).append('svg')
|
||||
.attr('width', w)
|
||||
.attr('height', h)
|
||||
.append('g')
|
||||
.attr('transform', "translate(#{@pl},#{@pt})")
|
||||
|
||||
vis.append('g')
|
||||
.attr('class', 'x axis')
|
||||
.call(@xaxis)
|
||||
.selectAll('g')
|
||||
.classed('minor', (d, i) -> i % 5 == 0 && i % 15 != 0)
|
||||
.style 'display', (d, i) ->
|
||||
if i % 15 == 0 || i % 5 == 0 || i == data.length - 1
|
||||
'block'
|
||||
else
|
||||
'none'
|
||||
|
||||
@bars = vis.selectAll('rect.bar')
|
||||
.data(data)
|
||||
.enter().append('rect')
|
||||
.attr('x', (d, i) => @x i)
|
||||
.attr('height', (d, i) => h - @y(d.value) - @pt - @pb)
|
||||
.attr('y', (d) => @y(d.value))
|
||||
.attr('width', @x.rangeBand())
|
||||
.attr('class', 'bar')
|
||||
|
||||
clearInterval(@updateInterval)
|
||||
updater = => @update() if @isOnDom()
|
||||
setTimeout(updater, 100)
|
||||
@updateInterval = setInterval(updater, 5000)
|
||||
|
||||
update: ->
|
||||
newData = d3.entries @stats.eventLog
|
||||
max = d3.max newData, (d) -> d.value
|
||||
@y.domain [0, max]
|
||||
h = @height()
|
||||
@bars.data(newData).transition()
|
||||
.attr('height', (d, i) => h - @y(d.value) - @pt - @pb)
|
||||
.attr('y', (d, i) => @y(d.value))
|
||||
@bars.classed('max', (d, i) -> d.value == max)
|
||||
|
||||
toggle: (@stats) ->
|
||||
if @hasParent()
|
||||
@detach()
|
||||
else
|
||||
@attach()
|
||||
|
||||
attach: ->
|
||||
rootView.vertical.append(this)
|
||||
@draw()
|
||||
|
||||
detach: ->
|
||||
super
|
||||
clearInterval(@updateInterval)
|
||||
rootView.focus()
|
||||
19
src/packages/editor-stats/lib/editor-stats.coffee
Normal file
19
src/packages/editor-stats/lib/editor-stats.coffee
Normal file
@@ -0,0 +1,19 @@
|
||||
StatsTracker = require './stats-tracker'
|
||||
|
||||
module.exports =
|
||||
stats: null
|
||||
editorStatsView: null
|
||||
|
||||
activate: ->
|
||||
@stats = new StatsTracker()
|
||||
rootView.command 'editor-stats:toggle', => @createView().toggle(@stats)
|
||||
|
||||
deactivate: ->
|
||||
@editorStatsView = null
|
||||
@stats = null
|
||||
|
||||
createView: ->
|
||||
unless @editorStatsView
|
||||
EditorStatsView = require 'editor-stats/lib/editor-stats-view'
|
||||
@editorStatsView = new EditorStatsView()
|
||||
@editorStatsView
|
||||
32
src/packages/editor-stats/lib/stats-tracker.coffee
Normal file
32
src/packages/editor-stats/lib/stats-tracker.coffee
Normal file
@@ -0,0 +1,32 @@
|
||||
module.exports =
|
||||
class StatsTracker
|
||||
startDate: new Date
|
||||
hours: 6
|
||||
eventLog: []
|
||||
|
||||
constructor: ->
|
||||
date = new Date(@startDate)
|
||||
future = new Date(date.getTime() + (36e5 * @hours))
|
||||
@eventLog[@time(date)] = 0
|
||||
|
||||
while date < future
|
||||
@eventLog[@time(date)] = 0
|
||||
|
||||
rootView.on 'keydown', => @track()
|
||||
rootView.on 'mouseup', => @track()
|
||||
|
||||
clear: ->
|
||||
@eventLog = []
|
||||
|
||||
track: ->
|
||||
date = new Date
|
||||
times = @time date
|
||||
@eventLog[times] ?= 0
|
||||
@eventLog[times] += 1
|
||||
@eventLog.shift() if @eventLog.length > (@hours * 60)
|
||||
|
||||
time: (date) ->
|
||||
date.setTime(date.getTime() + 6e4)
|
||||
hour = date.getHours()
|
||||
minute = date.getMinutes()
|
||||
"#{hour}:#{minute}"
|
||||
Reference in New Issue
Block a user