internal/frontend: display exact importedby count

Since the same logic is now used for importedby count across the site,
there isn't any reason to display only an estimated number. The exact
count is now displayed across the site.

Fixes golang/go#39138

Change-Id: I994233a2f4ffa202e876eac730881f1d4f88db5f
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/288910
Trust: Julie Qiu <julie@golang.org>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
diff --git a/internal/frontend/imports.go b/internal/frontend/imports.go
index 286c0eb..b268de0 100644
--- a/internal/frontend/imports.go
+++ b/internal/frontend/imports.go
@@ -7,6 +7,7 @@
 import (
 	"context"
 	"fmt"
+	"strconv"
 	"strings"
 
 	"golang.org/x/pkgsite/internal"
@@ -82,9 +83,6 @@
 }
 
 var (
-	// mainPageImportedByLimit determines whether the main (unit) page displays
-	// an exact or an approximate number of importers.
-	mainPageImportedByLimit = 21
 	// tabImportedByLimit is the maximum number of importers displayed on the imported
 	// by page.
 	tabImportedByLimit = 20001
@@ -109,7 +107,7 @@
 	}
 	sections := Sections(importedBy, nextPrefixAccount)
 
-	display := formatImportedByCount(numImportedBy)
+	display := strconv.Itoa(numImportedBy)
 	if numImportedBy >= tabImportedByLimit {
 		display += fmt.Sprintf(" (displaying %d packages)", tabImportedByLimit-1)
 	}
diff --git a/internal/frontend/imports_test.go b/internal/frontend/imports_test.go
index dccf731..97c0cbd 100644
--- a/internal/frontend/imports_test.go
+++ b/internal/frontend/imports_test.go
@@ -167,7 +167,7 @@
 			pkg := sample.UnitForPackage(sample.PackagePath, sample.ModulePath, sample.VersionString, sample.PackageName, true)
 			wantDetails := &ImportedByDetails{
 				ModulePath:           sample.ModulePath,
-				NumImportedByDisplay: fmt.Sprintf("%s (displaying 20000 packages)", formatImportedByCount(count)),
+				NumImportedByDisplay: fmt.Sprintf("%d (displaying 20000 packages)", count),
 				Total:                count,
 			}
 			checkFetchImportedByDetails(ctx, t, pkg, wantDetails)
diff --git a/internal/frontend/search.go b/internal/frontend/search.go
index 46eea01..00f9b7f 100644
--- a/internal/frontend/search.go
+++ b/internal/frontend/search.go
@@ -40,7 +40,7 @@
 	DisplayVersion string
 	Licenses       []string
 	CommitTime     string
-	NumImportedBy  string
+	NumImportedBy  int
 	Approximate    bool
 }
 
@@ -63,7 +63,7 @@
 			DisplayVersion: displayVersion(r.Version, r.ModulePath),
 			Licenses:       r.Licenses,
 			CommitTime:     elapsedTime(r.CommitTime),
-			NumImportedBy:  formatImportedByCount(int(r.NumImportedBy)),
+			NumImportedBy:  int(r.NumImportedBy),
 		})
 	}
 
diff --git a/internal/frontend/unit.go b/internal/frontend/unit.go
index 297a1ba..3fbfb56 100644
--- a/internal/frontend/unit.go
+++ b/internal/frontend/unit.go
@@ -9,6 +9,7 @@
 	"errors"
 	"fmt"
 	"net/http"
+	"strconv"
 	"strings"
 	"time"
 
@@ -146,7 +147,7 @@
 	page.Details = d
 	main, ok := d.(*MainDetails)
 	if ok {
-		page.MetaDescription = metaDescription(main.ImportedByCount)
+		page.MetaDescription = metaDescription(strconv.Itoa(main.ImportedByCount))
 	}
 	s.servePage(ctx, w, tabSettings.TemplateName, page)
 	return nil
