Skip to content

Commit

Permalink
add duration format
Browse files Browse the repository at this point in the history
  • Loading branch information
datewu committed Sep 11, 2023
1 parent 45aa940 commit 1e70e00
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
31 changes: 26 additions & 5 deletions front/table.tpl.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package front

import (
"fmt"
"html/template"
"io"
"time"
Expand Down Expand Up @@ -45,16 +46,36 @@ type Resource struct {
Containers []Container
Name string
Replicas int
Age time.Duration
Age string
}

func (r *Resource) formatAge(d time.Duration) {
age := ""
if d.Hours() > 24 {
days := d.Hours() / 24
if days > 365 {
years := days / 365
y := int(years)
age = fmt.Sprintf("%dy", y)
d -= time.Duration(y*365*24) * time.Hour
days = d.Hours() / 24
}
if days > 1 {
i := int(days)
age = fmt.Sprintf("%dd", i)
d -= time.Duration(i*24) * time.Hour
}
}
age += d.String()
}

func newDeployResource(d *apps.Deployment) *Resource {
res := &Resource{
Name: d.Name,
Replicas: int(*d.Spec.Replicas),
Age: time.Now().Round(time.Second).
Sub((d.ObjectMeta.GetCreationTimestamp().Round(time.Second))),
}
res.formatAge(time.Now().Round(time.Second).
Sub((d.ObjectMeta.GetCreationTimestamp().Round(time.Second))))
containes := d.Spec.Template.Spec.Containers
cs := make([]Container, len(containes))
for i, c := range containes {
Expand All @@ -70,9 +91,9 @@ func newStsResource(s *apps.StatefulSet) *Resource {
res := &Resource{
Name: s.Name,
Replicas: int(*s.Spec.Replicas),
Age: time.Now().Round(time.Second).
Sub((s.ObjectMeta.GetCreationTimestamp().Round(time.Second))),
}
res.formatAge(time.Now().Round(time.Second).
Sub((s.ObjectMeta.GetCreationTimestamp().Round(time.Second))))
containes := s.Spec.Template.Spec.Containers
cs := make([]Container, len(containes))
for i, c := range containes {
Expand Down
2 changes: 0 additions & 2 deletions internal/k8s/deploy_crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package k8s
import (
"context"
"errors"
"fmt"
"time"

"github.com/datewu/gtea/jsonlog"
Expand Down Expand Up @@ -86,7 +85,6 @@ func setDeployImg(id *ContainerPath, labels ...string) error {
}
if labels != nil {
ls := d.GetLabels()
fmt.Println("getLabes:", ls)
if err = checkLabels(ls, labels); err != nil {
return err
}
Expand Down

0 comments on commit 1e70e00

Please sign in to comment.