|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "math" |
| 8 | + "math/rand" |
| 9 | + "os" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/aws/aws-sdk-go/aws/session" |
| 13 | + "github.com/aws/aws-sdk-go/service/kinesis" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + latitudeStart = 40.749167 |
| 18 | + longitudeStart = -73.985184 |
| 19 | + latitudeOffset = 110540 |
| 20 | + longitudeOffset = 111320 |
| 21 | + pointsStart = 150 |
| 22 | + minDistance = 29.0 |
| 23 | + maxDistance = 31.0 |
| 24 | +) |
| 25 | + |
| 26 | +type unicornStatus struct { |
| 27 | + Distance float64 |
| 28 | + HealthPoints int |
| 29 | + Latitude float64 |
| 30 | + Longitude float64 |
| 31 | + MagicPoints int |
| 32 | + Name *string |
| 33 | + StatusTime string |
| 34 | +} |
| 35 | + |
| 36 | +func main() { |
| 37 | + name := flag.String("name", "Shadowfax", "Unicorn Name") |
| 38 | + stream := flag.String("stream", "wildrydes", "Stream Name") |
| 39 | + |
| 40 | + flag.Parse() |
| 41 | + |
| 42 | + sess := session.Must( |
| 43 | + session.NewSessionWithOptions( |
| 44 | + session.Options{ |
| 45 | + SharedConfigState: session.SharedConfigEnable, |
| 46 | + }, |
| 47 | + ), |
| 48 | + ) |
| 49 | + |
| 50 | + simulateUnicorn(kinesis.New(sess), name, stream) |
| 51 | +} |
| 52 | + |
| 53 | +func simulateUnicorn(client *kinesis.Kinesis, name, stream *string) { |
| 54 | + rand.Seed(time.Now().UnixNano()) |
| 55 | + |
| 56 | + magicPoints := pointsStart |
| 57 | + healthPoints := pointsStart |
| 58 | + latitude := latitudeStart |
| 59 | + longitude := longitudeStart |
| 60 | + bearing := rand.Float64() * math.Pi * 2 |
| 61 | + ticker := time.NewTicker(time.Second) |
| 62 | + |
| 63 | + for range ticker.C { |
| 64 | + magicPoints = nextPoints(magicPoints) |
| 65 | + healthPoints = nextPoints(healthPoints) |
| 66 | + distance := float64(rand.Intn(maxDistance-minDistance)+minDistance) + rand.Float64() |
| 67 | + latitude, longitude = nextLocation(latitude, longitude, bearing, distance) |
| 68 | + status, _ := json.Marshal( |
| 69 | + &unicornStatus{ |
| 70 | + Distance: distance, |
| 71 | + HealthPoints: healthPoints, |
| 72 | + Latitude: latitude, |
| 73 | + Longitude: longitude, |
| 74 | + MagicPoints: magicPoints, |
| 75 | + Name: name, |
| 76 | + StatusTime: time.Now().Format("2006-01-02 15:04:05.000"), |
| 77 | + }, |
| 78 | + ) |
| 79 | + putRecordInput := &kinesis.PutRecordInput{ |
| 80 | + Data: append([]byte(status), "\n"...), |
| 81 | + PartitionKey: name, |
| 82 | + StreamName: stream, |
| 83 | + } |
| 84 | + |
| 85 | + if _, err := client.PutRecord(putRecordInput); err != nil { |
| 86 | + fmt.Println(err) |
| 87 | + os.Exit(1) |
| 88 | + } |
| 89 | + |
| 90 | + fmt.Print(".") |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func nextLocation(latitude, longitude, bearing float64, distance float64) (nextLatitude, nextLongitude float64) { |
| 95 | + nextLatitude = latitude + float64(distance)*math.Sin(bearing)/latitudeOffset |
| 96 | + nextLongitude = longitude + float64(distance)*math.Cos(bearing)/(longitudeOffset*math.Cos(math.Pi*latitude/180)) |
| 97 | + |
| 98 | + return |
| 99 | +} |
| 100 | + |
| 101 | +func nextPoints(points int) int { |
| 102 | + y := rand.Intn(2) |
| 103 | + |
| 104 | + if rand.Int()%2 == 0 { |
| 105 | + y = y * -1 |
| 106 | + } |
| 107 | + |
| 108 | + return points + y |
| 109 | +} |
0 commit comments