Skip to content

Commit

Permalink
set zoekt.latest_commit_date
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanhengl committed Sep 23, 2024
1 parent 5b7cac9 commit 0d92ed3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
28 changes: 22 additions & 6 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,13 +635,29 @@ func (r *Repository) UnmarshalJSON(data []byte) error {
r.ID = uint32(id)
}

// Sourcegraph indexserver doesn't set repo.Rank, so we set it here based on the
// latest commit date. Setting it on read instead of during indexing allows us
// to avoid a complete reindex.
if r.Rank == 0 {
// We use the number of months since 1970 as a simple measure of repo freshness
// It has the nice property of being stable across re-indexes and restarts.
// Sourcegraph indexserver doesn't set repo.Rank, so we set it here. Setting it
// on read instead of during indexing allows us to avoid a complete reindex.
//
// Prefer "latest_commit_date" over "priority" for ranking. We keep priority for
// backwards compatibility.
if _, ok := repo.RawConfig["latest_commit_date"]; ok {
// We use the number of months since 1970 as a simple measure of repo freshness.
// It is monotonically increasing and stable across re-indexes and restarts.
r.Rank = monthsSince1970(repo.LatestCommitDate)
} else if v, ok := repo.RawConfig["priority"]; ok {
r.priority, err = strconv.ParseFloat(v, 64)
if err != nil {
r.priority = 0
}

// Sourcegraph indexserver doesn't set repo.Rank, so we set it here
// based on priority. Setting it on read instead of during indexing
// allows us to avoid a complete reindex.
if r.Rank == 0 && r.priority > 0 {
// Normalize the repo score within [0, 1), with the midpoint at 5,000. This means popular
// repos (roughly ones with over 5,000 stars) see diminishing returns from more stars.
r.Rank = uint16(r.priority / (5000.0 + r.priority) * maxUInt16)
}
}

return nil
Expand Down
11 changes: 6 additions & 5 deletions cmd/zoekt-sourcegraph-indexserver/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,12 @@ func (o *indexArgs) BuildOptions() *build.Options {
Name: o.Name,
Branches: o.Branches,
RawConfig: map[string]string{
"repoid": strconv.Itoa(int(o.IndexOptions.RepoID)),
"priority": strconv.FormatFloat(o.Priority, 'g', -1, 64),
"public": marshalBool(o.Public),
"fork": marshalBool(o.Fork),
"archived": marshalBool(o.Archived),
"repoid": strconv.Itoa(int(o.IndexOptions.RepoID)),
"priority": strconv.FormatFloat(o.Priority, 'g', -1, 64),
"public": marshalBool(o.Public),
"fork": marshalBool(o.Fork),
"archived": marshalBool(o.Archived),
"latest_commit_date": "1",
},
},
IndexDir: o.IndexDir,
Expand Down
9 changes: 7 additions & 2 deletions cmd/zoekt-sourcegraph-indexserver/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import (
"time"

"github.com/sourcegraph/log/logtest"
proto "github.com/sourcegraph/zoekt/cmd/zoekt-sourcegraph-indexserver/protos/sourcegraph/zoekt/configuration/v1"
"github.com/sourcegraph/zoekt/ctags"
"google.golang.org/grpc"
"google.golang.org/protobuf/testing/protocmp"
"google.golang.org/protobuf/types/known/timestamppb"

proto "github.com/sourcegraph/zoekt/cmd/zoekt-sourcegraph-indexserver/protos/sourcegraph/zoekt/configuration/v1"
"github.com/sourcegraph/zoekt/ctags"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"

Expand Down Expand Up @@ -493,6 +494,7 @@ func TestIndex(t *testing.T) {
"git -C $TMPDIR/test%2Frepo.git update-ref HEAD deadbeef",
"git -C $TMPDIR/test%2Frepo.git config zoekt.archived 0",
"git -C $TMPDIR/test%2Frepo.git config zoekt.fork 0",
"git -C $TMPDIR/test%2Frepo.git config zoekt.latest_commit_date 1",
"git -C $TMPDIR/test%2Frepo.git config zoekt.name test/repo",
"git -C $TMPDIR/test%2Frepo.git config zoekt.priority 0",
"git -C $TMPDIR/test%2Frepo.git config zoekt.public 0",
Expand All @@ -515,6 +517,7 @@ func TestIndex(t *testing.T) {
"git -C $TMPDIR/test%2Frepo.git update-ref HEAD deadbeef",
"git -C $TMPDIR/test%2Frepo.git config zoekt.archived 0",
"git -C $TMPDIR/test%2Frepo.git config zoekt.fork 0",
"git -C $TMPDIR/test%2Frepo.git config zoekt.latest_commit_date 1",
"git -C $TMPDIR/test%2Frepo.git config zoekt.name test/repo",
"git -C $TMPDIR/test%2Frepo.git config zoekt.priority 0",
"git -C $TMPDIR/test%2Frepo.git config zoekt.public 0",
Expand Down Expand Up @@ -546,6 +549,7 @@ func TestIndex(t *testing.T) {
"git -C $TMPDIR/test%2Frepo.git update-ref refs/heads/dev feebdaed",
"git -C $TMPDIR/test%2Frepo.git config zoekt.archived 0",
"git -C $TMPDIR/test%2Frepo.git config zoekt.fork 0",
"git -C $TMPDIR/test%2Frepo.git config zoekt.latest_commit_date 1",
"git -C $TMPDIR/test%2Frepo.git config zoekt.name test/repo",
"git -C $TMPDIR/test%2Frepo.git config zoekt.priority 0",
"git -C $TMPDIR/test%2Frepo.git config zoekt.public 0",
Expand Down Expand Up @@ -593,6 +597,7 @@ func TestIndex(t *testing.T) {
"git -C $TMPDIR/test%2Frepo.git update-ref refs/heads/release 12345678",
"git -C $TMPDIR/test%2Frepo.git config zoekt.archived 0",
"git -C $TMPDIR/test%2Frepo.git config zoekt.fork 0",
"git -C $TMPDIR/test%2Frepo.git config zoekt.latest_commit_date 1",
"git -C $TMPDIR/test%2Frepo.git config zoekt.name test/repo",
"git -C $TMPDIR/test%2Frepo.git config zoekt.priority 0",
"git -C $TMPDIR/test%2Frepo.git config zoekt.public 0",
Expand Down

0 comments on commit 0d92ed3

Please sign in to comment.