diff --git a/internal/database/schema.go b/internal/database/schema.go index abc0a7d..97d9bc4 100644 --- a/internal/database/schema.go +++ b/internal/database/schema.go @@ -44,9 +44,10 @@ func MigrateDb(db *DB) error { } func getSchemaVersion(db *DB) int { - _, err := db.Exec(`CREATE TABLE IF NOT EXISTS tbl_schema_ver ( - version INT(5) UNSIGNED NOT NULL - )`) + _, err := db.Exec(` + CREATE TABLE IF NOT EXISTS tbl_schema_ver ( + version INT(5) UNSIGNED NOT NULL + )`) if err != nil { return -1 } @@ -68,14 +69,15 @@ func v1(db *DB) error { slog.Debug("[DB] Migrating database schema version 1") // table: tbl_admin slog.Debug("[DB] Creating table: tbl_admin") - _, err := db.Exec(`CREATE TABLE tbl_admin ( - id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, - username VARCHAR(255) NOT NULL, - password VARCHAR(255) NOT NULL, - lastactive_ts INT(11) UNSIGNED NOT NULL DEFAULT 0, - created_ts INT(11) UNSIGNED NOT NULL DEFAULT 0, - PRIMARY KEY (id) - )`) + _, err := db.Exec(` + CREATE TABLE tbl_admin ( + id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, + username VARCHAR(255) NOT NULL, + password VARCHAR(255) NOT NULL, + lastactive_ts INT(11) UNSIGNED NOT NULL DEFAULT 0, + created_ts INT(11) UNSIGNED NOT NULL DEFAULT 0, + PRIMARY KEY (id) + )`) if err != nil { return err } @@ -87,73 +89,86 @@ func v1(db *DB) error { // table: tbl_cron slog.Debug("[DB] Creating table: tbl_cron") - _, err = db.Exec(`CREATE TABLE tbl_cron ( - id INT(8) UNSIGNED NOT NULL AUTO_INCREMENT, - title VARCHAR(255) NOT NULL DEFAULT '', - slug VARCHAR(255) NOT NULL DEFAULT '', - description VARCHAR(255) NOT NULL DEFAULT '', - run_every INT(8) UNSIGNED NOT NULL DEFAULT 60 COMMENT 'in seconds', - last_run INT(11) UNSIGNED NOT NULL DEFAULT 0, - next_run INT(11) UNSIGNED NOT NULL DEFAULT 0, - run_time FLOAT(7,3) UNSIGNED NOT NULL DEFAULT 0.000, - cron_state TINYINT(1) UNSIGNED NOT NULL DEFAULT 0, - is_enabled TINYINT(1) UNSIGNED NOT NULL DEFAULT 1, - PRIMARY KEY (id) - )`) + _, err = db.Exec(` + CREATE TABLE tbl_cron ( + id INT(8) UNSIGNED NOT NULL AUTO_INCREMENT, + title VARCHAR(255) NOT NULL DEFAULT '', + slug VARCHAR(255) NOT NULL DEFAULT '', + description VARCHAR(255) NOT NULL DEFAULT '', + run_every INT(8) UNSIGNED NOT NULL DEFAULT 60 COMMENT 'in seconds', + last_run INT(11) UNSIGNED NOT NULL DEFAULT 0, + next_run INT(11) UNSIGNED NOT NULL DEFAULT 0, + run_time FLOAT(7,3) UNSIGNED NOT NULL DEFAULT 0.000, + cron_state TINYINT(1) UNSIGNED NOT NULL DEFAULT 0, + is_enabled TINYINT(1) UNSIGNED NOT NULL DEFAULT 1, + PRIMARY KEY (id) + )`) if err != nil { return err } slog.Debug("[DB] Adding default cron jobs to table: tbl_cron") - _, err = db.Exec(`INSERT INTO tbl_cron (title, slug, description, run_every) - VALUES ('Delete old probe logs', 'delete_old_probe_logs', 'Delete old probe log from the database',120);`) + _, err = db.Exec(` + INSERT INTO tbl_cron ( + title, + slug, + description, + run_every + ) VALUES ( + 'Delete old probe logs', + 'delete_old_probe_logs', + 'Delete old probe log from the database', + 120 + );`) if err != nil { return err } // table: tbl_node slog.Debug("[DB] Creating table: tbl_node") - _, err = db.Exec(`CREATE TABLE tbl_node ( - id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, - protocol VARCHAR(6) NOT NULL DEFAULT 'http' COMMENT 'http | https', - hostname VARCHAR(255) NOT NULL, - port INT(6) UNSIGNED NOT NULL DEFAULT 0, - is_tor TINYINT(1) UNSIGNED NOT NULL DEFAULT 0, - is_available TINYINT(1) UNSIGNED NOT NULL DEFAULT 0, - nettype VARCHAR(100) NOT NULL COMMENT 'mainnet | stagenet | testnet', - height BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, - adjusted_time BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, - database_size BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, - difficulty BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, - version VARCHAR(200) NOT NULL DEFAULT '', - uptime float(5,2) UNSIGNED NOT NULL DEFAULT 0.00, - estimate_fee INT(9) UNSIGNED NOT NULL DEFAULT 0, - ip_addr VARCHAR(200) NOT NULL, - asn INT(9) UNSIGNED NOT NULL DEFAULT 0, - asn_name VARCHAR(255) NOT NULL DEFAULT '', - country VARCHAR(100) NOT NULL DEFAULT '', - country_name VARCHAR(255) NOT NULL DEFAULT '', - city VARCHAR(255) NOT NULL DEFAULT '', - lat FLOAT NOT NULL DEFAULT 0 COMMENT 'latitude', - lon FLOAT NOT NULL DEFAULT 0 COMMENT 'longitude', - date_entered INT(11) UNSIGNED NOT NULL DEFAULT 0, - last_checked INT(11) UNSIGNED NOT NULL DEFAULT 0, - last_check_status TEXT DEFAULT NULL, - cors_capable TINYINT(1) UNSIGNED NOT NULL DEFAULT 0, - PRIMARY KEY (id) - )`) + _, err = db.Exec(` + CREATE TABLE tbl_node ( + id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, + protocol VARCHAR(6) NOT NULL DEFAULT 'http' COMMENT 'http | https', + hostname VARCHAR(255) NOT NULL, + port INT(6) UNSIGNED NOT NULL DEFAULT 0, + is_tor TINYINT(1) UNSIGNED NOT NULL DEFAULT 0, + is_available TINYINT(1) UNSIGNED NOT NULL DEFAULT 0, + nettype VARCHAR(100) NOT NULL COMMENT 'mainnet | stagenet | testnet', + height BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, + adjusted_time BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, + database_size BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, + difficulty BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, + version VARCHAR(200) NOT NULL DEFAULT '', + uptime float(5,2) UNSIGNED NOT NULL DEFAULT 0.00, + estimate_fee INT(9) UNSIGNED NOT NULL DEFAULT 0, + ip_addr VARCHAR(200) NOT NULL, + asn INT(9) UNSIGNED NOT NULL DEFAULT 0, + asn_name VARCHAR(255) NOT NULL DEFAULT '', + country VARCHAR(100) NOT NULL DEFAULT '', + country_name VARCHAR(255) NOT NULL DEFAULT '', + city VARCHAR(255) NOT NULL DEFAULT '', + lat FLOAT NOT NULL DEFAULT 0 COMMENT 'latitude', + lon FLOAT NOT NULL DEFAULT 0 COMMENT 'longitude', + date_entered INT(11) UNSIGNED NOT NULL DEFAULT 0, + last_checked INT(11) UNSIGNED NOT NULL DEFAULT 0, + last_check_status TEXT DEFAULT NULL, + cors_capable TINYINT(1) UNSIGNED NOT NULL DEFAULT 0, + PRIMARY KEY (id) + )`) if err != nil { return err } // table: tbl_prober slog.Debug("[DB] Creating table: tbl_prober") - _, err = db.Exec(`CREATE TABLE tbl_prober ( - id INT(9) UNSIGNED NOT NULL AUTO_INCREMENT, - name VARCHAR(255) NOT NULL, - api_key VARCHAR(36) NOT NULL, - last_submit_ts INT(11) UNSIGNED NOT NULL DEFAULT 0, - PRIMARY KEY (id) - )`) + _, err = db.Exec(` + CREATE TABLE tbl_prober ( + id INT(9) UNSIGNED NOT NULL AUTO_INCREMENT, + name VARCHAR(255) NOT NULL, + api_key VARCHAR(36) NOT NULL, + last_submit_ts INT(11) UNSIGNED NOT NULL DEFAULT 0, + PRIMARY KEY (id) + )`) if err != nil { return err } @@ -166,21 +181,22 @@ func v1(db *DB) error { // table: tbl_probe_log slog.Debug("[DB] Creating table: tbl_probe_log") - _, err = db.Exec(`CREATE TABLE tbl_probe_log ( - id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, - node_id INT(11) UNSIGNED NOT NULL DEFAULT 0, - prober_id INT(9) UNSIGNED NOT NULL DEFAULT 0, - is_available TINYINT(1) UNSIGNED NOT NULL DEFAULT 0, - height BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, - adjusted_time BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, - database_size BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, - difficulty BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, - estimate_fee INT(9) UNSIGNED NOT NULL DEFAULT 0, - date_checked INT(11) UNSIGNED NOT NULL DEFAULT 0, - failed_reason TEXT NOT NULL DEFAULT '', - fetch_runtime FLOAT(5,2) UNSIGNED NOT NULL DEFAULT 0.00, - PRIMARY KEY (id) - )`) + _, err = db.Exec(` + CREATE TABLE tbl_probe_log ( + id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, + node_id INT(11) UNSIGNED NOT NULL DEFAULT 0, + prober_id INT(9) UNSIGNED NOT NULL DEFAULT 0, + is_available TINYINT(1) UNSIGNED NOT NULL DEFAULT 0, + height BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, + adjusted_time BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, + database_size BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, + difficulty BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, + estimate_fee INT(9) UNSIGNED NOT NULL DEFAULT 0, + date_checked INT(11) UNSIGNED NOT NULL DEFAULT 0, + failed_reason TEXT NOT NULL DEFAULT '', + fetch_runtime FLOAT(5,2) UNSIGNED NOT NULL DEFAULT 0.00, + PRIMARY KEY (id) + )`) if err != nil { return err } diff --git a/internal/repo/cron.go b/internal/repo/cron.go index 82ac724..5368488 100644 --- a/internal/repo/cron.go +++ b/internal/repo/cron.go @@ -69,24 +69,52 @@ func (repo *CronRepo) RunCronProcess() { } func (repo *CronRepo) Crons() ([]Cron, error) { - query := `SELECT id, title, slug, description, run_every, last_run, next_run, run_time, cron_state, is_enabled FROM tbl_cron` - var tasks []Cron - err := repo.db.Select(&tasks, query) + err := repo.db.Select(&tasks, ` + SELECT + id, + title, + slug, + description, + run_every, + last_run, + next_run, + run_time, + cron_state, + is_enabled + FROM + tbl_cron`) return tasks, err } func (repo *CronRepo) queueList() ([]Cron, error) { tasks := []Cron{} - query := `SELECT id, run_every, last_run, slug, next_run, cron_state FROM tbl_cron - WHERE is_enabled = ? AND next_run <= ?` + query := ` + SELECT + id, + run_every, + last_run, + slug, + next_run, + cron_state + FROM + tbl_cron + WHERE + is_enabled = ? + AND next_run <= ?` err := repo.db.Select(&tasks, query, 1, time.Now().Unix()) return tasks, err } func (repo *CronRepo) preRunTask(id int, lastRunTs int64) { - query := `UPDATE tbl_cron SET cron_state = ?, last_run = ? WHERE id = ?` + query := ` + UPDATE tbl_cron + SET + cron_state = ?, + last_run = ? + WHERE + id = ?` row, err := repo.db.Query(query, 1, lastRunTs, id) if err != nil { slog.Error(fmt.Sprintf("[CRON] Failed to update pre cron state: %s", err)) @@ -95,7 +123,14 @@ func (repo *CronRepo) preRunTask(id int, lastRunTs int64) { } func (repo *CronRepo) postRunTask(id int, nextRun int64, runtime float64) { - query := `UPDATE tbl_cron SET cron_state = ?, next_run = ?, run_time = ? WHERE id = ?` + query := ` + UPDATE tbl_cron + SET + cron_state = ?, + next_run = ?, + run_time = ? + WHERE + id = ?` row, err := repo.db.Query(query, 0, nextRun, runtime, id) if err != nil { slog.Error(fmt.Sprintf("[CRON] Failed to update post cron state: %s", err))