Exemplo n.º 1
0
 public override async Task ProcessEventAsync(LambdaScheduleEvent schedule)
 {
     if (schedule.Name == null)
     {
         LogWarn("missing region in scheduled event");
         return;
     }
     await GenerateCloudFormationSpecificationAsync(schedule.Name);
 }
Exemplo n.º 2
0
        public override async Task ProcessEventAsync(LambdaScheduleEvent schedule)
        {
            // generate extended IAM specification for region
            LogInfo($"fetching latest IAM specification");
            var specification = await _converter.GenerateIamSpecificationAsync();

            // serialize IAM specification into a brotli compressed stream
            var compressedJsonSpecificationStream = new MemoryStream();

            using (var brotliStream = new BrotliStream(compressedJsonSpecificationStream, CompressionLevel.Optimal, leaveOpen: true)) {
                await JsonSerializer.SerializeAsync(brotliStream, specification, new JsonSerializerOptions {
                    IgnoreNullValues = true,
                    IncludeFields    = true,
                    NumberHandling   = JsonNumberHandling.AllowReadingFromString,
                    Encoder          = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
                });

                brotliStream.Flush();
            }
            compressedJsonSpecificationStream.Position = 0;

            // compute MD5 of new compressed IAM specification
            byte[] newMD5Hash;
            using (var md5 = MD5.Create()) {
                newMD5Hash = md5.ComputeHash(compressedJsonSpecificationStream);
                compressedJsonSpecificationStream.Position = 0;
            }
            var newETag = $"\"{string.Concat(newMD5Hash.Select(x => x.ToString("x2")))}\"";

            LogInfo($"compressed IAM specification ETag is {newETag} (size: {compressedJsonSpecificationStream.Length:N0} bytes)");

            // update compressed IAM specification in S3
            LogInfo($"uploading new IAM specification");
            await _s3Client.PutObjectAsync(new PutObjectRequest {
                BucketName  = _destinationBucketName,
                Key         = $"AWS/IamSpecification.json.br",
                InputStream = compressedJsonSpecificationStream,
                MD5Digest   = Convert.ToBase64String(newMD5Hash),
                Headers     =
                {
                    ContentEncoding = "br",
                    ContentType     = "application/json; charset=utf-8",
                    ContentMD5      = newETag
                }
            });

            LogInfo($"done");
        }
Exemplo n.º 3
0
        public override async Task ProcessEventAsync(LambdaScheduleEvent schedule)
        {
            // fetch Bitcoin price from API
            var response = await HttpClient.GetAsync("https://api.coindesk.com/v1/bpi/currentprice.json");

            if (response.IsSuccessStatusCode)
            {
                LogInfo("Fetched price from API");

                // publish Bitcoin JSON on topic
                await _snsClient.PublishAsync(_topicArn, await response.Content.ReadAsStringAsync());
            }
            else
            {
                LogWarn("Unable to fetch price");
            }
        }
Exemplo n.º 4
0
        public override async Task ProcessEventAsync(LambdaScheduleEvent schedule)
        {
            var response = await HttpClient.GetAsync("https://api.coindesk.com/v1/bpi/currentprice.json");

            if (response.IsSuccessStatusCode)
            {
                LogInfo("Fetched price from API");

                // read price from API response
                dynamic json  = JObject.Parse(await response.Content.ReadAsStringAsync());
                var     price = (double)json.bpi.USD.rate_float;

                // log and send CloudWatch event
                LogEvent(new BitcoinPriceEvent {
                    Price = price
                });
            }
            else
            {
                LogInfo("Unable to fetch price");
            }
        }
Exemplo n.º 5
0
 public override async Task ProcessEventAsync(LambdaScheduleEvent schedule)
 {
     LogInfo($"Id = {schedule.Id}");
     LogInfo($"Time = {schedule.Time}");
     LogInfo($"Name = '{schedule.Name}'");
 }
Exemplo n.º 6
0
 public override async Task ProcessEventAsync(LambdaScheduleEvent schedule)
 {
     // TO-DO: add business logic
 }
