Class: PostRunner::MonitoringStatistics

Inherits:
Object
  • Object
show all
Includes:
Fit4Ruby::Converters
Defined in:
lib/postrunner/MonitoringStatistics.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) ⇒ MonitoringStatistics

Create a new MonitoringStatistics object.

Parameters:

  • monitoring_files (Array of Fit4Ruby::Monitoring_B)

    FIT files



30
31
32
33
34
# File 'lib/postrunner/MonitoringStatistics.rb', line 30

def initialize(monitoring_files)
  @monitoring_files = monitoring_files
  # Week starts on Monday
  @first_day_of_week = 1
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.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/postrunner/MonitoringStatistics.rb', line 38

def daily(day)
  sleep_analyzer = DailySleepAnalyzer.new(@monitoring_files, day,
                                          +12 * 60 * 60)
  monitoring_analyzer = DailyMonitoringAnalyzer.new(@monitoring_files, day)

  str = "Daily Monitoring Report for #{day}\n\n" +
        "#{daily_goals_table(monitoring_analyzer)}\n" +
        "#{daily_stats_table(monitoring_analyzer, sleep_analyzer)}\n"
  if sleep_analyzer.sleep_cycles.empty?
    str += 'No sleep data available for this day'
  else
    str += "Sleep Statistics for " +
           "#{sleep_analyzer.window_start_time.strftime('%Y-%m-%d')} - " +
           "#{sleep_analyzer.window_end_time.strftime('%Y-%m-%d')}\n\n" +
      daily_sleep_cycle_table(sleep_analyzer).to_s
  end

  str
end

#daily_html(day, doc) ⇒ Object

Generate a report for a certain day.

Parameters:

  • day (String)

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/postrunner/MonitoringStatistics.rb', line 60

def daily_html(day, doc)
  sleep_analyzer = DailySleepAnalyzer.new(@monitoring_files, day,
                                          +12 * 60 * 60)
  monitoring_analyzer = DailyMonitoringAnalyzer.new(@monitoring_files, day)

  doc.div {
    doc.h2("Daily Monitoring Report for #{day}")
    daily_goals_table(monitoring_analyzer).to_html(doc)
    doc.br
    daily_stats_table(monitoring_analyzer, sleep_analyzer).to_html(doc)

    if sleep_analyzer.sleep_cycles.empty?
      doc.h3('No sleep data available for this day')
    else
      doc.h2("Sleep Statistics for " +
             "#{sleep_analyzer.window_start_time.strftime('%Y-%m-%d')} - " +
             "#{sleep_analyzer.window_end_time.strftime('%Y-%m-%d')}")
      daily_sleep_cycle_table(sleep_analyzer).to_html(doc)
    end
  }
end

#monthly(day) ⇒ Object

Generate a report for a certain month.

Parameters:

  • day (String)

    Date of a day in that months as YYYY-MM-DD string.



93
94
95
96
97
98
99
100
101
102
# File 'lib/postrunner/MonitoringStatistics.rb', line 93

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

  "Monitoring Statistics for #{day_as_time.strftime('%B %Y')}\n\n" +
    monthly_goal_table(year, month, last_day_of_month).to_s + "\n" +
    monthly_sleep_table(year, month, last_day_of_month).to_s
end

#weekly(start_day) ⇒ Object

Generate a report for a certain week.

Parameters:

  • start_day (String)

    Date of a day in that week as YYYY-MM-DD string.



84
85
86
87
88
89
# File 'lib/postrunner/MonitoringStatistics.rb', line 84

def weekly(start_day)
  "Monitoring Statistics for the week of " +
    "#{start_day.strftime('%Y-%m-%d')}\n\n" +
    weekly_goal_table(start_day).to_s + "\n" +
    weekly_sleep_table(start_day).to_s
end