Skip to content

Commit 7078a58

Browse files
authored
shards: populate RepoList.Stats.Repos (#605)
Previously this was only populated by the web package for serving a specific endpoint. We now populate it in all places we do aggregation. Test Plan: updated unit tests. Additionally loaded zoekt-webserver then added to the index a repository which required 2 shards to index. The repository count shown only increased by 1.
1 parent 1686b50 commit 7078a58

File tree

6 files changed

+18
-7
lines changed

6 files changed

+18
-7
lines changed

api.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,9 @@ type IndexMetadata struct {
684684
// Statistics of a (collection of) repositories.
685685
type RepoStats struct {
686686
// Repos is used for aggregrating the number of repositories.
687+
//
688+
// Note: This field is not populated on RepoListEntry.Stats (individual) but
689+
// only for RepoList.Stats (aggregate).
687690
Repos int
688691

689692
// Shards is the total number of search shards.

eval.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,10 @@ func (d *indexData) List(ctx context.Context, q query.Q, opts *ListOptions) (rl
731731

732732
}
733733

734+
// Only one of these fields is populated and in all cases the size of that
735+
// field is the number of Repos in this shard.
736+
l.Stats.Repos = len(l.Repos) + len(l.Minimal) + len(l.ReposMap)
737+
734738
return &l, nil
735739
}
736740

index_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,6 +1711,7 @@ func TestListRepos(t *testing.T) {
17111711
},
17121712
}},
17131713
Stats: RepoStats{
1714+
Repos: 1,
17141715
Documents: 4,
17151716
ContentBytes: 68,
17161717
Shards: 1,
@@ -1772,6 +1773,7 @@ func TestListRepos(t *testing.T) {
17721773
},
17731774
},
17741775
Stats: RepoStats{
1776+
Repos: 1,
17751777
Shards: 1,
17761778
Documents: 4,
17771779
IndexBytes: 412,

shards/shards.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,13 @@ func (ss *shardedSearcher) List(ctx context.Context, r query.Q, opts *zoekt.List
987987
agg.Repos = append(agg.Repos, r)
988988
}
989989

990+
// Only one of these fields is populated and in all cases the size of that
991+
// field is the number of Repos.
992+
//
993+
// Note: we don't just add individual Stats.Repos since a repository can
994+
// have multiple shards.
995+
agg.Stats.Repos = len(uniq) + len(agg.Minimal) + len(agg.ReposMap)
996+
990997
if isAll && len(agg.Repos) > 0 {
991998
reportListAllMetrics(agg.Repos)
992999
}

shards/shards_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ func TestShardedSearcher_List(t *testing.T) {
472472

473473
aggStats := stats
474474
aggStats.Add(&aggStats) // since both repos have the exact same stats, this works
475+
aggStats.Repos = 2 // Add doesn't populate Repos, this is done in Shards based on the response sizes.
475476

476477
for _, tc := range []struct {
477478
name string

web/server.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -349,13 +349,7 @@ func (s *Server) fetchStats(ctx context.Context) (*zoekt.RepoStats, error) {
349349
return nil, err
350350
}
351351

352-
stats = &zoekt.RepoStats{}
353-
names := map[string]struct{}{}
354-
for _, r := range repos.Repos {
355-
stats.Add(&r.Stats)
356-
names[r.Repository.Name] = struct{}{}
357-
}
358-
stats.Repos = len(names)
352+
stats = &repos.Stats
359353

360354
s.lastStatsMu.Lock()
361355
s.lastStatsTS = time.Now()

0 commit comments

Comments
 (0)