Esempio n. 1
0
        public async Task<string> AsyncExecute(string input, ILambdaContext context) {

                var waqiProxy = Waqi.create("sometoken"); // add token
                var feeds = new List<string>();
                var stationFeedsObtained = new List<WaqiCityFeed>();

                feeds.AddRange(new string[]{@"mexico/guadalajara/tlaquepaque"
                , @"mexico/guadalajara/vallarta"
                , @"mexico/guadalajara/miravalle"
                });
                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);}
                }
                var cityFeedDto = CityFeed.From(stationFeedsObtained);

                return JsonSerializer.Serialize(cityFeedDto).ToString();
                //System.Console.WriteLine(JsonSerializer.Serialize(cityFeedDto).ToString());
        }
Esempio n. 2
0
        public void ShouldDetectMaxAqiFromMultipleStations()
        {
            var waqiStations = GuadalajaraCityStations;

            var cityFeed = CityFeed.From(waqiStations);

            Assert.Equal(MaxAqi, cityFeed.MaxAQI);
        }
Esempio n. 3
0
        public void ShouldDetectAndFindBestCityName()
        {
            var waqiStations = GuadalajaraCityStations;

            var cityFeed = CityFeed.From(waqiStations);

            Assert.Equal(cityFeed.CityName, GuadalajaraCity);
        }
Esempio n. 4
0
        public void ShouldGetMaxAqiStation()
        {
            var waqiStations = GuadalajaraCityStations;

            var cityFeed = CityFeed.From(waqiStations);

            // max aqi station is set on constructor, currently is Tlaquepaque
            Assert.Equal(TlaquepaqueStationId, cityFeed.MaxAqiStation.Id);
        }
Esempio n. 5
0
        public void ShouldContainAllStations()
        {
            var waqiStations = GuadalajaraCityStations;

            var cityFeed = CityFeed.From(waqiStations);

            Assert.NotNull(cityFeed);
            Assert.NotNull(cityFeed.CityName);
            Assert.Equal(GuadalajaraCityStations.Count, cityFeed.Stations.Count);
        }
Esempio n. 6
0
        public void ShouldCreateCityFeedFromExternalJson()
        {
            var waqiStations = GuadalajaraCityStations;

            var cityFeed = CityFeed.From(waqiStations);

            Assert.NotNull(cityFeed);
            Assert.NotNull(cityFeed.CityName);
            Assert.NotEmpty(cityFeed.Stations);
        }
Esempio n. 7
0
        public void ShouldParseCityFeedWithInvalidAqiValue()
        {
            var waqiStations = new List <WaqiCityFeed>();

            waqiStations.Add(Values.Waqi.CreateWaqiCityFeedResponseWithInvalidAqi(GuadalajaraCity, FailingStation, int.Parse(FailingStationId)));
            var cityFeed = CityFeed.From(waqiStations);

            Assert.NotNull(cityFeed);
            Assert.NotNull(cityFeed.CityName);
            Assert.NotEmpty(cityFeed.Stations);
        }
Esempio n. 8
0
        public void ShouldHaveAtLeastOneAttribution()
        {
            var waqiStations = GuadalajaraCityStations;

            var cityFeed = CityFeed.From(waqiStations);

            // max aqi station is set on constructor, currently is Tlaquepaque
            Assert.NotEmpty(cityFeed.MaxAqiStation.Attributions);
            // attributions by default contain Waqi's URL, we should expect "n = waqiStations.Length" matches
            var attributions = from station in cityFeed.Stations
                               from attribution in station.Attributions
                               where attribution.Url.Contains(Waqi.WaqiAttributionUrl)
                               select attribution;

            Assert.Equal(attributions.Count(), waqiStations.Count());
        }
Esempio n. 9
0
        public async Task <string> AsyncExecute(string input, ILambdaContext context)
        {
            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 response = await sendCityFeed(cityFeedsDTO);

            System.Console.WriteLine(response.ToString());
            return("done");
        }
Esempio n. 10
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");
        }