Ejemplo n.º 1
0
class SetupModel(db.Model):
    __tablename__ = 'setup'

    setup_id = db.Column(UUID(as_uuid=True),
                         primary_key=True,
                         default=uuid.uuid4,
                         unique=True,
                         nullable=False)

    training_id = db.Column(UUID(as_uuid=True),
                            db.ForeignKey('training.training_id'),
                            nullable=False)
    bike_id = db.Column(UUID(as_uuid=True),
                        db.ForeignKey('bike.bike_id'),
                        nullable=False)

    operating_hours = db.Column(db.Float, nullable=False)
    weather_current = db.Column(JSON, nullable=True)
    slick_pressure_front = db.Column(db.Float, nullable=True)
    slick_pressure_rear = db.Column(db.Float, nullable=True)
    rain_pressure_front = db.Column(db.Float, nullable=True)
    rain_pressure_rear = db.Column(db.Float, nullable=True)
    setup = db.Column(JSON, nullable=True)
    comment = db.Column(db.Text, nullable=True)

    datetime_created = db.Column(db.DateTime,
                                 nullable=False,
                                 default=db.utcnow)
    datetime_last_modified = db.Column(db.DateTime,
                                       nullable=False,
                                       default=db.utcnow,
                                       onupdate=db.utcnow)
    datetime_display = db.Column(db.DateTime,
                                 nullable=False,
                                 default=db.utcnow)

    bike = db.relationship('BikeModel',
                           backref=db.backref(
                               'setups',
                               order_by='SetupModel.datetime_display.asc()'))
    training = db.relationship(
        'TrainingModel',
        backref=db.backref('setups',
                           order_by='SetupModel.datetime_display.asc()'))

    def __repr__(self):
        return f"Setup[" \
               f"'{self.setup_id}': (" \
               f"'{self.operating_hours}', " \
               f"'{self.comment}', " \
               f"'{self.datetime_display}', " \
               f"training_id: '{self.training_id}', " \
               f"bike_id: '{self.bike_id}'" \
               f")]"
Ejemplo n.º 2
0
class SessionModel(db.Model):
    __tablename__ = 'session'

    session_id = db.Column(UUID(as_uuid=True),
                           primary_key=True,
                           default=uuid.uuid4,
                           unique=True,
                           nullable=False)

    training_id = db.Column(UUID(as_uuid=True),
                            db.ForeignKey('training.training_id'),
                            nullable=False)
    setup_id = db.Column(UUID(as_uuid=True),
                         db.ForeignKey('setup.setup_id'),
                         nullable=True)
    bike_id = db.Column(UUID(as_uuid=True),
                        db.ForeignKey('bike.bike_id'),
                        nullable=True)

    application = db.Column(db.String(50), nullable=True)

    datetime_created = db.Column(db.DateTime,
                                 nullable=False,
                                 default=db.utcnow)
    datetime_last_modified = db.Column(db.DateTime,
                                       nullable=False,
                                       default=db.utcnow,
                                       onupdate=db.utcnow)
    datetime_display = db.Column(db.DateTime,
                                 nullable=False,
                                 default=db.utcnow)

    bike = db.relationship('BikeModel',
                           backref=db.backref(
                               'sessions',
                               order_by='SessionModel.datetime_display.asc()'))
    training = db.relationship(
        'TrainingModel',
        backref=db.backref('sessions',
                           order_by='SessionModel.datetime_display.asc()'))
    setup = db.relationship(
        'SetupModel',
        backref=db.backref('sessions',
                           order_by='SessionModel.datetime_display.asc()'))

    def __repr__(self):
        return f"Session[" \
               f"'{self.session_id}': (" \
               f"'{self.datetime_display}', " \
               f"training_id: '{self.training_id}', " \
               f"bike_id: '{self.bike_id}', " \
               f"setup_id: '{self.setup_id}', " \
               f")]"
