-
Notifications
You must be signed in to change notification settings - Fork 300
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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>( | ||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.