2013-08-28 00:18:03 +02:00
|
|
|
import logging
|
2013-09-11 00:05:31 +02:00
|
|
|
|
|
|
|
|
2013-08-28 00:18:03 +02:00
|
|
|
import sys
|
2013-09-11 00:05:31 +02:00
|
|
|
|
2013-08-28 00:18:03 +02:00
|
|
|
try:
|
2013-12-03 21:07:17 +01:00
|
|
|
from sqlalchemy import create_engine, Column, Integer, String, DateTime
|
|
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from sqlalchemy.ext.declarative import declarative_base
|
2013-08-28 00:18:03 +02:00
|
|
|
except:
|
2013-12-03 21:07:17 +01:00
|
|
|
sys.exit("No SQLAlchemy.")
|
2013-08-28 00:18:03 +02:00
|
|
|
|
|
|
|
|
2013-09-11 00:05:31 +02:00
|
|
|
logging.basicConfig(level=logging.INFO)
|
2013-08-28 00:18:03 +02:00
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
PAGESIZE = 10
|
2013-08-28 01:09:53 +02:00
|
|
|
|
|
|
|
"""
|
2013-11-26 14:45:31 +01:00
|
|
|
This class describe a single Record (Rec() class) and the
|
|
|
|
records manager (RecDB() class)
|
2013-08-28 01:09:53 +02:00
|
|
|
"""
|
|
|
|
Base = declarative_base()
|
|
|
|
|
2013-12-03 21:07:17 +01:00
|
|
|
|
2013-08-28 01:09:53 +02:00
|
|
|
class Rec(Base):
|
2013-12-03 21:07:17 +01:00
|
|
|
'''Entry on the DB'''
|
2013-09-10 21:18:45 +02:00
|
|
|
__tablename__ = 'rec'
|
2013-12-02 20:58:43 +01:00
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
recid = Column(String)
|
|
|
|
name = Column(String, nullable=True)
|
|
|
|
starttime = Column(DateTime, nullable=True)
|
|
|
|
endtime = Column(DateTime, nullable=True)
|
2013-12-03 21:07:17 +01:00
|
|
|
filename = Column(String, nullable=True)
|
2013-09-11 00:05:31 +02:00
|
|
|
|
2013-12-03 21:07:17 +01:00
|
|
|
def __init__(self, recid="", name="", starttime=None, endtime=None,
|
|
|
|
filename=None):
|
|
|
|
self.name = name
|
|
|
|
self.starttime = starttime
|
|
|
|
self.endtime = endtime
|
|
|
|
self.recid = recid
|
|
|
|
self.filename = filename
|
2013-08-28 01:09:53 +02:00
|
|
|
|
2013-12-02 20:58:43 +01:00
|
|
|
def serialize(self):
|
2013-12-03 21:07:17 +01:00
|
|
|
'''json-friendly encoding'''
|
2013-12-02 20:58:43 +01:00
|
|
|
return {'name': self.name,
|
2013-12-03 21:07:17 +01:00
|
|
|
'starttime': self.starttime,
|
|
|
|
'endtime': self.endtime,
|
2013-12-02 20:58:43 +01:00
|
|
|
'recid': self.recid,
|
2013-12-03 21:07:17 +01:00
|
|
|
'filename': self.filename
|
2013-12-02 20:58:43 +01:00
|
|
|
}
|
2013-12-03 21:07:17 +01:00
|
|
|
|
2013-09-10 21:18:45 +02:00
|
|
|
def __repr__(self):
|
2013-12-03 21:07:17 +01:00
|
|
|
contents = "id:'%s',recid:'%s',name:'%s',Start: '%s',End: '%s'" % \
|
|
|
|
(self.id, self.recid, self.name, self.starttime, self.endtime)
|
|
|
|
if self.filename is not None:
|
|
|
|
contents += ",Filename: '%s'" % self.filename
|
|
|
|
return "<Rec(%s)>" % contents
|
2013-08-28 01:09:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
class RecDB:
|
2013-12-04 16:26:08 +01:00
|
|
|
def __init__(self, uri):
|
|
|
|
self.engine = create_engine(uri, echo=False)
|
2013-09-11 00:05:31 +02:00
|
|
|
self.conn = self.engine.connect()
|
|
|
|
|
|
|
|
logging.getLogger('sqlalchemy.engine').setLevel(logging.FATAL)
|
2013-12-02 20:58:43 +01:00
|
|
|
logging.getLogger('sqlalchemy.engine.base.Engine')\
|
|
|
|
.setLevel(logging.FATAL)
|
2013-09-11 00:05:31 +02:00
|
|
|
logging.getLogger('sqlalchemy.dialects').setLevel(logging.FATAL)
|
|
|
|
logging.getLogger('sqlalchemy.pool').setLevel(logging.FATAL)
|
|
|
|
logging.getLogger('sqlalchemy.orm').setLevel(logging.FATAL)
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-09-11 00:05:31 +02:00
|
|
|
Base.metadata.create_all(self.engine) # create Database
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-09-11 00:05:31 +02:00
|
|
|
Session = sessionmaker(bind=self.engine)
|
2013-11-26 14:45:31 +01:00
|
|
|
self.session = Session()
|
2013-11-24 23:43:56 +01:00
|
|
|
|
|
|
|
self.err = ""
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-09-11 00:05:31 +02:00
|
|
|
def add(self, simplerecord):
|
2013-11-24 23:43:56 +01:00
|
|
|
print self.session.add( simplerecord )
|
2013-09-11 00:05:31 +02:00
|
|
|
self.commit()
|
2013-11-24 23:43:56 +01:00
|
|
|
logging.info("New Record: %s" % simplerecord)
|
2013-09-11 00:05:31 +02:00
|
|
|
return ( simplerecord )
|
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
def update(self, recid, rec):
|
|
|
|
|
2013-12-02 20:58:43 +01:00
|
|
|
## TODO: rlist = results list
|
2013-11-24 23:43:56 +01:00
|
|
|
_rlist = self._search(recid=recid)
|
|
|
|
if not len(_rlist) == 1:
|
2013-12-03 21:07:17 +01:00
|
|
|
raise ValueError('Too many recs with id=%s' % recid)
|
2013-11-24 23:43:56 +01:00
|
|
|
|
|
|
|
logging.info("DB:: Update request %s:%s " % (recid, rec))
|
|
|
|
logging.info("DB:: Update: data before %s" % _rlist[0])
|
|
|
|
|
|
|
|
# 2013-11-24 22:22:42
|
2013-12-03 21:07:17 +01:00
|
|
|
_rlist[0].starttime = rec["starttime"]
|
|
|
|
_rlist[0].endtime = rec["endtime"]
|
2013-12-02 20:58:43 +01:00
|
|
|
if 'name' in rec:
|
|
|
|
_rlist[0].name = rec["name"]
|
2013-11-24 23:43:56 +01:00
|
|
|
logging.info("DB:: Update: data AFTER %s" % _rlist[0])
|
|
|
|
|
|
|
|
self.commit()
|
|
|
|
logging.info("DB:: Update complete")
|
2013-12-03 21:07:17 +01:00
|
|
|
return _rlist[0]
|
2013-11-24 23:43:56 +01:00
|
|
|
|
2013-11-25 00:29:24 +01:00
|
|
|
def delete(self,recid):
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-11-25 00:29:24 +01:00
|
|
|
_rlist = self._search(recid=recid)
|
2013-11-26 14:45:31 +01:00
|
|
|
|
|
|
|
if len(_rlist) == 0:
|
2013-11-24 23:43:56 +01:00
|
|
|
logging.info("DB: Delete: no record found!")
|
|
|
|
self.err = "No rec found"
|
|
|
|
return False
|
2013-11-26 14:45:31 +01:00
|
|
|
|
|
|
|
if len(_rlist) > 1:
|
2013-11-24 23:43:56 +01:00
|
|
|
logging.info("DB: Delete: multilpe records found!")
|
|
|
|
self.err = "multiple ID Found %s" % (_rlist)
|
|
|
|
return False
|
|
|
|
|
|
|
|
self.session.delete( _rlist[0] )
|
|
|
|
logging.info("DB: Delete: delete complete")
|
|
|
|
self.commit()
|
|
|
|
return True
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-09-11 00:05:31 +02:00
|
|
|
def commit(self):
|
2013-11-24 23:43:56 +01:00
|
|
|
logging.info("DB: Commit!!")
|
2013-09-11 00:05:31 +02:00
|
|
|
self.session.commit()
|
2013-08-28 01:09:53 +02:00
|
|
|
|
2013-11-19 23:16:03 +01:00
|
|
|
def get_all(self, page=0, page_size=PAGESIZE):
|
2013-09-11 00:05:31 +02:00
|
|
|
return self._search(page=page, page_size=page_size)
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-12-02 20:58:43 +01:00
|
|
|
def _search(self, _id=None, name=None, recid=None, starttime=None,
|
2013-12-03 21:07:17 +01:00
|
|
|
endtime=None, page=0, page_size=PAGESIZE):
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-12-03 21:07:17 +01:00
|
|
|
logging.info("DB: Search => id:%s recid:%s name:%s starttime:%s endtime=%s" % (_id,recid,name,starttime,endtime))
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-09-11 00:05:31 +02:00
|
|
|
query = self.session.query(Rec)
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-12-02 20:58:43 +01:00
|
|
|
if _id is not None:
|
|
|
|
query = query.filter_by(id=_id)
|
|
|
|
if recid is not None:
|
|
|
|
query = query.filter_by(recid=recid)
|
|
|
|
if name is not None:
|
|
|
|
query = query.filter(Rec.name.like("%"+name+"%"))
|
2013-11-24 23:43:56 +01:00
|
|
|
try:
|
2013-12-02 20:58:43 +01:00
|
|
|
if starttime is not None:
|
2013-12-03 21:07:17 +01:00
|
|
|
_st = starttime
|
2013-12-02 20:58:43 +01:00
|
|
|
query = query.filter(Rec.starttime > _st)
|
2013-11-24 23:43:56 +01:00
|
|
|
except:
|
2013-11-26 14:45:31 +01:00
|
|
|
logging.info("DB: search : no valid starttime")
|
2013-12-02 20:58:43 +01:00
|
|
|
raise ValueError('starttime not valid')
|
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
try:
|
2013-12-02 20:58:43 +01:00
|
|
|
if endtime is not None:
|
2013-12-03 21:07:17 +01:00
|
|
|
_et = endtime
|
2013-12-02 20:58:43 +01:00
|
|
|
query = query.filter(Rec.endtime < _et)
|
2013-11-24 23:43:56 +01:00
|
|
|
except ValueError:
|
2013-11-26 14:45:31 +01:00
|
|
|
logging.info("DB: search : no valid endtime")
|
2013-11-24 23:43:56 +01:00
|
|
|
|
2013-12-02 20:58:43 +01:00
|
|
|
if page_size:
|
|
|
|
page_size = int(page_size)
|
|
|
|
query = query.limit(page_size)
|
|
|
|
if page:
|
|
|
|
query = query.offset(page*page_size)
|
2013-11-24 23:43:56 +01:00
|
|
|
print query
|
|
|
|
ret = query.all()
|
|
|
|
# print "Sending: %s" % ret
|
|
|
|
return ret
|
2013-11-26 14:45:31 +01:00
|
|
|
|
|
|
|
def get_err(self):
|
2013-11-24 23:43:56 +01:00
|
|
|
print "DB error: %s" % (self.err)
|
|
|
|
t = self.err
|
|
|
|
self.err = ""
|
|
|
|
return t
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-09-13 12:51:16 +02:00
|
|
|
|
2013-09-10 21:18:45 +02:00
|
|
|
if __name__ == "__main__":
|
2013-12-03 21:07:17 +01:00
|
|
|
from datetime import datetime
|
|
|
|
|
2013-12-02 20:58:43 +01:00
|
|
|
def printall(queryres):
|
|
|
|
for record in queryres:
|
|
|
|
print "Record: %s" % record
|
|
|
|
|
2013-09-10 21:18:45 +02:00
|
|
|
db = RecDB()
|
2013-12-02 20:58:43 +01:00
|
|
|
_mytime = datetime(2014,05,23,15,12,17)
|
|
|
|
_endtime = datetime(2014,05,24,17,45,17)
|
2013-09-13 12:51:16 +02:00
|
|
|
|
|
|
|
a = Rec(name="Mimmo1", starttime=_mytime, endtime=_endtime)
|
2013-11-24 23:43:56 +01:00
|
|
|
printall( db._search() )
|
2013-09-13 12:51:16 +02:00
|
|
|
sys.exit("End test job")
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-09-13 12:51:16 +02:00
|
|
|
# a = Rec(name="Mimmo1", starttime=_mytime, endtime=None)
|
2013-09-11 00:05:31 +02:00
|
|
|
print "Aggiunto", db.add( a )
|
|
|
|
printall( db.get_all(page_size=5,page=0) )
|
2013-11-26 14:45:31 +01:00
|
|
|
|
2013-09-11 00:05:31 +02:00
|
|
|
print "Mimmo "
|
|
|
|
printall( db._search(name="Mimmo1"))
|
|
|
|
print "Search"
|
2013-12-02 20:58:43 +01:00
|
|
|
printall( db._search(name="Mimmo1",starttime=datetime(2014,05,24,15,16,1) ))
|
2013-09-11 00:05:31 +02:00
|
|
|
a = db.get_by_id(5)
|
|
|
|
a.start()
|
|
|
|
db.delete(1)
|
|
|
|
db.delete(2)
|
|
|
|
db.delete(4)
|
|
|
|
db.delete(1)
|
|
|
|
printall( db._search() )
|