Ejemplo n.º 3
0
class Agent(db.Model):
    __tablename__ = "Agent"
    Id = db.Column(db.Integer, primary_key=True)
    Name = db.Column(db.String)
    StagingId = db.Column(db.String)
    AesPassword = db.Column(db.String)
    Username = db.Column(db.String)
    Hostname = db.Column(db.String)
    PID = db.Column(db.Integer)
    OperatingSystem = db.Column(db.String)
    Admin = db.Column(db.Boolean)
    AgentTypeId = db.Column(db.Integer,
                            db.ForeignKey('AgentType.Id'),
                            nullable=False)
    StagingResponseId = db.Column(db.Integer,
                                  db.ForeignKey('StagingMessage.Id'),
                                  nullable=False)
    PayloadId = db.Column(db.Integer,
                          db.ForeignKey('Payload.Id'),
                          nullable=False)
    TransportId = db.Column(db.Integer,
                            db.ForeignKey('Transport.Id'),
                            nullable=False)
    InternalIP = db.Column(db.String)
    ExternalIP = db.Column(db.String)
    InitialCheckin = db.Column(db.DateTime)
    LastCheckin = db.Column(db.DateTime)
    BeaconInterval = db.Column(db.Integer)
    Jitter = db.Column(db.Float)
    Tasks = db.relationship('AgentTask', backref='Agent', lazy=True)
    ConsoleMessages = db.relationship("ConsoleMessage",
                                      backref='Agent',
                                      lazy=True)
    AvailableTransports = db.relationship('Transport',
                                          secondary=AgentsTransportsXREF,
                                          lazy='subquery',
                                          backref=db.backref('AvailableAgents',
                                                             lazy=True))
    AvailableModules = db.relationship('Module',
                                       secondary=AgentModulesXREF,
                                       lazy='subquery',
                                       backref=db.backref('AvailableAgents',
                                                          lazy=True))
    Visible = db.Column(db.Boolean)

    def __repr__(self):
        if self.Name:
            return '<Agent: %s>' % self.Name
        else:
            return '<Agent: %s>' % str(self.Id)
Ejemplo n.º 4
0
class HistoryModel(db.Model):
    __tablename__ = 'history'

    history_id = db.Column(UUID(as_uuid=True),
                           primary_key=True,
                           default=uuid.uuid4,
                           unique=True,
                           nullable=False)

    maintenance_id = db.Column(UUID(as_uuid=True),
                               db.ForeignKey('maintenance.maintenance_id'),
                               nullable=False)
    bike_id = db.Column(UUID(as_uuid=True),
                        db.ForeignKey('bike.bike_id'),
                        nullable=False)

    operating_hours = db.Column(db.Float, nullable=False)
    comment = db.Column(db.Text, nullable=True)
    tags = db.Column(ARRAY(db.String), nullable=True)

    datetime_created = db.Column(db.DateTime,
                                 nullable=False,
                                 default=db.utcnow)
    datetime_last_modified = db.Column(db.DateTime,
                                       nullable=False,
                                       default=db.utcnow,
                                       onupdate=db.utcnow)
    datetime_display = db.Column(db.DateTime,
                                 nullable=False,
                                 default=db.utcnow)

    bike = db.relationship('BikeModel', backref=db.backref('history'))
    maintenance = db.relationship(
        'MaintenanceModel',
        backref=db.backref('history',
                           order_by='HistoryModel.operating_hours.desc()'))

    def __repr__(self):
        return f"History[" \
               f"'{self.history_id}': (" \
               f"'{self.operating_hours}', " \
               f"'{self.comment}', " \
               f"'{self.datetime_display}', " \
               f"mtn_id: '{self.maintenance_id}'," \
               f"bike_id: '{self.bike_id}'" \
               f")]"
