A More Secure Internet Connection for Your Home https://fen.gg
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

129 lines
3.2 KiB

  1. package model
  2. //
  3. // Fengg Security Gateway Server Application
  4. // Copyright (C) 2020 Lukas Matt <support@fen.gg>
  5. //
  6. // This program is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. //
  18. import (
  19. "time"
  20. "fmt"
  21. "github.com/jmoiron/sqlx"
  22. _ "github.com/lib/pq"
  23. )
  24. const (
  25. WatcherResultNamedInsertTmpl = `
  26. INSERT INTO watcher_results (
  27. created_at, updated_at, worker_name, alive, retries, error
  28. ) VALUES (
  29. now(), now(), :worker_name, :alive, :retries, :error
  30. ) RETURNING id;`
  31. WatcherResultNamedUpdateTmpl = `
  32. UPDATE watcher_results
  33. SET updated_at=now(), alive=:alive, retries=:retries, error=:error
  34. WHERE id=:id
  35. OR worker_name=:worker_name;`
  36. WatcherResultResetRetriesTmpl = `UPDATE watcher_results SET retries=0 WHERE retries > 0;`
  37. WatcherResultQueryTmpl = `SELECT * FROM watcher_results %s;`
  38. )
  39. type WatcherResult struct {
  40. ID uint `db:"id" json:"id"`
  41. CreatedAt time.Time `db:"created_at" json:"createdAt"`
  42. UpdatedAt time.Time `db:"updated_at" json:"updatedAt"`
  43. WorkerName string `db:"worker_name" json:"workerName"`
  44. Alive bool `db:"alive" json:"alive"`
  45. Retries int `db:"retries" json:"retries"`
  46. Error *string `db:"error" json:"error,omitempty"`
  47. }
  48. type WatcherResults []WatcherResult
  49. func NewWatcherResult() *WatcherResult {
  50. return &WatcherResult{CreatedAt: time.Now()}
  51. }
  52. func (results *WatcherResults) FindAll() error {
  53. db, err := sqlx.Connect(dbDriver, dbConnect)
  54. if err != nil {
  55. return err
  56. }
  57. defer db.Close()
  58. return db.Select(results, fmt.Sprintf(WatcherResultQueryTmpl, ""))
  59. }
  60. func (result *WatcherResult) Create() error {
  61. db, err := sqlx.Connect(dbDriver, dbConnect)
  62. if err != nil {
  63. return err
  64. }
  65. defer db.Close()
  66. rows, err := db.NamedQuery(WatcherResultNamedInsertTmpl, result)
  67. if err != nil {
  68. return err
  69. }
  70. defer rows.Close()
  71. for rows.Next() {
  72. err = rows.Scan(&result.ID)
  73. if err != nil {
  74. return err
  75. }
  76. }
  77. return nil
  78. }
  79. func (result *WatcherResult) Exists() bool {
  80. db, err := sqlx.Connect(dbDriver, dbConnect)
  81. if err != nil {
  82. logger.Error().Err(err).Msg("cannot connect to database")
  83. return false
  84. }
  85. defer db.Close()
  86. return db.Get(result,
  87. fmt.Sprintf(WatcherResultQueryTmpl, `WHERE worker_name=$1 OR id=$2`),
  88. result.WorkerName, result.ID,
  89. ) == nil
  90. }
  91. func (result *WatcherResult) Update() error {
  92. db, err := sqlx.Connect(dbDriver, dbConnect)
  93. if err != nil {
  94. return err
  95. }
  96. defer db.Close()
  97. _, err = db.NamedExec(WatcherResultNamedUpdateTmpl, result)
  98. return err
  99. }
  100. func (results *WatcherResults) ResetRetries() error {
  101. db, err := sqlx.Connect(dbDriver, dbConnect)
  102. if err != nil {
  103. return err
  104. }
  105. defer db.Close()
  106. _, err = db.Exec(WatcherResultResetRetriesTmpl)
  107. return err
  108. }