Skip to content

Commit

Permalink
added sqlite queries for search handler
Browse files Browse the repository at this point in the history
  • Loading branch information
pk910 committed Aug 14, 2023
1 parent 80fc873 commit 17116b4
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 35 deletions.
12 changes: 4 additions & 8 deletions cmd/explorer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ func main() {
}

startFrontend()
} else {
utils.WaitForCtrlC()

logger.Println("exiting...")
}

utils.WaitForCtrlC()
logger.Println("exiting...")
db.MustCloseDB()
}

func startFrontend() {
Expand Down Expand Up @@ -121,8 +121,4 @@ func startFrontend() {
logger.WithError(err).Fatal("Error serving frontend")
}
}()

utils.WaitForCtrlC()

logger.Println("exiting...")
}
11 changes: 11 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ func MustInitDB() {
}
}

func MustCloseDB() {
err := WriterDb.Close()
if err != nil {
logger.Errorf("Error closing writer db connection: %v", err)
}
err = ReaderDb.Close()
if err != nil {
logger.Errorf("Error closing reader db connection: %v", err)
}
}

func ApplyEmbeddedDbSchema(version int64) error {
var engineDialect string
var schemaDirectory string
Expand Down
64 changes: 44 additions & 20 deletions handlers/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/sirupsen/logrus"

"github.com/pk910/light-beaconchain-explorer/db"
"github.com/pk910/light-beaconchain-explorer/dbtypes"
"github.com/pk910/light-beaconchain-explorer/services"
"github.com/pk910/light-beaconchain-explorer/templates"
"github.com/pk910/light-beaconchain-explorer/types/models"
Expand All @@ -33,11 +34,18 @@ func Search(w http.ResponseWriter, r *http.Request) {
_, err := strconv.Atoi(searchQuery)
if err == nil {
blockResult := &models.SearchBlockResult{}
err = db.ReaderDb.Select(blockResult, `
SELECT slot, ENCODE(root, 'hex') AS root, orphaned
FROM blocks
WHERE slot = $1
LIMIT 1`, searchQuery)
err = db.ReaderDb.Select(blockResult, db.EngineQuery(map[dbtypes.DBEngineType]string{
dbtypes.DBEnginePgsql: `
SELECT slot, ENCODE(root, 'hex') AS root, orphaned
FROM blocks
WHERE slot = $1
LIMIT 1`,
dbtypes.DBEngineSqlite: `
SELECT slot, root, orphaned
FROM blocks
WHERE slot = $1
LIMIT 1`,
}), searchQuery)
if err == nil {
if blockResult.Orphaned {
http.Redirect(w, r, fmt.Sprintf("/slot/0x%x", blockResult.Root), http.StatusMovedPermanently)
Expand Down Expand Up @@ -71,11 +79,18 @@ func Search(w http.ResponseWriter, r *http.Request) {
}

graffiti := &models.SearchGraffitiResult{}
err = db.ReaderDb.Get(graffiti, `
SELECT graffiti
FROM blocks
WHERE graffiti_text ILIKE LOWER($1)
LIMIT 1`, "%"+searchQuery+"%")
err = db.ReaderDb.Get(graffiti, db.EngineQuery(map[dbtypes.DBEngineType]string{
dbtypes.DBEnginePgsql: `
SELECT graffiti
FROM blocks
WHERE graffiti_text ILIKE LOWER($1)
LIMIT 1`,
dbtypes.DBEngineSqlite: `
SELECT graffiti
FROM blocks
WHERE graffiti_text LIKE LOWER($1)
LIMIT 1`,
}), "%"+searchQuery+"%")
if err == nil {
http.Redirect(w, r, "/slots?q="+searchQuery, http.StatusMovedPermanently)
return
Expand Down Expand Up @@ -128,13 +143,13 @@ func SearchAhead(w http.ResponseWriter, r *http.Request) {
result = &models.SearchAheadSlotsResult{
{
Slot: fmt.Sprintf("%v", uint64(cachedBlock.Header.Data.Header.Message.Slot)),
Root: fmt.Sprintf("%x", []byte(cachedBlock.Header.Data.Root)),
Root: cachedBlock.Header.Data.Root,
Orphaned: cachedBlock.Orphaned,
},
}
} else {
err = db.ReaderDb.Select(result, `
SELECT slot, ENCODE(root, 'hex') AS root, orphaned
SELECT slot, root, orphaned
FROM blocks
WHERE root = $1 OR
state_root = $1
Expand All @@ -147,21 +162,30 @@ func SearchAhead(w http.ResponseWriter, r *http.Request) {
}
} else if _, convertErr := strconv.ParseInt(search, 10, 32); convertErr == nil {
err = db.ReaderDb.Select(result, `
SELECT slot, ENCODE(root, 'hex') AS root, orphaned
SELECT slot, root, orphaned
FROM blocks
WHERE slot = $1
ORDER BY slot LIMIT 10`, search)
}
}
case "graffiti":
graffiti := &models.SearchAheadGraffitiResult{}
err = db.ReaderDb.Select(graffiti, `
SELECT graffiti, count(*)
FROM blocks
WHERE graffiti_text ILIKE LOWER($1)
GROUP BY graffiti
ORDER BY count desc
LIMIT 10`, "%"+search+"%")
err = db.ReaderDb.Select(graffiti, db.EngineQuery(map[dbtypes.DBEngineType]string{
dbtypes.DBEnginePgsql: `
SELECT graffiti, count(*) as count
FROM blocks
WHERE graffiti_text ILIKE LOWER($1)
GROUP BY graffiti
ORDER BY count desc
LIMIT 10`,
dbtypes.DBEngineSqlite: `
SELECT graffiti, count(*) as count
FROM blocks
WHERE graffiti_text LIKE LOWER($1)
GROUP BY graffiti
ORDER BY count desc
LIMIT 10`,
}), "%"+search+"%")
if err == nil {
for i := range *graffiti {
(*graffiti)[i].Graffiti = utils.FormatGraffitiString((*graffiti)[i].Graffiti)
Expand Down
2 changes: 1 addition & 1 deletion static/js/explorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
if (data.orphaned) {
status = `<span class="search-cell"><span class="badge rounded-pill text-bg-info">Orphaned</span></span>`;
}
return `<div class="text-monospace"><div class="search-table"><span class="search-cell">${data.slot}:</span><span class="search-cell search-truncate">0x${data.root}</span>${status}</div></div>`;
return `<div class="text-monospace"><div class="search-table"><span class="search-cell">${data.slot}:</span><span class="search-cell search-truncate">${data.root}</span>${status}</div></div>`;
},
},
},
Expand Down
14 changes: 8 additions & 6 deletions types/models/search.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package models

import "github.com/pk910/light-beaconchain-explorer/rpctypes"

// SearchBlockResult is a struct to hold the search block result with a given graffiti
type SearchBlockResult struct {
Slot uint64 `json:"slot,omitempty"`
Root []byte `json:"root,omitempty"`
Orphaned bool `json:"orphaned,omitempty"`
Slot uint64 `json:"slot,omitempty"`
Root rpctypes.BytesHexStr `json:"root,omitempty"`
Orphaned bool `json:"orphaned,omitempty"`
}

// SearchGraffitiResult is a struct to hold the search block result with a given graffiti
Expand All @@ -19,9 +21,9 @@ type SearchAheadEpochsResult []struct {

// SearchAheadSlotsResult is a struct to hold the search ahead slots results
type SearchAheadSlotsResult []struct {
Slot string `json:"slot,omitempty"`
Root string `json:"root,omitempty"`
Orphaned bool `json:"orphaned,omitempty"`
Slot string `json:"slot,omitempty"`
Root rpctypes.BytesHexStr `json:"root,omitempty"`
Orphaned bool `json:"orphaned,omitempty"`
}

// SearchAheadGraffitiResult is a struct to hold the search ahead blocks results with a given graffiti
Expand Down

0 comments on commit 17116b4

Please sign in to comment.