diff --git a/internal/frontend/unit_main.go b/internal/frontend/unit_main.go
index bafb16d..ea24a83 100644
--- a/internal/frontend/unit_main.go
+++ b/internal/frontend/unit_main.go
@@ -7,10 +7,7 @@
 import (
 	"context"
 	"errors"
-	"fmt"
-	"math"
 	"sort"
-	"strconv"
 	"strings"
 
 	"github.com/google/safehtml"
@@ -71,7 +68,7 @@
 	// ImportedByCount is the number of packages that import this path.
 	// When the count is > limit it will read as 'limit+'. This field
 	// is not supported when using a datasource proxy.
-	ImportedByCount string
+	ImportedByCount int
 
 	DocBody       safehtml.HTML
 	DocOutline    safehtml.HTML
@@ -235,7 +232,7 @@
 		SourceURL:         um.SourceInfo.DirectoryURL(internal.Suffix(um.Path, um.ModulePath)),
 		MobileOutline:     docParts.MobileOutline,
 		NumImports:        unit.NumImports,
-		ImportedByCount:   formatImportedByCount(unit.NumImportedBy),
+		ImportedByCount:   unit.NumImportedBy,
 		IsPackage:         unit.IsPackage(),
 		ModFileURL:        um.SourceInfo.ModuleURL() + "/go.mod",
 		IsTaggedVersion:   isTaggedVersion,
@@ -349,31 +346,3 @@
 	log.Errorf(ctx, "unit %s (%s@%s) missing documentation source", u.Path, u.ModulePath, u.Version)
 	return &dochtml.Parts{Body: template.MustParseAndExecuteToHTML(missingDocReplacement)}, nil
 }
-
-// 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 formatImportedByCount(numImportedBy int) string {
-	if numImportedBy < mainPageImportedByLimit {
-		// Exact number is less than the limit, so just return that.
-		return strconv.Itoa(numImportedBy)
-	}
-
-	// Exact number is greater than the limit, so fetch an approximate value
-	// from search_documents.num_imported_by. This number might be different
-	// than the result of GetImportedBy because alternative modules and internal
-	// packages are excluded.
-	// Treat the result as approximate.
-	return fmt.Sprintf("%d+", approximateLowerBound(numImportedBy))
-}
-
-// approximateLowerBound rounds n down to a multiple of a power of 10.
-// See the test for examples.
-func approximateLowerBound(n int) int {
-	if n == 0 {
-		return 0
-	}
-	f := float64(n)
-	powerOf10 := math.Pow(10, math.Floor(math.Log10(f)))
-	return int(powerOf10 * math.Floor(f/powerOf10))
-}
diff --git a/internal/frontend/unit_main_test.go b/internal/frontend/unit_main_test.go
index 6605b46..534b1c4 100644
--- a/internal/frontend/unit_main_test.go
+++ b/internal/frontend/unit_main_test.go
@@ -144,28 +144,26 @@
 		}
 	}
 
-	mainPageImportedByLimit = 2
-	tabImportedByLimit = 3
 	for _, test := range []struct {
 		mod  *internal.Module
-		want string
+		want int
 	}{
 		{
 			mod:  mod3,
-			want: "0",
+			want: 0,
 		},
 		{
 			mod:  mod2,
-			want: "1",
+			want: 1,
 		},
 		{
 			mod:  mod1,
-			want: "2+",
+			want: 2,
 		},
 	} {
 		pkg := test.mod.Packages()[0]
 		t.Run(test.mod.ModulePath, func(t *testing.T) {
-			got := formatImportedByCount(pkg.NumImportedBy)
+			got := pkg.NumImportedBy
 			if diff := cmp.Diff(test.want, got); diff != "" {
 				t.Errorf("getImportedByCount(ctx, db, %q) mismatch (-want +got):\n%s", pkg.Path, diff)
 			}
@@ -173,27 +171,6 @@
 	}
 }
 
-func TestApproximateLowerBound(t *testing.T) {
-	for _, test := range []struct {
-		in, want int
-	}{
-		{0, 0},
-		{1, 1},
-		{5, 5},
-		{10, 10},
-		{11, 10},
-		{23, 20},
-		{57, 50},
-		{124, 100},
-		{2593, 2000},
-	} {
-		got := approximateLowerBound(test.in)
-		if got != test.want {
-			t.Errorf("approximateLowerBound(%d) = %d, want %d", test.in, got, test.want)
-		}
-	}
-}
-
 func TestUnitDirectories(t *testing.T) {
 	subdirectories := []*Subdirectory{
 		{Suffix: "accessapproval"},