aven-core 0.1.35

Core library for the Aven local-first task manager
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use crate::ids::{TaskId, WorkspaceId};
use crate::metadata::TaskMetadataValue;
use std::collections::{HashMap, HashSet};

use crate::query::{
    AttachmentMetadata, EpicRollup, RecentActionItem, TaskConflictValue, TaskDependencyLink,
    TaskNote, TaskRecurrenceSummary,
};
use crate::refs::DisplayRefContext;
use anyhow::Result;
use sqlx::sqlite::SqliteRow;
use sqlx::{QueryBuilder, Row, Sqlite, SqliteConnection};

mod attachments;
mod dependencies;
mod epics;
mod notes;

const SQLITE_BIND_CHUNK_SIZE: usize = 900;

#[derive(Default)]
pub struct TaskEnrichment {
    pub labels_by_task: HashMap<TaskId, Vec<String>>,
    pub notes_by_task: HashMap<TaskId, Vec<TaskNote>>,
    pub task_ids_with_notes: HashSet<TaskId>,
    pub attachments_by_task: HashMap<TaskId, Vec<AttachmentMetadata>>,
    pub live_attachment_counts_by_task: HashMap<TaskId, u32>,
    pub metadata_by_task: HashMap<TaskId, Vec<TaskMetadataValue>>,
    pub activity_by_task: HashMap<TaskId, Vec<RecentActionItem>>,
    pub conflicts_by_task: HashMap<TaskId, Vec<TaskConflictValue>>,
    pub conflicted_task_ids: HashSet<TaskId>,
    pub unresolved_blocker_counts_by_task: HashMap<TaskId, i64>,
    pub dependent_counts_by_task: HashMap<TaskId, i64>,
    pub depends_on_by_task: HashMap<TaskId, Vec<TaskDependencyLink>>,
    pub blocks_by_task: HashMap<TaskId, Vec<TaskDependencyLink>>,
    pub related_by_task: HashMap<TaskId, Vec<crate::query::TaskRelatedLink>>,
    pub epic_children_by_task: HashMap<TaskId, Vec<TaskDependencyLink>>,
    pub epic_child_dependencies_by_task: HashMap<TaskId, Vec<TaskDependencyLink>>,
    pub epic_parent_by_task: HashMap<TaskId, TaskDependencyLink>,
    pub epic_rollups_by_task: HashMap<TaskId, EpicRollup>,
    pub recurrence_by_task: HashMap<TaskId, TaskRecurrenceSummary>,
}

pub async fn load_task_enrichment(
    conn: &mut SqliteConnection,
    workspace_id: &WorkspaceId,
    task_ids: &[TaskId],
    display_refs: &DisplayRefContext,
) -> Result<TaskEnrichment> {
    load_task_enrichment_with_detail(conn, workspace_id, task_ids, display_refs, true, true).await
}

pub(crate) async fn load_task_enrichment_without_activity(
    conn: &mut SqliteConnection,
    workspace_id: &WorkspaceId,
    task_ids: &[TaskId],
    display_refs: &DisplayRefContext,
) -> Result<TaskEnrichment> {
    load_task_enrichment_with_detail(conn, workspace_id, task_ids, display_refs, true, false).await
}

pub(crate) async fn load_task_list_enrichment(
    conn: &mut SqliteConnection,
    workspace_id: &WorkspaceId,
    task_ids: &[TaskId],
    display_refs: &DisplayRefContext,
) -> Result<TaskEnrichment> {
    load_task_enrichment_with_detail(conn, workspace_id, task_ids, display_refs, false, false).await
}

pub(crate) async fn load_task_bulk_update_enrichment(
    conn: &mut SqliteConnection,
    workspace_id: &WorkspaceId,
    task_ids: &[TaskId],
) -> Result<TaskEnrichment> {
    Ok(TaskEnrichment {
        labels_by_task: labels_for_tasks(conn, workspace_id, task_ids).await?,
        metadata_by_task: crate::metadata::metadata_by_task_ids(conn, workspace_id, task_ids)
            .await?,
        recurrence_by_task: crate::query::task_recurrence_summaries(conn, workspace_id, task_ids)
            .await?,
        ..TaskEnrichment::default()
    })
}

