Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Added multiple region checking for pdouct api
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmarmour committed Sep 29, 2020
1 parent 3bc1c0c commit dafe373
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 40 deletions.
24 changes: 21 additions & 3 deletions cmd/nvidia-clerk-api-status/nvidia-clerk-api-status.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package main

import (
"fmt"
"log"
"sync"
"time"

"github.com/ianmarmour/nvidia-clerk/internal/alert"
"github.com/ianmarmour/nvidia-clerk/internal/config"
Expand All @@ -11,17 +13,33 @@ import (

func main() {
var wg sync.WaitGroup
wg.Add(len(config.RegionalConfigs) + 2)

cfg, err := config.Get("USA", "2060", 1, false, true, false, false, false, true)
if err != nil {
log.Fatal(err)
}

wg.Add(3)
// Monitor a single region for the session
go alert.StartDiscordAPINotifications("USA", "session", *cfg, &wg)

go alert.StartDiscordAPINotifications("session", *cfg, &wg)
go alert.StartDiscordAPINotifications("checkout", *cfg, &wg)
// Monitor only USA for the shields API sorry other regions.
go rest.StartShieldsAPIServer(*cfg.ShieldsConfig, &wg)

// Setup Notifications for all other regions avoiding rate limiting.
for id := range config.RegionalConfigs {
time.Sleep(10 * time.Second)
tempID := id
c, err := config.Get(tempID, "2060", 1, false, true, false, false, false, false)
if err != nil {
log.Println(fmt.Sprintf("Error getting configuration for %s", tempID))
wg.Add(-1)
continue
}

log.Println(fmt.Sprintf("Starting goroutine for %s", tempID))
go alert.StartDiscordAPINotifications(tempID, "checkout", *c, &wg)
}

wg.Wait()
}
60 changes: 27 additions & 33 deletions internal/alert/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"sync"
"time"
Expand Down Expand Up @@ -98,8 +99,10 @@ func SendDiscordMessage(message DiscordMessage, config config.DiscordConfig, cli
}

// StartDiscordAPINotifications Runs a loop and notifies discord when there is a status change.
func StartDiscordAPINotifications(api string, config config.Config, wg *sync.WaitGroup) {
client := &http.Client{Timeout: 3 * time.Second}
func StartDiscordAPINotifications(region string, api string, config config.Config, wg *sync.WaitGroup) {
defer wg.Done()

client := &http.Client{Timeout: 10 * time.Second}
previousStatus := ""

ticker := time.NewTicker(time.Second)
Expand All @@ -108,7 +111,7 @@ func StartDiscordAPINotifications(api string, config config.Config, wg *sync.Wai
check := make(chan bool)

go func() {
time.Sleep(10 * time.Second)
time.Sleep(61 * time.Second)
check <- true
}()

Expand All @@ -122,49 +125,40 @@ func StartDiscordAPINotifications(api string, config config.Config, wg *sync.Wai
if previousStatus != "offline" {
message := DiscordAPIMessage{}
message.Set("Store Session", "offline")

SendDiscordMessage(&message, *config.DiscordConfig, client)
previousStatus = "offline"
log.Println(fmt.Sprintf("Sending Discord Notification for %s", config.Locale))
}
} else {
if previousStatus != "online" {
message := DiscordAPIMessage{}
message.Set("Store Session", "online")
SendDiscordMessage(&message, *config.DiscordConfig, client)
previousStatus = "online"
log.Println(fmt.Sprintf("Sending Discord Notification for %s", config.Locale))
}

previousStatus = "offline"

continue
}
if previousStatus != "online" {
message := DiscordAPIMessage{}
message.Set("Store Session", "online")

SendDiscordMessage(&message, *config.DiscordConfig, client)
}

previousStatus = "online"
case "checkout":
token, _ := rest.GetSessionToken(client)
_, chkErr := rest.AddToCheckout(*config.SKU, token.Value, config.NvidiaLocale, client)
if chkErr != nil {
if previousStatus != "offline" {
message := DiscordAPIMessage{}
message.Set("Store Product Checkout", "offline")

message.Set(fmt.Sprintf("%s Store Product Checkout", region), "offline")
SendDiscordMessage(&message, *config.DiscordConfig, client)
log.Println(fmt.Sprintf("Sending Discord Notification for %s", config.Locale))
previousStatus = "offline"
}
} else {
if previousStatus != "online" {
message := DiscordAPIMessage{}
message.Set(fmt.Sprintf("%s Store Product Checkout", region), "online")
SendDiscordMessage(&message, *config.DiscordConfig, client)
log.Println(fmt.Sprintf("Sending Discord Notification for %s", config.Locale))
previousStatus = "online"
}

previousStatus = "offline"

continue
}

if previousStatus != "online" {
message := DiscordAPIMessage{}
message.Set("Store Product Checkout", "online")

SendDiscordMessage(&message, *config.DiscordConfig, client)
}

previousStatus = "online"
}

return
}
}
}
8 changes: 4 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ type Config struct {
}

// Hardcoded SKU to locale/currency mappings to avoid user pain of having to lookup and enter these.
var regionalConfig = map[string]RegionalConfig{
var RegionalConfigs = map[string]RegionalConfig{
"AUT": {
Models: map[string]Model{
"2060": {
Expand Down Expand Up @@ -676,8 +676,8 @@ func getShields() (*ShieldsConfig, error) {

//Get Generates Configuration for application from environmental variables.
func Get(region string, model string, delay int64, sms bool, discord bool, twitter bool, telegram bool, toast bool, shields bool) (*Config, error) {
if regionConfig, ok := regionalConfig[region]; ok {
models := getSupportedModels(regionalConfig[region])
if regionConfig, ok := RegionalConfigs[region]; ok {
models := getSupportedModels(RegionalConfigs[region])
isSupportedModel := contains(models, model)
if isSupportedModel == false {
log.Println(fmt.Sprintf("Please choose one of the following supported models: %v by using -model=XXX", models))
Expand Down Expand Up @@ -759,7 +759,7 @@ func contains(s []string, e string) bool {
func getSupportedRegions() []string {
keys := []string{}

for k := range regionalConfig {
for k := range RegionalConfigs {
keys = append(keys, k)
}

Expand Down

0 comments on commit dafe373

Please sign in to comment.