summaryrefslogtreecommitdiff
path: root/human.go
blob: e1fffefb4c64025f309988da7e31ba25c3bc1110 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package virtbuf

// mostly just functions related to making STDOUT
// more readable by us humans

// also function shortcuts the do limited formatting (haha, who remembers COBOL?)
// so reporting tables of the status of what droplets and hypervisors
// are in text columns and rows that can be easily read in a terminal

import (
	"errors"
	"fmt"
	"net/http"
	"strings"
	"time"

	"go.wit.com/log"
)

func oldGetDurationStamp(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)
}

// This isn't for the marketing department
// so this isn't going to use 'MiB' and 'GiB'
func HumanFormatBytes(b int64) string {
	if b < 2000 {
		return fmt.Sprintf("%d B", b)
	}

	kb := int(b / 1024)
	if kb < 2000 {
		return fmt.Sprintf("%d KB", kb)
	}

	mb := int(b / (1024 * 1024))
	if mb < 2000 {
		return fmt.Sprintf("%d MB", mb)
	}

	gb := int(b / (1024 * 1024 * 1024))
	if gb < 2000 {
		return fmt.Sprintf("%d GB", gb)
	}

	tb := int(b / (1024 * 1024 * 1024 * 1024))
	return fmt.Sprintf("%d TB", tb)
}

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)
	}
	if ms > 0 {
		result += fmt.Sprintf("%dms", ms)
	}

	// totally not necessary but wth
	var t time.Duration
	t = time.Duration(ms) * time.Millisecond
	nanos := d - t
	result += fmt.Sprintf("%dnanos", nanos)
	return result
}

func (d *Droplet) SprintHeader() string {
	if d.Current == nil {
		d.Current = new(Current)
	}
	header := fmt.Sprintf("%-3.3s %-9.9s %-20.20s", d.Current.State, d.Current.Hypervisor, d.Hostname)

	switch d.Current.State {
	case DropletState_ON:
		var dur string
		if d.Current.OnSince != nil {
			dur = ""
		} else {
			t := time.Since(d.Current.OnSince.AsTime()) // time since 'OFF'
			dur = FormatDuration(t)
		}
		header += fmt.Sprintf(" (on :%3s)", dur)
	case DropletState_OFF:
		var dur string
		if d.Current.OffSince != nil {
			dur = ""
		} else {
			t := time.Since(d.Current.OffSince.AsTime()) // time since 'OFF'
			dur = FormatDuration(t)
		}
		header += fmt.Sprintf(" (off:%3s)", dur)
	default:
		header += fmt.Sprintf(" (?? :%3s)", "")
	}
	return header
}

func (d *Droplet) SprintDumpHeader() string {
	var macs []string
	for _, n := range d.Networks {
		macs = append(macs, n.Mac)
	}

	// this line in golang could replace 80 lines of COBOL
	header := fmt.Sprintf("%-4.4s%20s %-8s", d.Current.State, strings.Join(macs, " "), d.Current.Hypervisor)

	if d.Current == nil {
		d.Current = new(Current)
	}

	switch d.Current.State {
	case DropletState_ON:
		var dur string
		if d.Current.OnSince != nil {
			dur = ""
		} else {
			t := time.Since(d.Current.OnSince.AsTime()) // time since 'ON'
			dur = FormatDuration(t)
		}
		header += fmt.Sprintf(" (on :%3s)", dur)
	case DropletState_OFF:
		var dur string
		if d.Current.OffSince != nil {
			dur = ""
		} else {
			t := time.Since(d.Current.OffSince.AsTime()) // time since 'OFF'
			dur = FormatDuration(t)
		}
		header += fmt.Sprintf(" (off:%3s)", dur)
	default:
		header += fmt.Sprintf(" (?? :%3s)", "")
	}
	return header
}

func (d *Droplet) DumpDroplet(w http.ResponseWriter, r *http.Request) (string, error) {
	if d == nil {
		reason := "DumpDroplet() got d == nil"
		log.Warn(reason)
		fmt.Fprintln(w, reason)
		return "", errors.New(reason)
	}
	t := d.FormatTEXT()
	log.Info(t)
	fmt.Fprintln(w, t)
	return t, nil
}

func (c *Cluster) DumpDroplet(w http.ResponseWriter, r *http.Request) (string, error) {
	hostname := r.URL.Query().Get("hostname")
	d := c.FindDropletByName(hostname)
	if d == nil {
		result := "can not find droplet hostname=" + hostname
		log.Info(result)
		fmt.Fprintln(w, result)
		return result, errors.New(result)
	}
	return d.DumpDroplet(w, r)
}