diff --git a/visual_playground/api.rb b/visual_playground/api.rb index 8af20ca..2e4de1d 100644 --- a/visual_playground/api.rb +++ b/visual_playground/api.rb @@ -4,18 +4,30 @@ require "date" class API def fetch(key:, group_by:, start_date:) + # + # Modify and verify parameters + # group_by = "year#{group_by}" unless group_by.include?("year") group_by = group_by.to_sym raise "Invalid group_by" unless [:yearmonth, :yearweek].include?(group_by) - # TODO: Filter start date - results = database.fetch("SELECT ?, AVG(value::numeric) as avg from raw_data where key = ? GROUP BY ? ORDER BY ?", group_by, key, group_by, group_by) + raise "`start_date` must be in format '2019-04'" unless start_date.match(/\d\d\d\d\-\d\d/) + start_timestamp = Date.strptime(start_date, "%Y-%m").strftime("%Q") + # + # Run the query + # + results = database.fetch("SELECT ?, AVG(value::numeric) as avg, SUM(value::numeric) as sum from raw_data where key = ? AND timestamp > ? GROUP BY ? ORDER BY ?", group_by, key, start_timestamp, group_by, group_by) + + # + # Return the results + # return { total_count: raw_data.where(key: key).count, grouped_count: results.count, rows: results.to_a.collect do |row| row[:avg] = row[:avg].truncate(5).to_s('F').to_f # convert from BigFloat to float + row[:sum] = row[:sum].truncate(5).to_s('F').to_f # convert from BigFloat to float if group_by == :yearmonth row[:as_date] = Date.strptime(row[:yearmonth].to_s, "%Y%m") else diff --git a/visual_playground/frontend.html b/visual_playground/frontend.html index 30a7afd..e98860c 100644 --- a/visual_playground/frontend.html +++ b/visual_playground/frontend.html @@ -1,36 +1,126 @@ - -
- - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + +
KeyAvg
+
+ +

Attributes

+ + + + + +
Start date
+
-
+
diff --git a/visual_playground/server.rb b/visual_playground/server.rb index 3407e50..d3c478f 100644 --- a/visual_playground/server.rb +++ b/visual_playground/server.rb @@ -19,7 +19,7 @@ get "/data" do JSON.pretty_generate(api.fetch( key: params.fetch("key"), group_by: params.fetch("group_by", "month"), - start_date: params.fetch("start_date", "2019-04") + start_date: params.fetch("start_date", ENV["DEFAULT_MIN_DATE"].strip) )) end