Skip to content

Commit

Permalink
remove unnecessary func
Browse files Browse the repository at this point in the history
  • Loading branch information
kasugamirai committed Sep 29, 2024
1 parent a91dbb9 commit b231e12
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 68 deletions.
67 changes: 6 additions & 61 deletions websocket/crates/infra/src/persistence/project_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,40 +122,12 @@ impl ProjectSnapshotRepository<ProjectRepositoryError> for ProjectGcsRepository
&self,
snapshot: ProjectSnapshot,
) -> Result<(), ProjectRepositoryError> {
let snapshot_metadata = serde_json::to_vec(&snapshot.metadata)?;

// Upload the serialized data to GCS, overwriting the existing file
let path = format!("snapshot/{}", snapshot.metadata.project_id);
self.client.upload(path.clone(), &snapshot_metadata).await?;

// Update the latest snapshot reference
let latest_path = format!("snapshot/{}:latest_snapshot", snapshot.metadata.project_id);
self.client.upload(latest_path, &snapshot).await?;

//Get the latest snapshot state
let latest_snapshot_state_path = format!(
"snapshot/{}:latest_snapshot_state",
snapshot.metadata.project_id
);

let latest_snapshot_state: Vec<u8> = self
.client
.download(latest_snapshot_state_path.clone())
.await?;

// Update the latest snapshot state
let mut latest_snapshot_state: SnapshotInfo =
serde_json::from_slice(&latest_snapshot_state)?;
latest_snapshot_state.updated_at = Some(Utc::now());
latest_snapshot_state
.changes_by
.push(snapshot.info.created_by.unwrap_or_default());

self.client
.upload(latest_snapshot_state_path, &latest_snapshot_state)
.await?;

Ok(())
// Implementation details would depend on your storage solution
// For example, with GCS, you might:
// 1. Serialize the snapshot
// 2. Upload the serialized data to GCS, overwriting the existing file
// 3. Update any metadata as necessary
unimplemented!("update_snapshot needs to be implemented")
}
}

Expand Down Expand Up @@ -282,33 +254,6 @@ mod tests {
Ok((temp_dir, repo))
}

#[test]
async fn test_get_project_non_existent() -> Result<(), Box<dyn std::error::Error>> {
let (_temp_dir, repo) = setup().await?;
let project_id = "non_existent_project";
assert!(repo.get_project(project_id).await?.is_none());
Ok(())
}

#[test]
async fn test_get_project_existing() -> Result<(), Box<dyn std::error::Error>> {
let (_temp_dir, repo) = setup().await?;
let project_id = "test_project";
let project = Project {
id: project_id.to_string(),
workspace_id: "test_workspace".to_string(),
};

repo.client
.upload(format!("projects/{}", project_id), &project, true)
.await?;

let retrieved_project = repo.get_project(project_id).await?.unwrap();
assert_eq!(retrieved_project.id, project.id);
assert_eq!(retrieved_project.workspace_id, project.workspace_id);
Ok(())
}

fn create_test_snapshot(project_id: &str) -> ProjectSnapshot {
let now = Utc::now();

Expand Down
6 changes: 3 additions & 3 deletions websocket/crates/services/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// pub mod project;
// pub mod snapshot;
// pub mod manage_project_edit_session;
pub mod manage_project_edit_session;
pub mod project;
pub mod snapshot;
3 changes: 2 additions & 1 deletion websocket/crates/services/src/manage_project_edit_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use flow_websocket_domain::project::ProjectEditingSession;
use flow_websocket_domain::repository::{
ProjectEditingSessionRepository, ProjectSnapshotRepository,
};
use flow_websocket_domain::snapshot::Metadata;
use std::error::Error;
use std::sync::Arc;
use tokio::time::sleep;
Expand Down Expand Up @@ -94,7 +95,7 @@ impl ManageEditSessionService {
if snapshot_time_delta > MAX_SNAPSHOT_DELTA {
let (state, _) = session.get_state_update().await?;

let metadata = ProjectMetadata::new(
let metadata = Metadata::new(
generate_id(14, "snap"),
session.project_id.clone(),
Some(session.session_id.clone()),
Expand Down
28 changes: 25 additions & 3 deletions websocket/crates/services/src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use async_trait::async_trait;
use chrono::Utc;
use flow_websocket_domain::repository::ProjectSnapshotRepository;
use flow_websocket_domain::snapshot::{
ObjectDelete, ObjectTenant, ProjectMetadata, ProjectSnapshot,
Metadata, ObjectDelete, ObjectTenant, ProjectSnapshot, SnapshotInfo,
};
use std::error::Error;
use std::sync::Arc;
Expand All @@ -26,15 +26,15 @@ impl SnapshotService {
let snapshot_id = Uuid::new_v4().to_string();
let now = Utc::now();

let metadata = ProjectMetadata::new(
let metadata = Metadata::new(
snapshot_id.clone(),
data.project_id.clone(),
data.session_id,
data.name.unwrap_or_default(),
String::new(),
);

let state = SnapshotState::new(
let state = SnapshotInfo::new(
data.created_by,
data.changes_by,
data.tenant,
Expand Down Expand Up @@ -109,3 +109,25 @@ pub struct CreateSnapshotData {
pub tenant: ObjectTenant,
pub state: Vec<u8>,
}

impl CreateSnapshotData {
pub fn new(
project_id: String,
session_id: Option<String>,
name: Option<String>,
created_by: Option<String>,
changes_by: Vec<String>,
tenant: ObjectTenant,
state: Vec<u8>,
) -> Self {
Self {
project_id,
session_id,
name,
created_by,
changes_by,
tenant,
state,
}
}
}

0 comments on commit b231e12

Please sign in to comment.