Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
easyCZ committed Jun 9, 2022
1 parent 7677f71 commit 143d0ac
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions components/usage/pkg/controller/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,25 @@ type teamWithWorkspaces struct {
}

func (u *UsageReconciler) loadTeamsForWorkspaces(ctx context.Context, workspaces []workspaceWithInstances) ([]teamWithWorkspaces, error) {
// find owners of these workspaces
// find owner IDs of these workspaces
var ownerIDs []uuid.UUID
for _, workspace := range workspaces {
ownerIDs = append(ownerIDs, workspace.Workspace.OwnerID)
}

// Retrieve memberships. This gives a link between an Owner and a Team they belong to.
memberships, err := db.ListTeamMembershipsForUserIDs(ctx, u.conn, ownerIDs)
if err != nil {
return nil, fmt.Errorf("failed to list team memberships: %w", err)
}

membershipsByUserID := map[uuid.UUID]db.TeamMembership{}
for _, membership := range memberships {
// User can belong to multiple teams. For now, we're choosing the membership at random.
membershipsByUserID[membership.UserID] = membership
}

// Convert workspaces into a lookup so that we can index into them by Owner ID, needed for joining Teams with Workspaces
workspacesByOwnerID := map[uuid.UUID][]workspaceWithInstances{}
for _, workspace := range workspaces {
items, ok := workspacesByOwnerID[workspace.Workspace.OwnerID]
Expand All @@ -127,9 +130,11 @@ func (u *UsageReconciler) loadTeamsForWorkspaces(ctx context.Context, workspaces
items = append(items, workspace)
}

// Finally, join the datasets
// Because we iterate over memberships, and not workspaces, we're in effect ignoring Workspaces which are not in a team.
// This is intended as we focus on Team usage for now.
var teamsWithWorkspaces []teamWithWorkspaces
for userID, membership := range membershipsByUserID {

teamsWithWorkspaces = append(teamsWithWorkspaces, teamWithWorkspaces{
TeamID: membership.TeamID,
Workspaces: workspacesByOwnerID[userID],
Expand Down

0 comments on commit 143d0ac

Please sign in to comment.