2018-03-24 00:34:41 +00:00
from flask import request , make_response
from flask_restful import Resource
2018-03-24 23:27:19 +00:00
from cosmicpi . config import Config
2018-03-24 00:34:41 +00:00
import sqlite3
import io
import csv
SQLITE_LOCATION = Config . get ( " Storage " , " sqlite_location " )
class Series ( Resource ) :
2018-03-24 20:20:57 +00:00
def __init__ ( self ) :
self . _cpuserial = " 0000000000000000 "
try :
f = open ( ' /proc/cpuinfo ' , ' r ' )
for line in f :
if line [ 0 : 6 ] == ' Serial ' :
self . _cpuserial = line [ 10 : 26 ]
f . close ( )
except :
self . _cpuserial = " ERROR000000000 "
2018-03-24 00:34:41 +00:00
def get ( self ) :
format = request . args [ ' format ' ]
2018-03-24 10:08:49 +00:00
limit = int ( request . args . get ( ' limit ' , 20 ) )
since = int ( request . args . get ( ' from ' , 0 ) )
to = int ( request . args . get ( ' to ' , 2147483646 ) )
2018-03-24 00:34:41 +00:00
conn = sqlite3 . connect ( SQLITE_LOCATION , timeout = 60.0 )
cursor = conn . cursor ( )
# Get column names
cursor . execute ( " PRAGMA table_info(Events); " )
col_data = cursor . fetchall ( )
col_names = [ ]
for i in range ( 0 , len ( col_data ) ) :
col_names . append ( col_data [ i ] [ 1 ] )
# Get data from database
2018-03-24 10:08:49 +00:00
cursor . execute ( " SELECT * FROM Events WHERE (UTCUnixTime >= %d AND UTCUnixTime <= %d ) ORDER BY UTCUnixTime DESC, SubSeconds DESC LIMIT %d ; " % ( since , to , limit ) )
2018-03-24 00:34:41 +00:00
data = cursor . fetchall ( )
conn . close ( )
if format == ' json ' :
2018-03-24 20:20:57 +00:00
return self . _get_json ( col_names , data )
2018-03-24 00:34:41 +00:00
elif format == ' csv ' :
2018-03-24 20:20:57 +00:00
return self . _get_csv ( col_names , data )
2018-03-24 00:34:41 +00:00
2018-03-24 20:20:57 +00:00
def _get_json ( self , col_names , data ) :
2018-03-24 00:34:41 +00:00
items = [ ]
for row in data :
item = { }
2018-03-24 20:20:57 +00:00
item [ ' HardwareSerial ' ] = self . _cpuserial
2018-03-24 00:34:41 +00:00
for i in range ( len ( col_names ) ) :
item [ col_names [ i ] ] = row [ i ]
items . append ( item )
return items
2018-03-24 20:20:57 +00:00
def _get_csv ( self , col_names , data ) :
2018-03-24 00:34:41 +00:00
"""
Write CSV export to memory
"""
output = io . BytesIO ( )
writer = csv . writer ( output )
writer . writerow ( col_names )
writer . writerows ( data )
response = make_response ( output . getvalue ( ) )
response . headers [ ' Content-Type ' ] = ' text/csv '
return response