add time duration to cluster

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-10-31 04:52:14 -05:00
parent 96f29d6f3b
commit 67cb013c83
5 changed files with 82 additions and 9 deletions

13
add.go
View File

@ -2,8 +2,10 @@ package virtbuf
import (
"fmt"
"time"
"github.com/google/uuid"
"go.wit.com/log"
)
// can the json protobuf output use a string and have a type handler
@ -129,6 +131,17 @@ func (epb *Events) AppendEvent(e *Event) {
epb.Events = append(epb.Events, e)
}
func (c *Cluster) ClusterStable() (bool, string) {
last := time.Since(c.Unstable.AsTime())
if last > c.UnstableTimeout.AsDuration() {
// the cluster has not been stable for 133 seconds
log.Warn("clusterReady() is stable for ", FormatDuration(c.UnstableTimeout.AsDuration()), " secs")
return true, fmt.Sprintln("clusterReady() is stable ", FormatDuration(c.UnstableTimeout.AsDuration()), " secs")
}
log.Warn("clusterReady() is unstable for", FormatDuration(last))
return false, "clusterReady() is unstable for " + FormatDuration(last)
}
// check the cluster and droplet to make sure it's ready to start
func (c *Cluster) DropletReady(d *Droplet) (bool, string) {
if c == nil {

View File

@ -4,6 +4,8 @@ package virtbuf;
import "droplet.proto";
import "hypervisor.proto";
import "event.proto";
import "google/protobuf/timestamp.proto"; // Import the well-known type for Timestamp
import "google/protobuf/duration.proto"; // Import the well-known type for Timestamp
message Cluster {
int64 id = 1;
@ -16,4 +18,6 @@ message Cluster {
Droplets d = 6;
Hypervisors h = 7;
Events e = 8;
google.protobuf.Timestamp unstable = 9; // the last time we heard anything from this droplet
google.protobuf.Duration unstable_timeout = 10; // the last time we heard anything from this droplet
}

View File

@ -1,7 +1,7 @@
syntax = "proto3";
package virtbuf;
import "google/protobuf/any.proto";
// import "google/protobuf/any.proto";
import "google/protobuf/timestamp.proto"; // Import the well-known type for Timestamp
message Droplets {

View File

@ -1,8 +0,0 @@
syntax = "proto3";
package virtbuf;
message Cluster {
int64 id = 1;
repeated string s = 2;
repeated int i = 3;
}

64
time.go Normal file
View File

@ -0,0 +1,64 @@
package virtbuf
import (
"fmt"
"time"
)
func FormatDuration(d time.Duration) string {
result := ""
// check if it's more than a year
years := int(d.Hours()) / (24 * 365)
if years > 0 {
result += fmt.Sprintf("%dy ", years)
return result
}
// check if it's more than a day
days := int(d.Hours()) / 24
if days > 0 {
result += fmt.Sprintf("%dd ", days)
return result
}
// check if it's more than an hour
hours := int(d.Hours()) % 24
if hours > 0 {
result += fmt.Sprintf("%dh ", hours)
return result
}
// check if it's more than a minute
minutes := int(d.Minutes()) % 60
if minutes > 0 {
result += fmt.Sprintf("%dm ", minutes)
return result
}
// check if it's more than a second
seconds := int(d.Seconds()) % 60
if seconds > 0 {
result += fmt.Sprintf("%ds", seconds)
return result
}
// report in milliseconds
ms := int(d.Milliseconds())
if ms > 100 {
// todo: print .3s, etc ?
return fmt.Sprintf("%1.2fs", seconds/1000)
}
result += fmt.Sprintf("%dms", ms)
return result
}
func GetDurationStamp(t time.Time) string {
// Get the current time
currentTime := time.Now()
// Calculate the duration between t current time
duration := currentTime.Sub(t)
return FormatDuration(duration)
}