pax_global_header00006660000000000000000000000064130763343210014514gustar00rootroot0000000000000052 comment=aea89b9c277c0674a2485a3eb94a7269bb2346be are-we-there-yet-1.1.4/000077500000000000000000000000001307633432100146035ustar00rootroot00000000000000are-we-there-yet-1.1.4/.gitignore000066400000000000000000000000511307633432100165670ustar00rootroot00000000000000*~ .#* node_modules coverage .nyc_output are-we-there-yet-1.1.4/.travis.yml000066400000000000000000000000771307633432100167200ustar00rootroot00000000000000language: node_js sudo: false node_js: - "7" - "6" - "4" are-we-there-yet-1.1.4/CHANGES.md000066400000000000000000000021231307633432100161730ustar00rootroot00000000000000Hi, figured we could actually use a changelog now: ## 1.1.4 2017-04-21 * Fix typo in package.json ## 1.1.3 2017-04-21 * Improve documentation and limit files included in the distribution. ## 1.1.2 2016-03-15 * Add tracker group cycle detection and tests for it ## 1.1.1 2016-01-29 * Fix a typo in stream completion tracker ## 1.1.0 2016-01-29 * Rewrote completion percent computation to be low impact– no more walking a tree of completion groups every time we need this info. Previously, with medium sized tree of completion groups, even a relatively modest number of calls to the top level `completed()` method would result in absurd numbers of calls overall as it walked down the tree. We now, instead, keep track as we bubble up changes, so the computation is limited to when data changes and to the depth of that one branch, instead of _every_ node. (Plus, we were already incurring _this_ cost, since we already bubbled out changes.) * Moved different tracker types out to their own files. * Made tests test for TOO MANY events too. * Standarized the source code formatting are-we-there-yet-1.1.4/LICENSE000066400000000000000000000013351307633432100156120ustar00rootroot00000000000000Copyright (c) 2015, Rebecca Turner Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. are-we-there-yet-1.1.4/README.md000066400000000000000000000142361307633432100160700ustar00rootroot00000000000000are-we-there-yet ---------------- Track complex hiearchies of asynchronous task completion statuses. This is intended to give you a way of recording and reporting the progress of the big recursive fan-out and gather type workflows that are so common in async. What you do with this completion data is up to you, but the most common use case is to feed it to one of the many progress bar modules. Most progress bar modules include a rudamentary version of this, but my needs were more complex. Usage ===== ```javascript var TrackerGroup = require("are-we-there-yet").TrackerGroup var top = new TrackerGroup("program") var single = top.newItem("one thing", 100) single.completeWork(20) console.log(top.completed()) // 0.2 fs.stat("file", function(er, stat) { if (er) throw er var stream = top.newStream("file", stat.size) console.log(top.completed()) // now 0.1 as single is 50% of the job and is 20% complete // and 50% * 20% == 10% fs.createReadStream("file").pipe(stream).on("data", function (chunk) { // do stuff with chunk }) top.on("change", function (name) { // called each time a chunk is read from "file" // top.completed() will start at 0.1 and fill up to 0.6 as the file is read }) }) ``` Shared Methods ============== * var completed = tracker.completed() Implemented in: `Tracker`, `TrackerGroup`, `TrackerStream` Returns the ratio of completed work to work to be done. Range of 0 to 1. * tracker.finish() Implemented in: `Tracker`, `TrackerGroup` Marks the tracker as completed. With a TrackerGroup this marks all of its components as completed. Marks all of the components of this tracker as finished, which in turn means that `tracker.completed()` for this will now be 1. This will result in one or more `change` events being emitted. Events ====== All tracker objects emit `change` events with the following arguments: ``` function (name, completed, tracker) ``` `name` is the name of the tracker that originally emitted the event, or if it didn't have one, the first containing tracker group that had one. `completed` is the percent complete (as returned by `tracker.completed()` method). `tracker` is the tracker object that you are listening for events on. TrackerGroup ============ * var tracker = new TrackerGroup(**name**) * **name** *(optional)* - The name of this tracker group, used in change notifications if the component updating didn't have a name. Defaults to undefined. Creates a new empty tracker aggregation group. These are trackers whose completion status is determined by the completion status of other trackers. * tracker.addUnit(**otherTracker**, **weight**) * **otherTracker** - Any of the other are-we-there-yet tracker objects * **weight** *(optional)* - The weight to give the tracker, defaults to 1. Adds the **otherTracker** to this aggregation group. The weight determines how long you expect this tracker to take to complete in proportion to other units. So for instance, if you add one tracker with a weight of 1 and another with a weight of 2, you're saying the second will take twice as long to complete as the first. As such, the first will account for 33% of the completion of this tracker and the second will account for the other 67%. Returns **otherTracker**. * var subGroup = tracker.newGroup(**name**, **weight**) The above is exactly equivalent to: ```javascript var subGroup = tracker.addUnit(new TrackerGroup(name), weight) ``` * var subItem = tracker.newItem(**name**, **todo**, **weight**) The above is exactly equivalent to: ```javascript var subItem = tracker.addUnit(new Tracker(name, todo), weight) ``` * var subStream = tracker.newStream(**name**, **todo**, **weight**) The above is exactly equivalent to: ```javascript var subStream = tracker.addUnit(new TrackerStream(name, todo), weight) ``` * console.log( tracker.debug() ) Returns a tree showing the completion of this tracker group and all of its children, including recursively entering all of the children. Tracker ======= * var tracker = new Tracker(**name**, **todo**) * **name** *(optional)* The name of this counter to report in change events. Defaults to undefined. * **todo** *(optional)* The amount of work todo (a number). Defaults to 0. Ordinarily these are constructed as a part of a tracker group (via `newItem`). * var completed = tracker.completed() Returns the ratio of completed work to work to be done. Range of 0 to 1. If total work to be done is 0 then it will return 0. * tracker.addWork(**todo**) * **todo** A number to add to the amount of work to be done. Increases the amount of work to be done, thus decreasing the completion percentage. Triggers a `change` event. * tracker.completeWork(**completed**) * **completed** A number to add to the work complete Increase the amount of work complete, thus increasing the completion percentage. Will never increase the work completed past the amount of work todo. That is, percentages > 100% are not allowed. Triggers a `change` event. * tracker.finish() Marks this tracker as finished, tracker.completed() will now be 1. Triggers a `change` event. TrackerStream ============= * var tracker = new TrackerStream(**name**, **size**, **options**) * **name** *(optional)* The name of this counter to report in change events. Defaults to undefined. * **size** *(optional)* The number of bytes being sent through this stream. * **options** *(optional)* A hash of stream options The tracker stream object is a pass through stream that updates an internal tracker object each time a block passes through. It's intended to track downloads, file extraction and other related activities. You use it by piping your data source into it and then using it as your data source. If your data has a length attribute then that's used as the amount of work completed when the chunk is passed through. If it does not (eg, object streams) then each chunk counts as completing 1 unit of work, so your size should be the total number of objects being streamed. * tracker.addWork(**todo**) * **todo** Increase the expected overall size by **todo** bytes. Increases the amount of work to be done, thus decreasing the completion percentage. Triggers a `change` event. are-we-there-yet-1.1.4/index.js000066400000000000000000000002431307633432100162470ustar00rootroot00000000000000'use strict' exports.TrackerGroup = require('./tracker-group.js') exports.Tracker = require('./tracker.js') exports.TrackerStream = require('./tracker-stream.js') are-we-there-yet-1.1.4/package.json000066400000000000000000000015001307633432100170650ustar00rootroot00000000000000{ "name": "are-we-there-yet", "version": "1.1.4", "description": "Keep track of the overall completion of many disparate processes", "main": "index.js", "scripts": { "test": "standard && tap test/*.js" }, "repository": { "type": "git", "url": "https://github.com/iarna/are-we-there-yet.git" }, "author": "Rebecca Turner (http://re-becca.org)", "license": "ISC", "bugs": { "url": "https://github.com/iarna/are-we-there-yet/issues" }, "homepage": "https://github.com/iarna/are-we-there-yet", "devDependencies": { "standard": "^6.0.8", "tap": "^5.7.0" }, "dependencies": { "delegates": "^1.0.0", "readable-stream": "^2.0.6" }, "files": [ "index.js", "tracker-base.js", "tracker-group.js", "tracker-stream.js", "tracker.js", "CHANGES.md" ] } are-we-there-yet-1.1.4/test/000077500000000000000000000000001307633432100155625ustar00rootroot00000000000000are-we-there-yet-1.1.4/test/lib/000077500000000000000000000000001307633432100163305ustar00rootroot00000000000000are-we-there-yet-1.1.4/test/lib/test-event.js000066400000000000000000000015761307633432100207750ustar00rootroot00000000000000'use strict' var util = require('util') module.exports = function (obj, event, next) { var timeout = setTimeout(gotTimeout, 10) obj.once(event, gotResult) function gotTimeout () { obj.removeListener(event, gotResult) next(new Error('Timeout listening for ' + event)) } var result = [] function gotResult () { result = Array.prototype.slice.call(arguments) clearTimeout(timeout) timeout = setTimeout(gotNoMoreResults, 10) obj.once(event, gotTooManyResults) } function gotNoMoreResults () { obj.removeListener(event, gotTooManyResults) var args = [null].concat(result) next.apply(null, args) } function gotTooManyResults () { var secondResult = Array.prototype.slice.call(arguments) clearTimeout(timeout) next(new Error('Got too many results, first ' + util.inspect(result) + ' and then ' + util.inspect(secondResult))) } } are-we-there-yet-1.1.4/test/tracker.js000066400000000000000000000030361307633432100175550ustar00rootroot00000000000000'use strict' var test = require('tap').test var Tracker = require('../index.js').Tracker var testEvent = require('./lib/test-event.js') var name = 'test' test('initialization', function (t) { var simple = new Tracker(name) t.is(simple.completed(), 0, 'Nothing todo is 0 completion') t.done() }) var track var todo = 100 test('completion', function (t) { track = new Tracker(name, todo) t.is(track.completed(), 0, 'Nothing done is 0 completion') testEvent(track, 'change', afterCompleteWork) track.completeWork(todo) t.is(track.completed(), 1, 'completeWork: 100% completed') function afterCompleteWork (er, onChangeName) { t.is(er, null, 'completeWork: on change event fired') t.is(onChangeName, name, 'completeWork: on change emits the correct name') t.done() } }) test('add more work', function (t) { testEvent(track, 'change', afterAddWork) track.addWork(todo) t.is(track.completed(), 0.5, 'addWork: 50% completed') function afterAddWork (er, onChangeName) { t.is(er, null, 'addWork: on change event fired') t.is(onChangeName, name, 'addWork: on change emits the correct name') t.done() } }) test('complete more work', function (t) { track.completeWork(200) t.is(track.completed(), 1, 'completeWork: Over completion is still only 100% complete') t.done() }) test('finish is always 100%', function (t) { var finishtest = new Tracker(name, todo) finishtest.completeWork(50) finishtest.finish() t.is(finishtest.completed(), 1, 'finish: Explicitly finishing moves to 100%') t.done() }) are-we-there-yet-1.1.4/test/trackergroup.js000066400000000000000000000072361307633432100206400ustar00rootroot00000000000000'use strict' var test = require('tap').test var TrackerGroup = require('../index.js').TrackerGroup var testEvent = require('./lib/test-event.js') test('TrackerGroup', function (t) { var name = 'test' var track = new TrackerGroup(name) t.is(track.completed(), 0, 'Nothing todo is 0 completion') testEvent(track, 'change', afterFinishEmpty) track.finish() var a, b function afterFinishEmpty (er, onChangeName, completion) { t.is(er, null, 'finishEmpty: on change event fired') t.is(onChangeName, name, 'finishEmpty: on change emits the correct name') t.is(completion, 1, 'finishEmpty: passed through completion was correct') t.is(track.completed(), 1, 'finishEmpty: Finishing an empty group actually finishes it') track = new TrackerGroup(name) a = track.newItem('a', 10, 1) b = track.newItem('b', 10, 1) t.is(track.completed(), 0, 'Initially empty') testEvent(track, 'change', afterCompleteWork) a.completeWork(5) } function afterCompleteWork (er, onChangeName, completion) { t.is(er, null, 'on change event fired') t.is(onChangeName, 'a', 'on change emits the correct name') t.is(completion, 0.25, 'Complete half of one is a quarter overall') t.is(track.completed(), 0.25, 'Complete half of one is a quarter overall') testEvent(track, 'change', afterFinishAll) track.finish() } function afterFinishAll (er, onChangeName, completion) { t.is(er, null, 'finishAll: on change event fired') t.is(onChangeName, name, 'finishAll: on change emits the correct name') t.is(completion, 1, 'Finishing everything ') t.is(track.completed(), 1, 'Finishing everything ') track = new TrackerGroup(name) a = track.newItem('a', 10, 2) b = track.newItem('b', 10, 1) t.is(track.completed(), 0, 'weighted: Initially empty') testEvent(track, 'change', afterWeightedCompleteWork) a.completeWork(5) } function afterWeightedCompleteWork (er, onChangeName, completion) { t.is(er, null, 'weighted: on change event fired') t.is(onChangeName, 'a', 'weighted: on change emits the correct name') t.is(Math.floor(completion * 100), 33, 'weighted: Complete half of double weighted') t.is(Math.floor(track.completed() * 100), 33, 'weighted: Complete half of double weighted') testEvent(track, 'change', afterWeightedFinishAll) track.finish() } function afterWeightedFinishAll (er, onChangeName, completion) { t.is(er, null, 'weightedFinishAll: on change event fired') t.is(onChangeName, name, 'weightedFinishAll: on change emits the correct name') t.is(completion, 1, 'weightedFinishaAll: Finishing everything ') t.is(track.completed(), 1, 'weightedFinishaAll: Finishing everything ') track = new TrackerGroup(name) a = track.newGroup('a', 10) b = track.newGroup('b', 10) var a1 = a.newItem('a.1', 10) a1.completeWork(5) t.is(track.completed(), 0.25, 'nested: Initially quarter done') testEvent(track, 'change', afterNestedComplete) b.finish() } function afterNestedComplete (er, onChangeName, completion) { t.is(er, null, 'nestedComplete: on change event fired') t.is(onChangeName, 'b', 'nestedComplete: on change emits the correct name') t.is(completion, 0.75, 'nestedComplete: Finishing everything ') t.is(track.completed(), 0.75, 'nestedComplete: Finishing everything ') t.end() } }) test('cycles', function (t) { var track = new TrackerGroup('top') testCycle(track, track) var layer1 = track.newGroup('layer1') testCycle(layer1, track) t.end() function testCycle (addTo, toAdd) { try { addTo.addUnit(toAdd) t.fail(toAdd.name) } catch (ex) { console.log(ex) t.pass(toAdd.name) } } }) are-we-there-yet-1.1.4/test/trackerstream.js000066400000000000000000000027031307633432100207710ustar00rootroot00000000000000'use strict' var test = require('tap').test var util = require('util') var stream = require('readable-stream') var TrackerStream = require('../index.js').TrackerStream var testEvent = require('./lib/test-event.js') var Sink = function () { stream.Writable.apply(this, arguments) } util.inherits(Sink, stream.Writable) Sink.prototype._write = function (data, encoding, cb) { cb() } test('TrackerStream', function (t) { t.plan(9) var name = 'test' var track = new TrackerStream(name) t.is(track.completed(), 0, 'Nothing todo is 0 completion') var todo = 10 track = new TrackerStream(name, todo) t.is(track.completed(), 0, 'Nothing done is 0 completion') track.pipe(new Sink()) testEvent(track, 'change', afterCompleteWork) track.write('0123456789') function afterCompleteWork (er, onChangeName) { t.is(er, null, 'write: on change event fired') t.is(onChangeName, name, 'write: on change emits the correct name') t.is(track.completed(), 1, 'write: 100% completed') testEvent(track, 'change', afterAddWork) track.addWork(10) } function afterAddWork (er, onChangeName) { t.is(er, null, 'addWork: on change event fired') t.is(track.completed(), 0.5, 'addWork: 50% completed') testEvent(track, 'change', afterAllWork) track.write('ABCDEFGHIJKLMNOPQRST') } function afterAllWork (er) { t.is(er, null, 'allWork: on change event fired') t.is(track.completed(), 1, 'allWork: 100% completed') } }) are-we-there-yet-1.1.4/tracker-base.js000066400000000000000000000004221307633432100175020ustar00rootroot00000000000000'use strict' var EventEmitter = require('events').EventEmitter var util = require('util') var trackerId = 0 var TrackerBase = module.exports = function (name) { EventEmitter.call(this) this.id = ++trackerId this.name = name } util.inherits(TrackerBase, EventEmitter) are-we-there-yet-1.1.4/tracker-group.js000066400000000000000000000062371307633432100177360ustar00rootroot00000000000000'use strict' var util = require('util') var TrackerBase = require('./tracker-base.js') var Tracker = require('./tracker.js') var TrackerStream = require('./tracker-stream.js') var TrackerGroup = module.exports = function (name) { TrackerBase.call(this, name) this.parentGroup = null this.trackers = [] this.completion = {} this.weight = {} this.totalWeight = 0 this.finished = false this.bubbleChange = bubbleChange(this) } util.inherits(TrackerGroup, TrackerBase) function bubbleChange (trackerGroup) { return function (name, completed, tracker) { trackerGroup.completion[tracker.id] = completed if (trackerGroup.finished) return trackerGroup.emit('change', name || trackerGroup.name, trackerGroup.completed(), trackerGroup) } } TrackerGroup.prototype.nameInTree = function () { var names = [] var from = this while (from) { names.unshift(from.name) from = from.parentGroup } return names.join('/') } TrackerGroup.prototype.addUnit = function (unit, weight) { if (unit.addUnit) { var toTest = this while (toTest) { if (unit === toTest) { throw new Error( 'Attempted to add tracker group ' + unit.name + ' to tree that already includes it ' + this.nameInTree(this)) } toTest = toTest.parentGroup } unit.parentGroup = this } this.weight[unit.id] = weight || 1 this.totalWeight += this.weight[unit.id] this.trackers.push(unit) this.completion[unit.id] = unit.completed() unit.on('change', this.bubbleChange) if (!this.finished) this.emit('change', unit.name, this.completion[unit.id], unit) return unit } TrackerGroup.prototype.completed = function () { if (this.trackers.length === 0) return 0 var valPerWeight = 1 / this.totalWeight var completed = 0 for (var ii = 0; ii < this.trackers.length; ii++) { var trackerId = this.trackers[ii].id completed += valPerWeight * this.weight[trackerId] * this.completion[trackerId] } return completed } TrackerGroup.prototype.newGroup = function (name, weight) { return this.addUnit(new TrackerGroup(name), weight) } TrackerGroup.prototype.newItem = function (name, todo, weight) { return this.addUnit(new Tracker(name, todo), weight) } TrackerGroup.prototype.newStream = function (name, todo, weight) { return this.addUnit(new TrackerStream(name, todo), weight) } TrackerGroup.prototype.finish = function () { this.finished = true if (!this.trackers.length) this.addUnit(new Tracker(), 1, true) for (var ii = 0; ii < this.trackers.length; ii++) { var tracker = this.trackers[ii] tracker.finish() tracker.removeListener('change', this.bubbleChange) } this.emit('change', this.name, 1, this) } var buffer = ' ' TrackerGroup.prototype.debug = function (depth) { depth = depth || 0 var indent = depth ? buffer.substr(0, depth) : '' var output = indent + (this.name || 'top') + ': ' + this.completed() + '\n' this.trackers.forEach(function (tracker) { if (tracker instanceof TrackerGroup) { output += tracker.debug(depth + 1) } else { output += indent + ' ' + tracker.name + ': ' + tracker.completed() + '\n' } }) return output } are-we-there-yet-1.1.4/tracker-stream.js000066400000000000000000000016571307633432100200760ustar00rootroot00000000000000'use strict' var util = require('util') var stream = require('readable-stream') var delegate = require('delegates') var Tracker = require('./tracker.js') var TrackerStream = module.exports = function (name, size, options) { stream.Transform.call(this, options) this.tracker = new Tracker(name, size) this.name = name this.id = this.tracker.id this.tracker.on('change', delegateChange(this)) } util.inherits(TrackerStream, stream.Transform) function delegateChange (trackerStream) { return function (name, completion, tracker) { trackerStream.emit('change', name, completion, trackerStream) } } TrackerStream.prototype._transform = function (data, encoding, cb) { this.tracker.completeWork(data.length ? data.length : 1) this.push(data) cb() } TrackerStream.prototype._flush = function (cb) { this.tracker.finish() cb() } delegate(TrackerStream.prototype, 'tracker') .method('completed') .method('addWork') are-we-there-yet-1.1.4/tracker.js000066400000000000000000000014721307633432100166000ustar00rootroot00000000000000'use strict' var util = require('util') var TrackerBase = require('./tracker-base.js') var Tracker = module.exports = function (name, todo) { TrackerBase.call(this, name) this.workDone = 0 this.workTodo = todo || 0 } util.inherits(Tracker, TrackerBase) Tracker.prototype.completed = function () { return this.workTodo === 0 ? 0 : this.workDone / this.workTodo } Tracker.prototype.addWork = function (work) { this.workTodo += work this.emit('change', this.name, this.completed(), this) } Tracker.prototype.completeWork = function (work) { this.workDone += work if (this.workDone > this.workTodo) this.workDone = this.workTodo this.emit('change', this.name, this.completed(), this) } Tracker.prototype.finish = function () { this.workTodo = this.workDone = 1 this.emit('change', this.name, 1, this) }