Example #1
0
        public static IList <CountryData> GetData()
        {
            lock (countryData)
            {
                if (countryData.Count == 0)
                {
                    if (CacheFilePath != null && File.Exists(CacheFilePath) && File.GetLastWriteTime(CacheFilePath).Date == DateTime.Today && false)
                    {
                        using (var jtr = new JsonTextReader(new StreamReader(CacheFilePath)))
                        {
                            var js = new JsonSerializer();
                            foreach (var item in js.Deserialize <CountryData[]>(jtr))
                            {
                                countryData.TryAdd(item.ID, item);
                            }
                        }
                    }
                    else
                    {
                        var lastYear = DateTime.Today.Year - 1;

                        var    firstYear              = lastYear - 5;
                        string gdpServiceUrl          = String.Format(indicatorServiceUrl, Indicators.Gdp, firstYear, lastYear);
                        string gdpGrowthServiceUrl    = String.Format(indicatorServiceUrl, Indicators.GdpGrowthRate, firstYear, lastYear);
                        string gniPerCapitaServiceUrl = String.Format(indicatorServiceUrl, Indicators.GniPerCapita, firstYear, lastYear);

                        Parallel.Invoke(new Action[] {
                            () => { FetchIndicator(gdpServiceUrl, (c, v) => { c.GDP = v; }); },
                            () => { FetchIndicator(gdpGrowthServiceUrl, (c, v) => { c.GDPGrowth = v; }); },
                            () => { FetchIndicator(gniPerCapitaServiceUrl, (c, v) => { c.GniPerCapita = v; }); }
                        });

                        var maxYear = countryData.Max(a => a.Value.Year);
                        var minYear = countryData.Min(a => a.Value.Year);

                        if (maxYear == lastYear)
                        {
                            CountryData cData = null;
                            foreach (var item in countryData.Where(a => a.Value.Year == minYear))
                            {
                                countryData.TryRemove(item.Key, out cData);
                            }
                        }

                        if (CacheFilePath != null)
                        {
                            //Cache data on disk
                            using (var jtw = new JsonTextWriter(new StreamWriter(CacheFilePath)))
                            {
                                var js = new JsonSerializer();
                                js.Serialize(jtw, countryData.Values);
                            }
                        }
                    }
                }

                return(countryData.Values.ToArray());
            }
        }
Example #2
0
            public void Process(int lastYear, CountryData rec)
            {
                var index = lastYear - rec.Year;
                if (index >= 5)
                    return;

                GDP[index] = rec.GDP;
                GDPGrowth[index] = rec.GDPGrowth;
                GNIPC[index] = rec.GniPerCapita;
            }