Skip to content

Commit

Permalink
improved search (add search for validator names to main search)
Browse files Browse the repository at this point in the history
  • Loading branch information
pk910 committed Sep 4, 2023
1 parent 949dec1 commit e508bb1
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 5 deletions.
5 changes: 5 additions & 0 deletions dbtypes/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ type SearchAheadGraffitiResult []struct {
Graffiti string `db:"graffiti"`
Count uint64 `db:"count"`
}

type SearchAheadValidatorNameResult []struct {
Name string `db:"name"`
Count uint64 `db:"count"`
}
2 changes: 2 additions & 0 deletions handlers/pageData.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func InitPageData(w http.ResponseWriter, r *http.Request, active, path, title st
}

isMainnet := utils.Config.Chain.Config.ConfigName == "mainnet"
buildTime, _ := time.Parse("2006-01-02T15:04:05Z", utils.Buildtime)
data := &types.PageData{
Meta: &types.Meta{
Title: fullTitle,
Expand All @@ -39,6 +40,7 @@ func InitPageData(w http.ResponseWriter, r *http.Request, active, path, title st
Active: active,
Data: &types.Empty{},
Version: "git-" + utils.BuildVersion,
BuildTime: fmt.Sprintf("%v", buildTime.Unix()),
Year: time.Now().UTC().Year(),
ExplorerTitle: utils.Config.Frontend.SiteName,
ExplorerSubtitle: utils.Config.Frontend.SiteSubtitle,
Expand Down
50 changes: 49 additions & 1 deletion handlers/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,24 @@ func Search(w http.ResponseWriter, r *http.Request) {
}
}

var namesCount uint64
err = db.ReaderDb.Get(namesCount, db.EngineQuery(map[dbtypes.DBEngineType]string{
dbtypes.DBEnginePgsql: `
SELECT COUNT(*)
FROM validator_names
WHERE name ILIKE LOWER($1)
LIMIT 1`,
dbtypes.DBEngineSqlite: `
SELECT COUNT(*)
FROM validator_names
WHERE name LIKE LOWER($1)
LIMIT 1`,
}), "%"+searchQuery+"%")
if err == nil {
http.Redirect(w, r, "/slots/filtered?f&f.missing=1&f.orphaned=1&f.pname="+searchQuery, http.StatusMovedPermanently)
return
}

graffiti := &dbtypes.SearchGraffitiResult{}
err = db.ReaderDb.Get(graffiti, db.EngineQuery(map[dbtypes.DBEngineType]string{
dbtypes.DBEnginePgsql: `
Expand All @@ -85,7 +103,7 @@ func Search(w http.ResponseWriter, r *http.Request) {
LIMIT 1`,
}), "%"+searchQuery+"%")
if err == nil {
http.Redirect(w, r, "/filtered?f&f.graffiti="+searchQuery, http.StatusMovedPermanently)
http.Redirect(w, r, "/slots/filtered?f&f.missing=1&f.orphaned=1&f.graffiti="+searchQuery, http.StatusMovedPermanently)
return
}

Expand Down Expand Up @@ -342,6 +360,36 @@ func SearchAhead(w http.ResponseWriter, r *http.Request) {
}
result = model
}
case "valname":
names := &dbtypes.SearchAheadValidatorNameResult{}
err = db.ReaderDb.Select(names, db.EngineQuery(map[dbtypes.DBEngineType]string{
dbtypes.DBEnginePgsql: `
SELECT name, count(*) as count
FROM validator_names
LEFT JOIN slot_assignments ON validator_names."index" = slot_assignments.proposer
WHERE name ILIKE LOWER($1)
GROUP BY name
ORDER BY count desc
LIMIT 10`,
dbtypes.DBEngineSqlite: `
SELECT name, count(*) as count
FROM validator_names
LEFT JOIN slot_assignments ON validator_names."index" = slot_assignments.proposer
WHERE name LIKE LOWER($1)
GROUP BY name
ORDER BY count desc
LIMIT 10`,
}), "%"+search+"%")
if err == nil {
model := make([]models.SearchAheadValidatorNameResult, len(*names))
for i, entry := range *names {
model[i] = models.SearchAheadValidatorNameResult{
Name: utils.FormatGraffitiString(entry.Name),
Count: fmt.Sprintf("%v", entry.Count),
}
}
result = model
}

default:
http.Error(w, "Not found", 404)
Expand Down
33 changes: 31 additions & 2 deletions static/js/explorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@
maxPendingRequests: requestNum,
},
});
var bhValNames = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace,
identify: function (obj) {
return obj.name
},
remote: {
url: "/search/valname?q=",
prepare: prepareQueryFn,
maxPendingRequests: requestNum,
},
});


