Skip to content

Commit

Permalink
Merge pull request #3 from flanksource/job-history
Browse files Browse the repository at this point in the history
Add job history model
  • Loading branch information
moshloop authored Jan 9, 2023
2 parents fa4f03c + f70c0e7 commit 0aea9d5
Show file tree
Hide file tree
Showing 4 changed files with 410 additions and 0 deletions.
13 changes: 13 additions & 0 deletions go.mod
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
)
8 changes: 8 additions & 0 deletions go.sum
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=
61 changes: 61 additions & 0 deletions models/job_history.go
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
}
Loading

0 comments on commit 0aea9d5

Please sign in to comment.