From 1a1a710edf0b3b99e08e1b588fe5969955e44c60 Mon Sep 17 00:00:00 2001 From: GotthardG <51994228+GotthardG@users.noreply.github.com> Date: Wed, 4 Dec 2024 11:23:21 +0100 Subject: [PATCH] added pet data for samples events --- backend/app/data/__init__.py | 2 +- backend/app/data/data.py | 60 ++++++++++++++++++++++++++++++++++-- backend/app/database.py | 4 +-- backend/app/models.py | 13 +++++++- backend/app/schemas.py | 3 ++ 5 files changed, 75 insertions(+), 7 deletions(-) diff --git a/backend/app/data/__init__.py b/backend/app/data/__init__.py index ae8b1e9..2760707 100644 --- a/backend/app/data/__init__.py +++ b/backend/app/data/__init__.py @@ -1,2 +1,2 @@ -from .data import contacts, return_addresses, dewars, proposals, shipments, pucks, samples, dewar_types, serial_numbers +from .data import contacts, return_addresses, dewars, proposals, shipments, pucks, samples, dewar_types, serial_numbers, sample_events from .slots_data import slots diff --git a/backend/app/data/data.py b/backend/app/data/data.py index a60c214..f3bbb40 100644 --- a/backend/app/data/data.py +++ b/backend/app/data/data.py @@ -1,5 +1,5 @@ -from app.models import ContactPerson, Address, Dewar, Proposal, Shipment, Puck, Sample, DewarType, DewarSerialNumber, Slot -from datetime import datetime +from app.models import ContactPerson, Address, Dewar, Proposal, Shipment, Puck, Sample, DewarType, DewarSerialNumber, Slot, SampleEvent +from datetime import datetime, timedelta import random import time import hashlib @@ -182,4 +182,58 @@ for puck in pucks: puck_id=puck.id ) samples.append(sample) - sample_id_counter += 1 \ No newline at end of file + sample_id_counter += 1 + +# Define possible event types for samples +event_types = ["Mounted", "Failed", "Unmounted", "Lost"] + + +def generate_sample_events(samples, chance_no_event=0.2, chance_lost=0.1): + """Generate events for samples with timestamps increasing between different samples.""" + + events = [] + # Set the start time to yesterday at 9:33 AM + start_time = datetime.now().replace(hour=9, minute=33, second=0, microsecond=0) - timedelta(days=1) + + for sample in samples: + current_time = start_time + + # Skip some samples with no events + if random.random() < chance_no_event: + # Increment start_time for the next sample to reflect time passage + start_time += timedelta(minutes=10) + continue + + # Determine initial event type + event_type = "Failed" if random.random() < 0.05 else "Mounted" + + # Append the initial event + events.append(SampleEvent( + sample_id=sample.id, + event_type=event_type, + timestamp=current_time + )) + current_time += timedelta(seconds=50) # Increment the time for subsequent events + + # Proceed if mounted and it's not the last sample + if event_type == "Mounted" and sample is not samples[-1]: + # Determine follow-up event + if random.random() < chance_lost: + events.append(SampleEvent( + sample_id=sample.id, + event_type="Lost", + timestamp=current_time + )) + else: + events.append(SampleEvent( + sample_id=sample.id, + event_type="Unmounted", + timestamp=current_time + )) + + # Increment start_time for the next sample + start_time += timedelta(minutes=10) + + return events + +sample_events = generate_sample_events(samples) diff --git a/backend/app/database.py b/backend/app/database.py index f1e308c..efcc11b 100644 --- a/backend/app/database.py +++ b/backend/app/database.py @@ -27,11 +27,11 @@ def init_db(): def load_sample_data(session: Session): # Import models inside function to avoid circular dependency - from app.data import contacts, return_addresses, dewars, proposals, shipments, pucks, samples, dewar_types, serial_numbers, slots + from app.data import contacts, return_addresses, dewars, proposals, shipments, pucks, samples, dewar_types, serial_numbers, slots, sample_events # If any data already exists, skip seeding if session.query(models.ContactPerson).first(): return - session.add_all(contacts + return_addresses + dewars + proposals + shipments + pucks + samples + dewar_types + serial_numbers + slots) + session.add_all(contacts + return_addresses + dewars + proposals + shipments + pucks + samples + dewar_types + serial_numbers + slots + sample_events) session.commit() \ No newline at end of file diff --git a/backend/app/models.py b/backend/app/models.py index c2a2c4e..b9e1ff6 100644 --- a/backend/app/models.py +++ b/backend/app/models.py @@ -133,6 +133,7 @@ class Sample(Base): # Foreign keys and relationships puck_id = Column(Integer, ForeignKey('pucks.id')) puck = relationship("Puck", back_populates="samples") + events = relationship("SampleEvent", back_populates="sample") class Slot(Base): @@ -157,4 +158,14 @@ class LogisticsEvent(Base): event_type = Column(String, index=True) timestamp = Column(DateTime, default=datetime.utcnow) dewar = relationship("Dewar", back_populates="events") - slot = relationship("Slot", back_populates="events") \ No newline at end of file + slot = relationship("Slot", back_populates="events") + +class SampleEvent(Base): + __tablename__ = "sample_events" + + id = Column(Integer, primary_key=True, index=True) + sample_id = Column(Integer, ForeignKey('samples.id')) + event_type = Column(String, index=True) + timestamp = Column(DateTime, default=datetime.utcnow) + + sample = relationship("Sample", back_populates="events") diff --git a/backend/app/schemas.py b/backend/app/schemas.py index 919fa39..1ac9a13 100644 --- a/backend/app/schemas.py +++ b/backend/app/schemas.py @@ -76,6 +76,8 @@ class DataCollectionParameters(BaseModel): class Config: from_attributes = True +class SampleEventCreate(BaseModel): + event_type: str class Results(BaseModel): # Define attributes for Results here @@ -142,6 +144,7 @@ class SampleCreate(BaseModel): position: int = Field(..., alias="positioninpuck") data_collection_parameters: Optional[DataCollectionParameters] = None results: Optional[Results] = None + events: Optional[List[str]] = None class Config: populate_by_name = True