Skip to content

Commit

Permalink
Merge pull request #2 from CoLearn-Dev/status-list
Browse files Browse the repository at this point in the history
- maintain status lists of all tasks
- bump version to 0.1.1
  • Loading branch information
stneng authored May 12, 2022
2 parents 24ed955 + d514e8a commit 367cbf1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "colink-server"
version = "0.1.0"
version = "0.1.1"
edition = "2021"

[dependencies]
Expand Down
46 changes: 37 additions & 9 deletions src/service/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,13 @@ impl crate::server::MyService {
Ok(Response::new(Empty::default()))
}

async fn remove_task_old_status(&self, user_id: &str, task: &Task) -> Result<(), Status> {
let list_key = format!("protocols:{}:{}", task.protocol_name, task.status);
let list = self._internal_storage_read(user_id, &list_key).await?;
async fn remove_task_from_list_in_storage(
&self,
user_id: &str,
task: &Task,
list_key: &str,
) -> Result<(), Status> {
let list = self._internal_storage_read(user_id, list_key).await?;
let mut list: CoLinkInternalTaskIdList = Message::decode(&*list).unwrap();
let mut index = list.task_ids_with_key_paths.len();
for i in 0..list.task_ids_with_key_paths.len() {
Expand All @@ -349,14 +353,28 @@ impl crate::server::MyService {
list.task_ids_with_key_paths.remove(index);
let mut payload = vec![];
list.encode(&mut payload).unwrap();
self._internal_storage_update(user_id, &list_key, &payload)
self._internal_storage_update(user_id, list_key, &payload)
.await?;
Ok(())
}

async fn add_task_new_status(&self, user_id: &str, task: &Task) -> Result<(), Status> {
async fn remove_task_old_status(&self, user_id: &str, task: &Task) -> Result<(), Status> {
let list_key = format!("protocols:{}:{}", task.protocol_name, task.status);
let latest_key = format!("protocols:{}:{}:latest", task.protocol_name, task.status);
self.remove_task_from_list_in_storage(user_id, task, &list_key)
.await?;
let list_key = format!("tasks:status:{}", task.status);
self.remove_task_from_list_in_storage(user_id, task, &list_key)
.await?;
Ok(())
}

async fn add_task_to_list_in_storage(
&self,
user_id: &str,
task: &Task,
list_key: &str,
) -> Result<(), Status> {
let latest_key = format!("{}:latest", list_key);
let mut payload = vec![];
Task {
task_id: task.task_id.clone(),
Expand All @@ -367,8 +385,8 @@ impl crate::server::MyService {
let key_path = self
._internal_storage_update(user_id, &latest_key, &payload)
.await?;
let mut list = if self._internal_storage_contains(user_id, &list_key).await? {
let list = self._internal_storage_read(user_id, &list_key).await?;
let mut list = if self._internal_storage_contains(user_id, list_key).await? {
let list = self._internal_storage_read(user_id, list_key).await?;
Message::decode(&*list).unwrap()
} else {
CoLinkInternalTaskIdList {
Expand All @@ -382,7 +400,17 @@ impl crate::server::MyService {
});
payload = vec![];
list.encode(&mut payload).unwrap();
self._internal_storage_update(user_id, &list_key, &payload)
self._internal_storage_update(user_id, list_key, &payload)
.await?;
Ok(())
}

async fn add_task_new_status(&self, user_id: &str, task: &Task) -> Result<(), Status> {
let list_key = format!("protocols:{}:{}", task.protocol_name, task.status);
self.add_task_to_list_in_storage(user_id, task, &list_key)
.await?;
let list_key = format!("tasks:status:{}", task.status);
self.add_task_to_list_in_storage(user_id, task, &list_key)
.await?;
Ok(())
}
Expand Down

0 comments on commit 367cbf1

Please sign in to comment.