/
step_06_generate_metrics.rb
130 lines (109 loc) · 4.5 KB
/
step_06_generate_metrics.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
65
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env ruby
require 'date'
require 'net/http'
require 'json'
require 'uri'
require './lib/utils.rb'
stats_dataset = Utils::load_metadata()
metrics = []
def add_metric(metrics, metric, value)
puts "#{metric}: #{value}".colorize(:light_blue)
metrics.push({:metric => metric, :value => value})
end
# Total Breakdowns
add_metric(metrics, "Total Things Watched", stats_dataset.select {|row| row[:runtime] != ''}.size)
add_metric(metrics, "Total Movies Watched", stats_dataset.select {|row| row[:type] == "movie" }.size)
add_metric(metrics, "Total Shows Watched", stats_dataset.select {|row| row[:type] == "series" }.size)
add_metric(metrics, "Total Series Watched", stats_dataset.uniq! {|row| row[:series_title] }.size - stats_dataset.select {|row| row[:type] == "movie" }.size)
# Get watch times
minutes_watched = stats_dataset.map {|row| row[:runtime].to_i}.reduce(0, :+)
add_metric(metrics, "Minutes Watched", minutes_watched)
add_metric(metrics, "Hours Watched", minutes_watched / 60)
add_metric(metrics, "Days Watched", (minutes_watched / 60) / 24)
# Overview
first_watch = stats_dataset.sort_by { |row| row[:date] }.first[:date]
last_watch = stats_dataset.sort_by { |row| row[:date] }.last[:date]
days_on_netflix = (last_watch - first_watch).to_i
hours_on_netflix = days_on_netflix * 24
add_metric(metrics, "First Watch", first_watch)
add_metric(metrics, "Last Watch", last_watch)
add_metric(metrics, "Years On Netflix", (days_on_netflix / 365.0).to_f)
add_metric(metrics, "Days On Netflix", days_on_netflix)
add_metric(metrics, "Hours On Netflix", hours_on_netflix)
add_metric(metrics, "Percentage Of All Time", ((minutes_watched / 60).to_f / hours_on_netflix.to_f) * 100.0)
# Assumes the average person sleeps 8 hours at night and works 8 hours at day
add_metric(metrics, "Percentage Of A Normal Sleepers Time", ((minutes_watched / 60).to_f / (days_on_netflix * 8).to_f) * 100.0)
first_day = stats_dataset.sort_by { |row| row[:date] }.first[:date]
last_day = stats_dataset.sort_by { |row| row[:date] }.last[:date]
daily_time_series = []
max_streak = { :start => nil, :end => nil, :streak => 0 }
current_streak = nil
# Daily Time Series
first_day.upto(last_day) do |date|
watches_on_date = stats_dataset.select {|row| row[:date] == date }
if watches_on_date.size > 0
if current_streak.nil?
# puts "start streak: #{date.to_s}"
current_streak = { :start => date }
else
# puts "streak: #{date.to_s}"
end
else
if !current_streak.nil?
# puts "--- end streak"
# get the length
streak_length = (date - current_streak[:start]).to_i
if streak_length > max_streak[:streak]
# puts "------- new record: #{streak_length}"
max_streak = { :start => current_streak[:start], :end => date, :streak => streak_length}
else
# puts "-- not record: #{streak_length}"
end
current_streak = nil
end
end
end
add_metric(metrics, "Streak Start", max_streak[:start])
add_metric(metrics, "Streak End", max_streak[:end])
add_metric(metrics, "Highest Streak Of Days In A Row", max_streak[:streak])
# Weekly Averages
# Sunday: average watch times
# Monday: average watch times
# Tuesday: average watch times
# Wednesday: average watch times
# Thursday: average watch times
# Friday: average watch times
# Saturday: average watch times
# Sunday: total watch times
# Monday: total watch times
# Tuesday: total watch times
# Wednesday: total watch times
# Thursday: total watch times
# Friday: total watch times
# Saturday: total watch times
# Sunday: % of total
# Monday: % of total
# Tuesday: % of total
# Wednesday: % of total
# Thursday: % of total
# Friday: % of total
# Saturday: % of total
# puts "date of first item watch: #{first_day}"
# puts "date of last item watch: #{last_day}"
# puts "days on netflix: #{days_on_netflix}"
# puts "total potential hours: #{total_potential_hours}"
# puts "minutes watching: #{minutes_watching}"
# puts "hours watching: #{hours_watching}"
# puts "days watching: #{days_watching}"
# puts "percentage of all time watching netflix: #{watch_hour_percentage}"
# puts "number of total things: #{total_items_watched}"
# puts "total movies watched: #{total_movies_watched}"
# puts "total tv shows watched: #{total_shows_watched}"
# puts "total series watched: #{total_series_watched}"
# puts "items needing triage: #{stats_dataset.select {|row| row[:triage] == "true" }.size}"
File.open(METRICS_OUTPUT, "w") do |file|
file.puts "Metric,Value"
metrics.each do |row|
file.puts "#{row[:metric]},#{row[:value]}"
end
end