Compare commits

..

No commits in common. "46c209d2b920ffc6995e18b19940c6d1b5fec286" and "37798bd3fa17de4e1609b9823caac24b4ed146f9" have entirely different histories.

4 changed files with 31 additions and 29 deletions

View file

@ -1 +1 @@
v0.0.6 v0.0.5

View file

@ -1,6 +1,6 @@
{ {
"name": "xmr-nodes-frontend", "name": "xmr-nodes-frontend",
"version": "v0.0.6", "version": "v0.0.5",
"private": true, "private": true,
"scripts": { "scripts": {
"dev": "VITE_API_URL=http://127.0.0.1:18901 vite dev --host 127.0.0.1", "dev": "VITE_API_URL=http://127.0.0.1:18901 vite dev --host 127.0.0.1",

View file

@ -87,7 +87,7 @@ type QueryNodes struct {
} }
// toSQL generates SQL query from query parameters // toSQL generates SQL query from query parameters
func (q *QueryNodes) toSQL() (args []interface{}, where string) { func (q QueryNodes) toSQL() (args []interface{}, where, sortBy, sortDirection string) {
wq := []string{} wq := []string{}
if q.Host != "" { if q.Host != "" {
@ -128,14 +128,17 @@ func (q *QueryNodes) toSQL() (args []interface{}, where string) {
where = "WHERE " + strings.Join(wq, " AND ") where = "WHERE " + strings.Join(wq, " AND ")
} }
if !slices.Contains([]string{"last_checked", "uptime"}, q.SortBy) { as := []string{"last_checked", "uptime"}
q.SortBy = "last_checked" sortBy = "last_checked"
if slices.Contains(as, q.SortBy) {
sortBy = q.SortBy
} }
if q.SortDirection != "asc" { sortDirection = "DESC"
q.SortDirection = "DESC" if q.SortDirection == "asc" {
sortDirection = "ASC"
} }
return args, where return args, where, sortBy, sortDirection
} }
// Nodes represents a list of nodes // Nodes represents a list of nodes
@ -147,7 +150,7 @@ type Nodes struct {
// Get nodes from database // Get nodes from database
func (r *moneroRepo) Nodes(q QueryNodes) (Nodes, error) { func (r *moneroRepo) Nodes(q QueryNodes) (Nodes, error) {
args, where := q.toSQL() args, where, sortBy, sortDirection := q.toSQL()
var nodes Nodes var nodes Nodes
@ -171,12 +174,12 @@ func (r *moneroRepo) Nodes(q QueryNodes) (Nodes, error) {
* *
FROM FROM
tbl_node tbl_node
%s %s -- where query if any
ORDER BY ORDER BY
%s %s
%s %s
LIMIT ? LIMIT ?
OFFSET ?`, where, q.SortBy, q.SortDirection) OFFSET ?`, where, sortBy, sortDirection)
err = r.db.Select(&nodes.Items, query, args...) err = r.db.Select(&nodes.Items, query, args...)
return nodes, err return nodes, err

View file

@ -87,18 +87,18 @@ func TestQueryNodes_toSQL(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
gotArgs, gotWhere := tt.query.toSQL() gotArgs, gotWhere, gotSortBy, gotSortDirection := tt.query.toSQL()
if !equalArgs(gotArgs, tt.wantArgs) { if !equalArgs(gotArgs, tt.wantArgs) {
t.Errorf("QueryNodes.toSQL() gotArgs = %v, want %v", gotArgs, tt.wantArgs) t.Errorf("QueryNodes.toSQL() gotArgs = %v, want %v", gotArgs, tt.wantArgs)
} }
if gotWhere != tt.wantWhere { if gotWhere != tt.wantWhere {
t.Errorf("QueryNodes.toSQL() gotWhere = %v, want %v", gotWhere, tt.wantWhere) t.Errorf("QueryNodes.toSQL() gotWhere = %v, want %v", gotWhere, tt.wantWhere)
} }
if tt.query.SortBy != tt.wantSortBy { if gotSortBy != tt.wantSortBy {
t.Errorf("QueryNodes.toSQL() gotSortBy = %v, want %v", tt.query.SortBy, tt.wantSortBy) t.Errorf("QueryNodes.toSQL() gotSortBy = %v, want %v", gotSortBy, tt.wantSortBy)
} }
if tt.query.SortDirection != tt.wantSortDirection { if gotSortDirection != tt.wantSortDirection {
t.Errorf("QueryNodes.toSQL() gotSortDirection = %v, want %v", tt.query.SortDirection, tt.wantSortDirection) t.Errorf("QueryNodes.toSQL() gotSortDirection = %v, want %v", gotSortDirection, tt.wantSortDirection)
} }
}) })
} }
@ -107,7 +107,8 @@ func TestQueryNodes_toSQL(t *testing.T) {
// Single bench test: // Single bench test:
// go test ./internal/monero -bench QueryNodes_toSQL -benchmem -run=^$ -v // go test ./internal/monero -bench QueryNodes_toSQL -benchmem -run=^$ -v
func Benchmark_QueryNodes_toSQL(b *testing.B) { func Benchmark_QueryNodes_toSQL(b *testing.B) {
q := QueryNodes{ for i := 0; i < b.N; i++ {
_, _, _, _ = QueryNodes{
Host: "test", Host: "test",
Nettype: "any", Nettype: "any",
Protocol: "any", Protocol: "any",
@ -118,9 +119,7 @@ func Benchmark_QueryNodes_toSQL(b *testing.B) {
Page: 1, Page: 1,
SortBy: "last_checked", SortBy: "last_checked",
SortDirection: "desc", SortDirection: "desc",
} }.toSQL()
for i := 0; i < b.N; i++ {
_, _ = q.toSQL()
} }
} }