Skip to content

Instantly share code, notes, and snippets.

@Fonsan
Created June 14, 2018 10:56
Show Gist options
  • Save Fonsan/1bc94fd309027c7649e0b378ad853c29 to your computer and use it in GitHub Desktop.
Save Fonsan/1bc94fd309027c7649e0b378ad853c29 to your computer and use it in GitHub Desktop.
require 'json'
require 'time'
counting_hash = Hash.new(0)
line_enumerator = STDIN.each_line
line_enumerator.each do |line|
puts line
end
line_enumerator.each do |line|
timestamp, count_string = JSON.parse(line)
count = Float(count_string)
counting_hash[timestamp] += count
end
# {
# 1514764800000 => 1,
# }
counts_by_timestamp_list = counting_hash.to_a
# [
# [1514764800000, 1]
# ]
sorted_counts_by_timestamp_list = counts_by_timestamp_list.sort_by do |timestamp, value|
# foo
timestamp
end
# [
# [1514764800000, 1]
# ]
time_count_list = sorted_counts_by_timestamp_list.map do |timestamp, count|
time = Time.at(timestamp / 1000).utc
time_string = time.iso8601
[time_string, count]
end
# [
# ["2018-06-11T00:00:00Z", 1]
# ]
list_of_lines = time_count_list.map {|array| array.join("\t") }
# [ [ "2018-06-11T00:00:00Z\t541730.0"] ]
puts list_of_lines.join("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment