mirror of
https://github.com/ditatompel/xmr-remote-nodes.git
synced 2025-01-08 05:52:10 +07:00
chore: Make SQL queries in more readable
This commit is contained in:
parent
fae32d9a74
commit
7d69e9af95
3 changed files with 370 additions and 48 deletions
|
@ -49,9 +49,19 @@ func (repo *AdminRepo) CreateAdmin(admin *Admin) (*Admin, error) {
|
||||||
if repo.isUsernameExists(admin.Username) {
|
if repo.isUsernameExists(admin.Username) {
|
||||||
return nil, errors.New("username already exists")
|
return nil, errors.New("username already exists")
|
||||||
}
|
}
|
||||||
|
_, err = repo.db.Exec(`
|
||||||
query := `INSERT INTO tbl_admin (username, password, created_ts) VALUES (?, ?, ?)`
|
INSERT INTO tbl_admin (
|
||||||
_, err = repo.db.Exec(query, admin.Username, admin.Password, admin.CreatedTs)
|
username,
|
||||||
|
password,
|
||||||
|
created_ts
|
||||||
|
) VALUES (
|
||||||
|
?,
|
||||||
|
?,
|
||||||
|
?
|
||||||
|
)`,
|
||||||
|
admin.Username,
|
||||||
|
admin.Password,
|
||||||
|
admin.CreatedTs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -59,8 +69,16 @@ func (repo *AdminRepo) CreateAdmin(admin *Admin) (*Admin, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (repo *AdminRepo) Login(username, password string) (*Admin, error) {
|
func (repo *AdminRepo) Login(username, password string) (*Admin, error) {
|
||||||
query := `SELECT id, username, password FROM tbl_admin WHERE username = ? LIMIT 1`
|
row, err := repo.db.Query(`
|
||||||
row, err := repo.db.Query(query, username)
|
SELECT
|
||||||
|
id,
|
||||||
|
username,
|
||||||
|
password
|
||||||
|
FROM
|
||||||
|
tbl_admin
|
||||||
|
WHERE
|
||||||
|
username = ?
|
||||||
|
LIMIT 1`, username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -87,8 +105,10 @@ func (repo *AdminRepo) Login(username, password string) (*Admin, error) {
|
||||||
return nil, errors.New("Invalid username or password")
|
return nil, errors.New("Invalid username or password")
|
||||||
}
|
}
|
||||||
|
|
||||||
update := `UPDATE tbl_admin SET lastactive_ts = ? WHERE id = ?`
|
_, err = repo.db.Exec(`
|
||||||
_, err = repo.db.Exec(update, time.Now().Unix(), admin.Id)
|
UPDATE tbl_admin
|
||||||
|
SET lastactive_ts = ?
|
||||||
|
WHERE id = ?`, time.Now().Unix(), admin.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -98,8 +118,14 @@ func (repo *AdminRepo) Login(username, password string) (*Admin, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (repo *AdminRepo) isUsernameExists(username string) bool {
|
func (repo *AdminRepo) isUsernameExists(username string) bool {
|
||||||
query := `SELECT id FROM tbl_admin WHERE username = ? LIMIT 1`
|
row, err := repo.db.Query(`
|
||||||
row, err := repo.db.Query(query, username)
|
SELECT
|
||||||
|
id
|
||||||
|
FROM
|
||||||
|
tbl_admin
|
||||||
|
WHERE
|
||||||
|
username = ?
|
||||||
|
LIMIT 1`, username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
|
@ -151,7 +151,12 @@ func (repo *MoneroRepo) Nodes(q MoneroQueryParams) (MoneroNodes, error) {
|
||||||
|
|
||||||
nodes := MoneroNodes{}
|
nodes := MoneroNodes{}
|
||||||
|
|
||||||
queryTotalRows := fmt.Sprintf("SELECT COUNT(id) AS total_rows FROM tbl_node %s", where)
|
queryTotalRows := fmt.Sprintf(`
|
||||||
|
SELECT
|
||||||
|
COUNT(id) AS total_rows
|
||||||
|
FROM
|
||||||
|
tbl_node
|
||||||
|
%s`, where)
|
||||||
|
|
||||||
err := repo.db.QueryRow(queryTotalRows, queryParams...).Scan(&nodes.TotalRows)
|
err := repo.db.QueryRow(queryTotalRows, queryParams...).Scan(&nodes.TotalRows)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -169,7 +174,42 @@ func (repo *MoneroRepo) Nodes(q MoneroQueryParams) (MoneroNodes, error) {
|
||||||
sortDirection = "ASC"
|
sortDirection = "ASC"
|
||||||
}
|
}
|
||||||
|
|
||||||
query := fmt.Sprintf("SELECT id, protocol, hostname, port, is_tor, is_available, nettype, height, adjusted_time, database_size, difficulty, version, uptime, estimate_fee, ip_addr, asn, asn_name, country, country_name, city, lat, lon, date_entered, last_checked, last_check_status, cors_capable FROM tbl_node %s ORDER BY %s %s LIMIT ? OFFSET ?", where, sortBy, sortDirection)
|
query := fmt.Sprintf(`
|
||||||
|
SELECT
|
||||||
|
id,
|
||||||
|
protocol,
|
||||||
|
hostname,
|
||||||
|
port,
|
||||||
|
is_tor,
|
||||||
|
is_available,
|
||||||
|
nettype,
|
||||||
|
height,
|
||||||
|
adjusted_time,
|
||||||
|
database_size,
|
||||||
|
difficulty,
|
||||||
|
version,
|
||||||
|
uptime,
|
||||||
|
estimate_fee,
|
||||||
|
ip_addr,
|
||||||
|
asn,
|
||||||
|
asn_name,
|
||||||
|
country,
|
||||||
|
country_name,
|
||||||
|
city,
|
||||||
|
lat,
|
||||||
|
lon,
|
||||||
|
date_entered,
|
||||||
|
last_checked,
|
||||||
|
last_check_status,
|
||||||
|
cors_capable
|
||||||
|
FROM
|
||||||
|
tbl_node
|
||||||
|
%s -- where query if any
|
||||||
|
ORDER BY
|
||||||
|
%s
|
||||||
|
%s
|
||||||
|
LIMIT ?
|
||||||
|
OFFSET ?`, where, sortBy, sortDirection)
|
||||||
|
|
||||||
row, err := repo.db.Query(query, queryParams...)
|
row, err := repo.db.Query(query, queryParams...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -181,7 +221,33 @@ func (repo *MoneroRepo) Nodes(q MoneroQueryParams) (MoneroNodes, error) {
|
||||||
|
|
||||||
for row.Next() {
|
for row.Next() {
|
||||||
node := MoneroNode{}
|
node := MoneroNode{}
|
||||||
err = row.Scan(&node.Id, &node.Protocol, &node.Hostname, &node.Port, &node.IsTor, &node.IsAvailable, &node.NetType, &node.Height, &node.AdjustedTime, &node.DatabaseSize, &node.Difficulty, &node.Version, &node.Uptime, &node.EstimateFee, &node.Ip, &node.Asn, &node.AsnName, &node.CountryCode, &node.CountryName, &node.City, &node.Lat, &node.Lon, &node.DateEntered, &node.LastChecked, &node.LastCheckStatus, &node.CorsCapable)
|
err = row.Scan(
|
||||||
|
&node.Id,
|
||||||
|
&node.Protocol,
|
||||||
|
&node.Hostname,
|
||||||
|
&node.Port,
|
||||||
|
&node.IsTor,
|
||||||
|
&node.IsAvailable,
|
||||||
|
&node.NetType,
|
||||||
|
&node.Height,
|
||||||
|
&node.AdjustedTime,
|
||||||
|
&node.DatabaseSize,
|
||||||
|
&node.Difficulty,
|
||||||
|
&node.Version,
|
||||||
|
&node.Uptime,
|
||||||
|
&node.EstimateFee,
|
||||||
|
&node.Ip,
|
||||||
|
&node.Asn,
|
||||||
|
&node.AsnName,
|
||||||
|
&node.CountryCode,
|
||||||
|
&node.CountryName,
|
||||||
|
&node.City,
|
||||||
|
&node.Lat,
|
||||||
|
&node.Lon,
|
||||||
|
&node.DateEntered,
|
||||||
|
&node.LastChecked,
|
||||||
|
&node.LastCheckStatus,
|
||||||
|
&node.CorsCapable)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nodes, err
|
return nodes, err
|
||||||
}
|
}
|
||||||
|
@ -265,7 +331,28 @@ func (repo *MoneroRepo) Logs(q MoneroLogQueryParams) (MoneroNodeFetchLogs, error
|
||||||
sortDirection = "ASC"
|
sortDirection = "ASC"
|
||||||
}
|
}
|
||||||
|
|
||||||
query := fmt.Sprintf("SELECT id, node_id, prober_id, is_available, height, adjusted_time, database_size, difficulty, estimate_fee, date_checked, failed_reason, fetch_runtime FROM tbl_probe_log %s ORDER BY %s %s LIMIT ? OFFSET ?", where, sortBy, sortDirection)
|
query := fmt.Sprintf(`
|
||||||
|
SELECT
|
||||||
|
id,
|
||||||
|
node_id,
|
||||||
|
prober_id,
|
||||||
|
is_available,
|
||||||
|
height,
|
||||||
|
adjusted_time,
|
||||||
|
database_size,
|
||||||
|
difficulty,
|
||||||
|
estimate_fee,
|
||||||
|
date_checked,
|
||||||
|
failed_reason,
|
||||||
|
fetch_runtime
|
||||||
|
FROM
|
||||||
|
tbl_probe_log
|
||||||
|
%s -- where query
|
||||||
|
ORDER BY
|
||||||
|
%s
|
||||||
|
%s
|
||||||
|
LIMIT ?
|
||||||
|
OFFSET ?`, where, sortBy, sortDirection)
|
||||||
|
|
||||||
row, err := repo.db.Query(query, queryParams...)
|
row, err := repo.db.Query(query, queryParams...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -277,7 +364,19 @@ func (repo *MoneroRepo) Logs(q MoneroLogQueryParams) (MoneroNodeFetchLogs, error
|
||||||
|
|
||||||
for row.Next() {
|
for row.Next() {
|
||||||
probeLog := ProbeLog{}
|
probeLog := ProbeLog{}
|
||||||
err = row.Scan(&probeLog.Id, &probeLog.NodeId, &probeLog.ProberId, &probeLog.Status, &probeLog.Height, &probeLog.AdjustedTime, &probeLog.DatabaseSize, &probeLog.Difficulty, &probeLog.EstimateFee, &probeLog.DateChecked, &probeLog.FailedReason, &probeLog.FetchRuntime)
|
err = row.Scan(
|
||||||
|
&probeLog.Id,
|
||||||
|
&probeLog.NodeId,
|
||||||
|
&probeLog.ProberId,
|
||||||
|
&probeLog.Status,
|
||||||
|
&probeLog.Height,
|
||||||
|
&probeLog.AdjustedTime,
|
||||||
|
&probeLog.DatabaseSize,
|
||||||
|
&probeLog.Difficulty,
|
||||||
|
&probeLog.EstimateFee,
|
||||||
|
&probeLog.DateChecked,
|
||||||
|
&probeLog.FailedReason,
|
||||||
|
&probeLog.FetchRuntime)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fetchLogs, err
|
return fetchLogs, err
|
||||||
}
|
}
|
||||||
|
@ -323,8 +422,16 @@ func (repo *MoneroRepo) Add(protocol string, hostname string, port uint) error {
|
||||||
ip = hostIp.String()
|
ip = hostIp.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
check := `SELECT id FROM tbl_node WHERE protocol = ? AND hostname = ? AND port = ? LIMIT 1`
|
row, err := repo.db.Query(`
|
||||||
row, err := repo.db.Query(check, protocol, hostname, port)
|
SELECT
|
||||||
|
id
|
||||||
|
FROM
|
||||||
|
tbl_node
|
||||||
|
WHERE
|
||||||
|
protocol = ?
|
||||||
|
AND hostname = ?
|
||||||
|
AND port = ?
|
||||||
|
LIMIT 1`, protocol, hostname, port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -334,9 +441,43 @@ func (repo *MoneroRepo) Add(protocol string, hostname string, port uint) error {
|
||||||
return errors.New("Node already monitored")
|
return errors.New("Node already monitored")
|
||||||
}
|
}
|
||||||
statusDb, _ := json.Marshal([5]int{2, 2, 2, 2, 2})
|
statusDb, _ := json.Marshal([5]int{2, 2, 2, 2, 2})
|
||||||
|
_, err = repo.db.Exec(`
|
||||||
query := `INSERT INTO tbl_node (protocol, hostname, port, is_tor, nettype, ip_addr, lat, lon, date_entered, last_checked, last_check_status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
INSERT INTO tbl_node (
|
||||||
_, err = repo.db.Exec(query, protocol, hostname, port, is_tor, "", ip, 0, 0, time.Now().Unix(), 0, string(statusDb))
|
protocol,
|
||||||
|
hostname,
|
||||||
|
port,
|
||||||
|
is_tor,
|
||||||
|
nettype,
|
||||||
|
ip_addr,
|
||||||
|
lat,
|
||||||
|
lon,
|
||||||
|
date_entered,
|
||||||
|
last_checked,
|
||||||
|
last_check_status
|
||||||
|
) VALUES (
|
||||||
|
?,
|
||||||
|
?,
|
||||||
|
?,
|
||||||
|
?,
|
||||||
|
?,
|
||||||
|
?,
|
||||||
|
?,
|
||||||
|
?,
|
||||||
|
?,
|
||||||
|
?,
|
||||||
|
?
|
||||||
|
)`,
|
||||||
|
protocol,
|
||||||
|
hostname,
|
||||||
|
port,
|
||||||
|
is_tor,
|
||||||
|
"",
|
||||||
|
ip,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
time.Now().Unix(),
|
||||||
|
0,
|
||||||
|
string(statusDb))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -371,14 +512,35 @@ func (repo *MoneroRepo) GiveJob(acceptTor int) (MoneroNode, error) {
|
||||||
|
|
||||||
node := MoneroNode{}
|
node := MoneroNode{}
|
||||||
|
|
||||||
query := fmt.Sprintf(`SELECT id, hostname, port, protocol, is_tor, last_check_status FROM tbl_node %s ORDER BY last_checked ASC LIMIT 1`, where)
|
query := fmt.Sprintf(`
|
||||||
err := repo.db.QueryRow(query, queryParams...).Scan(&node.Id, &node.Hostname, &node.Port, &node.Protocol, &node.IsTor, &node.LastCheckStatus)
|
SELECT
|
||||||
|
id,
|
||||||
|
hostname,
|
||||||
|
port,
|
||||||
|
protocol,
|
||||||
|
is_tor,
|
||||||
|
last_check_status
|
||||||
|
FROM
|
||||||
|
tbl_node
|
||||||
|
%s -- where query if any
|
||||||
|
ORDER BY
|
||||||
|
last_checked ASC
|
||||||
|
LIMIT 1`, where)
|
||||||
|
err := repo.db.QueryRow(query, queryParams...).Scan(
|
||||||
|
&node.Id,
|
||||||
|
&node.Hostname,
|
||||||
|
&node.Port,
|
||||||
|
&node.Protocol,
|
||||||
|
&node.IsTor,
|
||||||
|
&node.LastCheckStatus)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return node, err
|
return node, err
|
||||||
}
|
}
|
||||||
|
|
||||||
update := `UPDATE tbl_node SET last_checked = ? WHERE id = ?`
|
_, err = repo.db.Exec(`
|
||||||
_, err = repo.db.Exec(update, time.Now().Unix(), node.Id)
|
UPDATE tbl_node
|
||||||
|
SET last_checked = ?
|
||||||
|
WHERE id = ?`, time.Now().Unix(), node.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return node, err
|
return node, err
|
||||||
}
|
}
|
||||||
|
@ -397,9 +559,44 @@ func (repo *MoneroRepo) ProcessJob(report ProbeReport, proberId int64) error {
|
||||||
return errors.New("Invalid node")
|
return errors.New("Invalid node")
|
||||||
}
|
}
|
||||||
|
|
||||||
qInsertLog := `INSERT INTO tbl_probe_log (node_id, prober_id, is_available, height, adjusted_time, database_size, difficulty, estimate_fee, date_checked, failed_reason, fetch_runtime) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
qInsertLog := `
|
||||||
|
INSERT INTO tbl_probe_log (
|
||||||
_, err := repo.db.Exec(qInsertLog, report.NodeInfo.Id, proberId, report.NodeInfo.IsAvailable, report.NodeInfo.Height, report.NodeInfo.AdjustedTime, report.NodeInfo.DatabaseSize, report.NodeInfo.Difficulty, report.NodeInfo.EstimateFee, time.Now().Unix(), report.Message, report.TookTime)
|
node_id,
|
||||||
|
prober_id,
|
||||||
|
is_available,
|
||||||
|
height,
|
||||||
|
adjusted_time,
|
||||||
|
database_size,
|
||||||
|
difficulty,
|
||||||
|
estimate_fee,
|
||||||
|
date_checked,
|
||||||
|
failed_reason,
|
||||||
|
fetch_runtime
|
||||||
|
) VALUES (
|
||||||
|
?,
|
||||||
|
?,
|
||||||
|
?,
|
||||||
|
?,
|
||||||
|
?,
|
||||||
|
?,
|
||||||
|
?,
|
||||||
|
?,
|
||||||
|
?,
|
||||||
|
?,
|
||||||
|
?
|
||||||
|
)`
|
||||||
|
_, err := repo.db.Exec(qInsertLog,
|
||||||
|
report.NodeInfo.Id,
|
||||||
|
proberId,
|
||||||
|
report.NodeInfo.IsAvailable,
|
||||||
|
report.NodeInfo.Height,
|
||||||
|
report.NodeInfo.AdjustedTime,
|
||||||
|
report.NodeInfo.DatabaseSize,
|
||||||
|
report.NodeInfo.Difficulty,
|
||||||
|
report.NodeInfo.EstimateFee,
|
||||||
|
time.Now().Unix(),
|
||||||
|
report.Message,
|
||||||
|
report.TookTime)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -413,11 +610,16 @@ func (repo *MoneroRepo) ProcessJob(report ProbeReport, proberId int64) error {
|
||||||
TotalFetched uint `db:"total_fetched"`
|
TotalFetched uint `db:"total_fetched"`
|
||||||
}{}
|
}{}
|
||||||
|
|
||||||
qstats := `SELECT
|
qstats := `
|
||||||
SUM(if(is_available='1',1,0)) AS online,
|
SELECT
|
||||||
SUM(if(is_available='0',1,0)) AS offline,
|
SUM(if(is_available='1',1,0)) AS online,
|
||||||
SUM(if(id='0',0,1)) AS total_fetched
|
SUM(if(is_available='0',1,0)) AS offline,
|
||||||
FROM tbl_probe_log WHERE node_id = ? AND date_checked > ?`
|
SUM(if(id='0',0,1)) AS total_fetched
|
||||||
|
FROM
|
||||||
|
tbl_probe_log
|
||||||
|
WHERE
|
||||||
|
node_id = ?
|
||||||
|
AND date_checked > ?`
|
||||||
repo.db.Get(&nodeStats, qstats, report.NodeInfo.Id, limitTs)
|
repo.db.Get(&nodeStats, qstats, report.NodeInfo.Id, limitTs)
|
||||||
|
|
||||||
avgUptime := (float64(nodeStats.OnlineCount) / float64(nodeStats.TotalFetched)) * 100
|
avgUptime := (float64(nodeStats.OnlineCount) / float64(nodeStats.TotalFetched)) * 100
|
||||||
|
@ -458,17 +660,59 @@ func (repo *MoneroRepo) ProcessJob(report ProbeReport, proberId int64) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if report.NodeInfo.IsAvailable {
|
if report.NodeInfo.IsAvailable {
|
||||||
update := `UPDATE tbl_node SET
|
update := `
|
||||||
is_available = ?, nettype = ?, height = ?, adjusted_time = ?,
|
UPDATE tbl_node
|
||||||
database_size = ?, difficulty = ?, version = ?, uptime = ?,
|
SET
|
||||||
estimate_fee = ?, ip_addr = ?, asn = ?, asn_name = ?, country = ?,
|
is_available = ?,
|
||||||
country_name = ?, city = ?, last_checked = ?, last_check_status = ?,
|
nettype = ?,
|
||||||
|
height = ?,
|
||||||
|
adjusted_time = ?,
|
||||||
|
database_size = ?,
|
||||||
|
difficulty = ?,
|
||||||
|
version = ?,
|
||||||
|
uptime = ?,
|
||||||
|
estimate_fee = ?,
|
||||||
|
ip_addr = ?,
|
||||||
|
asn = ?,
|
||||||
|
asn_name = ?,
|
||||||
|
country = ?,
|
||||||
|
country_name = ?,
|
||||||
|
city = ?,
|
||||||
|
last_checked = ?,
|
||||||
|
last_check_status = ?,
|
||||||
cors_capable = ?
|
cors_capable = ?
|
||||||
WHERE id = ?`
|
WHERE
|
||||||
|
id = ?`
|
||||||
_, err = repo.db.Exec(update,
|
_, err = repo.db.Exec(update,
|
||||||
nodeAvailable, report.NodeInfo.NetType, report.NodeInfo.Height, report.NodeInfo.AdjustedTime, report.NodeInfo.DatabaseSize, report.NodeInfo.Difficulty, report.NodeInfo.Version, report.NodeInfo.Uptime, report.NodeInfo.EstimateFee, report.NodeInfo.Ip, report.NodeInfo.Asn, report.NodeInfo.AsnName, report.NodeInfo.CountryCode, report.NodeInfo.CountryName, report.NodeInfo.City, now.Unix(), string(statuesValueToDb), report.NodeInfo.CorsCapable, report.NodeInfo.Id)
|
nodeAvailable,
|
||||||
|
report.NodeInfo.NetType,
|
||||||
|
report.NodeInfo.Height,
|
||||||
|
report.NodeInfo.AdjustedTime,
|
||||||
|
report.NodeInfo.DatabaseSize,
|
||||||
|
report.NodeInfo.Difficulty,
|
||||||
|
report.NodeInfo.Version,
|
||||||
|
report.NodeInfo.Uptime,
|
||||||
|
report.NodeInfo.EstimateFee,
|
||||||
|
report.NodeInfo.Ip,
|
||||||
|
report.NodeInfo.Asn,
|
||||||
|
report.NodeInfo.AsnName,
|
||||||
|
report.NodeInfo.CountryCode,
|
||||||
|
report.NodeInfo.CountryName,
|
||||||
|
report.NodeInfo.City,
|
||||||
|
now.Unix(),
|
||||||
|
string(statuesValueToDb),
|
||||||
|
report.NodeInfo.CorsCapable,
|
||||||
|
report.NodeInfo.Id)
|
||||||
} else {
|
} else {
|
||||||
update := `UPDATE tbl_node SET is_available = ?, uptime = ?, last_checked = ?, last_check_status = ? WHERE id = ?`
|
update := `
|
||||||
|
UPDATE tbl_node
|
||||||
|
SET
|
||||||
|
is_available = ?,
|
||||||
|
uptime = ?,
|
||||||
|
last_checked = ?,
|
||||||
|
last_check_status = ?
|
||||||
|
WHERE
|
||||||
|
id = ?`
|
||||||
_, err = repo.db.Exec(update, nodeAvailable, report.NodeInfo.Uptime, now.Unix(), string(statuesValueToDb), report.NodeInfo.Id)
|
_, err = repo.db.Exec(update, nodeAvailable, report.NodeInfo.Uptime, now.Unix(), string(statuesValueToDb), report.NodeInfo.Id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -477,7 +721,10 @@ func (repo *MoneroRepo) ProcessJob(report ProbeReport, proberId int64) error {
|
||||||
repo.Delete(report.NodeInfo.Id)
|
repo.Delete(report.NodeInfo.Id)
|
||||||
}
|
}
|
||||||
|
|
||||||
repo.db.Exec(`UPDATE tbl_prober SET last_submit_ts = ? WHERE id = ?`, now.Unix(), proberId)
|
repo.db.Exec(`
|
||||||
|
UPDATE tbl_prober
|
||||||
|
SET last_submit_ts = ?
|
||||||
|
WHERE id = ?`, now.Unix(), proberId)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -494,7 +741,20 @@ func (repo *MoneroRepo) NetFee() []NetFee {
|
||||||
|
|
||||||
for _, net := range netTypes {
|
for _, net := range netTypes {
|
||||||
fees := NetFee{}
|
fees := NetFee{}
|
||||||
err := repo.db.Get(&fees, `SELECT COUNT(id) AS node_count, nettype, estimate_fee FROM tbl_node WHERE nettype = ? GROUP BY estimate_fee ORDER BY node_count DESC LIMIT 1`, net)
|
err := repo.db.Get(&fees, `
|
||||||
|
SELECT
|
||||||
|
COUNT(id) AS node_count,
|
||||||
|
nettype,
|
||||||
|
estimate_fee
|
||||||
|
FROM
|
||||||
|
tbl_node
|
||||||
|
WHERE
|
||||||
|
nettype = ?
|
||||||
|
GROUP BY
|
||||||
|
estimate_fee
|
||||||
|
ORDER BY
|
||||||
|
node_count DESC
|
||||||
|
LIMIT 1`, net)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("WARN:", err.Error())
|
fmt.Println("WARN:", err.Error())
|
||||||
continue
|
continue
|
||||||
|
@ -513,7 +773,16 @@ type MoneroCountries struct {
|
||||||
|
|
||||||
func (repo *MoneroRepo) Countries() ([]MoneroCountries, error) {
|
func (repo *MoneroRepo) Countries() ([]MoneroCountries, error) {
|
||||||
countries := []MoneroCountries{}
|
countries := []MoneroCountries{}
|
||||||
|
err := repo.db.Select(&countries, `
|
||||||
err := repo.db.Select(&countries, `SELECT COUNT(id) AS total_nodes, country, country_name FROM tbl_node GROUP BY country ORDER BY country ASC`)
|
SELECT
|
||||||
|
COUNT(id) AS total_nodes,
|
||||||
|
country,
|
||||||
|
country_name
|
||||||
|
FROM
|
||||||
|
tbl_node
|
||||||
|
GROUP BY
|
||||||
|
country
|
||||||
|
ORDER BY
|
||||||
|
country ASC`)
|
||||||
return countries, err
|
return countries, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,16 @@ func NewProberRepo(db *database.DB) ProberRepository {
|
||||||
|
|
||||||
func (repo *ProberRepo) Add(name string) (Prober, error) {
|
func (repo *ProberRepo) Add(name string) (Prober, error) {
|
||||||
apiKey := uuid.New()
|
apiKey := uuid.New()
|
||||||
query := `INSERT INTO tbl_prober (name, api_key, last_submit_ts) VALUES (?, ?, ?)`
|
query := `
|
||||||
|
INSERT INTO tbl_prober (
|
||||||
|
name,
|
||||||
|
api_key,
|
||||||
|
last_submit_ts
|
||||||
|
) VALUES (
|
||||||
|
?,
|
||||||
|
?,
|
||||||
|
?
|
||||||
|
)`
|
||||||
_, err := repo.db.Exec(query, name, apiKey, 0)
|
_, err := repo.db.Exec(query, name, apiKey, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Prober{}, err
|
return Prober{}, err
|
||||||
|
@ -59,8 +68,7 @@ func (repo *ProberRepo) Edit(id int, name string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (repo *ProberRepo) Delete(id int) error {
|
func (repo *ProberRepo) Delete(id int) error {
|
||||||
query := `DELETE FROM tbl_prober WHERE id = ?`
|
res, err := repo.db.Exec(`DELETE FROM tbl_prober WHERE id = ?`, id)
|
||||||
res, err := repo.db.Exec(query, id)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -107,7 +115,16 @@ func (repo *ProberRepo) Probers(q ProbersQueryParams) ([]Prober, error) {
|
||||||
sortDirection = "ASC"
|
sortDirection = "ASC"
|
||||||
}
|
}
|
||||||
|
|
||||||
query := fmt.Sprintf("SELECT id, name, api_key, last_submit_ts FROM tbl_prober %s ORDER BY %s %s", where, sortBy, sortDirection)
|
query := fmt.Sprintf(`
|
||||||
|
SELECT
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
api_key,
|
||||||
|
last_submit_ts
|
||||||
|
FROM
|
||||||
|
tbl_prober
|
||||||
|
%s -- where clause if any
|
||||||
|
ORDER BY %s %s`, where, sortBy, sortDirection)
|
||||||
|
|
||||||
row, err := repo.db.Query(query, queryParams...)
|
row, err := repo.db.Query(query, queryParams...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -128,7 +145,17 @@ func (repo *ProberRepo) Probers(q ProbersQueryParams) ([]Prober, error) {
|
||||||
|
|
||||||
func (repo *ProberRepo) CheckApi(key string) (Prober, error) {
|
func (repo *ProberRepo) CheckApi(key string) (Prober, error) {
|
||||||
prober := Prober{}
|
prober := Prober{}
|
||||||
query := `SELECT id, name, api_key, last_submit_ts FROM tbl_prober WHERE api_key = ? LIMIT 1`
|
query := `
|
||||||
|
SELECT
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
api_key,
|
||||||
|
last_submit_ts
|
||||||
|
FROM
|
||||||
|
tbl_prober
|
||||||
|
WHERE
|
||||||
|
api_key = ?
|
||||||
|
LIMIT 1`
|
||||||
err := repo.db.QueryRow(query, key).Scan(&prober.Id, &prober.Name, &prober.ApiKey, &prober.LastSubmitTs)
|
err := repo.db.QueryRow(query, key).Scan(&prober.Id, &prober.Name, &prober.ApiKey, &prober.LastSubmitTs)
|
||||||
return prober, err
|
return prober, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue