Class: PostRunner::SleepStatistics

Inherits:
Object
  • Object
show all
Includes:
Fit4Ruby::Converters
Defined in:
lib/postrunner/SleepStatistics.rb

Overview

This class can be used to generate reports for sleep data. It uses the DailySleepAnalyzer class to compute the data and generates the report for a certain time period.

Instance Method Summary collapse

Constructor Details

#initialize(monitoring_files) ⇒ SleepStatistics

Create a new SleepStatistics object.

Parameters:

  • monitoring_files (Array of Fit4Ruby::Monitoring_B)

    FIT files



29
30
31
# File 'lib/postrunner/SleepStatistics.rb', line 29

def initialize(monitoring_files)
  @monitoring_files = monitoring_files
end

Instance Method Details

#daily(day) ⇒ Object

Generate a report for a certain day.

Parameters:

  • day (String)

    Date of the day as YYYY-MM-DD string.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/postrunner/SleepStatistics.rb', line 35

def daily(day)
  analyzer = DailySleepAnalyzer.new(@monitoring_files, day)

  if analyzer.sleep_intervals.empty?
    return 'No sleep data available for this day'
  end

  ti = FlexiTable.new
  ti.head
  ti.row([ 'From', 'To', 'Sleep phase' ])
  ti.body
  utc_offset = analyzer.utc_offset
  analyzer.sleep_intervals.each do |i|
    ti.cell(i[:from_time].localtime(utc_offset).strftime('%H:%M'))
    ti.cell(i[:to_time].localtime(utc_offset).strftime('%H:%M'))
    ti.cell(i[:phase])
    ti.new_row
  end

  tt = FlexiTable.new
  tt.head
  tt.row([ 'Total Sleep', 'Deep Sleep', 'Light Sleep' ])
  tt.body
  tt.cell(secsToHM(analyzer.total_sleep), { :halign => :right })
  tt.cell(secsToHM(analyzer.deep_sleep), { :halign => :right })
  tt.cell(secsToHM(analyzer.light_sleep), { :halign => :right })
  tt.new_row

  "Sleep Statistics for #{day}\n\n#{ti}\n#{tt}"
end

#monthly(day) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/postrunner/SleepStatistics.rb', line 66

def monthly(day)
  day_as_time = Time.parse(day)
  year = day_as_time.year
  month = day_as_time.month
  last_day_of_month = Date.new(year, month, -1).day

  t = FlexiTable.new
  left = { :halign => :left }
  right = { :halign => :right }
  t.set_column_attributes([ left, right, right, right ])
  t.head
  t.row([ 'Date', 'Total Sleep', 'Deep Sleep', 'Light Sleep' ])
  t.body
  totals = Hash.new(0)
  counted_days = 0

  1.upto(last_day_of_month).each do |dom|
    day_str = Time.new(year, month, dom).strftime('%Y-%m-%d')
    t.cell(day_str)

    analyzer = DailySleepAnalyzer.new(@monitoring_files, day_str)

    if analyzer.sleep_intervals.empty?
      t.cell('-')
      t.cell('-')
      t.cell('-')
    else
      totals[:total_sleep] += analyzer.total_sleep
      totals[:deep_sleep] += analyzer.deep_sleep
      totals[:light_sleep] += analyzer.light_sleep
      counted_days += 1

      t.cell(secsToHM(analyzer.total_sleep))
      t.cell(secsToHM(analyzer.deep_sleep))
      t.cell(secsToHM(analyzer.light_sleep))
    end
    t.new_row
  end
  t.foot
  t.cell('Averages')
  t.cell(secsToHM(totals[:total_sleep] / counted_days))
  t.cell(secsToHM(totals[:deep_sleep] / counted_days))
  t.cell(secsToHM(totals[:light_sleep] / counted_days))
  t.new_row

  "Sleep Statistics for #{day_as_time.strftime('%B')} #{year}\n\n#{t}"
end