Exemplo n.º 7
0
        public override async Task ProcessEventAsync(LambdaScheduleEvent request)
        {
            var lastId = 0L;

            // read last_id from table
            var document = await _table.GetItemAsync("last");

            if (
                (document != null) &&
                document.TryGetValue("Query", out var queryEntry) &&
                (queryEntry.AsString() == _twitterSearchQuery) &&
                document.TryGetValue("LastId", out var lastIdEntry)
                )
            {
                lastId = lastIdEntry.AsLong();
            }

            // query for tweets since last id
            LogInfo($"searching for tweets: query='{_twitterSearchQuery}', last_id={lastId}");
            var tweets = TwitterSearch.SearchTweets(new SearchTweetsParameters(_twitterSearchQuery)
            {
                Lang            = null,
                TweetSearchType = TweetSearchType.OriginalTweetsOnly,
                SinceId         = lastId
            }) ?? Enumerable.Empty <ITweet>();

            // check if any tweets were found
            LogInfo($"found {tweets.Count():N0} tweets");
            if (tweets.Any())
            {
                var languages = string.Join(",", _twitterLanguageFilter.OrderBy(language => language));

                // send all tweets to topic
                var tasks = tweets

                            // convert tweet object back to JSON to extract the ISO language
                            .Select(tweet => JObject.Parse(tweet.ToJson()))
                            .Select(json => new {
                    Json        = json,
                    IsoLanguage = (string)json["metadata"]["iso_language_code"]
                })

                            // only keep tweets that match the ISO language filter if one is set
                            .Where(item => {
                    if (_twitterLanguageFilter.Any() && !_twitterLanguageFilter.Contains(item.IsoLanguage))
                    {
                        LogInfo($"tweet language '{item.IsoLanguage}' did not match '{languages}'");
                        return(false);
                    }
                    return(true);
                })

                            // group tweets by language for sentiment analysis
                            .GroupBy(item => item.IsoLanguage, item => item.Json)
                            .Select(async group => {
                    if (_twitterSentimentFilter == "SKIP")
                    {
                        // skip sentiment analysis
                        return(group);
                    }

                    // loop over all tweets
                    var remaining = group.ToList();
                    while (remaining.Any())
                    {
                        // batch analyze tweets
                        var batch    = remaining.Take(25).ToArray();
                        var response = await _comprehendClient.BatchDetectSentimentAsync(new BatchDetectSentimentRequest {
                            LanguageCode = group.Key,
                            TextList     = batch.Select(item => (string)item["full_text"]).ToList()
                        });

                        // set result for successfully analyzed tweets
                        foreach (var result in response.ResultList)
                        {
                            batch[result.Index]["aws_sentiment"] = result.Sentiment.Value;
                        }

                        // set 'N/A' for tweets that failed analysis
                        foreach (var result in response.ErrorList)
                        {
                            batch[result.Index]["aws_sentiment"] = "N/A";
                        }

                        // skip analyzed tweets
                        remaining = remaining.Skip(25).ToList();
                    }
                    return(group);
                })
                            .SelectMany(group => group.Result)
                            .Where(jsonTweet => {
                    // check if message should be filtered based-on sentiment
                    switch (_twitterSentimentFilter)
                    {
                    case "SKIP":
                    case "ALL":
                        return(true);

                    default:
                        if (((string)jsonTweet["aws_sentiment"]) != _twitterSentimentFilter)
                        {
                            LogInfo($"tweet sentiment '{jsonTweet["aws_sentiment"]}' did not match '{_twitterSentimentFilter}'");
                            return(false);
                        }
                        return(true);
                    }
                })
                            .Select((jsonTweet, index) => {
                    LogInfo($"sending tweet #{index + 1}: {jsonTweet["full_text"]}\n{jsonTweet}");
                    return(_snsClient.PublishAsync(new PublishRequest {
                        TopicArn = _notificationTopic,
                        Message = jsonTweet.ToString(Formatting.None)
                    }));
                })
                            .ToList();

                // store updated last_id
                await _table.PutItemAsync(new Document {
                    ["Id"]     = "last",
                    ["LastId"] = tweets.Max(tweet => tweet.Id),
                    ["Query"]  = _twitterSearchQuery
                });

                // wait for all tasks to finish before exiting
                LogInfo($"waiting for all tweets to be sent");
                await Task.WhenAll(tasks.ToArray());

                LogInfo($"all done");
            }
        }
Exemplo n.º 8
0
 public override async Task ProcessEventAsync(LambdaScheduleEvent schedule)
 {
     await _logic.Run();
 }