Make it more obvious that we're using dates in charts

The `shared_keys` and `k` variable names could mean anything, so it
wasn't clear what these variables contained.
This commit is contained in:
Javi Martín
2024-05-08 17:44:16 +02:00
parent d44cff630d
commit 86e131df26

View File

@@ -13,19 +13,19 @@ module Ahoy
# chart # chart
def add(name, collection) def add(name, collection)
collections.push data: collection, name: name collections.push data: collection, name: name
shared_keys.merge(collection.keys) dates.merge(collection.keys)
end end
def build def build
data = { x: [] } data = { x: [] }
shared_keys.each do |k| dates.each do |date|
# Add the key with a valid date format # Add the key with a valid date format
data[:x].push k.strftime("%Y-%m-%d") data[:x].push date.strftime("%Y-%m-%d")
# Add the value for each column, or 0 if not present # Add the value for each column, or 0 if not present
collections.each do |col| collections.each do |col|
data[col[:name]] ||= [] data[col[:name]] ||= []
count = col[:data][k] || 0 count = col[:data][date] || 0
data[col[:name]].push count data[col[:name]].push count
end end
end end
@@ -39,8 +39,8 @@ module Ahoy
@collections ||= [] @collections ||= []
end end
def shared_keys def dates
@shared_keys ||= Set.new @dates ||= Set.new
end end
end end
end end