ttrack 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/bin/ttrack +49 -13
  2. data/lib/ttrack/datastore.rb +9 -8
  3. data/lib/ttrack.rb +62 -45
  4. metadata +4 -4
data/bin/ttrack CHANGED
@@ -1,25 +1,61 @@
1
1
  #!/Users/alex/.rvm/rubies/ruby-1.9.2-p290/bin/ruby -w
2
2
  require 'ttrack'
3
3
 
4
- tt = TTrack.new
4
+ def usage(tt)
5
+ puts 'Usage: ttrack [%s]' % (tt.commands * '|')
6
+ end
7
+
8
+ databasename = "%s/%s" % [ENV['HOME'], ".timetrackerdb"]
9
+ tt = TTrack.new databasename
5
10
  case ARGV[0]
6
11
  when 'start' then
7
- tt.start ARGV[1], ARGV[2]
12
+ if tt.start(ARGV[1], ARGV[2])
13
+ puts 'Start tracking issue "%s"' % ARGV[1]
14
+ else
15
+ usage(tt)
16
+ end
17
+
8
18
  when 'stop' then
9
- tt.stop
19
+ unless tt.stop
20
+ puts 'Not tracking'
21
+ end
22
+
10
23
  when 'status' then
11
- tt.status ARGV[1]
24
+ status = tt.status(ARGV[1])
25
+ unless status.nil?
26
+ puts 'Total time tracked for task "%s": %d seconds' % [status[:task], status[:elapsed]]
27
+ else
28
+ puts "Currently not tracking"
29
+ end
30
+
12
31
  when 'init' then
32
+ puts 'Initlializing empty sqlite database: %s' % databasename
13
33
  tt.init
34
+
14
35
  when 'report' then
15
- tt.report ARGV[1]
16
- when 'beguin' then
17
- timestamp = ARGV[3] ? "%s %s" % ARGV[2..3] : ARGV[2]
18
- tt.setstart ARGV[1], timestamp
36
+ report = tt.report(ARGV[1])
37
+ unless report.nil?
38
+ report.each do |line|
39
+ puts line
40
+ end
41
+ end
42
+
43
+ when 'begin' then
44
+ begin
45
+ timestamp = ARGV[3] ? "%s %s" % ARGV[2..3] : ARGV[2]
46
+ tt.set_tstart! ARGV[1], timestamp
47
+ rescue Exception => e
48
+ usage(tt)
49
+ end
50
+
19
51
  when 'end' then
20
- timestamp = ARGV[3] ? "%s %s" % ARGV[2..3] : ARGV[2]
21
- tt.setstop ARGV[1], timestamp
22
- when 'help' then
23
- tt.usage
24
- else tt.start ARGV[0], ARGV[1]
52
+ begin
53
+ timestamp = ARGV[3] ? "%s %s" % ARGV[2..3] : ARGV[2]
54
+ tt.set_tstop! ARGV[1], timestamp
55
+ rescue
56
+ usage(tt)
57
+ end
58
+
59
+ else
60
+ usage(tt)
25
61
  end
@@ -3,8 +3,8 @@ require 'sqlite3'
3
3
 
4
4
  class TTrack::DataStore
5
5
 
6
- def initialize dbname=".timetrackerdb", timezone="+02:00", version='v0.1.4', verbosity=0
7
- @dbname = "%s/%s" % [ENV['HOME'], dbname]
6
+ def initialize dbname, timezone="+02:00", version='v0.1.4', verbosity=0
7
+ @dbname = dbname
8
8
  @timezone = timezone
9
9
  @version = version
10
10
  @verbosity = verbosity
@@ -63,7 +63,7 @@ class TTrack::DataStore
63
63
  public
64
64
  def getcurrent
65
65
  current = @db.execute "SELECT current FROM system"
66
- current[0][0] == "" ? nil : current[0][0]
66
+ current[0][0] == "" ? false : current[0][0]
67
67
  end
68
68
 
69
69
  def getstatus
@@ -76,7 +76,7 @@ class TTrack::DataStore
76
76
  end
77
77
  end
78
78
 
79
- def startnew issuename, notes=''
79
+ def startnew issuename, notes
80
80
  @db.execute "INSERT INTO timesheet ( name, notes ) VALUES ( '%s', '%s' )" %
81
81
  [issuename, notes]
82
82
  latest = @db.execute "SELECT id FROM timesheet ORDER BY id DESC LIMIT 1"
@@ -88,8 +88,9 @@ class TTrack::DataStore
88
88
  if current
89
89
  @db.execute "UPDATE timesheet set tstop = datetime('now') WHERE id = %d" % current
90
90
  @db.execute "UPDATE system SET current = '', latest = '%d'" % current
91
+ true
91
92
  else
92
- p "Not tracking"
93
+ false
93
94
  end
94
95
  end
95
96
 
@@ -104,7 +105,7 @@ class TTrack::DataStore
104
105
  end
105
106
 
106
107
  def gettimesheet
107
- @db.execute "SELECT id,name,tstart,tstop,notes FROM timesheet"
108
+ @db.execute "SELECT id,name,tstart,tstop,synced,notes FROM timesheet"
108
109
  end
109
110
 
110
111
  def gettimesbyissuename issuename
@@ -115,8 +116,8 @@ class TTrack::DataStore
115
116
  @db.execute "SELECT tstart,tstop FROM timesheet WHERE name='%s'" % issuename
116
117
  end
117
118
 
118
- def getissuesidsbyname issuename
119
- @db.execute "SELECT id,tstart,tstop,notes FROM timesheet WHERE name='%s'" % issuename
119
+ def getissuesbyname issuename
120
+ @db.execute "SELECT id,tstart,tstop,synced, notes FROM timesheet WHERE name='%s'" % issuename
120
121
  end
121
122
 
122
123
  def settstart id, tstart
data/lib/ttrack.rb CHANGED
@@ -7,89 +7,106 @@
7
7
  class TTrack
8
8
 
9
9
  # At each call we need to declare the datastore
10
- def initialize
11
- @db = DataStore.new
10
+ def initialize datastore
11
+ @db = DataStore.new datastore
12
12
  end
13
13
 
14
- def start issuename, notes
15
- unless issuename.nil?
16
- if @db.getcurrent
17
- @db.stoprunning
18
- end
14
+ # Start tracking an issue, stop the current one if running
15
+ # Return boolean to indicate if the action was succesfull or not
16
+ def start issuename, notes=''
17
+ unless issuename.nil? or issuename.empty?
18
+ @db.stoprunning if @db.getcurrent
19
19
  @db.startnew issuename, notes
20
+ true
20
21
  else
21
- usage
22
+ false
22
23
  end
23
24
  end
24
25
 
26
+ # Stop the current running task
27
+ # Return false if no current running task, true otherwise
25
28
  def stop
26
- @db.stoprunning
29
+ @db.stoprunning ? true : false
27
30
  end
28
31
 
29
- def status issuename
30
- unless issuename
32
+ # Get current running task status if no issuename given
33
+ # Otherwise get total duration for named task (i.e. for any matching time entry)
34
+ def status issuename=nil
35
+ if issuename.nil?
31
36
  status = @db.getstatus
32
37
  if status
33
38
  delta = Time.now - gettstart(@db.getcurrent)
34
- puts "Tracking task '%s' | Run time: %d seconds (%.2f hours)" % [status[1], delta, delta / 3600]
35
- else
36
- puts "Not tracking"
39
+ {:task => status[1], :elapsed => delta}
37
40
  end
38
41
  else
39
42
  total = gettotalissueduration(issuename)
40
- unless total == 0
41
- puts "Total seconds for task %s: %d (%.2f hours)" %
42
- [
43
- issuename,
44
- total,
45
- total / 3600
46
- ]
47
- else
48
- puts "No entry for issue '%s'" % issuename
43
+ unless total.nil?
44
+ {:task => issuename, :elapsed => total}
49
45
  end
50
46
  end
51
47
  end
52
48
 
53
- def report issuename
54
- if issuename.nil?
49
+ # Get a report for the given issue name
50
+ # If no issue is give, get a report for everything
51
+ def report issuename=nil
52
+ result = Array.new
53
+
54
+ if issuename.nil? or issuename.empty?
55
55
  r = @db.gettimesheet
56
- pstring = "%s | %s | %s | %s | %s | %d"
57
- puts "id | name | tstart | tstop | notes | duration"
58
- else
59
- r = @db.getissuesidsbyname issuename
60
- pstring = "%s | %s | %s | %s | %d"
61
- puts "id | tstart | tstop | notes | duration"
62
- end
63
- unless r[0].nil?
64
- r.each do |x|
65
- x << getissueduration(x[0])
66
- puts pstring % x
56
+ r.each do |line|
57
+ result << {
58
+ :task => line[0],
59
+ :name => line[1],
60
+ :tstart => line[2],
61
+ :tstop => line[3],
62
+ :synced => line[4],
63
+ :notes => line[5],
64
+ :elapsed => getissueduration(line[0])
65
+ }
67
66
  end
68
67
  else
69
- usage
68
+ r = @db.getissuesbyname issuename
69
+ r.each do |line|
70
+ result << {
71
+ :task => line[0],
72
+ :tstart => line[1],
73
+ :tstop => line[2],
74
+ :synced => line[3],
75
+ :notes => line[4],
76
+ :elapsed => getissueduration(line[0])
77
+ }
78
+ end
70
79
  end
80
+ result.empty? ? nil : result
71
81
  end
72
82
 
73
- def setstart issueid, timestamp
74
- if validateissueid issueid
75
- t = validatetimestamp timestamp
76
- @db.settstart issueid, t
83
+ # Overwrite tstart for given issue with given timestamp
84
+ def set_tstart! issueid, timestamp
85
+ if validateissueid(issueid)
86
+ t = validatetimestamp(timestamp)
87
+ @db.settstart(issueid, t)
88
+ true
89
+ else
90
+ false
77
91
  end
78
92
  end
79
93
 
80
- def setstop issueid, timestamp
94
+ # Override tstop for given issue
95
+ def set_tstop! issueid, timestamp
81
96
  if validateissueid issueid
82
97
  t = validatetimestamp timestamp
83
98
  @db.settstop issueid, t
84
99
  end
85
100
  end
86
101
 
102
+ # Cleanup the database
87
103
  def init
88
104
  @db.cleanup
89
105
  end
90
106
 
91
- def usage
92
- puts "Usage: ttrack [start|stop|status|init|report|begin|end] issue_name"
107
+ # List of available public methods
108
+ def commands
109
+ [:start, :stop, :status, :init, :report, :begin, :end]
93
110
  end
94
111
 
95
112
  private
@@ -145,7 +162,7 @@ class TTrack
145
162
  total = total + (t1 - t0)
146
163
  end
147
164
  end
148
- total
165
+ r[0].nil? ? nil : total
149
166
  end
150
167
 
151
168
  def getissueduration issueid
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ttrack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-08 00:00:00.000000000Z
12
+ date: 2012-10-13 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sqlite3
16
- requirement: &70310507347300 !ruby/object:Gem::Requirement
16
+ requirement: &70131255576880 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: 1.3.6
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70310507347300
24
+ version_requirements: *70131255576880
25
25
  description: A simple CLI time tracker
26
26
  email: alexander.fortin@gmail.com
27
27
  executables: