Monitor With Data Storage + Datas
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/acarl005/stripansi"
|
||||
)
|
||||
|
||||
type ArgoWatch struct {
|
||||
@@ -13,6 +18,7 @@ type ArgoWatch struct {
|
||||
Started string
|
||||
Duration string
|
||||
Progress string
|
||||
Logs []string
|
||||
}
|
||||
|
||||
type Conditions struct {
|
||||
@@ -20,53 +26,103 @@ type Conditions struct {
|
||||
Completed bool
|
||||
}
|
||||
|
||||
func (a *ArgoWatch) Equals(arg ArgoWatch) bool {
|
||||
func (a *ArgoWatch) Equals(arg *ArgoWatch) bool {
|
||||
if arg == nil {
|
||||
return false
|
||||
}
|
||||
return a.Status == arg.Status && a.Progress == arg.Progress && a.Conditions.PodRunning == arg.Conditions.PodRunning && a.Conditions.Completed == arg.Conditions.Completed
|
||||
}
|
||||
|
||||
// Take the slice of string that make up one round of stderr outputs from the --watch option in argo submit
|
||||
func NewArgoLogs(inputs []string) *ArgoWatch {
|
||||
var workflow ArgoWatch
|
||||
func NewArgoLogs(name string, namespace string, stepMax int) *ArgoLogs {
|
||||
return &ArgoLogs{
|
||||
Name: "oc-monitor-" + name,
|
||||
Namespace: namespace,
|
||||
CreatedDate: time.Now().Format("2006-01-02 15:04:05"),
|
||||
StepCount: 0,
|
||||
StepMax: stepMax,
|
||||
stop: false,
|
||||
}
|
||||
}
|
||||
|
||||
type ArgoLogs struct {
|
||||
Name string
|
||||
Namespace string
|
||||
CreatedDate string
|
||||
StepCount int
|
||||
StepMax int
|
||||
stop bool
|
||||
Started time.Time
|
||||
}
|
||||
|
||||
func (a *ArgoLogs) StartStepRecording() {
|
||||
a.StepCount += 1
|
||||
a.Started = time.Now()
|
||||
}
|
||||
|
||||
func (a *ArgoLogs) StopStepRecording(inputs []string) *ArgoWatch {
|
||||
fn := strings.Split(a.Name, "_")
|
||||
logs := []string{}
|
||||
err := false
|
||||
end := ""
|
||||
for _, input := range inputs {
|
||||
line := strings.TrimSpace(input)
|
||||
if line == "" {
|
||||
if line == "" || !strings.Contains(line, fn[0]) || !strings.Contains(line, ":") {
|
||||
continue
|
||||
}
|
||||
switch {
|
||||
case strings.HasPrefix(line, "Name:"):
|
||||
workflow.Name = parseValue(line)
|
||||
case strings.HasPrefix(line, "Namespace:"):
|
||||
workflow.Namespace = parseValue(line)
|
||||
case strings.HasPrefix(line, "Status:"):
|
||||
workflow.Status = parseValue(line)
|
||||
case strings.HasPrefix(line, "PodRunning"):
|
||||
workflow.PodRunning = parseBoolValue(line)
|
||||
case strings.HasPrefix(line, "Completed"):
|
||||
workflow.Completed = parseBoolValue(line)
|
||||
case strings.HasPrefix(line, "Created:"):
|
||||
workflow.Created = parseValue(line)
|
||||
case strings.HasPrefix(line, "Started:"):
|
||||
workflow.Started = parseValue(line)
|
||||
case strings.HasPrefix(line, "Duration:"):
|
||||
workflow.Duration = parseValue(line)
|
||||
case strings.HasPrefix(line, "Progress:"):
|
||||
workflow.Progress = parseValue(line)
|
||||
step := strings.Split(line, ":")
|
||||
if strings.Contains(line, "sub-process exited") {
|
||||
b := strings.Split(line, "time=\"")
|
||||
if len(b) > 1 {
|
||||
end = b[1][:19]
|
||||
}
|
||||
}
|
||||
if len(step) < 2 || strings.Contains(line, "time=") || strings.TrimSpace(strings.Join(step[1:], " : ")) == "" || strings.TrimSpace(strings.Join(step[1:], " : ")) == a.Name {
|
||||
continue
|
||||
}
|
||||
log := stripansi.Strip(strings.TrimSpace(strings.Join(step[1:], " : ")))
|
||||
t, e := strconv.Unquote(log)
|
||||
if e == nil {
|
||||
logs = append(logs, t)
|
||||
} else {
|
||||
logs = append(logs, strings.ReplaceAll(log, "\"", "`"))
|
||||
}
|
||||
|
||||
if strings.Contains(logs[len(logs)-1], "Error") {
|
||||
err = true
|
||||
}
|
||||
}
|
||||
|
||||
return &workflow
|
||||
}
|
||||
|
||||
func parseValue(line string) string {
|
||||
parts := strings.SplitN(line, ":", 2)
|
||||
if len(parts) < 2 {
|
||||
return ""
|
||||
status := "Pending"
|
||||
if a.StepCount > 0 {
|
||||
status = "Running"
|
||||
}
|
||||
return strings.TrimSpace(parts[1])
|
||||
}
|
||||
|
||||
func parseBoolValue(line string) bool {
|
||||
value := parseValue(line)
|
||||
return value == "True"
|
||||
if a.StepCount == a.StepMax {
|
||||
if err {
|
||||
status = "Failed"
|
||||
} else {
|
||||
status = "Succeeded"
|
||||
}
|
||||
}
|
||||
duration := float64(0)
|
||||
if end != "" {
|
||||
timeE, _ := time.Parse("2006-01-02T15:04:05", end)
|
||||
duration = timeE.Sub(a.Started).Seconds()
|
||||
}
|
||||
argo := &ArgoWatch{
|
||||
Name: a.Name,
|
||||
Namespace: a.Namespace,
|
||||
Status: status,
|
||||
Created: a.CreatedDate,
|
||||
Started: a.Started.Format("2006-01-02 15:04:05"),
|
||||
Conditions: Conditions{
|
||||
PodRunning: a.StepCount > 0 && a.StepCount < a.StepMax,
|
||||
Completed: a.StepCount == a.StepMax,
|
||||
},
|
||||
Progress: fmt.Sprintf("%v/%v", a.StepCount, a.StepMax),
|
||||
Duration: fmt.Sprintf("%v", fmt.Sprintf("%.2f", duration)+"s"),
|
||||
Logs: logs,
|
||||
}
|
||||
if !argo.Completed {
|
||||
a.StartStepRecording()
|
||||
}
|
||||
return argo
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user