class Session(db.Model):
    __tablename__ = 'session'

    id = db.Column(UUIDType(), primary_key=True)
    uid = db.Column(UUIDType(), db.ForeignKey('user.id'), nullable=False)
    user = db.relationship('User', backref=db.backref('sessions', lazy=True))
    expire = db.Column(db.DateTime(), nullable=False)

    @staticmethod
    def new_session(username, expire=None) -> 'Session':
        if expire == None:
            expire = datetime.now() + timedelta(hours=3)
        if type(expire) in (int, float):
            expire = datetime.fromtimestamp(expire)

        user = User.get_by_username(username)
        return Session(
            id=uuid4(),
            user=user,
            expire=expire,
        )

    @staticmethod
    def get_by_id(id) -> 'Session':
        if type(id) != UUID:
            try:
                id = UUID(id)
            except:
                raise AttributeError("Invalid UUID")

        query = Session.query.filter_by(id=id)

        if query.count() == 0:
            raise ValueError(f"Session {str(id)} not found.")

        session = query.first()

        if session.expired():
            db.session.delete(session)
            db.session.commit()
            raise ValueError(f"Session {str(id)} not found.")

        return session

    def json(self) -> dict:
        return {
            "id": str(self.id),
            "user": self.user.json(),
            "expire": int(self.expire.timestamp()),
        }

    def expired(self) -> bool:
        return self.expire < datetime.now()
Ejemplo n.º 6
0
class LaptimeModel(db.Model):
    __tablename__ = 'laptime'

    lap_id = db.Column(UUID(as_uuid=True),
                       primary_key=True,
                       default=uuid.uuid4,
                       unique=True,
                       nullable=False)

    session_id = db.Column(UUID(as_uuid=True),
                           db.ForeignKey('session.session_id'),
                           nullable=False)

    lap_no = db.Column(db.Integer, nullable=False)
    valid = db.Column(db.Boolean, nullable=False, default=True)
    track_layout = db.Column(db.String, nullable=False, default="A")
    laptime_seconds = db.Column(db.Float, nullable=False)
    sectors = db.Column(JSON, nullable=False)

    datetime_created = db.Column(db.DateTime,
                                 nullable=False,
                                 default=db.utcnow)
    datetime_last_modified = db.Column(db.DateTime,
                                       nullable=False,
                                       default=db.utcnow,
                                       onupdate=db.utcnow)
    datetime_display = db.Column(db.DateTime,
                                 nullable=False,
                                 default=db.utcnow)

    session = db.relationship(
        'SessionModel',
        backref=db.backref('laptimes',
                           order_by='LaptimeModel.datetime_display.asc()'))

    def __repr__(self):
        return f"Lap time[" \
               f"'{self.lap_id}': (" \
               f"'{self.datetime_display}', " \
               f"training_id: '{self.training_id}', " \
               f"bike_id: '{self.bike_id}', " \
               f"setup_id: '{self.setup_id}', " \
               f")]"
Ejemplo n.º 7
0
class SparepartModel(db.Model):
    __tablename__ = 'sparepart'

    sparepart_id = db.Column(UUID(as_uuid=True),
                             primary_key=True,
                             default=uuid.uuid4,
                             unique=True,
                             nullable=False)

    name = db.Column(db.String, nullable=False)
    items = db.relationship('SparepartitemModel',
                            backref=db.backref('sparepart'))

    module = db.Column(db.String, nullable=False)
    min_stock = db.Column(db.Integer, nullable=True)
    current_stock = db.column_property(
        db.select([func.sum(SparepartitemModel.stock)
                   ]).where(SparepartitemModel.sparepart_id ==
                            sparepart_id).correlate_except(SparepartitemModel))

    datetime_created = db.Column(db.DateTime,
                                 nullable=False,
                                 default=db.utcnow)
    datetime_last_modified = db.Column(db.DateTime,
                                       nullable=False,
                                       default=db.utcnow,
                                       onupdate=db.utcnow)
    datetime_display = db.Column(db.DateTime,
                                 nullable=False,
                                 default=db.utcnow)

    def __repr__(self):
        return f"Sparepart[" \
               f"'{self.sparepart_id}': (" \
               f"'{self.module}', " \
               f"'{self.alarm}', " \
               f"'{self.datetime_display}'" \
               f")]"