Skip to content

Support subteams that have subteams #1857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2023
Merged
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
64 changes: 59 additions & 5 deletions src/teams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use handlebars::{Context, Handlebars, Helper, HelperResult, Output, RenderContex
use percent_encoding::{utf8_percent_encode, AsciiSet, NON_ALPHANUMERIC};
use rust_team_data::v1::{Team, TeamKind, Teams, BASE_URL};
use std::cmp::Reverse;
use std::collections::HashMap;
use std::error::Error;
use std::fmt;
use std::time::Instant;
Expand Down Expand Up @@ -101,25 +102,78 @@ impl Data {
return Err(TeamNotFound.into());
}

// Then find all the subteams and wgs
let mut subteams = Vec::new();
// Then find all the subteams, working groups and project groups.
let mut raw_subteams = Vec::new();
let mut wgs = Vec::new();
let mut project_groups = Vec::new();

let superteams: HashMap<_, _> = self
.teams
.iter()
.filter(|team| matches!(team.kind, TeamKind::Team))
.filter_map(|team| Some((team.name.clone(), team.subteam_of.clone()?)))
.collect();

self.teams
.into_iter()
.filter(|team| team.website_data.is_some())
.filter(|team| team.subteam_of.as_ref() == Some(&main_team.name))
.filter(|team| {
// For teams find not only direct subteams but also transitive ones.
if let TeamKind::Team = team.kind {
let mut team = &team.name;

// The graph of teams is guaranteed to be acyclic by the CI script of
// the team repository. Therefore this loop has to terminate eventually.
while let Some(superteam) = superteams.get(team) {
if superteam == &main_team.name {
return true;
}

team = superteam;
}

return false;
}

team.subteam_of.as_ref() == Some(&main_team.name)
})
.for_each(|team| match team.kind {
TeamKind::Team => subteams.push(team),
TeamKind::Team => raw_subteams.push(team),
TeamKind::WorkingGroup => wgs.push(team),
TeamKind::ProjectGroup => project_groups.push(team),
_ => {}
});

subteams.sort_by_key(|team| Reverse(team.website_data.as_ref().unwrap().weight));
raw_subteams.sort_by_key(|team| Reverse(team.website_data.as_ref().unwrap().weight));
wgs.sort_by_key(|team| Reverse(team.website_data.as_ref().unwrap().weight));
project_groups.sort_by_key(|team| Reverse(team.website_data.as_ref().unwrap().weight));

// Lay out subteams according to their hierarchy.
// Superteams come first and subteams come after, recursively.
// The ordering of sibling teams is unaffected, they remain sorted first by weight, then
// alphabetically if we assume that the Team API serves the teams alphabetically.
let mut subteams = Vec::with_capacity(raw_subteams.len());
lay_out_subteams_hierarchically(&mut subteams, None, &main_team.name, &raw_subteams);

fn lay_out_subteams_hierarchically<'a>(
Copy link
Member Author

@fmease fmease Aug 23, 2023

Choose a reason for hiding this comment

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

If you're wondering why we need to re-lay out the subteams at all after having fetched all direct and transitive subteams, it's because raw_subteams is not necessarily ordered hierarchically.

This might sound obvious but in the case of rustdoc and rustdoc-contributors, the original ordering is actually correct (coincidentally) due to the naming of the two teams (them being sorted alphabetically implies being sorted hierarchically as they share the same prefix and the latter is longer).

However, in general this is not the case (the names might not necessarily reflect the hierarchy). Especially if we add weights to the equation: E.g., we don't want to place subsubteams with weight 1000 at the top of the page above everything else (including their parent team!). The weight should be relative to the level / rank / tier. lay_out_subteams_hierarchically guarantees that.

result: &mut Vec<Team>,
team: Option<&Team>,
main_team: &str,
subteams: &[Team],
) {
let name = team.map(|team| team.name.as_str()).or(Some(main_team));

if let Some(team) = team {
result.push(team.clone());
}

for subteam in subteams {
Copy link
Member Author

Choose a reason for hiding this comment

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

Not super happy about this loop inside this recursive function. Complexity could be better but input size should be relatively low. Let me know if there's a nice way to improve the algorithmic complexity.

if subteam.subteam_of.as_deref() == name {
lay_out_subteams_hierarchically(result, Some(subteam), main_team, subteams);
}
}
}

Ok(PageData {
team: main_team,
zulip_domain: crate::ZULIP_DOMAIN,
Expand Down