Example #1
0
        public async Task <string> AsyncExecute(System.IO.Stream input, ILambdaContext context)
        {
            _ = input;                                                                                                // discard
            var waqiProxy = Waqi.create(_token);
            var cities    = JsonSerializer.Deserialize <List <Latincoder.AirQuality.Model.Config.City> >(_citiesRaw); // default C# serialization
            // process multiple cities
            var stationFeedsByCity = from city in cities
                                     from station in city.Stations
                                     let feed = new { Uri = $"{city.Country}/{city.Name}/{station}", CityName = city.Name }
            group feed by $"{city.Country}-{city.Name}" into stationsByCity
            select stationsByCity;

            var cityFeedsDTO = new List <CityFeed>();

            foreach (var cityFeeds in stationFeedsByCity)
            {
                var feeds = from value in cityFeeds
                            select value.Uri;
                var stationFeedsObtained = new List <WaqiCityFeed>();
                foreach (var feed in feeds)
                {
                    System.Console.WriteLine($"Requesting feed for:{feed}");
                    string mutableString = string.Empty;
                    try {
                        mutableString = await waqiProxy.getCityFeed(feed);

                        if (mutableString != string.Empty)
                        {
                            System.Console.WriteLine("found this result:\n");
                            System.Console.WriteLine(mutableString);
                            var wcf = JsonSerializer.Deserialize <WaqiCityFeed>(mutableString);
                            stationFeedsObtained.Add(wcf);
                        }
                    } catch (Exception e) { System.Console.WriteLine(e); }
                }
                try {
                    cityFeedsDTO.Add(CityFeed.From(stationFeedsObtained));
                } catch (Exception e) { System.Console.WriteLine(e); }
            }

            var aqiTable             = Table.LoadTable(_dynamoClient, _AqiTableName);
            var notificationIsUrgent = NotificationValidator.CreateyValidator();

            notificationIsUrgent.AddRules(Rules.AqiAboveUnhealthyThreshold);
            // store in DynamoDB if meets notification criteria
            foreach (var feed in cityFeedsDTO)
            {
                // Gather previous and new feeds
                var prevFeedDoc = await aqiTable.GetItemAsync(CityFeedDocument.PartitionKeyName);

                var newFeed = prevFeedDoc != null?
                              CityFeedDocument.From(feed, prevFeedDoc[CityFeedDocument.FieldMaxAqi].AsInt()) :
                                  CityFeedDocument.From(feed);

                // if no previous stuff
                if (prevFeedDoc == null)
                {
                    Console.WriteLine($"FIRST TIME: Store in dynamo now for {feed.CityName}");
                    await aqiTable.PutItemAsync(newFeed);

                    continue;
                }

                var prevFeed = CityFeedDocument.ToDTO(prevFeedDoc);

                // each feed will has a custom validator with it's own criteria since this is done per city
                var notificationIsNotSpam = NotificationValidator.CreateyValidator();
                // spam means max aqi changed at least by 5 points, and is at least 20 minutes apart frm last notification
                notificationIsNotSpam.AddRules(
                    Rules.MinutesApartFrom(prevFeed, 30),
                    Rules.AbsoluteAqiChangedBy(prevFeed, 5));
                // urgent or meets criteria
                if (notificationIsUrgent.MeetsGlobalCriteria(feed) && notificationIsNotSpam.MeetsGlobalCriteria(feed))
                {
                    Console.WriteLine($"Store in dynamo now for {feed.CityName}");
                    await aqiTable.PutItemAsync(newFeed);
                }
                else
                {
                    Console.WriteLine("Does not meet rules...");
                }
            }

            return("done");
        }