time_remaining 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README.md +52 -0
- data/Rakefile +1 -0
- data/lib/time_remaining.rb +77 -0
- data/lib/time_remaining/version.rb +3 -0
- data/spec/time_remaining_spec.rb +20 -0
- data/time_remaining.gemspec +22 -0
- metadata +90 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 John McDowall
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
T I M E - R E M A I N I N G
|
2
|
+
===========================
|
3
|
+
|
4
|
+
Given a process that will take some amount of time, and we know how much we have done,
|
5
|
+
and how much is still to do, what is the time remaining for the process to reach completion?
|
6
|
+
|
7
|
+
U S A G E
|
8
|
+
-----------
|
9
|
+
|
10
|
+
require 'time_remaining'_
|
11
|
+
|
12
|
+
time_remaining = McDowall::TimeRemaining.new
|
13
|
+
tasks_done = 0.0
|
14
|
+
total_tasks = 20.0
|
15
|
+
tasks_remaining = total_tasks
|
16
|
+
|
17
|
+
def do_task
|
18
|
+
sleep (1 + rand(2))
|
19
|
+
end
|
20
|
+
|
21
|
+
while( tasks_done < total_tasks ) do
|
22
|
+
do_task( ) # Do whatever it is, transfer a byte, check a file etc.
|
23
|
+
tasks_done+=1
|
24
|
+
tasks_remaining-=1
|
25
|
+
|
26
|
+
time_remaining.add ( (tasks_done/total_tasks).to_f )
|
27
|
+
|
28
|
+
p Time.now.to_s + " | " + time_remaining.completed_at.to_s # Prints "2011-06-22 22:45:38 -0400"
|
29
|
+
p time_remaining # Prints "03:01:00"
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
R U N N I N G T H E T E S T S U I T E
|
34
|
+
----------------------------------------
|
35
|
+
|
36
|
+
rspec spec
|
37
|
+
|
38
|
+
C O N T R I B U T I N G to T I M E - R E M A I N I N G
|
39
|
+
--------------------------------------------------------
|
40
|
+
|
41
|
+
* Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet
|
42
|
+
* Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it
|
43
|
+
* Fork the project
|
44
|
+
* Start a feature/bugfix branch
|
45
|
+
* Commit and push until you are happy with your contribution
|
46
|
+
* Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.
|
47
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
48
|
+
|
49
|
+
C O P Y R I G H T
|
50
|
+
-----------------
|
51
|
+
|
52
|
+
Copyright © 2011 John McDowall. See LICENSE for further details.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require "time_remaining/version"
|
2
|
+
require 'rubygems'
|
3
|
+
require 'algorithms'
|
4
|
+
require 'time_diff'
|
5
|
+
|
6
|
+
module McDowall
|
7
|
+
class TimeRemaining
|
8
|
+
|
9
|
+
def initialize( min_data=2, max_duration=2 )
|
10
|
+
@minimum_data = min_data
|
11
|
+
@maximum_ticks = max_duration * 10
|
12
|
+
@queue = Containers::Queue.new
|
13
|
+
@start_time = Time.now
|
14
|
+
|
15
|
+
@current = {}
|
16
|
+
@oldest = {}
|
17
|
+
end
|
18
|
+
|
19
|
+
def reset
|
20
|
+
@queue.clear
|
21
|
+
@start_time = nil
|
22
|
+
end
|
23
|
+
|
24
|
+
def add progress
|
25
|
+
clear_expired
|
26
|
+
current_ticks = elapsed_ticks
|
27
|
+
@current = {:current_ticks => current_ticks,:progress=>progress}
|
28
|
+
|
29
|
+
@queue.push(@current);
|
30
|
+
|
31
|
+
if @queue.size == 1
|
32
|
+
@oldest = @current
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def completed_in
|
37
|
+
oldest = @oldest;
|
38
|
+
current = @current;
|
39
|
+
|
40
|
+
if (@queue.size < @minimum_data || oldest[:progress] == current[:progress])
|
41
|
+
return 99999
|
42
|
+
end
|
43
|
+
|
44
|
+
#puts "(1.0 - #{current[:progress].to_f}) * ((#{current[:current_ticks]} - #{oldest[:current_ticks]}) / (#{current[:progress]} - #{oldest[:progress]}))"
|
45
|
+
|
46
|
+
finishedInTicks = (1.0 - current[:progress].to_f) * ((current[:current_ticks] - oldest[:current_ticks]) / (current[:progress] - oldest[:progress]));
|
47
|
+
finishedInTicks
|
48
|
+
end
|
49
|
+
|
50
|
+
def completed_at
|
51
|
+
Time.at(Time.now.to_i + completed_in)
|
52
|
+
end
|
53
|
+
|
54
|
+
def time_remaining_available?
|
55
|
+
return (@queue.size >= @minimum_data && @oldestcurrent[:progress] != @currentcurrent[:progress]);
|
56
|
+
end
|
57
|
+
|
58
|
+
def to_s
|
59
|
+
Time.diff( completed_at, Time.now-1 )[:diff]
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def elapsed_ticks
|
65
|
+
(Time.now-@start_time).to_i
|
66
|
+
end
|
67
|
+
|
68
|
+
def clear_expired
|
69
|
+
expired = elapsed_ticks - @maximum_ticks;
|
70
|
+
|
71
|
+
while (@queue.size > @minimum_data && @queue.next[:current_ticks] < expired) do
|
72
|
+
@oldest = @queue.pop
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "./lib/time_remaining"
|
2
|
+
|
3
|
+
describe "The Time Remaining class" do
|
4
|
+
|
5
|
+
let(:time_remaining) { McDowall::TimeRemaining.new }
|
6
|
+
|
7
|
+
describe "when no data has accumulated" do
|
8
|
+
it "should return 99999 as estimated finish time" do
|
9
|
+
time_remaining.completed_in.should == 99999
|
10
|
+
end
|
11
|
+
|
12
|
+
it "time_remaining_available? returns false" do
|
13
|
+
time_remaining.time_remaining_available?.should == false
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "when data hase accumulated" do
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
require "time_remaining/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "time_remaining"
|
8
|
+
s.version = TimeRemaining::VERSION
|
9
|
+
s.authors = ["johnmcdowall"]
|
10
|
+
s.email = ["john@mcdowall.info"]
|
11
|
+
s.homepage = "https://github.com/johnmcdowall/time-remaining"
|
12
|
+
s.summary = %q{Determine the time remaining to complete a list of tasks when the total number of tasks and the number of tasks completed are known.}
|
13
|
+
s.description = %q{This gem uses a moving average of previous task execution durations and how much work is still remaining to calculate how long it will take to finish the remaining tasks.}
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_dependency "algorithms"
|
21
|
+
s.add_dependency "time_diff"
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: time_remaining
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.8
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- johnmcdowall
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-10 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: algorithms
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: time_diff
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: This gem uses a moving average of previous task execution durations and
|
47
|
+
how much work is still remaining to calculate how long it will take to finish the
|
48
|
+
remaining tasks.
|
49
|
+
email:
|
50
|
+
- john@mcdowall.info
|
51
|
+
executables: []
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- .gitignore
|
56
|
+
- Gemfile
|
57
|
+
- LICENSE
|
58
|
+
- README.md
|
59
|
+
- Rakefile
|
60
|
+
- lib/time_remaining.rb
|
61
|
+
- lib/time_remaining/version.rb
|
62
|
+
- spec/time_remaining_spec.rb
|
63
|
+
- time_remaining.gemspec
|
64
|
+
homepage: https://github.com/johnmcdowall/time-remaining
|
65
|
+
licenses: []
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.8.24
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: Determine the time remaining to complete a list of tasks when the total number
|
88
|
+
of tasks and the number of tasks completed are known.
|
89
|
+
test_files:
|
90
|
+
- spec/time_remaining_spec.rb
|