local _M = {}
local db = require('db.mysql')
_M['date'] = os.date('%Y-%m-%d %H:%M:%S')

function _M.get(limit,offset)
	local cur 
	if limit == nil then
		cur = db.cursor("SELECT * FROM vpnlogs ORDER BY started DESC")
	else
		if offset == nil then offset = 0 end
		cur = db.cursor("SELECT * FROM vpnlogs ORDER BY started DESC LIMIT "..limit.." OFFSET "..offset)
	end	
	return db.results(cur)
end

function _M.period(start,ends,user)
	local cur 
	if user == nil then
		cur = db.cursor("SELECT user,client_ip,DATE_ADD(logs.started, INTERVAL 4 HOUR) as day, ROUND(bytesin/1000,2) as bin, ROUND(bytesout/1000,2) as bout FROM vpnlogs WHERE started BETWEEN '"..start.."' AND '"..ends.."' ORDER BY started DESC")
	else
		cur = db.cursor("SELECT user,client_ip,DATE_ADD(logs.started, INTERVAL 4 HOUR) as day, ROUND(bytesin/1000,2) as bin, ROUND(bytesout/1000,2) as bout FROM vpnlogs WHERE started BETWEEN '"..start.."' AND '"..ends.."' AND user = '"..user.."' ORDER BY started DESC")
	end
	return db.results(cur)
end

function _M.connects(start,ends,user)
	local cur 
	if user == nil then
		cur = db.cursor("SELECT user, ROUND(SUM(bytesin)/1000000) as bin, ROUND(SUM(bytesout)/1000000) as bout, COUNT(*) as connects FROM vpnlogs WHERE started BETWEEN '"..start.."' AND '"..ends.."' GROUP BY user ORDER BY connects DESC")
	else
		cur = db.cursor("SELECT user, ROUND(SUM(bytesin)/1000000) as bin, ROUND(SUM(bytesout)/1000000) as bout, COUNT(*) as connects FROM vpnlogs WHERE started BETWEEN '"..start.."' AND '"..ends.."' AND user = '"..user.."'  GROUP BY user ORDER BY connects DESC")
	end
	return db.results(cur)
end

function _M.traffic(start,ends,user)
	local cur 
	local results = {}
	if user == nil then
		cur = db.cursor("SELECT ROUND(SUM(bytesin)/1000000) as bin, ROUND(SUM(bytesout)/1000000) as bout, ROUND(SUM(bytesin)/1000000) + ROUND(SUM(bytesout)/1000000) as summ FROM vpnlogs WHERE started BETWEEN '"..start.."' AND '"..ends.."'")
	else 
		cur = db.cursor("SELECT ROUND(SUM(bytesin)/1000000) as bin, ROUND(SUM(bytesout)/1000000) as bout, ROUND(SUM(bytesin)/1000000) + ROUND(SUM(bytesout)/1000000) as summ FROM vpnlogs WHERE started BETWEEN '"..start.."' AND '"..ends.."' AND user = '"..user.."'")
	end
	results = db.results(cur)
	if results[1] then results = results[1] end
	return results
end

function _M.daystats(start,ends,user)
	local cur 
	local results = {}
	if user == nil then
		cur = db.cursor("SELECT DATE_FORMAT(started,'%d.%m') as day, ROUND(SUM(bytesin)/1000000) + ROUND(SUM(bytesout)/1000000) as summ FROM vpnlogs WHERE started BETWEEN '"..start.."' AND '"..ends.."' GROUP BY day ORDER BY day ASC")
	else
		cur = db.cursor("SELECT DATE_FORMAT(started,'%d.%m') as day, ROUND(SUM(bytesin)/1000000) + ROUND(SUM(bytesout)/1000000) as summ FROM vpnlogs WHERE started BETWEEN '"..start.."' AND '"..ends.."' AND user = '"..user.."' GROUP BY day ORDER BY day ASC")
	end
	return db.results(cur)
end

function _M.users()
	local cur 
	local results = {}
	cur = db.cursor("SELECT user FROM vpnlogs WHERE bytesin>0 GROUP BY user ORDER BY user DESC")
	return db.results(cur)
end

return _M
