#!/usr/bin/env ruby22
# -*- coding: utf-8 -*-
require 'cgi'
require 'json'
require 'sqlite3'
dbfile = '../db/point.sq3'
c = CGI.new({'accept_charset' => 'utf-8'})
#print "Content-type: application/json; charset=UTF-8\n\n"
print "Content-type: text/plain; charset=UTF-8\n\n"
value = c['value'].to_f
#tag = c['tag']
#now = Time.now.to_s
db = SQLite3::Database.new(dbfile)
db.busy_timeout = 1000
#db.execute("CREATE TABLE IF NOT EXISTS foo(tag text, time text, val real);")
db.execute("CREATE TABLE IF NOT EXISTS foo(time text, val real);")
#db.execute("INSERT INTO foo VALUES(?, ?, ?);", tag, now, value)
val = {}
#val['合計'] =
# db.execute("SELECT sum(val) FROM foo WHERE tag=?", tag)[0][0].to_f
#val['平均'] =
# db.execute("SELECT avg(val) FROM foo WHERE tag=?", tag)[0][0].to_f.round(2)
#res = db.execute("SELECT val FROM foo WHERE tag=?", tag)
#val['全件'] = res.collect{|row| row[0]} # 先頭カラムを集める
val['差'] = value
puts JSON.pretty_generate(val)