use crate::enums::*; use crate::schema::*; use crate::types::*; use chrono::{DateTime, Utc}; use diesel::prelude::*; use serde::{Deserialize, Serialize}; use serde_json::Value; use uuid::Uuid; #[derive(Queryable, Insertable, Identifiable, Associations, Debug)] #[diesel(belongs_to(User, foreign_key = owner_id))] #[diesel(table_name = api_keys)] pub struct ApiKey { pub id: Uuid, pub owner_id: Uuid, pub key: String, pub organization_id: Uuid, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, } #[derive(Queryable, Insertable, Identifiable, Debug, Clone, Serialize)] #[diesel(table_name = dashboard_files)] pub struct DashboardFile { pub id: Uuid, pub name: String, pub file_name: String, pub content: DashboardYml, pub filter: Option, pub organization_id: Uuid, pub created_by: Uuid, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, pub publicly_accessible: bool, pub publicly_enabled_by: Option, pub public_expiry_date: Option>, pub version_history: VersionHistory, pub public_password: Option, } #[derive(Queryable, Insertable, Identifiable, Associations, Debug, Clone, Serialize)] #[diesel(belongs_to(User, foreign_key = created_by))] #[diesel(table_name = messages)] pub struct Message { pub id: Uuid, pub request_message: Option, pub response_messages: Value, pub reasoning: Value, pub title: String, pub raw_llm_messages: Value, pub final_reasoning_message: Option, pub chat_id: Uuid, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, pub created_by: Uuid, pub feedback: Option, } #[derive(Queryable, Insertable, Debug)] #[diesel(table_name = messages_to_files)] pub struct MessageToFile { pub id: Uuid, pub message_id: Uuid, pub file_id: Uuid, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, pub is_duplicate: bool, pub version_number: i32, } #[derive(Queryable, Insertable, Identifiable, Debug, Clone, Serialize)] #[diesel(table_name = metric_files)] pub struct MetricFile { pub id: Uuid, pub name: String, pub file_name: String, pub content: MetricYml, pub verification: Verification, pub evaluation_obj: Option, pub evaluation_summary: Option, pub evaluation_score: Option, pub organization_id: Uuid, pub created_by: Uuid, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, pub publicly_accessible: bool, pub publicly_enabled_by: Option, pub public_expiry_date: Option>, pub version_history: VersionHistory, pub data_metadata: Option, pub public_password: Option, } #[derive(Queryable, Insertable, Identifiable, Associations, Debug, Clone, Serialize)] #[diesel(belongs_to(Organization))] #[diesel(belongs_to(User, foreign_key = created_by))] #[diesel(table_name = chats)] pub struct Chat { pub id: Uuid, pub title: String, pub organization_id: Uuid, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, pub created_by: Uuid, pub updated_by: Uuid, pub publicly_accessible: bool, pub publicly_enabled_by: Option, pub public_expiry_date: Option>, pub most_recent_file_id: Option, pub most_recent_file_type: Option, pub most_recent_version_number: Option, } #[derive(Queryable, Insertable, Associations, Debug)] #[diesel(belongs_to(PermissionGroup, foreign_key = permission_group_id))] #[diesel(belongs_to(User, foreign_key = user_id))] #[diesel(table_name = permission_groups_to_users)] pub struct PermissionGroupToUser { pub permission_group_id: Uuid, pub user_id: Uuid, pub created_at: DateTime, } allow_columns_to_appear_in_same_group_by_clause!( dataset_groups::id, dataset_groups::name, dataset_permissions::id, dataset_groups_permissions::id, teams::id, teams::name, permission_groups_to_identities::permission_group_id, teams_to_users::user_id, teams_to_users::role, permission_groups::id, permission_groups::name, permission_groups_to_identities::identity_id, permission_groups_to_identities::identity_type, users::id, users::name, users::email, users_to_organizations::role, datasets::id, datasets::name, datasets::created_at, datasets::updated_at, datasets::enabled, datasets::imported, data_sources::id, data_sources::name, ); #[derive(Queryable, Insertable, Identifiable, Associations, Debug, Clone, Serialize)] #[diesel(belongs_to(Dashboard, foreign_key = dashboard_id))] #[diesel(table_name = dashboard_versions)] pub struct DashboardVersion { pub id: Uuid, pub dashboard_id: Uuid, pub config: Value, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, } #[derive( Serialize, Deserialize, Selectable, Identifiable, Queryable, Insertable, Associations, AsChangeset, Debug, Clone, )] #[diesel(belongs_to(User, foreign_key = created_by, foreign_key = updated_by))] #[diesel(table_name = dashboards)] pub struct Dashboard { pub id: Uuid, pub name: String, pub description: Option, pub config: Value, pub publicly_accessible: bool, pub publicly_enabled_by: Option, pub public_expiry_date: Option>, #[serde(skip_serializing)] pub password_secret_id: Option, pub created_by: Uuid, pub updated_by: Uuid, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, pub organization_id: Uuid, } #[derive( Serialize, Selectable, Insertable, Queryable, Identifiable, Associations, Debug, Clone, )] #[diesel(belongs_to(Organization))] #[diesel(belongs_to(User, foreign_key = created_by, foreign_key = updated_by))] #[diesel(table_name = data_sources)] #[diesel(check_for_backend(diesel::pg::Pg))] pub struct DataSource { pub id: Uuid, pub name: String, #[serde(rename = "db_type")] pub type_: DataSourceType, pub secret_id: Uuid, pub onboarding_status: DataSourceOnboardingStatus, pub onboarding_error: Option, pub organization_id: Uuid, pub created_by: Uuid, pub updated_by: Uuid, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, pub env: String, } #[derive( Serialize, Queryable, Insertable, Identifiable, Associations, Debug, Clone, AsChangeset, Selectable, )] #[diesel(belongs_to(Dataset, foreign_key = dataset_id))] #[diesel(table_name = dataset_columns)] pub struct DatasetColumn { pub id: Uuid, pub dataset_id: Uuid, pub name: String, #[serde(rename = "type")] pub type_: String, pub description: Option, pub nullable: bool, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, pub stored_values: Option, pub stored_values_status: Option, pub stored_values_error: Option, pub stored_values_count: Option, pub stored_values_last_synced: Option>, pub semantic_type: Option, pub dim_type: Option, pub expr: Option, } #[derive( Serialize, Deserialize, Clone, Selectable, QueryableByName, Queryable, Insertable, Identifiable, Associations, Debug, PartialEq, Eq, Hash, AsChangeset, )] #[diesel(belongs_to(DataSource, foreign_key = data_source_id))] #[diesel(belongs_to(User, foreign_key = created_by, foreign_key = updated_by))] #[diesel(table_name = datasets)] pub struct Dataset { pub id: Uuid, pub name: String, pub database_name: String, pub when_to_use: Option, pub when_not_to_use: Option, #[serde(rename = "type")] pub type_: DatasetType, pub definition: String, pub schema: String, pub enabled: bool, pub imported: bool, pub data_source_id: Uuid, pub organization_id: Uuid, pub created_by: Uuid, pub updated_by: Uuid, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, pub model: Option, pub yml_file: Option, pub database_identifier: Option, } #[derive(Insertable, Queryable, Associations, Debug)] #[diesel(belongs_to(Dataset, foreign_key = dataset_id))] #[diesel(belongs_to(PermissionGroup, foreign_key = permission_group_id))] #[diesel(table_name = datasets_to_permission_groups)] pub struct DatasetToPermissionGroup { pub dataset_id: Uuid, pub permission_group_id: Uuid, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, } #[derive( Queryable, Insertable, Identifiable, Associations, Clone, Serialize, Deserialize, Debug, )] #[diesel(belongs_to(User, foreign_key = created_by, foreign_key = updated_by))] #[diesel(table_name = collections)] pub struct Collection { pub id: Uuid, pub name: String, pub description: Option, pub created_by: Uuid, pub updated_by: Uuid, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, pub organization_id: Uuid, } #[derive(Queryable, Insertable, Identifiable, Debug, Clone, Serialize, Deserialize)] #[diesel(table_name = organizations)] pub struct Organization { pub id: Uuid, pub name: String, pub domain: Option, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, } #[derive( Queryable, Insertable, Identifiable, Associations, Debug, Clone, Serialize, Deserialize, )] #[diesel(belongs_to(Organization))] #[diesel(table_name = teams)] pub struct Team { pub id: Uuid, pub name: String, pub organization_id: Uuid, pub sharing_setting: SharingSetting, pub edit_sql: bool, pub upload_csv: bool, pub export_assets: bool, pub email_slack_enabled: bool, #[serde(skip_serializing)] pub created_by: Uuid, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, } #[derive(Queryable, Insertable, Associations, Debug)] #[diesel(belongs_to(Team))] #[diesel(belongs_to(User))] #[diesel(table_name = teams_to_users)] pub struct TeamToUser { pub team_id: Uuid, pub user_id: Uuid, pub role: TeamToUserRole, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, } #[derive(Selectable, Insertable, Queryable, Identifiable, Debug, Clone, Serialize, Deserialize)] #[diesel(table_name = users)] #[diesel(check_for_backend(diesel::pg::Pg))] pub struct User { pub id: Uuid, pub email: String, pub name: Option, #[serde(skip_serializing)] pub config: Value, pub created_at: DateTime, pub updated_at: DateTime, pub attributes: Value, pub avatar_url: Option, } #[derive( Clone, Insertable, Queryable, QueryableByName, Identifiable, Associations, Debug, Serialize, Selectable, AsChangeset, sqlx::FromRow, )] #[diesel(belongs_to(User, foreign_key = sent_by))] #[diesel(belongs_to(Dataset))] #[diesel(table_name = messages_deprecated)] pub struct MessageDeprecated { pub id: Uuid, pub thread_id: Uuid, pub sent_by: Uuid, pub message: String, #[serde(rename = "response")] pub responses: Option, pub code: Option, #[serde(skip_serializing)] pub context: Option, pub title: Option, pub feedback: Option, #[serde(rename = "status")] pub verification: Verification, pub dataset_id: Option, pub chart_config: Option, pub chart_recommendations: Option, pub time_frame: Option, pub data_metadata: Option, pub draft_session_id: Option, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, #[serde(skip_serializing)] pub draft_state: Option, #[serde(rename = "description")] pub summary_question: Option, pub sql_evaluation_id: Option, } #[derive(Selectable, Queryable, Insertable, Identifiable, Associations, Debug, Serialize)] #[diesel(belongs_to(User, foreign_key = created_by, foreign_key = updated_by))] #[diesel(table_name = permission_groups)] pub struct PermissionGroup { pub id: Uuid, pub name: String, pub organization_id: Uuid, pub created_by: Uuid, pub updated_by: Uuid, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, } #[derive( Serialize, Insertable, Queryable, QueryableByName, Identifiable, Associations, Debug, Clone, Hash, PartialEq, Eq, )] #[diesel(belongs_to(User, foreign_key = created_by, foreign_key = updated_by))] #[diesel(table_name = terms)] pub struct Term { pub id: Uuid, pub name: String, pub definition: Option, pub sql_snippet: Option, pub organization_id: Uuid, pub created_by: Uuid, pub updated_by: Uuid, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, } #[derive( Clone, Queryable, Insertable, Identifiable, Associations, Debug, Serialize, Selectable, AsChangeset, )] #[diesel(belongs_to(User, foreign_key = created_by, foreign_key = updated_by))] #[diesel(table_name = threads_deprecated)] pub struct ThreadDeprecated { pub id: Uuid, pub created_by: Uuid, pub updated_by: Uuid, pub publicly_accessible: bool, pub publicly_enabled_by: Option, pub public_expiry_date: Option>, #[serde(skip_serializing)] pub password_secret_id: Option, pub state_message_id: Option, pub parent_thread_id: Option, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, pub organization_id: Uuid, } #[derive(Queryable, Insertable, Associations, Debug)] #[diesel(belongs_to(ThreadDeprecated, foreign_key = thread_id))] #[diesel(belongs_to(Dashboard, foreign_key = dashboard_id))] #[diesel(belongs_to(User, foreign_key = added_by))] #[diesel(table_name = threads_to_dashboards)] pub struct ThreadToDashboard { pub thread_id: Uuid, pub dashboard_id: Uuid, pub added_by: Uuid, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, } #[derive(Queryable, Insertable, Associations, Debug)] #[diesel(belongs_to(PermissionGroup, foreign_key = permission_group_id))] #[diesel(table_name = permission_groups_to_identities)] pub struct PermissionGroupToIdentity { pub permission_group_id: Uuid, pub identity_id: Uuid, pub identity_type: IdentityType, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, pub created_by: Uuid, pub updated_by: Uuid, } #[derive(Queryable, Insertable, Debug)] #[diesel(table_name = asset_permissions)] pub struct AssetPermission { pub identity_id: Uuid, pub identity_type: IdentityType, pub asset_id: Uuid, pub asset_type: AssetType, pub role: AssetPermissionRole, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, pub created_by: Uuid, pub updated_by: Uuid, } #[derive(Queryable, Insertable, Associations, Debug)] #[diesel(belongs_to(Collection, foreign_key = collection_id))] #[diesel(table_name = collections_to_assets)] pub struct CollectionToAsset { pub collection_id: Uuid, pub asset_id: Uuid, pub asset_type: AssetType, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, pub created_by: Uuid, pub updated_by: Uuid, } #[derive(Queryable, Insertable, Associations, Debug)] #[diesel(belongs_to(User, foreign_key = user_id))] #[diesel(belongs_to(Organization, foreign_key = organization_id))] #[diesel(table_name = users_to_organizations)] pub struct UserToOrganization { pub user_id: Uuid, pub organization_id: Uuid, pub role: UserOrganizationRole, pub sharing_setting: SharingSetting, pub edit_sql: bool, pub upload_csv: bool, pub export_assets: bool, pub email_slack_enabled: bool, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, pub created_by: Uuid, pub updated_by: Uuid, pub deleted_by: Option, pub status: UserOrganizationStatus, } #[derive(Queryable, Insertable, Associations, Debug)] #[diesel(belongs_to(User, foreign_key = user_id))] #[diesel(table_name = user_favorites)] pub struct UserFavorite { pub user_id: Uuid, pub asset_id: Uuid, pub asset_type: AssetType, pub order_index: i32, pub created_at: DateTime, pub deleted_at: Option>, } #[derive(Queryable, Insertable, Associations, Debug)] #[diesel(belongs_to(Term, foreign_key = term_id))] #[diesel(belongs_to(Dataset, foreign_key = dataset_id))] #[diesel(table_name = terms_to_datasets)] pub struct TermToDataset { pub term_id: Uuid, pub dataset_id: Uuid, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, } #[derive(Queryable, Insertable, Debug, Serialize, Deserialize)] #[diesel(table_name = sql_evaluations)] pub struct SqlEvaluation { pub id: Uuid, pub evaluation_obj: serde_json::Value, pub evaluation_summary: String, pub score: String, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, } #[derive(Queryable, Insertable, Debug)] #[diesel(table_name = entity_relationship)] pub struct EntityRelationship { pub primary_dataset_id: Uuid, pub foreign_dataset_id: Uuid, pub relationship_type: String, pub created_at: DateTime, } #[derive(Queryable, Insertable, Debug)] #[diesel(table_name = dataset_groups)] pub struct DatasetGroup { pub id: Uuid, pub organization_id: Uuid, pub name: String, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, } #[derive(Queryable, Insertable, Debug)] #[diesel(table_name = dataset_permissions)] pub struct DatasetPermission { pub id: Uuid, pub organization_id: Uuid, pub dataset_id: Uuid, pub permission_id: Uuid, pub permission_type: String, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, } #[derive(Queryable, Insertable, Debug)] #[diesel(table_name = dataset_groups_permissions)] pub struct DatasetGroupPermission { pub id: Uuid, pub dataset_group_id: Uuid, pub permission_id: Uuid, pub permission_type: String, pub organization_id: Uuid, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, } #[derive(Queryable, Insertable, Associations, Debug)] #[diesel(belongs_to(Dataset, foreign_key = dataset_id))] #[diesel(belongs_to(DatasetGroup, foreign_key = dataset_group_id))] #[diesel(table_name = datasets_to_dataset_groups)] pub struct DatasetToDatasetGroup { pub dataset_id: Uuid, pub dataset_group_id: Uuid, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, } // TODO: These are json types that go in the db. #[derive(Serialize, Deserialize, Debug, Clone)] #[serde(untagged)] pub enum MinMaxValue { Number(f64), String(String), } #[derive(Serialize, Deserialize, Debug, Clone)] pub struct ColumnMetadata { pub name: String, #[serde(rename = "type")] pub type_: String, pub simple_type: Option, pub unique_values: i32, pub min_value: Option, pub max_value: Option, } #[derive(Serialize, Deserialize, Debug, Clone)] pub struct DataMetadataJsonBody { pub column_count: i32, pub row_count: i32, pub column_metadata: Vec, } #[derive(Serialize, Deserialize, Debug, Clone)] pub struct MessageResponses { pub messages: Vec, } #[derive(Serialize, Deserialize, Debug, Clone)] pub struct UserConfig { pub color_palettes: Option>>, pub last_used_color_palette: Option>, } #[derive(Serialize, Deserialize, Debug, Clone)] #[serde(rename_all = "camelCase")] pub enum StepProgress { InProgress, Completed, Failed, } #[derive(Queryable, Insertable, Associations, Debug)] #[diesel(belongs_to(MetricFile, foreign_key = metric_file_id))] #[diesel(belongs_to(DashboardFile, foreign_key = dashboard_file_id))] #[diesel(table_name = metric_files_to_dashboard_files)] pub struct MetricFileToDashboardFile { pub metric_file_id: Uuid, pub dashboard_file_id: Uuid, pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, pub created_by: Uuid, }