예제 #1
0
        public async Task performAggregateOperation()
        {
            Task <List <string> > CsvData       = IOOperations.ReadFileByLineAsync("../../../../AggregateGDPPopulation/data/datafile.csv");
            Task <string>         MapDataString = IOOperations.ReadFileToEndAsync("../../../../AggregateGDPPopulation/data/countrytocontinentmap.json");

            List <string> CsvDataList = await CsvData;

            string[] CsvDataHeaders  = CsvDataList[0].Replace("\"", string.Empty).Split(',');
            int      CountryIndex    = Array.IndexOf(CsvDataHeaders, "Country Name");
            int      PopulationIndex = Array.IndexOf(CsvDataHeaders, "Population (Millions) 2012");
            int      GDPIndex        = Array.IndexOf(CsvDataHeaders, "GDP Billions (USD) 2012");

            CsvDataList.Remove(CsvDataList[0]);
            List <string[]>   CsvDataListSplitByComma = new List <string[]>();
            AddOrUpdateOutput output = new AddOrUpdateOutput();

            foreach (string item in CsvDataList)
            {
                CsvDataListSplitByComma.Add(item.Replace("\"", string.Empty).Split(','));
            }

            string MapData            = await MapDataString;
            var    CountryContinetMap = JSONOperations.JSONDeserialize(MapData);

            foreach (string[] row in CsvDataListSplitByComma)
            {
                float Population = float.Parse(row[PopulationIndex]);
                float GDP        = float.Parse(row[GDPIndex]);
                try
                {
                    string Continent = CountryContinetMap.GetValue(row[CountryIndex]).ToString();
                    output.AddOrUpdate(Continent, Population, GDP);
                }
                catch (Exception) { }
            }

            string Output = JSONOperations.JSONSerialize(output.AggregateOutput);
            await IOOperations.WriteToFileAsync("../../../../AggregateGDPPopulation/output/output.json", Output);
        }
예제 #2
0
        public async Task <JObject> AggregateCalculations()
        {
            Task <string> FileContentsTask     = FileOperations.ReadfileAsync(CSVPath);
            Task <string> JSONMapTask          = FileOperations.ReadfileAsync(CountryContinentMapPath);
            string        FileContentsComplete = await FileContentsTask;

            string[] FileContents = FileContentsComplete.Split('\n');
            string   headerText   = FileContents[0];

            string[] headers = headerText.Replace("\"", "").Split(',');

            int IndexOfCountry    = Array.IndexOf(headers, "Country Name");;
            int IndexOfPopulation = Array.IndexOf(headers, "Population (Millions) 2012");
            int IndexOfGDP        = Array.IndexOf(headers, "GDP Billions (USD) 2012");

            string CountryContinentJSONFileContents = await JSONMapTask;

            var CountryContinentMap = JSONOperations.DeSerializeString(CountryContinentJSONFileContents);

            for (int i = 1; i < FileContents.Length - 1; i++)
            {
                List <string> RowOfData  = FileContents[i].Split(',').ToList();
                string        Country    = RowOfData[IndexOfCountry].Trim('\"');
                float         Population = float.Parse(RowOfData[IndexOfPopulation].Trim('\"'));
                float         Gdp        = float.Parse(RowOfData[IndexOfGDP].Trim('\"'));
                try
                {
                    string Continent = CountryContinentMap.GetValue(RowOfData[IndexOfCountry].Trim('\"')).ToString();
                    AggregatedData.AddOrUpdateAggGDPPopulation(Continent, Gdp, Population);
                }
                catch (Exception) { }
            }
            var JSONOutput     = JSONOperations.SerializeJObject(AggregatedData.AggGDPPopulation);
            var AggregatedJSON = JSONOperations.DeSerializeString(JSONOutput);

            FileOperations.WriteFileAsync(OutputPath, JSONOutput);
            return(AggregatedJSON);
        }