async fn load_task_enrichment_with_detail(
    conn: &mut SqliteConnection,
    workspace_id: &WorkspaceId,
    task_ids: &[TaskId],
    display_refs: &DisplayRefContext,
    include_detail: bool,
    include_activity: bool,
) -> Result<TaskEnrichment> {
    let (notes_by_task, task_ids_with_notes, attachments_by_task, metadata_by_task) =
        if include_detail {
            (
                notes::notes_for_tasks(conn, workspace_id, task_ids).await?,
                HashSet::new(),
                attachments::attachments_for_tasks(conn, workspace_id, task_ids).await?,
                crate::metadata::metadata_by_task_ids(conn, workspace_id, task_ids).await?,
            )
        } else {
            (
                HashMap::new(),
                notes::task_ids_with_notes(conn, workspace_id, task_ids).await?,
                HashMap::new(),
                HashMap::new(),
            )
        };
    let live_attachment_counts_by_task = if include_detail {
        attachments_by_task
            .iter()
            .map(|(task_id, attachments)| {
                (
                    task_id.clone(),
                    attachments.len().min(u32::MAX as usize) as u32,
                )
            })
            .collect()
    } else {
        attachments::live_attachment_counts_for_tasks(conn, workspace_id, task_ids).await?
    };
    let activity_by_task = if include_activity {
        crate::query::task_activity_for_tasks_in_workspace(conn, workspace_id, task_ids).await?
    } else {
        HashMap::new()
    };
    let (conflicted_task_ids, conflicts_by_task) = if include_detail {
        conflicts_for_tasks(conn, workspace_id, task_ids).await?
    } else {
        (
            tasks_with_unresolved_conflicts(conn, workspace_id, task_ids).await?,
            HashMap::new(),
        )
    };
    let epic_children_by_task =
        epics::epic_children_for_tasks(conn, workspace_id, task_ids, display_refs).await?;
    let epic_child_dependencies_by_task = if include_detail {
        let child_ids = epic_children_by_task
            .values()
            .flatten()
            .map(|child| child.task_id.clone())
            .collect::<HashSet<_>>()
            .into_iter()
            .collect::<Vec<_>>();
        dependencies::dependency_links_for_tasks(
            conn,
            workspace_id,
            &child_ids,
            false,
            display_refs,
        )
        .await?
    } else {
        HashMap::new()
    };
    Ok(TaskEnrichment {
        labels_by_task: labels_for_tasks(conn, workspace_id, task_ids).await?,
        notes_by_task,
        task_ids_with_notes,
        attachments_by_task,
        live_attachment_counts_by_task,
        metadata_by_task,
        activity_by_task,
        conflicts_by_task,
        conflicted_task_ids,
        unresolved_blocker_counts_by_task: dependencies::unresolved_blocker_counts_for_tasks(
            conn,
            workspace_id,
            task_ids,
        )
        .await?,
        dependent_counts_by_task: dependencies::dependent_counts_for_tasks(
            conn,
            workspace_id,
            task_ids,
        )
        .await?,
        depends_on_by_task: dependencies::dependency_links_for_tasks(
            conn,
            workspace_id,
            task_ids,
            false,
            display_refs,
        )
        .await?,
        blocks_by_task: dependencies::dependency_links_for_tasks(
            conn,
            workspace_id,
            task_ids,
            true,
            display_refs,
        )
        .await?,
        related_by_task: if include_detail {
            crate::query::related_links_for_tasks(conn, workspace_id, task_ids, display_refs)
                .await?
        } else {
            HashMap::new()
        },
        epic_children_by_task,
        epic_child_dependencies_by_task,
        epic_parent_by_task: epics::epic_parents_for_tasks(
            conn,
            workspace_id,
            task_ids,
            display_refs,
        )
        .await?,
        epic_rollups_by_task: epics::epic_rollups_for_tasks(conn, workspace_id, task_ids).await?,
        recurrence_by_task: crate::query::task_recurrence_summaries(conn, workspace_id, task_ids)
            .await?,
    })
}
pub(crate) async fn labels_for_tasks(
    conn: &mut SqliteConnection,
    workspace_id: &WorkspaceId,
    task_ids: &[TaskId],
) -> Result<HashMap<TaskId, Vec<String>>> {
    let mut labels_by_task = HashMap::new();
    if task_ids.is_empty() {
        return Ok(labels_by_task);
    }
    for chunk in task_ids.chunks(SQLITE_BIND_CHUNK_SIZE) {
        if chunk.is_empty() {
            continue;
        }
        let mut query = QueryBuilder::<Sqlite>::new(
            "SELECT task_id, label FROM task_labels WHERE workspace_id = ",
        );
        query.push_bind(workspace_id);
        query.push(" AND task_id IN (");
        {
            let mut separated = query.separated(", ");
            for task_id in chunk {
                separated.push_bind(task_id);
            }
        }
        query.push(") ORDER BY task_id, label");

        for row in query.build().fetch_all(&mut *conn).await? {
            let task_id: TaskId = row.get("task_id");
            let label: String = row.get("label");
            labels_by_task
                .entry(task_id)
                .or_insert_with(Vec::new)
                .push(label);
        }
    }
    Ok(labels_by_task)
}

