Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go mean time to repair #6250

Merged
merged 9 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/mean-time-to-repair/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module ministryofjustice/cloud-platform/cmd/mean-time-to-repair

go 1.23.1
134 changes: 134 additions & 0 deletions cmd/mean-time-to-repair/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package main

import (
"bufio"
"fmt"
"log"
"os"
"regexp"
"strconv"
"strings"
"time"
)

func readLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()

var lines []string
mikebell marked this conversation as resolved.
Show resolved Hide resolved
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}

func convertToRaw(data string) int {
s := strings.Split(data, " ")
total := 0

hours := 0
minutes := 0

for _, time := range s {
if strings.Contains(time, "h") {
time = strings.Replace(time, "h", "", -1)
i, err := strconv.Atoi(time)
if err != nil {
panic(err)
mikebell marked this conversation as resolved.
Show resolved Hide resolved
}
hours = convertHoursToMinutes(i)
}
if strings.Contains(time, "m") {
time = strings.Replace(time, "m", "", -1)
i, err := strconv.Atoi(time)
if err != nil {
panic(err)
}
minutes = i
}
}
total = hours + minutes
mikebell marked this conversation as resolved.
Show resolved Hide resolved

return total
}

func convertHoursToMinutes(i int) int {
i = i * 60
mikebell marked this conversation as resolved.
Show resolved Hide resolved
return i
}

func main() {
lines, err := readLines("../../runbooks/source/incident-log.html.md.erb")
if err != nil {
log.Fatalf("readLines: %s", err)
}

var data []string
mikebell marked this conversation as resolved.
Show resolved Hide resolved

var str strings.Builder

for _, line := range lines {
str.WriteString(line)

re := regexp.MustCompile(`---`)
match := re.FindString(line)

if match != "" {
data = append(data, str.String())
str.Reset()
}
}

for _, newline := range data {
reTitle := regexp.MustCompile(`(?U)## .\d \d* \(.*\)`)

title := reTitle.FindString((newline))

if title != "" {
fmt.Printf("%s\n", title)
}

re := regexp.MustCompile(`\*\*Time to repair\*\*: (\d*. \d*.|\d*.)`)
timeToRepair := 0
count := 0
for _, regmatch := range re.FindAllString(newline, -1) {
t := strings.Replace(regmatch, "**Time to repair**: ", "", -1)
timeToRepairTemp := convertToRaw(t)
timeToRepair = timeToRepair + timeToRepairTemp
count = count + 1
mikebell marked this conversation as resolved.
Show resolved Hide resolved
}

re2 := regexp.MustCompile(`\*\*Time to resolve\*\*: (\d*. \d*.|\d*.)`)
timeToResolve := 0
resolveCount := 0
for _, resolveMatch := range re2.FindAllString(newline, -1) {
t := strings.Replace(resolveMatch, "**Time to resolve**: ", "", -1)
timeToResolveTemp := convertToRaw(t)
timeToResolve = timeToResolve + timeToResolveTemp
resolveCount = resolveCount + 1
}

if count != 0 {
meanTimeToRepair := timeToRepair / count
d := time.Duration(meanTimeToRepair) * time.Minute
hours := int(d.Hours())
minutes := int(d.Minutes()) % 60
fmt.Printf("Incidents:%2d\n", count)
fmt.Printf("Mean time to repair: %2dh %02dm\n", hours, minutes)
}

if resolveCount != 0 {
meantTimeToResolve := timeToResolve / resolveCount
d := time.Duration(meantTimeToResolve) * time.Minute
hours := int(d.Hours())
minutes := int(d.Minutes()) % 60
fmt.Printf("Mean time to resolve: %2dh %02dm", hours, minutes)
fmt.Println("\n")
}

}
}
135 changes: 0 additions & 135 deletions runbooks/bin/mean-time-to-repair.rb

This file was deleted.

Loading
Loading