reply-hour-histogram 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +74 -0
- data/bin/reply-hour-histogram +18 -0
- data/lib/reply_hour_histogram.rb +151 -0
- metadata +62 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: db550bd19b5316db1a23eea0c1b4fb09024fad4600fb4908b50f8f5de699bde4
|
|
4
|
+
data.tar.gz: df4c2e2525c38fb1fb8f5928e58eb6a63310e48a51f84722556c8172e634d828
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 076822960a3f48ff6f7b98b00c6fb9540ac89b3efb2443e41ba395513278470c2561bb8602f5483d824f5b1c8c089a37bb648046daac85a9fa0e690f5f561669
|
|
7
|
+
data.tar.gz: 3cd44117ff11604684e4d7eb0eb21b792bcabf83d2c0361a7728a4e58aba6ab71969eabfae3efab46bbe9ea86594fe9cb98553ce0cbe57ab7184ea10c85c69b4
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 OneGTMLab
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# reply-hour-histogram
|
|
2
|
+
|
|
3
|
+
Ruby command line tool and library. Give it a CSV of replies with the date and time of each one and the campaign it belongs to. It counts the replies per hour of the day with a bar, lists the busiest hours, counts the replies per weekday, and shows the replies and the busiest hour of each campaign. It counts the replies you give and says nothing about the best time to send: a busy hour can simply be the hour you sent the most.
|
|
4
|
+
|
|
5
|
+
Part of the [OneGTMLab](https://www.onegtmlab.dev) free GTM tools. Background on the idea: [lemlist](https://www.onegtmlab.dev/tool-directory/sending/lemlist).
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
gem install reply-hour-histogram
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Use
|
|
14
|
+
|
|
15
|
+
CSV columns: reply_id, replied_at, campaign. replied_at is written yyyy-mm-dd HH:mm (24 hour clock) and all values must be in one and the same time zone of your choice; the tool reads the hour as written and does no time zone conversion. The weekday comes from the calendar date, Monday to Sunday. Hours run from 00 to 23: each shows its count, its share of all replies (rounded half up to 1 decimal) and a bar of # characters scaled so that the busiest hour is 20 wide, rounded half up, with at least 1 # for any hour that has a reply. Option --top N (a whole number of 1 or more, default 3, set by you) lists the N busiest hours, highest count first and the earlier hour first on a tie; hours with no replies are never listed, so fewer than N lines can appear. Campaign labels are trimmed and compared ignoring upper and lower case, and the first spelling seen is shown; campaigns are sorted by replies, highest first, then by name, and each shows its busiest hour with the number of its replies in that hour (the earlier hour wins a tie). A missing file, a missing column, a bad or impossible date or time or an empty campaign (the row is named), an empty or duplicate reply_id, a bad --top, an unknown option or a CSV with no rows stops the run with an error and a non-zero exit code.
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
$ ruby bin/reply-hour-histogram sample.csv --top 3
|
|
19
|
+
replies: 12
|
|
20
|
+
time zone: none applied, hours are as written in the file
|
|
21
|
+
|
|
22
|
+
hour count share bar
|
|
23
|
+
00 0 0.0%
|
|
24
|
+
01 0 0.0%
|
|
25
|
+
02 0 0.0%
|
|
26
|
+
03 0 0.0%
|
|
27
|
+
04 0 0.0%
|
|
28
|
+
05 0 0.0%
|
|
29
|
+
06 0 0.0%
|
|
30
|
+
07 0 0.0%
|
|
31
|
+
08 0 0.0%
|
|
32
|
+
09 5 41.7% ####################
|
|
33
|
+
10 1 8.3% ####
|
|
34
|
+
11 0 0.0%
|
|
35
|
+
12 0 0.0%
|
|
36
|
+
13 0 0.0%
|
|
37
|
+
14 4 33.3% ################
|
|
38
|
+
15 0 0.0%
|
|
39
|
+
16 1 8.3% ####
|
|
40
|
+
17 0 0.0%
|
|
41
|
+
18 0 0.0%
|
|
42
|
+
19 0 0.0%
|
|
43
|
+
20 1 8.3% ####
|
|
44
|
+
21 0 0.0%
|
|
45
|
+
22 0 0.0%
|
|
46
|
+
23 0 0.0%
|
|
47
|
+
|
|
48
|
+
busiest hours (top 3, set by you)
|
|
49
|
+
1. 09:00 count 5 41.7%
|
|
50
|
+
2. 14:00 count 4 33.3%
|
|
51
|
+
3. 10:00 count 1 8.3%
|
|
52
|
+
|
|
53
|
+
weekday
|
|
54
|
+
Monday 3 25.0%
|
|
55
|
+
Tuesday 2 16.7%
|
|
56
|
+
Wednesday 2 16.7%
|
|
57
|
+
Thursday 2 16.7%
|
|
58
|
+
Friday 1 8.3%
|
|
59
|
+
Saturday 1 8.3%
|
|
60
|
+
Sunday 1 8.3%
|
|
61
|
+
|
|
62
|
+
campaign replies busiest hour (replies in it)
|
|
63
|
+
Alpha 6 09:00 (4)
|
|
64
|
+
Beta 5 14:00 (3)
|
|
65
|
+
Gamma 1 20:00 (1)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Test
|
|
69
|
+
|
|
70
|
+
```
|
|
71
|
+
ruby test/run.rb
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Offline, no network calls, no dependencies beyond the language runtime. MIT licensed.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require_relative '../lib/reply_hour_histogram'
|
|
5
|
+
|
|
6
|
+
if ARGV.empty? || ARGV.include?('-h') || ARGV.include?('--help')
|
|
7
|
+
warn "usage: reply-hour-histogram replies.csv [--top N]\n" \
|
|
8
|
+
"CSV columns: reply_id, replied_at (yyyy-mm-dd HH:mm), campaign. More: https://www.onegtmlab.dev/tool-directory/sending/lemlist"
|
|
9
|
+
exit 2
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
begin
|
|
13
|
+
path, top = ReplyHourHistogram.parse_args(ARGV)
|
|
14
|
+
replies = ReplyHourHistogram.from_csv(File.read(path))
|
|
15
|
+
print ReplyHourHistogram.render(ReplyHourHistogram.analyze(replies, top: top))
|
|
16
|
+
rescue ArgumentError, SystemCallError, CSV::MalformedCSVError => e
|
|
17
|
+
abort "error: #{e.message}"
|
|
18
|
+
end
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'csv'
|
|
4
|
+
require 'date'
|
|
5
|
+
|
|
6
|
+
# Count the replies YOU give by hour of day, weekday and campaign. It counts only: it says nothing about the best time to send.
|
|
7
|
+
# All times are read as written, in one and the same time zone; no zone conversion is done.
|
|
8
|
+
module ReplyHourHistogram
|
|
9
|
+
WEEKDAYS = %w[Monday Tuesday Wednesday Thursday Friday Saturday Sunday].freeze
|
|
10
|
+
BAR_WIDTH = 20
|
|
11
|
+
DEFAULT_TOP = 3
|
|
12
|
+
STAMP = /\A(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})\z/.freeze
|
|
13
|
+
|
|
14
|
+
Reply = Struct.new(:id, :year, :month, :day, :hour, :minute, :campaign, keyword_init: true) do
|
|
15
|
+
# Monday is 0 and Sunday is 6.
|
|
16
|
+
def weekday
|
|
17
|
+
(Date.new(year, month, day).wday + 6) % 7
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Percentage of n in d, rounded half up to 1 decimal. 0.0 when d is 0.
|
|
22
|
+
def self.pct(n, d)
|
|
23
|
+
return 0.0 if d.zero?
|
|
24
|
+
|
|
25
|
+
Rational(n * 100, d).round(1).to_f
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Bar width: count scaled so that the largest count is 20 wide, rounded half up; at least 1 for any non-zero count.
|
|
29
|
+
def self.bar_width(count, max)
|
|
30
|
+
return 0 if count.zero?
|
|
31
|
+
|
|
32
|
+
[Rational(count * BAR_WIDTH, max).round, 1].max
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def self.parse_stamp(raw, line)
|
|
36
|
+
s = raw.to_s.strip
|
|
37
|
+
m = STAMP.match(s)
|
|
38
|
+
raise ArgumentError, "row #{line}: bad date-time #{s.inspect}, expected yyyy-mm-dd HH:mm" unless m
|
|
39
|
+
|
|
40
|
+
y, mo, d, h, mi = m.captures.map(&:to_i)
|
|
41
|
+
raise ArgumentError, "row #{line}: impossible date #{s.inspect}" unless Date.valid_date?(y, mo, d)
|
|
42
|
+
raise ArgumentError, "row #{line}: impossible time #{s.inspect}" unless h < 24 && mi < 60
|
|
43
|
+
|
|
44
|
+
[y, mo, d, h, mi]
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def self.parse_top(raw)
|
|
48
|
+
s = raw.to_s.strip
|
|
49
|
+
raise ArgumentError, "bad --top #{raw.inspect}, expected a whole number of 1 or more" unless s.match?(/\A\d+\z/) && s.to_i >= 1
|
|
50
|
+
|
|
51
|
+
s.to_i
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Returns an array of Reply in file order. Raises ArgumentError on bad input.
|
|
55
|
+
def self.from_csv(text)
|
|
56
|
+
table = CSV.parse(text.sub(/\A/, ''), headers: true, header_converters: ->(h) { h.to_s.strip.downcase })
|
|
57
|
+
missing = %w[reply_id replied_at campaign] - table.headers.compact
|
|
58
|
+
raise ArgumentError, "missing column(s): #{missing.join(', ')}" unless missing.empty?
|
|
59
|
+
raise ArgumentError, 'no rows in the CSV' if table.empty?
|
|
60
|
+
|
|
61
|
+
seen = {}
|
|
62
|
+
table.each_with_index.map do |r, i|
|
|
63
|
+
line = i + 2
|
|
64
|
+
id = r['reply_id'].to_s.strip
|
|
65
|
+
raise ArgumentError, "row #{line}: empty reply_id" if id.empty?
|
|
66
|
+
raise ArgumentError, "duplicate reply_id #{id.inspect} (row #{line})" if seen[id]
|
|
67
|
+
|
|
68
|
+
seen[id] = true
|
|
69
|
+
y, mo, d, h, mi = parse_stamp(r['replied_at'], line)
|
|
70
|
+
campaign = r['campaign'].to_s.strip
|
|
71
|
+
raise ArgumentError, "row #{line}: empty campaign for reply #{id.inspect}" if campaign.empty?
|
|
72
|
+
|
|
73
|
+
Reply.new(id: id, year: y, month: mo, day: d, hour: h, minute: mi, campaign: campaign)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Busiest hour of a list of replies: highest count, the earlier hour on a tie. Returns [hour, count].
|
|
78
|
+
def self.busiest_hour(replies)
|
|
79
|
+
counts = Hash.new(0)
|
|
80
|
+
replies.each { |r| counts[r.hour] += 1 }
|
|
81
|
+
counts.min_by { |h, c| [-c, h] }
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def self.analyze(replies, top: DEFAULT_TOP)
|
|
85
|
+
total = replies.size
|
|
86
|
+
hour_counts = Array.new(24, 0)
|
|
87
|
+
replies.each { |r| hour_counts[r.hour] += 1 }
|
|
88
|
+
max = hour_counts.max
|
|
89
|
+
hours = (0..23).map do |h|
|
|
90
|
+
{ hour: h, count: hour_counts[h], share: pct(hour_counts[h], total), bar: '#' * bar_width(hour_counts[h], max) }
|
|
91
|
+
end
|
|
92
|
+
busiest = hours.select { |h| h[:count].positive? }.sort_by { |h| [-h[:count], h[:hour]] }.first(top)
|
|
93
|
+
day_counts = Array.new(7, 0)
|
|
94
|
+
replies.each { |r| day_counts[r.weekday] += 1 }
|
|
95
|
+
weekdays = WEEKDAYS.each_with_index.map { |n, i| { name: n, count: day_counts[i], share: pct(day_counts[i], total) } }
|
|
96
|
+
groups = {}
|
|
97
|
+
replies.each { |r| (groups[r.campaign.downcase] ||= { name: r.campaign, replies: [] })[:replies] << r }
|
|
98
|
+
campaigns = groups.values.map do |g|
|
|
99
|
+
hour, count = busiest_hour(g[:replies])
|
|
100
|
+
{ name: g[:name], replies: g[:replies].size, busiest_hour: hour, busiest_count: count }
|
|
101
|
+
end
|
|
102
|
+
campaigns.sort_by! { |c| [-c[:replies], c[:name].downcase, c[:name]] }
|
|
103
|
+
{ replies: total, hours: hours, top: top, busiest: busiest, weekdays: weekdays, campaigns: campaigns }
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def self.hh(hour)
|
|
107
|
+
format('%02d', hour)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def self.render(a)
|
|
111
|
+
out = +"replies: #{a[:replies]}\n"
|
|
112
|
+
out << "time zone: none applied, hours are as written in the file\n\n"
|
|
113
|
+
out << format("%-4s %6s %7s %s\n", 'hour', 'count', 'share', 'bar')
|
|
114
|
+
a[:hours].each { |h| out << format("%-4s %6d %6.1f%% %s\n", hh(h[:hour]), h[:count], h[:share], h[:bar]).rstrip << "\n" }
|
|
115
|
+
out << "\nbusiest hours (top #{a[:top]}, set by you)\n"
|
|
116
|
+
a[:busiest].each_with_index do |h, i|
|
|
117
|
+
out << format(" %d. %s:00 count %d %5.1f%%\n", i + 1, hh(h[:hour]), h[:count], h[:share])
|
|
118
|
+
end
|
|
119
|
+
out << "\nweekday\n"
|
|
120
|
+
a[:weekdays].each { |d| out << format(" %-10s %6d %6.1f%%\n", d[:name], d[:count], d[:share]) }
|
|
121
|
+
out << "\ncampaign replies busiest hour (replies in it)\n"
|
|
122
|
+
a[:campaigns].each do |c|
|
|
123
|
+
out << format(" %-16s %6d %s:00 (%d)\n", c[:name], c[:replies], hh(c[:busiest_hour]), c[:busiest_count])
|
|
124
|
+
end
|
|
125
|
+
out
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Returns [path, top]; raises ArgumentError on bad input.
|
|
129
|
+
def self.parse_args(argv)
|
|
130
|
+
args = argv.dup
|
|
131
|
+
path = nil
|
|
132
|
+
top = DEFAULT_TOP
|
|
133
|
+
until args.empty?
|
|
134
|
+
arg = args.shift
|
|
135
|
+
case arg
|
|
136
|
+
when '--top'
|
|
137
|
+
raise ArgumentError, '--top needs a number' if args.empty?
|
|
138
|
+
|
|
139
|
+
top = parse_top(args.shift)
|
|
140
|
+
when /\A-/ then raise ArgumentError, "unknown option #{arg}"
|
|
141
|
+
else
|
|
142
|
+
raise ArgumentError, "unexpected extra argument #{arg.inspect}" if path
|
|
143
|
+
|
|
144
|
+
path = arg
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
raise ArgumentError, 'no CSV file given' unless path
|
|
148
|
+
|
|
149
|
+
[path, top]
|
|
150
|
+
end
|
|
151
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: reply-hour-histogram
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- OneGTMLab
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: csv
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
-
|
|
17
|
+
- ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: "0"
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
-
|
|
25
|
+
- ">="
|
|
26
|
+
- !ruby/object:Gem::Version
|
|
27
|
+
version: "0"
|
|
28
|
+
description: "Ruby CLI and library: from a CSV of reply ids, yyyy-mm-dd HH:mm timestamps and campaign labels, count replies per hour (with a bar), the busiest hours, replies per weekday, and replies and busiest hour per campaign. It counts only and says nothing about the best time to send. Offline."
|
|
29
|
+
executables:
|
|
30
|
+
- reply-hour-histogram
|
|
31
|
+
extensions: []
|
|
32
|
+
extra_rdoc_files: []
|
|
33
|
+
files:
|
|
34
|
+
- LICENSE
|
|
35
|
+
- README.md
|
|
36
|
+
- bin/reply-hour-histogram
|
|
37
|
+
- lib/reply_hour_histogram.rb
|
|
38
|
+
homepage: "https://www.onegtmlab.dev/tool-directory/sending/lemlist"
|
|
39
|
+
licenses:
|
|
40
|
+
- MIT
|
|
41
|
+
metadata:
|
|
42
|
+
source_code_uri: "https://github.com/jaizendra-netizen/reply-hour-histogram"
|
|
43
|
+
rdoc_options: []
|
|
44
|
+
require_paths:
|
|
45
|
+
- lib
|
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
47
|
+
requirements:
|
|
48
|
+
-
|
|
49
|
+
- ">="
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: "2.7"
|
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
53
|
+
requirements:
|
|
54
|
+
-
|
|
55
|
+
- ">="
|
|
56
|
+
- !ruby/object:Gem::Version
|
|
57
|
+
version: "0"
|
|
58
|
+
requirements: []
|
|
59
|
+
rubygems_version: 4.1.0
|
|
60
|
+
specification_version: 4
|
|
61
|
+
summary: "Count replies by hour of day, weekday and campaign from a CSV of reply timestamps"
|
|
62
|
+
test_files: []
|