33from datetime import datetime
44from typing import Any , Dict , List
55
6- from sqlalchemy import JSON , Boolean , DateTime , Enum , ForeignKey , String
6+ from sqlalchemy import (
7+ JSON ,
8+ Boolean ,
9+ DateTime ,
10+ Enum ,
11+ Float ,
12+ ForeignKey ,
13+ Integer ,
14+ String ,
15+ )
716from sqlalchemy .ext .declarative import declarative_base
817from sqlalchemy .orm import Mapped , mapped_column , relationship
918
19+ from DashAI .back .core .enums .metrics import LevelEnum , SplitEnum
1020from DashAI .back .core .enums .plugin_tags import PluginTag
1121from DashAI .back .core .enums .status import (
1222 ConverterListStatus ,
@@ -92,6 +102,12 @@ class Experiment(Base):
92102 task_name : Mapped [str ] = mapped_column (String , nullable = False )
93103 input_columns : Mapped [str ] = mapped_column (JSON , nullable = False )
94104 output_columns : Mapped [str ] = mapped_column (JSON , nullable = False )
105+
106+ # Metrics per split
107+ train_metrics : Mapped [list [str ]] = mapped_column (JSON , nullable = True )
108+ validation_metrics : Mapped [list [str ]] = mapped_column (JSON , nullable = True )
109+ test_metrics : Mapped [list [str ]] = mapped_column (JSON , nullable = True )
110+
95111 splits : Mapped [str ] = mapped_column (JSON , nullable = False )
96112 created : Mapped [DateTime ] = mapped_column (DateTime , default = datetime .now )
97113 last_modified : Mapped [DateTime ] = mapped_column (
@@ -134,10 +150,6 @@ class Run(Base):
134150 plot_importance_path : Mapped [str ] = mapped_column (String , nullable = True )
135151 # goal metrics
136152 goal_metric : Mapped [str ] = mapped_column (String )
137- # metrics
138- train_metrics : Mapped [JSON ] = mapped_column (JSON , nullable = True )
139- test_metrics : Mapped [JSON ] = mapped_column (JSON , nullable = True )
140- validation_metrics : Mapped [JSON ] = mapped_column (JSON , nullable = True )
141153 # artifacts
142154 artifacts : Mapped [str ] = mapped_column (JSON , nullable = True )
143155 # metadata
@@ -154,6 +166,7 @@ class Run(Base):
154166 predictions = relationship (
155167 "Prediction" , cascade = "all, delete-orphan" , back_populates = "run"
156168 )
169+ metrics = relationship ("Metric" , cascade = "all, delete-orphan" , back_populates = "run" )
157170
158171 def set_status_as_delivered (self ) -> None :
159172 """Update the status of the run to delivered and set delivery_time to now."""
@@ -223,6 +236,30 @@ def set_status_as_error(self) -> None:
223236 self .status = PredictionStatus .ERROR
224237
225238
239+ class Metric (Base ):
240+ __tablename__ = "metric"
241+ """
242+ Table to store all the information related to a metric
243+ """
244+ id : Mapped [int ] = mapped_column (primary_key = True )
245+ run_id : Mapped [int ] = mapped_column (
246+ ForeignKey ("run.id" , ondelete = "CASCADE" ), index = True
247+ )
248+ split : Mapped [SplitEnum ] = mapped_column (Enum (SplitEnum ), nullable = False )
249+ level : Mapped [LevelEnum ] = mapped_column (Enum (LevelEnum ), nullable = False )
250+
251+ name : Mapped [str ] = mapped_column (String , nullable = False )
252+ value : Mapped [float ] = mapped_column (Float , nullable = False )
253+ step : Mapped [int ] = mapped_column (Integer , nullable = False )
254+
255+ timestamp : Mapped [datetime ] = mapped_column (
256+ DateTime , default = datetime .now , index = True
257+ )
258+
259+ # Relationships
260+ run : Mapped ["Run" ] = relationship ("Run" , back_populates = "metrics" )
261+
262+
226263class Plugin (Base ):
227264 __tablename__ = "plugin"
228265 """
0 commit comments