about summary refs log tree commit diff
path: root/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'cmd')
-rw-r--r--cmd/slocalc/README.md20
-rw-r--r--cmd/slocalc/main.go51
2 files changed, 71 insertions, 0 deletions
diff --git a/cmd/slocalc/README.md b/cmd/slocalc/README.md
new file mode 100644
index 0000000..660b653
--- /dev/null
+++ b/cmd/slocalc/README.md
@@ -0,0 +1,20 @@
+# slocalc
+
+A simple SLO calculator for the command line.
+
+## Usage
+
+```bash
+slocalc <availability>
+```
+
+## Example
+
+```bash
+% slocalc 99.9
+daily    : 0 days, 0 hours, 0 minutes, 4 seconds
+weekly   : 0 days, 0 hours, 0 minutes, 30 seconds
+monthly  : 0 days, 0 hours, 2 minutes, 9 seconds
+quarterly: 0 days, 0 hours, 6 minutes, 28 seconds
+yearly   : 0 days, 0 hours, 26 minutes, 16 seconds
+```
diff --git a/cmd/slocalc/main.go b/cmd/slocalc/main.go
new file mode 100644
index 0000000..ae4998b
--- /dev/null
+++ b/cmd/slocalc/main.go
@@ -0,0 +1,51 @@
+package main
+
+import (
+	"fmt"
+	"os"
+	"strconv"
+)
+
+const (
+	secondsPerHour = 3600
+)
+
+func main() {
+	if len(os.Args) <= 1 {
+		fmt.Println("Please provide a value")
+		os.Exit(1)
+	}
+
+	uptime, _ := strconv.ParseFloat(os.Args[1], 64)
+	switch {
+	case uptime <= 0:
+		uptime = 0
+	case uptime >= 100:
+		uptime = 100
+	}
+
+	allowedDowntime := (((100 * 100) - (uptime * 100)) / (100 * 100))
+
+	dailySeconds := secondsPerHour * 24 * allowedDowntime
+	weekSeconds := dailySeconds * 7
+	monthSeconds := dailySeconds * 30
+	quarterSeconds := dailySeconds * 90
+	yearSeconds := dailySeconds * 365
+
+	fmt.Printf("daily    : %s\n", secondsToHumanTime(int(dailySeconds)))
+	fmt.Printf("weekly   : %s\n", secondsToHumanTime(int(weekSeconds)))
+	fmt.Printf("monthly  : %s\n", secondsToHumanTime(int(monthSeconds)))
+	fmt.Printf("quarterly: %s\n", secondsToHumanTime(int(quarterSeconds)))
+	fmt.Printf("yearly   : %s\n", secondsToHumanTime(int(yearSeconds)))
+}
+
+// secondsToHumanTime converts seconds to human readable time
+func secondsToHumanTime(seconds int) string {
+	days := seconds / 86400
+	seconds = seconds % 86400
+	hours := seconds / 3600
+	seconds = seconds % 3600
+	minutes := seconds / 60
+	seconds = seconds % 60
+	return fmt.Sprintf("%d days, %d hours, %d minutes, %d seconds", days, hours, minutes, seconds)
+}