content,internal: fix imported by count in unit page header

Updates unit page count to use db.GetImportedBy for data. This will report
incorrect numbers when using proxy datasource.

Change-Id: Ie5b0004f3ef7a00436cb6cddfb030f263cdacbe8
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/259197
Trust: Jamal Carvalho <jamal@golang.org>
Run-TryBot: Jamal Carvalho <jamal@golang.org>
Reviewed-by: Julie Qiu <julie@golang.org>
diff --git a/content/static/html/helpers/_unit_fixed_header.tmpl b/content/static/html/helpers/_unit_fixed_header.tmpl
index 12c73c4..aa6da15 100644
--- a/content/static/html/helpers/_unit_fixed_header.tmpl
+++ b/content/static/html/helpers/_unit_fixed_header.tmpl
@@ -69,7 +69,7 @@
               <span class="UnitHeaderFixed-detailItem UnitHeaderFixed-detailItem--lg">
                 <img height="16px" width="16px" src="/static/img/pkg-icon-boxClosed_16x16.svg">
                 <a href="{{$.CanonicalURLPath}}?tab=importedby" tabindex="-1">
-                  {{len .Unit.Imports}} <span>Imported by</span>
+                  {{.ImportedByCount}} <span>Imported by</span>
                 </a>
               </span>
             {{end}}
diff --git a/content/static/html/helpers/_unit_header.tmpl b/content/static/html/helpers/_unit_header.tmpl
index 760f228..14bd702 100644
--- a/content/static/html/helpers/_unit_header.tmpl
+++ b/content/static/html/helpers/_unit_header.tmpl
@@ -81,7 +81,7 @@
             <span class="UnitHeader-detailItem">
               <img height="16px" width="16px" src="/static/img/pkg-icon-boxClosed_16x16.svg">
               <a href="{{$.CanonicalURLPath}}?tab=importedby">
-                {{len .Unit.Imports}} <span>Imported by</span>
+                {{.ImportedByCount}} <span>Imported by</span>
               </a>
             </span>
           {{end}}
diff --git a/internal/frontend/unit.go b/internal/frontend/unit.go
index 24ecf52..b5dbebf 100644
--- a/internal/frontend/unit.go
+++ b/internal/frontend/unit.go
@@ -7,6 +7,7 @@
 import (
 	"context"
 	"net/http"
+	"strconv"
 
 	"github.com/google/safehtml"
 	"golang.org/x/pkgsite/internal"
@@ -15,6 +16,7 @@
 	"golang.org/x/pkgsite/internal/godoc"
 	"golang.org/x/pkgsite/internal/log"
 	"golang.org/x/pkgsite/internal/middleware"
+	"golang.org/x/pkgsite/internal/postgres"
 	"golang.org/x/pkgsite/internal/stdlib"
 )
 
@@ -79,6 +81,11 @@
 	// versions, licenses, imports, and importedby tabs.
 	PackageDetails interface{}
 
+	// 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
+
 	DocBody       safehtml.HTML
 	DocOutline    safehtml.HTML
 	MobileOutline safehtml.HTML
@@ -142,6 +149,23 @@
 		return err
 	}
 
+	// importedByCount is not supported when using a datasource proxy.
+	importedByCount := "0"
+	db, ok := ds.(*postgres.DB)
+	if ok {
+		importedBy, err := db.GetImportedBy(ctx, unit.Path, unit.ModulePath, importedByLimit)
+		if err != nil {
+			return err
+		}
+		// If we reached the query limit, then we don't know the total
+		// and we'll indicate that with a '+'. For example, if the limit
+		// is 101 and we get 101 results, then we'll show '100+ Imported by'.
+		importedByCount = strconv.Itoa(len(importedBy))
+		if len(importedBy) == importedByLimit {
+			importedByCount = strconv.Itoa(len(importedBy)-1) + "+"
+		}
+	}
+
 	nestedModules, err := ds.GetNestedModules(ctx, unit.ModulePath)
 	if err != nil {
 		return err
@@ -233,6 +257,7 @@
 		DocBody:         docBody,
 		SourceFiles:     files,
 		MobileOutline:   mobileOutline,
+		ImportedByCount: importedByCount,
 	}
 
 	if tab != tabDetails {