internal/frontend: match importedby counts on main and search pages
At the moment, the imported by count on the main unit page and search
page can differ on small values, where an exact count is shown.
Since we fetch search_documents.num_imported_by on every GetUnit
request, use that value consistently across the site.
For golang/go#39138
Change-Id: I61b60029626dc8ab6353886f38a6f158841147bb
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/288767
Trust: Julie Qiu <julie@golang.org>
Run-TryBot: Julie Qiu <julie@golang.org>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
diff --git a/internal/frontend/search.go b/internal/frontend/search.go
index 940166f..46eea01 100644
--- a/internal/frontend/search.go
+++ b/internal/frontend/search.go
@@ -40,7 +40,7 @@
DisplayVersion string
Licenses []string
CommitTime string
- NumImportedBy uint64
+ NumImportedBy string
Approximate bool
}
@@ -63,7 +63,7 @@
DisplayVersion: displayVersion(r.Version, r.ModulePath),
Licenses: r.Licenses,
CommitTime: elapsedTime(r.CommitTime),
- NumImportedBy: r.NumImportedBy,
+ NumImportedBy: formatImportedByCount(int(r.NumImportedBy)),
})
}
diff --git a/internal/frontend/search_test.go b/internal/frontend/search_test.go
index 108075a..005e358 100644
--- a/internal/frontend/search_test.go
+++ b/internal/frontend/search_test.go
@@ -120,7 +120,6 @@
DisplayVersion: moduleBar.Version,
Licenses: []string{"MIT"},
CommitTime: elapsedTime(moduleBar.CommitTime),
- NumImportedBy: 0,
},
},
},
@@ -147,7 +146,6 @@
DisplayVersion: moduleFoo.Version,
Licenses: []string{"MIT"},
CommitTime: elapsedTime(moduleFoo.CommitTime),
- NumImportedBy: 0,
},
},
},
@@ -161,6 +159,7 @@
opts := cmp.Options{
cmp.AllowUnexported(SearchPage{}, pagination{}),
+ cmpopts.IgnoreFields(SearchResult{}, "NumImportedBy"),
cmpopts.IgnoreFields(licenses.Metadata{}, "FilePath"),
cmpopts.IgnoreFields(pagination{}, "Approximate"),
cmpopts.IgnoreFields(basePage{}, "MetaDescription"),
diff --git a/internal/frontend/unit_main.go b/internal/frontend/unit_main.go
index f78cd04..dc72eac 100644
--- a/internal/frontend/unit_main.go
+++ b/internal/frontend/unit_main.go
@@ -22,7 +22,6 @@
"golang.org/x/pkgsite/internal/godoc/dochtml"
"golang.org/x/pkgsite/internal/log"
"golang.org/x/pkgsite/internal/middleware"
- "golang.org/x/pkgsite/internal/postgres"
"golang.org/x/pkgsite/internal/version"
)
@@ -160,10 +159,6 @@
if err != nil {
return nil, err
}
- importedByCount, err := getImportedByCount(ctx, ds, unit)
- if err != nil {
- return nil, err
- }
var (
docParts = &dochtml.Parts{}
docLinks, modLinks []link
@@ -240,7 +235,7 @@
SourceURL: um.SourceInfo.DirectoryURL(internal.Suffix(um.Path, um.ModulePath)),
MobileOutline: docParts.MobileOutline,
NumImports: unit.NumImports,
- ImportedByCount: importedByCount,
+ ImportedByCount: formatImportedByCount(unit.NumImportedBy),
IsPackage: unit.IsPackage(),
ModFileURL: um.SourceInfo.ModuleURL() + "/go.mod",
IsTaggedVersion: isTaggedVersion,
@@ -355,28 +350,13 @@
return &dochtml.Parts{Body: template.MustParseAndExecuteToHTML(missingDocReplacement)}, nil
}
-// getImportedByCount fetches the imported by count for the unit and returns a
+// formatImportedByCount fetches the imported by count for the unit and returns a
// string to be displayed. If the datasource does not support imported by, it
// will return N/A.
-func getImportedByCount(ctx context.Context, ds internal.DataSource, unit *internal.Unit) (_ string, err error) {
- defer derrors.Wrap(&err, "getImportedByCount(%q, %q, %q)", unit.Path, unit.ModulePath, unit.Version)
- defer middleware.ElapsedStat(ctx, "getImportedByCount")()
-
- db, ok := ds.(*postgres.DB)
- if !ok {
- return "N/A", nil
- }
-
- // Get an exact number for a small limit, to determine whether we should
- // fetch data from search_documents and display an approximate count, or
- // just use the exact count.
- importedBy, err := db.GetImportedBy(ctx, unit.Path, unit.ModulePath, mainPageImportedByLimit)
- if err != nil {
- return "", err
- }
- if len(importedBy) < mainPageImportedByLimit {
+func formatImportedByCount(numImportedBy int) string {
+ if numImportedBy < mainPageImportedByLimit {
// Exact number is less than the limit, so just return that.
- return strconv.Itoa(len(importedBy)), nil
+ return strconv.Itoa(numImportedBy)
}
// Exact number is greater than the limit, so fetch an approximate value
@@ -384,7 +364,7 @@
// than the result of GetImportedBy because alternative modules and internal
// packages are excluded.
// Treat the result as approximate.
- return fmt.Sprintf("%d+", approximateLowerBound(unit.NumImportedBy)), nil
+ return fmt.Sprintf("%d+", approximateLowerBound(numImportedBy))
}
// approximateLowerBound rounds n down to a multiple of a power of 10.
diff --git a/internal/frontend/unit_main_test.go b/internal/frontend/unit_main_test.go
index e95669f..6605b46 100644
--- a/internal/frontend/unit_main_test.go
+++ b/internal/frontend/unit_main_test.go
@@ -165,11 +165,7 @@
} {
pkg := test.mod.Packages()[0]
t.Run(test.mod.ModulePath, func(t *testing.T) {
- got, err := getImportedByCount(ctx, testDB, pkg)
- if err != nil {
- t.Fatalf("getImportedByCount(ctx, db, %q) = %v err = %v, want %v",
- pkg.Path, got, err, test.want)
- }
+ got := formatImportedByCount(pkg.NumImportedBy)
if diff := cmp.Diff(test.want, got); diff != "" {
t.Errorf("getImportedByCount(ctx, db, %q) mismatch (-want +got):\n%s", pkg.Path, diff)
}