Created
June 14, 2018 10:56
-
-
Save Fonsan/1bc94fd309027c7649e0b378ad853c29 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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