Class: Twstats::CSVReader

Inherits:
Object
  • Object
show all
Defined in:
lib/twstats/csv_reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ CSVReader

Create an object structure from the csv file which allows to easily read the content



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/twstats/csv_reader.rb', line 12

def initialize(file)
  @logs = Set.new([])
  @tags = Set.new([])
  @people = Set.new([])
  @tasks = Set.new([])
  @projects = Set.new([])
  # Initialize the reader with a csv file and fill the times as an array of columns
  CSV.foreach(file, DEFAULT_OPTIONS) do |row|
    update_info TwLog.new(row)
  end
end

Instance Attribute Details

#logsObject (readonly)

Returns the value of attribute logs.



6
7
8
# File 'lib/twstats/csv_reader.rb', line 6

def logs
  @logs
end

#peopleObject (readonly)

Returns the value of attribute people.



8
9
10
# File 'lib/twstats/csv_reader.rb', line 8

def people
  @people
end

#projectsObject (readonly)

Returns the value of attribute projects.



10
11
12
# File 'lib/twstats/csv_reader.rb', line 10

def projects
  @projects
end

#tagsObject (readonly)

Returns the value of attribute tags.



7
8
9
# File 'lib/twstats/csv_reader.rb', line 7

def tags
  @tags
end

#tasksObject (readonly)

Returns the value of attribute tasks.



9
10
11
# File 'lib/twstats/csv_reader.rb', line 9

def tasks
  @tasks
end

Instance Method Details

#get_total_time(object, element, filter_billable) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/twstats/csv_reader.rb', line 44

def get_total_time(object, element, filter_billable)
  billable = 0
  non_billable = 0
  case object
    when :projects
      billable = logs.select{|log| log.project == element && log.billable}.inject(0){ |sum, l| sum + l.decimal_time}
      non_billable = logs.select{|log| log.project == element && !log.billable}.inject(0){ |sum, l| sum + l.decimal_time}
    when :tags
      billable = logs.select{|log| log.tags.include?(element) && log.billable}.inject(0){ |sum, l| sum + l.decimal_time}
      non_billable = logs.select{|log| log.tags.include?(element) && !log.billable}.inject(0){ |sum, l| sum + l.decimal_time}
    when :people
      billable = logs.select{|log| log.who == element && log.billable}.inject(0){ |sum, l| sum + l.decimal_time}
      non_billable = logs.select{|log| log.who == element && !log.billable}.inject(0){ |sum, l| sum + l.decimal_time}
    when :tasks
      billable = logs.select{|log| log.task == element && log.billable}.inject(0){ |sum, l| sum + l.decimal_time}
      non_billable = logs.select{|log| log.task == element && !log.billable}.inject(0){ |sum, l| sum + l.decimal_time}
    when :all
      billable = logs.select{|log| log.billable}.inject(0){ |sum, l| sum + l.decimal_time}
      non_billable = logs.select{|log| !log.billable}.inject(0){ |sum, l| sum + l.decimal_time}
    else
      return -99999
  end
  if filter_billable
    return billable, non_billable
  else
    return billable + non_billable
  end
end

#is_weekly?Boolean

Returns:

  • (Boolean)


73
74
75
76
# File 'lib/twstats/csv_reader.rb', line 73

def is_weekly?
  sorted_logs = logs.map{|l| l.date}.sort
  (sorted_logs[-1] - sorted_logs[0]).to_i <= 7
end

#mean_timeObject



32
33
34
# File 'lib/twstats/csv_reader.rb', line 32

def mean_time
  logs.inject(0) {|sum, log| sum + log.decimal_time }.to_f/logs.size
end

#mean_time_per_taskObject



36
37
38
39
40
41
42
# File 'lib/twstats/csv_reader.rb', line 36

def mean_time_per_task
  time_per_task = []
  tasks.each do |task|
    time_per_task << logs.select{|ll| ll.task == task}.inject(0){|sum, ll| sum + ll.decimal_time}
  end
  time_per_task.inject(0){|sum, x| sum + x}/time_per_task.size
end

#update_info(log) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/twstats/csv_reader.rb', line 24

def update_info(log)
  @projects << log.project
  @people << log.who
  log.tags.each{|x| @tags << x}
  @tasks << log.task
  @logs << log
end