Skip to content

Commit

Permalink
Merge pull request #5 from MinaFoundation/PM-322-final-itn-analyzer-fix
Browse files Browse the repository at this point in the history
PM 322 final itn analyzer fix
  • Loading branch information
Smorci authored Oct 16, 2023
2 parents df3f16f + 366e2eb commit cdf8cf0
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 58 deletions.
8 changes: 4 additions & 4 deletions src/itn_uptime_analyzer/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
// The graphQLPort parameter should be optional but Golang doesn't permit that
// As a workaround we use a pointer that can be defined as nil
type Identity struct {
id, publicKey, publicIp, uptime string
graphQLPort *string
id, publicKey, publicIp string
graphQLPort, uptime *string
}

// Custom function to check if identity is in array
Expand Down Expand Up @@ -117,7 +117,7 @@ func GetFullIdentity(pubKey string, ip string, graphqlPort string) Identity {
id: hex.EncodeToString(id[:]),
publicKey: pubKey,
publicIp: ip,
uptime: "",
uptime: new(string),
graphQLPort: &graphqlPort,
}

Expand All @@ -134,7 +134,7 @@ func GetPartialIdentity(pubKey string, ip string) Identity {
id: hex.EncodeToString(id[:]),
publicKey: pubKey,
publicIp: ip,
uptime: "",
uptime: new(string),
}

return identity
Expand Down
14 changes: 6 additions & 8 deletions src/itn_uptime_analyzer/sheets.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (identity Identity) GetCell(config AppConfig, client *sheets.Service, log *
log.Fatalf("Unable to retrieve data from sheet: %v", err)
}

if *identity.graphQLPort != "" {
if identity.graphQLPort != nil {
identityString = strings.Join([]string{identity.publicKey, identity.publicIp, *identity.graphQLPort}, "-")
} else {
identityString = strings.Join([]string{identity.publicKey, identity.publicIp}, "-")
Expand Down Expand Up @@ -60,7 +60,7 @@ func (identity Identity) AppendNext(config AppConfig, client *sheets.Service, lo
spId := config.AnalyzerOutputGsheetId
var identityString string

if *identity.graphQLPort != "" {
if identity.graphQLPort != nil {
identityString = strings.Join([]string{identity.publicKey, identity.publicIp, *identity.graphQLPort}, "-")
} else {
identityString = strings.Join([]string{identity.publicKey, identity.publicIp}, "-")
Expand All @@ -85,7 +85,7 @@ func (identity Identity) InsertBelow(config AppConfig, client *sheets.Service, l
spId := config.AnalyzerOutputGsheetId
var identityString string

if *identity.graphQLPort != "" {
if identity.graphQLPort != nil {
identityString = strings.Join([]string{identity.publicKey, identity.publicIp, *identity.graphQLPort}, "-")
} else {
identityString = strings.Join([]string{identity.publicKey, identity.publicIp}, "-")
Expand Down Expand Up @@ -115,10 +115,8 @@ func (identity Identity) AppendUptime(config AppConfig, client *sheets.Service,

var nextEmptyColumn int = len(resp.Values[0])

updateRange := fmt.Sprintf("%s!%s%d", sheetTitle, string(nextEmptyColumn+65), rowIndex)

cellValue := []interface{}{strings.Join([]string{identity.uptime, "%"}, "")}

updateRange := fmt.Sprintf("%s!%s%d", sheetTitle, string(nextEmptyColumn+LETTER_A_ASCII_CODE), rowIndex)
cellValue := []interface{}{strings.Join([]string{*identity.uptime, "%"}, "")}
valueRange := sheets.ValueRange{
Values: [][]interface{}{cellValue},
}
Expand Down Expand Up @@ -197,7 +195,7 @@ func MarkExecution(config AppConfig, client *sheets.Service, log *logging.ZapEve

var nextEmptyColumn int = len(resp.Values[0])

updateRange := fmt.Sprintf("%s!%s%d", sheetTitle, string(nextEmptyColumn+65), 1)
updateRange := fmt.Sprintf("%s!%s%d", sheetTitle, string(nextEmptyColumn+LETTER_A_ASCII_CODE), 1)

cellValue := []interface{}{timeInterval}

Expand Down
2 changes: 1 addition & 1 deletion src/itn_uptime_analyzer/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func IdentifyWeek(config AppConfig, client *sheets.Service, log *logging.ZapEven

lastSheet := outputSheets[len(outputSheets)-1]
lastSheetSplit := strings.SplitAfter(lastSheet.Properties.Title, " - ")
lastSheetEndTime, err := time.Parse(lastSheetSplit[len(lastSheetSplit)-1], time.RFC3339)
lastSheetEndTime, err := time.Parse("2006-01-02", lastSheetSplit[len(lastSheetSplit)-1])
if err != nil {
log.Fatalf("Error parsing time: %v\n", err)
}
Expand Down
110 changes: 65 additions & 45 deletions src/itn_uptime_analyzer/uptime.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ func (identity Identity) GetUptime(config AppConfig, sheet *sheets.Service, ctx
if err != nil {
log.Fatalf("Error parsing time: %v\n", err)
}

//Open json file only if the pubkey matches the pubkey in the name
if regex.MatchString(*obj.Key) {
if (submissionTime.After(lastExecutionTime)) && (submissionTime.Before(currentTime)) {
Expand Down Expand Up @@ -82,56 +81,65 @@ func (identity Identity) GetUptime(config AppConfig, sheet *sheets.Service, ctx

if submissionDataToday.GraphqlControlPort != 0 {
if (identity.publicKey == submissionDataToday.Submitter.String()) && (identity.publicIp == submissionDataToday.RemoteAddr) && (*identity.graphQLPort == strconv.Itoa(submissionDataToday.GraphqlControlPort)) {
if lastSubmissionTimeString != "" {
lastSubmissionTime, err = time.Parse(time.RFC3339, lastSubmissionTimeString)
if err != nil {
log.Fatalf("Error parsing time: %v\n", err)
}
}

currentSubmissionTime, err := time.Parse(time.RFC3339, submissionDataToday.CreatedAt)
if err != nil {
log.Fatalf("Error parsing time: %v\n", err)
}

if (lastSubmissionTimeString != "") && (currentSubmissionTime.After(lastSubmissionTime.Add(time.Duration(syncPeriod-5) * time.Minute))) && (currentSubmissionTime.Before(lastSubmissionTime.Add(time.Duration(syncPeriod+5) * time.Minute))) {
uptimeYesterday = append(uptimeYesterday, true)
if lastSubmissionTimeString != "" {
lastSubmissionTime, err = time.Parse(time.RFC3339, lastSubmissionTimeString)
if err != nil {
log.Fatalf("Error parsing time: %v\n", err)
}
} else {
uptimeToday = append(uptimeToday, true)
lastSubmissionTimeString = submissionDataToday.CreatedAt
} else if lastSubmissionTimeString == "" {
uptimeYesterday = append(uptimeYesterday, true)
continue
}

if (lastSubmissionTimeString != "") && (currentSubmissionTime.After(lastSubmissionTime.Add(time.Duration(syncPeriod-5) * time.Minute))) {
uptimeToday = append(uptimeToday, true)
lastSubmissionTimeString = submissionDataToday.CreatedAt
continue
} else {
continue
}
}
} else {
if (identity.publicKey == submissionDataToday.Submitter.String()) && (identity.publicIp == submissionDataToday.RemoteAddr) {

currentSubmissionTime, err := time.Parse(time.RFC3339, submissionDataToday.CreatedAt)
if err != nil {
log.Fatalf("Error parsing time: %v\n", err)
}
if lastSubmissionTimeString != "" {
lastSubmissionTime, err = time.Parse(time.RFC3339, lastSubmissionTimeString)
if err != nil {
log.Fatalf("Error parsing time: %v\n", err)
}
} else {
uptimeToday = append(uptimeToday, true)
lastSubmissionTimeString = submissionDataToday.CreatedAt
continue
}

currentSubmissionTime, err := time.Parse(time.RFC3339, submissionDataToday.CreatedAt)
if err != nil {
log.Fatalf("Error parsing time: %v\n", err)
}

if (lastSubmissionTimeString != "") && (currentSubmissionTime.After(lastSubmissionTime.Add(time.Duration(syncPeriod-5) * time.Minute))) && (currentSubmissionTime.Before(lastSubmissionTime.Add(time.Duration(syncPeriod+5) * time.Minute))) {
uptimeToday = append(uptimeToday, true)
lastSubmissionTimeString = submissionDataToday.CreatedAt
} else if lastSubmissionTimeString == "" {
uptimeToday = append(uptimeToday, true)
lastSubmissionTimeString = submissionDataToday.CreatedAt
}
if (lastSubmissionTimeString != "") && (currentSubmissionTime.After(lastSubmissionTime.Add(time.Duration(syncPeriod-5) * time.Minute))) {
uptimeToday = append(uptimeToday, true)
lastSubmissionTimeString = submissionDataToday.CreatedAt
continue
} else {
continue
}
}
}
}
}
}
}

// If the current time is more than the execution interval than it means that submissions from pervious buckets have to be checked
if SubmissionsInMultipleBuckets(currentTime, executionInterval) {

yesterdaysDate := lastExecutionTime.Format("2006-01-02")

prefixYesterday := strings.Join([]string{ctx.Prefix, "submissions", yesterdaysDate}, "/")
Expand Down Expand Up @@ -184,46 +192,56 @@ func (identity Identity) GetUptime(config AppConfig, sheet *sheets.Service, ctx

if submissionDataYesterday.GraphqlControlPort != 0 {
if (identity.publicKey == submissionDataYesterday.Submitter.String()) && (identity.publicIp == submissionDataYesterday.RemoteAddr) && (*identity.graphQLPort == strconv.Itoa(submissionDataYesterday.GraphqlControlPort)) {
if lastSubmissionTimeString != "" {
lastSubmissionTime, err = time.Parse(time.RFC3339, lastSubmissionTimeString)
if err != nil {
log.Fatalf("Error parsing time: %v\n", err)
}
}

currentSubmissionTime, err := time.Parse(time.RFC3339, submissionDataYesterday.CreatedAt)
if err != nil {
log.Fatalf("Error parsing time: %v\n", err)
}

if (lastSubmissionTimeString != "") && (currentSubmissionTime.After(lastSubmissionTime.Add(time.Duration(syncPeriod-5) * time.Minute))) && (currentSubmissionTime.Before(lastSubmissionTime.Add(time.Duration(syncPeriod+5) * time.Minute))) {
if lastSubmissionTimeString != "" {
lastSubmissionTime, err = time.Parse(time.RFC3339, lastSubmissionTimeString)
if err != nil {
log.Fatalf("Error parsing time: %v\n", err)
}
} else {
uptimeYesterday = append(uptimeYesterday, true)
lastSubmissionTimeString = submissionDataYesterday.CreatedAt
} else if lastSubmissionTimeString == "" {
continue
}

if (lastSubmissionTimeString != "") && (currentSubmissionTime.After(lastSubmissionTime.Add(time.Duration(syncPeriod-5) * time.Minute))) {
uptimeYesterday = append(uptimeYesterday, true)
lastSubmissionTimeString = submissionDataYesterday.CreatedAt
continue
} else {
continue
}
}
} else {
if (identity.publicKey == submissionDataYesterday.Submitter.String()) && (identity.publicIp == submissionDataYesterday.RemoteAddr) {

currentSubmissionTime, err := time.Parse(time.RFC3339, submissionDataYesterday.CreatedAt)
if err != nil {
log.Fatalf("Error parsing time: %v\n", err)
}

if lastSubmissionTimeString != "" {
lastSubmissionTime, err = time.Parse(time.RFC3339, lastSubmissionTimeString)
if err != nil {
log.Fatalf("Error parsing time: %v\n", err)
}
} else {
uptimeYesterday = append(uptimeYesterday, true)
lastSubmissionTimeString = submissionDataYesterday.CreatedAt
continue
}

currentSubmissionTime, err := time.Parse(time.RFC3339, submissionDataToday.CreatedAt)
if err != nil {
log.Fatalf("Error parsing time: %v\n", err)
}

if (lastSubmissionTimeString != "") && (currentSubmissionTime.After(lastSubmissionTime.Add(time.Duration(syncPeriod-5) * time.Minute))) && (currentSubmissionTime.Before(lastSubmissionTime.Add(time.Duration(syncPeriod+5) * time.Minute))) {
uptimeYesterday = append(uptimeYesterday, true)
lastSubmissionTimeString = submissionDataYesterday.CreatedAt
} else if lastSubmissionTimeString == "" {
uptimeYesterday = append(uptimeYesterday, true)
lastSubmissionTimeString = submissionDataYesterday.CreatedAt
}
if (lastSubmissionTimeString != "") && (currentSubmissionTime.After(lastSubmissionTime.Add(time.Duration(syncPeriod-5) * time.Minute))) {
uptimeYesterday = append(uptimeYesterday, true)
lastSubmissionTimeString = submissionDataYesterday.CreatedAt
continue
} else {
continue
}
}
}
Expand All @@ -234,8 +252,10 @@ func (identity Identity) GetUptime(config AppConfig, sheet *sheets.Service, ctx
}

uptimePercent := (float64(len(uptimeToday)+len(uptimeYesterday)) / float64(numberOfSubmissionsNeeded)) * 100

if uptimePercent > 100.00 {
uptimePercent = 100.00
}
identity.uptime = strconv.FormatFloat(uptimePercent, 'f', 2, 64)
uptimePercentString := strconv.FormatFloat(uptimePercent, 'f', 2, 64)
*identity.uptime = uptimePercentString
}

0 comments on commit cdf8cf0

Please sign in to comment.