fn dependency_link_from_row(
    row: &SqliteRow,
    workspace_id: &WorkspaceId,
    display_refs: &DisplayRefContext,
) -> TaskDependencyLink {
    let task_id: TaskId = row.get("id");
    let project_prefix: String = row.get("project_prefix");
    TaskDependencyLink {
        project_key: row.get("project_key"),
        task_id: task_id.clone(),
        display_ref: display_refs.display_ref_for_id(workspace_id, &project_prefix, &task_id),
        title: row.get("title"),
        status: row.get("status"),
        priority: row.get("priority"),
        unresolved: row.get::<i64, _>("unresolved") != 0,
    }
}
async fn conflicts_for_tasks(
    conn: &mut SqliteConnection,
    workspace_id: &WorkspaceId,
    task_ids: &[TaskId],
) -> Result<(HashSet<TaskId>, HashMap<TaskId, Vec<TaskConflictValue>>)> {
    let mut conflicted = HashSet::new();
    let mut conflicts_by_task = HashMap::new();
    if task_ids.is_empty() {
        return Ok((conflicted, conflicts_by_task));
    }
    for chunk in task_ids.chunks(SQLITE_BIND_CHUNK_SIZE) {
        let mut query = QueryBuilder::<Sqlite>::new(
            "SELECT task_id, field, local_value, remote_value
             FROM conflicts WHERE workspace_id = ",
        );
        query.push_bind(workspace_id);
        query.push(" AND resolved = 0 AND task_id IN (");
        {
            let mut separated = query.separated(", ");
            for task_id in chunk {
                separated.push_bind(task_id);
            }
        }
        query.push(") ORDER BY task_id, field, id");

        for row in query.build().fetch_all(&mut *conn).await? {
            let task_id: TaskId = row.get("task_id");
            conflicted.insert(task_id.clone());
            conflicts_by_task
                .entry(task_id)
                .or_insert_with(Vec::new)
                .push(TaskConflictValue {
                    field: row.get("field"),
                    local_value: row.get("local_value"),
                    remote_value: row.get("remote_value"),
                });
        }
    }
    Ok((conflicted, conflicts_by_task))
}

async fn tasks_with_unresolved_conflicts(
    conn: &mut SqliteConnection,
    workspace_id: &WorkspaceId,
    task_ids: &[TaskId],
) -> Result<HashSet<TaskId>> {
    let mut conflicted = HashSet::new();
    if task_ids.is_empty() {
        return Ok(conflicted);
    }
    for chunk in task_ids.chunks(SQLITE_BIND_CHUNK_SIZE) {
        if chunk.is_empty() {
            continue;
        }
        let mut query = QueryBuilder::<Sqlite>::new(
            "SELECT DISTINCT task_id FROM conflicts WHERE workspace_id = ",
        );
        query.push_bind(workspace_id);
        query.push(" AND resolved = 0 AND task_id IN (");
        {
            let mut separated = query.separated(", ");
            for task_id in chunk {
                separated.push_bind(task_id);
            }
        }
        query.push(")");

        for row in query.build().fetch_all(&mut *conn).await? {
            conflicted.insert(row.get("task_id"));
        }
    }
    Ok(conflicted)
}
pub(crate) async fn epic_parents_for_tasks(
    conn: &mut SqliteConnection,
    workspace_id: &WorkspaceId,
    task_ids: &[TaskId],
    display_refs: &DisplayRefContext,
) -> Result<HashMap<TaskId, TaskDependencyLink>> {
    epics::epic_parents_for_tasks(conn, workspace_id, task_ids, display_refs).await
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn task_enrichment_loads_notes_across_bind_chunks() {
        let (_temp, mut conn) = crate::test_support::test_conn().await;
        let workspace_id = crate::workspaces::default_workspace_id();
        let task_ids = (0..=SQLITE_BIND_CHUNK_SIZE)
            .map(|index| format!("{index:016}").parse().unwrap())
            .collect::<Vec<TaskId>>();

        sqlx::query(
            "INSERT INTO notes(workspace_id, id, task_id, body, created_at, change_id)
             VALUES (?, 'note-first-old', '0000000000000000', 'older', '001', 'change-first-old'),
                    (?, 'note-first-new', '0000000000000000', 'newer', '002', 'change-first-new'),
                    (?, 'note-last', ?, 'last', '003', 'change-last')",
        )
        .bind(&workspace_id)
        .bind(&workspace_id)
        .bind(&workspace_id)
        .bind(task_ids.last().unwrap())
        .execute(&mut *conn)
        .await
        .unwrap();

        let display_refs = DisplayRefContext::for_workspace(&mut conn, &workspace_id)
            .await
            .unwrap();
        let enrichment = load_task_enrichment(&mut conn, &workspace_id, &task_ids, &display_refs)
            .await
            .unwrap();

        assert_eq!(
            enrichment
                .notes_by_task
                .get("0000000000000000")
                .unwrap()
                .iter()
                .map(|note| note.body.as_str())
                .collect::<Vec<_>>(),
            ["newer", "older"]
        );
        assert_eq!(
            enrichment
                .notes_by_task
                .get(task_ids.last().unwrap())
                .unwrap()[0]
                .body,
            "last"
        );
    }
}