|
- package model
- //
- // Fengg Security Gateway Server Application
- // Copyright (C) 2020 Lukas Matt <support@fen.gg>
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <https://www.gnu.org/licenses/>.
- //
- import (
- "time"
- "fmt"
-
- "github.com/jmoiron/sqlx"
- _ "github.com/lib/pq"
- )
-
- const (
- WatcherResultNamedInsertTmpl = `
- INSERT INTO watcher_results (
- created_at, updated_at, worker_name, alive, retries, error
- ) VALUES (
- now(), now(), :worker_name, :alive, :retries, :error
- ) RETURNING id;`
-
- WatcherResultNamedUpdateTmpl = `
- UPDATE watcher_results
- SET updated_at=now(), alive=:alive, retries=:retries, error=:error
- WHERE id=:id
- OR worker_name=:worker_name;`
-
- WatcherResultResetRetriesTmpl = `UPDATE watcher_results SET retries=0 WHERE retries > 0;`
-
- WatcherResultQueryTmpl = `SELECT * FROM watcher_results %s;`
- )
-
- type WatcherResult struct {
- ID uint `db:"id" json:"id"`
- CreatedAt time.Time `db:"created_at" json:"createdAt"`
- UpdatedAt time.Time `db:"updated_at" json:"updatedAt"`
-
- WorkerName string `db:"worker_name" json:"workerName"`
- Alive bool `db:"alive" json:"alive"`
- Retries int `db:"retries" json:"retries"`
- Error *string `db:"error" json:"error,omitempty"`
- }
-
- type WatcherResults []WatcherResult
-
- func NewWatcherResult() *WatcherResult {
- return &WatcherResult{CreatedAt: time.Now()}
- }
-
- func (results *WatcherResults) FindAll() error {
- db, err := sqlx.Connect(dbDriver, dbConnect)
- if err != nil {
- return err
- }
- defer db.Close()
-
- return db.Select(results, fmt.Sprintf(WatcherResultQueryTmpl, ""))
- }
-
- func (result *WatcherResult) Create() error {
- db, err := sqlx.Connect(dbDriver, dbConnect)
- if err != nil {
- return err
- }
- defer db.Close()
-
- rows, err := db.NamedQuery(WatcherResultNamedInsertTmpl, result)
- if err != nil {
- return err
- }
- defer rows.Close()
-
- for rows.Next() {
- err = rows.Scan(&result.ID)
- if err != nil {
- return err
- }
- }
- return nil
- }
-
- func (result *WatcherResult) Exists() bool {
- db, err := sqlx.Connect(dbDriver, dbConnect)
- if err != nil {
- logger.Error().Err(err).Msg("cannot connect to database")
- return false
- }
- defer db.Close()
-
- return db.Get(result,
- fmt.Sprintf(WatcherResultQueryTmpl, `WHERE worker_name=$1 OR id=$2`),
- result.WorkerName, result.ID,
- ) == nil
- }
-
- func (result *WatcherResult) Update() error {
- db, err := sqlx.Connect(dbDriver, dbConnect)
- if err != nil {
- return err
- }
- defer db.Close()
-
- _, err = db.NamedExec(WatcherResultNamedUpdateTmpl, result)
- return err
- }
-
- func (results *WatcherResults) ResetRetries() error {
- db, err := sqlx.Connect(dbDriver, dbConnect)
- if err != nil {
- return err
- }
- defer db.Close()
-
- _, err = db.Exec(WatcherResultResetRetriesTmpl)
- return err
- }
|