-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from flanksource/job-history
Add job history model
- Loading branch information
Showing
4 changed files
with
410 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module github.com/flanksource/duty | ||
|
||
go 1.19 | ||
|
||
require ( | ||
github.com/google/uuid v1.3.0 | ||
gorm.io/gorm v1.24.3 | ||
) | ||
|
||
require ( | ||
github.com/jinzhu/inflection v1.0.0 // indirect | ||
github.com/jinzhu/now v1.1.4 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= | ||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= | ||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= | ||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= | ||
github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas= | ||
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= | ||
gorm.io/gorm v1.24.3 h1:WL2ifUmzR/SLp85CSURAfybcHnGZ+yLSGSxgYXlFBHg= | ||
gorm.io/gorm v1.24.3/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package models | ||
|
||
import ( | ||
"os" | ||
"time" | ||
|
||
"github.com/flanksource/duty/types" | ||
"github.com/google/uuid" | ||
) | ||
|
||
const ( | ||
StatusRunning = "RUNNING" | ||
StatusFinished = "FINISHED" | ||
) | ||
|
||
type JobHistory struct { | ||
ID uuid.UUID `gorm:"default:generate_ulid()"` | ||
Name string | ||
SuccessCount int | ||
ErrorCount int | ||
Hostname string | ||
DurationMillis int64 | ||
ResourceType string | ||
ResourceID string | ||
Details types.JSONMap | ||
Status string | ||
TimeStart time.Time | ||
TimeEnd time.Time | ||
Errors []string `gorm:"-"` | ||
} | ||
|
||
func (h *JobHistory) Start() { | ||
h.TimeStart = time.Now() | ||
h.Status = StatusRunning | ||
h.Hostname, _ = os.Hostname() | ||
} | ||
|
||
func (h *JobHistory) End() { | ||
h.DurationMillis = time.Now().Sub(h.TimeStart).Milliseconds() | ||
h.Details = map[string]any{ | ||
"errors": h.Errors, | ||
} | ||
h.Status = StatusFinished | ||
} | ||
|
||
func (h *JobHistory) New(name, resourceType, resourceID string) { | ||
h.Name = name | ||
h.ResourceType = resourceType | ||
h.ResourceID = resourceID | ||
} | ||
|
||
func (h *JobHistory) AddError(err string) { | ||
h.ErrorCount += 1 | ||
if err != "" { | ||
h.Errors = append(h.Errors, err) | ||
} | ||
} | ||
|
||
func (h *JobHistory) IncrSuccess() { | ||
h.SuccessCount += 1 | ||
} |
Oops, something went wrong.