Created
January 27, 2018 00:03
-
-
Save itsanna/5ad74f375af65e95d1cf1c48b26e5ac9 to your computer and use it in GitHub Desktop.
architectureGraphDataGenerator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"encoding/csv" | |
"fmt" | |
"io" | |
"os" | |
"strings" | |
"log" | |
"strconv" | |
"flag" | |
) | |
type Requests map[string]map[string]int | |
func ParseCsv(filename string) ([][]string, error) { | |
csvFile, err := os.Open(filename) | |
if err != nil { | |
return nil, err | |
} | |
defer csvFile.Close() | |
r := csv.NewReader(bufio.NewReader(csvFile)) | |
lines := [][]string{} | |
for { | |
record, err := r.Read() | |
if err == io.EOF { | |
break | |
} | |
if err != nil { | |
return nil, err | |
} | |
lines = append(lines, record) | |
} | |
return lines, nil | |
} | |
const numOfService = 8 | |
func main() { | |
logMessagesFile := parseArgs() | |
records, _ := ParseCsv(logMessagesFile) | |
fmt.Printf("Parsed [%v] records from csv file", len(records)) | |
getServiceIndex := make(map[string]int) | |
getServiceName := make(map[int]string) | |
for i := 1; i < len(records); i++ { | |
raw := records[i][2] | |
rawStrings := strings.Split(raw, `caller":"`) | |
rawSplitByQuote := strings.Split(rawStrings[1], `"`) | |
source := rawSplitByQuote[0] | |
target := records[i][6] | |
if _, ok := getServiceIndex[source]; !ok { | |
length := len(getServiceIndex) | |
getServiceIndex[source] = length | |
getServiceName[length] = source | |
} | |
if _, ok := getServiceIndex[target]; !ok { | |
length := len(getServiceIndex) | |
getServiceIndex[target] = length | |
getServiceName[length] = target | |
} | |
} | |
fmt.Println("length of getServiceIndex -->>>", len(getServiceIndex)) | |
var matrix [numOfService][numOfService]int | |
for i := 1; i < len(records); i++ { | |
raw := records[i][2] | |
rawStrings := strings.Split(raw, `caller":"`) | |
rawSplitByQuote := strings.Split(rawStrings[1], `"`) | |
source := rawSplitByQuote[0] | |
target := records[i][6] | |
matrix[getServiceIndex[source]][getServiceIndex[target]]++ | |
} | |
fmt.Println() | |
for i := 0; i < numOfService; i++ { | |
fmt.Print(",", getServiceName[i]) | |
} | |
fmt.Println() | |
for i := 0; i < numOfService; i++ { | |
fmt.Print(getServiceName[i]) | |
for j := 0; j < numOfService; j++ { | |
fmt.Print(",", matrix[i][j]) | |
} | |
fmt.Println() | |
} | |
var outputStrings [numOfService + 1][numOfService + 1]string | |
for i := 1; i < numOfService+1; i++ { | |
outputStrings[0][i] = getServiceName[i-1] | |
outputStrings[i][0] = getServiceName[i-1] | |
} | |
for i := 1; i < numOfService+1; i++ { | |
for j := 1; j < numOfService+1; j++ { | |
outputStrings[i][j] = strconv.Itoa(matrix[i-1][j-1]) | |
} | |
} | |
file, err := os.Create("result.csv") | |
checkError("Cannot create file", err) | |
defer file.Close() | |
writer := csv.NewWriter(file) | |
defer writer.Flush() | |
for _, value := range outputStrings { | |
strArray := toStringArray(value) | |
err := writer.Write(strArray) | |
checkError("Cannot write to file", err) | |
} | |
} | |
func toStringArray(input [numOfService + 1]string) []string { | |
arr := []string{} | |
for _, item := range input { | |
arr = append(arr, item) | |
} | |
return arr | |
} | |
func checkError(message string, err error) { | |
if err != nil { | |
log.Fatal(message, err) | |
} | |
} | |
func parseArgs() string { | |
logMessagesFilePtr := flag.String("logFile", "", "Relative path to the file containing Sumo Logic logs") | |
flag.Parse() | |
logMessagesFile := *logMessagesFilePtr | |
if logMessagesFile == "" { | |
log.Fatal("Must specify a csv file") | |
} | |
return logMessagesFile | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment