<commit_before>from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import os

class Result(declarative_base()):
    __tablename__ = 'results'

    id = Column(Integer, primary_key=True)
    device_name = Column(String)
    time = Column(DateTime)
    raw_results = Column(String)

    def __repr__(self):
        return "Result(id='%s', device_name='%s', time='%s', raw_results:\n%s)" % (
                self.id, self.device_name, self.time, self.raw_results )

class Device(declarative_base()):
    __tablename__ = 'devices'

    device_id = Column(String, primary_key=True)
    device_type = Column(String)
    sensor_x = Column(Integer)
    sensor_y = Column(Integer)
    create_time = Column(DateTime)
    last_update = Column(DateTime)

    def __repr__(self):
        return "Device(id='%s', device_type='%s', x:y=%d:%d, created='%s', last_updated='%s" % (
                self.device_id,
                self.device_type,
                self.sensor_x,
                self.sensor_y,
                self.create_time.isoformat(),
                self.last_update.isoformat())

def setup_db(path):
    if check_db_exist(path):
        db = create_engine("sqlite:///%s" % (path))
        return sessionmaker(bind=db)

    else:
        db = create_engine("sqlite:///%s" % (path))

        db.echo = False

        devices = Table('devices', MetaData(bind=db),
          Column('device_id', String(40), primary_key = True),
          Column('device_type', String(40)),
          Column('sensor_x', Integer),
          Column('sensor_y', Integer),
          Column('create_time', DateTime),
          Column('last_update', DateTime))

        devices.create()

        results = Table('results', MetaData(bind=db),
          Column('id', Integer, primary_key=True, autoincrement=True),
          Column('device_name', String(40)),
          Column('time', DateTime),
          Column('raw_results', String))

        results.create()

        return sessionmaker(bind=db)

def check_db_exist(path):
    return os.path.isfile(path)

def insert_new_result(db_session, result):
    #result = 
    pass

def insert_new_device(db_session, device):
    pass

session = setup_db('scanner_store.db')<commit_msg>added db add, fetch methods<commit_after>from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import os, datetime

class Result(declarative_base()):
    __tablename__ = 'results'

    id = Column(Integer, primary_key=True)
    device_name = Column(String)
    time = Column(DateTime)
    raw_results = Column(String)

    def __repr__(self):
        return "Result(id='%s', device_name='%s', time='%s', raw_results:\n%s)" % (
                self.id, self.device_name, self.time, self.raw_results )

class Device(declarative_base()):
    __tablename__ = 'devices'

    device_id = Column(String, primary_key=True)
    device_type = Column(String)
    sensor_x = Column(Integer)
    sensor_y = Column(Integer)
    create_time = Column(DateTime)
    last_update = Column(DateTime)

    def __repr__(self):
        return "Device(id='%s', device_type='%s', x:y=%d:%d, created='%s', last_updated='%s" % (
                self.device_id,
                self.device_type,
                self.sensor_x,
                self.sensor_y,
                self.create_time.isoformat(),
                self.last_update.isoformat())

def setup_db(path):
    if check_db_exist(path):
        db = create_engine("sqlite:///%s" % (path))
    else:
        db = create_engine("sqlite:///%s" % (path))
        create_db(db)
    Session = sessionmaker(bind=db)
    return Session()


def create_db(db_obj):
        devices = Table('devices', MetaData(bind=db_obj),
          Column('device_id', String(40), primary_key = True),
          Column('device_type', String(40)),
          Column('sensor_x', Integer),
          Column('sensor_y', Integer),
          Column('create_time', DateTime),
          Column('last_update', DateTime))

        devices.create()

        results = Table('results', MetaData(bind=db_obj),
          Column('id', Integer, primary_key=True, autoincrement=True),
          Column('device_name', String(40)),
          Column('time', DateTime),
          Column('raw_results', String))

        results.create()

def check_db_exist(path):
    return os.path.isfile(path)

def insert_new_result(db_session, result):
    #result = 
    r = Result(device_name=result['device_name'],
               time=result['time'],
               raw_results=result['raw_results'])
    db_session.add(r)
    db_session.commit()


def insert_new_device(db_session, device):
    #result = 
    d = Device(device_id=device['device_name'],
               device_type=device['device_type'],
               sensor_x=device['sensor_x'],
               sensor_y=device['sensor_y'],
               create_time=datetime.datetime.now(),
               last_update=datetime.datetime.now())
    db_session.add(d)
    db_session.commit()

def get_latest_result(db_session, device_id):
    result = db_session.query(Result).filter(Result.device_name==device_id).order_by(Result.id.desc()).first()
    return result

session = setup_db('scanner_store.db')
{'fraction_non_alphanumeric': 0.13357332322849563, 'fraction_numerical': 0.0022735884804850324, 'mean_word_length': 2.927827380952381, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6369858', 'n_tokens_mistral': 1657, 'n_tokens_neox': 1607, 'n_words': 290}