Skip to content
This repository was archived by the owner on Nov 6, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
date range). The query returns the following fields:
- `openIssueCount`: The number of open issues.
- `resolvedIssueSizeDistribution`: The distribution of sizes for resolved issues.
- `avgResolutionDays`: The average resolution time in days for resolved issues.
- Added additional fields to the `issues` GraphQL query, providing detailed
information such as comments, labels, related sub-issues, linked pull
requests, issue descriptions, timestamps, and project-related metadata.
Expand Down
8 changes: 6 additions & 2 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ use jiff::Timestamp;
pub(crate) use self::discussion::Discussion;
use crate::database::Database;

/// The default page size for connections when neither `first` nor `last` is
/// provided.
// The title of the github project.
pub(crate) const TODO_LIST_PROJECT_TITLE: &str = "to-do list";
// The status name of the "Done" column in the github project.
pub(crate) const TODO_LIST_STATUS_DONE: &str = "Done";

/// The default page size for connections when neither `first` nor `last` is provided.
const DEFAULT_PAGE_SIZE: usize = 100;

/// A set of queries defined in the schema.
Expand Down
6 changes: 1 addition & 5 deletions src/api/issue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use async_graphql::{
use serde::{Deserialize, Serialize};

use crate::{
api,
api::DateTimeUtc,
api::{self, DateTimeUtc, TODO_LIST_PROJECT_TITLE, TODO_LIST_STATUS_DONE},
database::{self, Database, GitHubIssue, TryFromKeyValue},
outbound::issues::{IssueState, PullRequestState},
};
Expand Down Expand Up @@ -112,9 +111,6 @@ pub(crate) struct PullRequestRef {
pub(crate) url: String,
}

pub(super) const TODO_LIST_PROJECT_TITLE: &str = "to-do list";
pub(super) const TODO_LIST_STATUS_DONE: &str = "Done";

impl Issue {
/// We define an issue is "Resolved" if and only if
/// - Status of the issue is "Closed" AND
Expand Down
116 changes: 111 additions & 5 deletions src/api/issue_stat.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use std::collections::BTreeMap;

use anyhow::Context as _;
use async_graphql::{Context, Enum, InputObject, Object, Result, SimpleObject};
use jiff::{SpanTotal, Unit};
use num_traits::ToPrimitive;

use crate::{
api::{issue::Issue, DateTimeUtc},
api::{issue::Issue, DateTimeUtc, TODO_LIST_PROJECT_TITLE},
database::Iter,
outbound::issues::IssueState,
Database,
Expand Down Expand Up @@ -91,6 +94,9 @@ struct IssueStat {

/// The distribution of resolved issues by size.
resolved_issue_size_distribution: Vec<IssueSizeCount>,

/// The average resolution time in days for resolved issues.
avg_resolution_days: Option<f64>,
}

#[Object]
Expand Down Expand Up @@ -122,7 +128,7 @@ impl IssueStatQuery {
.project_items
.nodes
.iter()
.find(|item| item.project_title == super::issue::TODO_LIST_PROJECT_TITLE)
.find(|item| item.project_title == TODO_LIST_PROJECT_TITLE)
.and_then(|item| item.todo_size.as_deref())
.unwrap_or("None");
*acc.entry(size_str.into()).or_insert(0) += 1;
Expand All @@ -132,10 +138,47 @@ impl IssueStatQuery {
.map(|(size, count)| IssueSizeCount { size, count })
.collect();

let resolution_days: Vec<f64> = resolved_issues
.iter()
.filter_map(|issue| {
let closed_at = issue.closed_at?;
let created_at = issue.created_at;

let project_item = issue
.project_items
.nodes
.iter()
.find(|p| p.project_title == TODO_LIST_PROJECT_TITLE)?;

let pending_days = project_item.todo_pending_days.unwrap_or(0.0);

let span = created_at.0.until(closed_at.0).ok()?;
let resolution_days = span
.total(SpanTotal::from(Unit::Day).days_are_24_hours())
.ok()?;
let result_days = resolution_days - pending_days;

Some(f64::max(result_days, 0.0))
})
.collect();

let avg_resolution_days = if resolution_days.is_empty() {
None
} else {
Some(
resolution_days.iter().sum::<f64>()
/ resolution_days

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolution_days 가 0일 수도 있을까요?

@goder-0 goder-0 Sep 15, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

전체 코드는 다음과 같습니다. vector가 비어있을 경우, None으로 처리되어 0인 경우는 예외처리됩니다.

let avg_resolution_days = if resolution_days.is_empty() {
    None
} else {
    Some(
        resolution_days.iter().sum::<f64>()
            / resolution_days
                .len()
                .to_f64()
                .context("Failed to convert usize to f64")?,
    )
};

.len()
.to_f64()
.context("Failed to convert usize to f64")?,
)
};

Ok(IssueStat {
open_issue_count,
resolved_issue_count,
resolved_issue_size_distribution,
avg_resolution_days,
})
}
}
Expand All @@ -145,8 +188,7 @@ mod tests {
use jiff::Timestamp;

use crate::{
api::issue::{TODO_LIST_PROJECT_TITLE, TODO_LIST_STATUS_DONE},
api::TestSchema,
api::{TestSchema, TODO_LIST_PROJECT_TITLE, TODO_LIST_STATUS_DONE},
database::issue::{GitHubIssue, GitHubProjectV2Item, GitHubProjectV2ItemConnection},
outbound::issues::IssueState,
};
Expand Down Expand Up @@ -607,7 +649,7 @@ mod tests {
let query = r"
{
issueStat(filter: {}) {
resolvedIssueSizeDistribution {
resolvedIssueSizeDistribution {
size
count
}
Expand All @@ -624,4 +666,68 @@ mod tests {
])
);
}

#[tokio::test]
async fn avg_resolution_days() {
let schema = TestSchema::new();
let owner = "aicers";
let repo = "github-dashboard-server";
let mut resolved_issues = create_resolved_issues(1..=2);

// Issue 1: 10 days resolution, 2 pending days. Net: 8 days.
resolved_issues[0].created_at = parse("2025-01-01T00:00:00Z");
resolved_issues[0].closed_at = Some(parse("2025-01-11T00:00:00Z"));
resolved_issues[0].project_items.nodes[0].todo_pending_days = Some(2.0);

// Issue 2: 5 days resolution, 1 pending day. Net: 4 days.
resolved_issues[1].created_at = parse("2025-01-01T00:00:00Z");
resolved_issues[1].closed_at = Some(parse("2025-01-06T00:00:00Z"));
resolved_issues[1].project_items.nodes[0].todo_pending_days = Some(1.0);

schema
.db
.insert_issues(resolved_issues, owner, repo)
.unwrap();

let query = r"
{
issueStat(filter: {}) {
avgResolutionDays
}
}";
let data = schema.execute(query).await.data.into_json().unwrap();
// Average of 8 and 4 is 6.
assert_eq!(data["issueStat"]["avgResolutionDays"], 6.0);
}

#[tokio::test]
async fn avg_resolution_days_is_not_negative() {
let schema = TestSchema::new();
let owner = "aicers";
let repo = "github-dashboard-server";
let mut resolved_issues = create_resolved_issues(1..=1);

// Create an issue where the pending days (10 days) exceed the resolution days (5 days).
// In this case, the net resolution days should be 0, not negative.
// resolution_days (5.0) - pending_days (10.0) = -5.0
resolved_issues[0].created_at = parse("2025-01-01T00:00:00Z");
resolved_issues[0].closed_at = Some(parse("2025-01-06T00:00:00Z"));
resolved_issues[0].project_items.nodes[0].todo_pending_days = Some(10.0);

schema
.db
.insert_issues(resolved_issues, owner, repo)
.unwrap();

let query = r"
{
issueStat(filter: {}) {
avgResolutionDays
}
}";
let data = schema.execute(query).await.data.into_json().unwrap();

// The average resolution days should be 0.0, not negative.
assert_eq!(data["issueStat"]["avgResolutionDays"], 0.0);
}
}
Loading