Commit 6120a234 authored by taco@waag.org's avatar taco@waag.org
Browse files

csv export

parent ccca391c
......@@ -2,6 +2,7 @@
gem install sinatra
gem install json
gem install rest-client
gem install csv
# uitvoeren
ruby server.rb
......@@ -10,6 +11,8 @@ ruby server.rb
voorbeeld calls:
http://localhost:4567/sensor/204/pm10?start=2019-08-25&end=2019-08-31 # geeft alle waardes tussen start en end voor type pm10 van device 204
http://localhost:4567/sensor/204/pm10/csv?start=2019-08-25&end=2019-08-31 # geeft een csv bestand met alle waardes tussen start en end voor type pm10 van device 204
http://localhost:4567/sensor/pm25 # geeft de laatste waarde voor ieder device van type pm25
http://localhost:4567/sensor/all # geeft de laatste waarde voor ieder device
......
......@@ -4,6 +4,7 @@ require 'sinatra'
require 'rest-client'
require 'json'
require 'redis'
require 'csv'
$base_url = "https://api-samenmeten.rivm.nl/v1.0"
$redis = Redis.new({:host => 'localhost', :port => 6379, :db => 2})
......@@ -53,7 +54,40 @@ get '/sensor/:type' do
body
end
# json download
get '/sensor/:id/:type' do
retrieveSensorData(params)
end
# csv download
get '/sensor/:id/:type/csv' do
json = retrieveSensorData(params)
data = JSON.parse(json)
content_type 'application/csv'
attachment "#{params[:id]}_#{params[:type]}_data.csv"
#reformat to tabular data
to_csv(data)
end
# generate csv data
def to_csv(data)
CSV.generate do |csv|
csv << [:instance,:date,:value]
data.each do |ds|
ds.each_pair do |key,instance|
instance.each do |row|
# puts "#{key}, #{row["timestamp"]} #{row["value"]}"
csv << [key,row["timestamp"],row["value"]]
end
end
end
end
end
#convenience method for json / csv enpoint reuse
def retrieveSensorData(params)
id = params[:id]
type = params[:type]
......@@ -94,9 +128,11 @@ get '/sensor/:id/:type' do
body = all_pages.to_json
$redis.set uri, body, {:ex => 3600} #TODO: or maybe the number of seconds until the next whole hour?
end
body
body
end
def getPageRecursive(pages,uri)
response = RestClient.get uri
page = JSON.parse(response)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment