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



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

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.



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

def logs
  @logs
end

#peopleObject (readonly)

Returns the value of attribute people.



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

def people
  @people
end

#projectsObject (readonly)

Returns the value of attribute projects.



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

def projects
  @projects
end

#tagsObject (readonly)

Returns the value of attribute tags.



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

def tags
  @tags
end

#tasksObject (readonly)

Returns the value of attribute tasks.



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

def tasks
  @tasks
end

Instance Method Details

#get_total_time(object, element, filter_billable) ⇒ Object



43
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
# File 'lib/twstats/csv_reader.rb', line 43

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)


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

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

#mean_timeObject



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

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

#mean_time_per_taskObject



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

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

#not_tagged_tasksObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/twstats/csv_reader.rb', line 77

def not_tagged_tasks
  total_time = 0
  list = []
  logs.each do |log|
    if log.tags.empty?
      list << log
      total_time += log.decimal_time
    end
  end
  {
      total_time: total_time,
      list: list
  }
end

#update_info(log) ⇒ Object



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

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