| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
use std::sync::Arc; |
| 9 |
|
| 10 |
use serde::Serialize; |
| 11 |
use tauri::State; |
| 12 |
use tracing::instrument; |
| 13 |
|
| 14 |
use goingson_core::{LinkedTaskRef, ProjectId, TaskGraph, TaskId}; |
| 15 |
|
| 16 |
use super::ApiError; |
| 17 |
use super::task::TaskResponse; |
| 18 |
use crate::state::{AppState, DESKTOP_USER_ID}; |
| 19 |
|
| 20 |
|
| 21 |
#[derive(Debug, Serialize)] |
| 22 |
#[serde(rename_all = "camelCase")] |
| 23 |
pub struct LinkedTaskResponse { |
| 24 |
pub id: TaskId, |
| 25 |
pub title: String, |
| 26 |
pub status: String, |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
pub satisfied: bool, |
| 31 |
pub project_name: Option<String>, |
| 32 |
} |
| 33 |
|
| 34 |
impl From<LinkedTaskRef> for LinkedTaskResponse { |
| 35 |
fn from(r: LinkedTaskRef) -> Self { |
| 36 |
Self { |
| 37 |
satisfied: r.is_satisfied(), |
| 38 |
id: r.id, |
| 39 |
status: r.status.as_str().to_string(), |
| 40 |
title: r.title, |
| 41 |
project_name: r.project_name, |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
#[derive(Debug, Serialize)] |
| 48 |
#[serde(rename_all = "camelCase")] |
| 49 |
pub struct TaskDependencyResponse { |
| 50 |
|
| 51 |
pub blocked_by: Vec<LinkedTaskResponse>, |
| 52 |
|
| 53 |
pub blocks: Vec<LinkedTaskResponse>, |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
#[derive(Debug, Serialize)] |
| 58 |
#[serde(rename_all = "camelCase")] |
| 59 |
pub struct GraphNodeResponse { |
| 60 |
pub id: TaskId, |
| 61 |
pub title: String, |
| 62 |
pub status: String, |
| 63 |
pub priority: String, |
| 64 |
pub project_id: Option<ProjectId>, |
| 65 |
pub block_depth: u32, |
| 66 |
pub unblocks_count: u32, |
| 67 |
pub in_cycle: bool, |
| 68 |
pub is_blocked: bool, |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
#[derive(Debug, Serialize)] |
| 73 |
#[serde(rename_all = "camelCase")] |
| 74 |
pub struct GraphEdgeResponse { |
| 75 |
pub blocked_id: TaskId, |
| 76 |
pub blocker_id: TaskId, |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
#[derive(Debug, Serialize)] |
| 81 |
#[serde(rename_all = "camelCase")] |
| 82 |
pub struct TaskGraphResponse { |
| 83 |
pub nodes: Vec<GraphNodeResponse>, |
| 84 |
pub edges: Vec<GraphEdgeResponse>, |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
pub cycles: Vec<Vec<TaskId>>, |
| 89 |
} |
| 90 |
|
| 91 |
impl From<TaskGraph> for TaskGraphResponse { |
| 92 |
fn from(g: TaskGraph) -> Self { |
| 93 |
Self { |
| 94 |
nodes: g |
| 95 |
.nodes |
| 96 |
.into_iter() |
| 97 |
.map(|n| GraphNodeResponse { |
| 98 |
is_blocked: n.position.is_blocked() || n.position.in_cycle, |
| 99 |
block_depth: n.position.block_depth, |
| 100 |
unblocks_count: n.position.unblocks_count, |
| 101 |
in_cycle: n.position.in_cycle, |
| 102 |
id: n.id, |
| 103 |
title: n.title, |
| 104 |
status: n.status.as_str().to_string(), |
| 105 |
priority: n.priority.as_str().to_string(), |
| 106 |
project_id: n.project_id, |
| 107 |
}) |
| 108 |
.collect(), |
| 109 |
edges: g |
| 110 |
.edges |
| 111 |
.into_iter() |
| 112 |
.map(|e| GraphEdgeResponse { |
| 113 |
blocked_id: e.blocked_id, |
| 114 |
blocker_id: e.blocker_id, |
| 115 |
}) |
| 116 |
.collect(), |
| 117 |
cycles: g.cycles, |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
#[tauri::command] |
| 128 |
#[instrument(skip_all)] |
| 129 |
pub async fn get_task_dependencies( |
| 130 |
state: State<'_, Arc<AppState>>, |
| 131 |
id: TaskId, |
| 132 |
) -> Result<TaskDependencyResponse, ApiError> { |
| 133 |
let blocked_by = state.tasks.list_blockers(DESKTOP_USER_ID, id)?; |
| 134 |
let blocks = state.tasks.list_dependents(DESKTOP_USER_ID, id)?; |
| 135 |
|
| 136 |
Ok(TaskDependencyResponse { |
| 137 |
blocked_by: blocked_by.into_iter().map(Into::into).collect(), |
| 138 |
blocks: blocks.into_iter().map(Into::into).collect(), |
| 139 |
}) |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
#[tauri::command] |
| 153 |
#[instrument(skip_all)] |
| 154 |
pub async fn add_task_dependency( |
| 155 |
state: State<'_, Arc<AppState>>, |
| 156 |
blocked_id: TaskId, |
| 157 |
blocker_id: TaskId, |
| 158 |
) -> Result<TaskResponse, ApiError> { |
| 159 |
state |
| 160 |
.tasks |
| 161 |
.add_dependency(DESKTOP_USER_ID, blocked_id, blocker_id)?; |
| 162 |
|
| 163 |
let task = state |
| 164 |
.tasks |
| 165 |
.get_by_id(blocked_id, DESKTOP_USER_ID)? |
| 166 |
.ok_or_else(|| ApiError::not_found("task", blocked_id))?; |
| 167 |
Ok(TaskResponse::from(task)) |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
#[tauri::command] |
| 181 |
#[instrument(skip_all)] |
| 182 |
pub async fn remove_task_dependency( |
| 183 |
state: State<'_, Arc<AppState>>, |
| 184 |
blocked_id: TaskId, |
| 185 |
blocker_id: TaskId, |
| 186 |
) -> Result<TaskResponse, ApiError> { |
| 187 |
let removed = state |
| 188 |
.tasks |
| 189 |
.remove_dependency(DESKTOP_USER_ID, blocked_id, blocker_id)?; |
| 190 |
if !removed { |
| 191 |
return Err(ApiError::not_found("dependency", blocked_id)); |
| 192 |
} |
| 193 |
|
| 194 |
let task = state |
| 195 |
.tasks |
| 196 |
.get_by_id(blocked_id, DESKTOP_USER_ID)? |
| 197 |
.ok_or_else(|| ApiError::not_found("task", blocked_id))?; |
| 198 |
Ok(TaskResponse::from(task)) |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
#[tauri::command] |
| 211 |
#[instrument(skip_all)] |
| 212 |
pub async fn get_task_graph( |
| 213 |
state: State<'_, Arc<AppState>>, |
| 214 |
project_id: Option<ProjectId>, |
| 215 |
) -> Result<TaskGraphResponse, ApiError> { |
| 216 |
Ok(state.tasks.task_graph(DESKTOP_USER_ID, project_id)?.into()) |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
#[tauri::command] |
| 229 |
#[instrument(skip_all)] |
| 230 |
pub async fn list_ready_tasks( |
| 231 |
state: State<'_, Arc<AppState>>, |
| 232 |
project_id: Option<ProjectId>, |
| 233 |
depth: Option<u32>, |
| 234 |
limit: Option<i64>, |
| 235 |
) -> Result<Vec<TaskResponse>, ApiError> { |
| 236 |
let tasks = state |
| 237 |
.tasks |
| 238 |
.list_ready(DESKTOP_USER_ID, project_id, depth.unwrap_or(0), limit)?; |
| 239 |
Ok(tasks.into_iter().map(TaskResponse::from).collect()) |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
#[tauri::command] |
| 252 |
#[instrument(skip_all)] |
| 253 |
pub async fn recompute_task_graph(state: State<'_, Arc<AppState>>) -> Result<usize, ApiError> { |
| 254 |
Ok(state.tasks.recompute_graph(DESKTOP_USER_ID)?) |
| 255 |
} |
| 256 |
|