searchEl.typeahead(
Expand Down Expand Up @@ -170,6 +182,18 @@
},
},
},
{
limit: 5,
name: "name",
source: bhValNames,
display: "name",
templates: {
header: '<h3 class="h5">Slots (by vlidator name):</h3>',
suggestion: function (data) {
return `<div class="text-monospace" style="display:flex"><div class="text-truncate" style="flex:1 1 auto;">${data.name}</div><div style="max-width:fit-content;white-space:nowrap;">${data.count}</div></div>`
},
},
},
{
limit: 5,
name: "epoch",
Expand All @@ -188,7 +212,7 @@
source: bhGraffiti,
display: "graffiti",
templates: {
header: '<h3 class="h5">Block Graffitis:</h3>',
header: '<h3 class="h5">Blocks (by graffitis):</h3>',
suggestion: function (data) {
return `<div class="text-monospace" style="display:flex"><div class="text-truncate" style="flex:1 1 auto;">${data.graffiti}</div><div style="max-width:fit-content;white-space:nowrap;">${data.count}</div></div>`
},
Expand Down Expand Up @@ -221,7 +245,12 @@
// sug.graffiti is html-escaped to prevent xss, we need to unescape it
var el = document.createElement("textarea")
el.innerHTML = sug.graffiti
window.location = "/slots/filtered?f&f.graffiti=" + encodeURIComponent(el.value)
window.location = "/slots/filtered?f&f.orphaned=1&f.graffiti=" + encodeURIComponent(el.value)
} else if (sug.name !== undefined) {
// sug.name is html-escaped to prevent xss, we need to unescape it
var el = document.createElement("textarea")
el.innerHTML = sug.name
window.location = "/slots/filtered?f&f.missing=1&f.orphaned=1&f.pname=" + encodeURIComponent(el.value)
} else {
console.log("invalid typeahead-selection", sug)
}
Expand Down
4 changes: 2 additions & 2 deletions templates/_layout/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<link rel="preload" as="font" href="/webfonts/fa-solid-900.woff2" crossorigin />
<link rel="preload" as="font" href="/webfonts/fa-regular-400.woff2" crossorigin />
<link rel="preload" as="font" href="/webfonts/fa-brands-400.woff2" crossorigin />
<link id="app-style" rel="stylesheet" href="/css/layout.css" />
<link id="app-style" rel="stylesheet" href="/css/layout.css?{{ .BuildTime }}" />
{{ template "css" .Data }}

<script src="/js/jquery.min.js"></script>
Expand All @@ -54,7 +54,7 @@
</div>
<script src="/js/typeahead.min.js"></script>
<script src="/js/clipboard.min.js"></script>
<script src="/js/explorer.js"></script>
<script src="/js/explorer.js?{{ .BuildTime }}"></script>
{{ template "js" .Data }}
</body>
</html>
Expand Down
1 change: 1 addition & 0 deletions types/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type PageData struct {
Meta *Meta
Data interface{}
Version string
BuildTime string
Year int
ExplorerTitle string
ExplorerSubtitle string
Expand Down
6 changes: 6 additions & 0 deletions types/models/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ type SearchAheadGraffitiResult struct {
Graffiti string `json:"graffiti,omitempty"`
Count string `json:"count,omitempty"`
}

// SearchAheadValidatorNameResult is a struct to hold the search ahead blocks results with a given graffiti
type SearchAheadValidatorNameResult struct {
Name string `json:"name,omitempty"`
Count string `json:"count,omitempty"`
}

0 comments on commit e508bb1

Please sign in to comment.