Example #1
0
        static void Main(string[] args)
        {
            String json;
            DateTime runDateTime = DateTime.UtcNow;                   // Not Run all
            //DateTime runDateTime = new DateTime(2015, 8, 26);           // Run all
            int runHour = 0;
            String city = String.Empty;
            String country = String.Empty;
            Decimal latitude = 0;
            Decimal longitude = 0;

            //while (runDateTime < DateTime.Now.AddDays(-1))                  // Run all
            //{                                                               // Run all
            // We want the prevous hours data
            // If it's zero, we want the last hour of yesterday
            if (runDateTime.Hour == 0)
            {
                runDateTime = runDateTime.AddDays(-1);
                runHour = 23;
            }
            else
                runHour = runDateTime.Hour - 1;

            // Change run date time to zero minutes,secomnds
            runDateTime = new DateTime(runDateTime.Year, runDateTime.Month, runDateTime.Day, runHour, 0, 0, 0);

            //runHour = 0;                                                  // Run all

            // Run report and return report ID
            string reportValue = QueueOmnitureQueueReport(runDateTime);

            // We will wait for 10*30 seconds for the report to be ready
            int attemp = 10;
            Boolean reportReady = true;
            do
            {
                reportReady = true;
                json = GetOmnitureQueueReport(reportValue);
                if (json.Contains("(400) Bad Request"))
                {
                    Thread.Sleep(30000);            // wait 10 seconds and try again
                    reportReady = false;
                    attemp--;
                }
            } while (attemp > 0 && !reportReady);

            if (attemp != 0)
            {
                XmlDocument xd = new XmlDocument();
                xd = (XmlDocument)JsonConvert.DeserializeXmlNode("{\"Root\":" + json + "}");
                DataSet ds = new DataSet();
                ds.ReadXml(new XmlNodeReader(xd));
                AwarenessDataClassesDataContext dc = new AwarenessDataClassesDataContext();

                //for (int i = 0; i < 24; i++)                // Run all
                //{                                           // Run all
                //    runHour = i;                            // Run all
                    DataRow[] dataRows = ds.Tables["data"].Select(string.Format("[hour]='{0}'", runHour), "", DataViewRowState.CurrentRows);
                    foreach (DataRow dataRow in dataRows)
                    {
                        if (ds.Tables["breakdown"] != null)
                        {
                            DataRow[] breakdownRows = ds.Tables["breakdown"].Select(String.Format("[data_Id]={0}", dataRow["data_Id"]));
                            foreach (DataRow breakdownRow in breakdownRows)
                            {
                                string name = breakdownRow["name"].ToString();
                                string url = breakdownRow["url"].ToString();
                                string geo_city = string.Empty;
                                string geo_country = string.Empty;

                                DataRow[] countsRows = ds.Tables["counts"].Select(String.Format("[breakdown_Id]={0}", breakdownRow["breakdown_Id"]));
                                Int32 count0 = Convert.ToInt32(countsRows[0][0]);
                                Int32 count1 = Convert.ToInt32(countsRows[1][0]);
                                Int32 count2 = Convert.ToInt32(countsRows[2][0]);
                                if (count0 > 0 || count1 > 0 || count2 > 0)           // Save the row.
                                {
                                    city = String.Empty;
                                    country = String.Empty;
                                    latitude = 0;
                                    longitude = 0;

                                    GetCountryCity(name, ref city, ref country);

                                    // Se if the city is a country capitial
                                    GetCaptialLatitudeLongitude(city, country, ref latitude, ref longitude, ref geo_city, ref geo_country);
                                    // If not see if you can find the city in the CountryCity table
                                    if (latitude == 0 && longitude == 0)
                                        GetCityLatitudeLongitude(city, country, ref latitude, ref longitude, ref geo_city, ref geo_country);
                                    // If we've still not found the city/country then just get the capital for that country
                                    if (latitude == 0 && longitude == 0)
                                        GetCaptialLatitudeLongitude(country, ref latitude, ref longitude, ref geo_city, ref geo_country);
                                    // if we still don't have ac lat/long then list in under "Unknown"
                                    if (latitude == 0 && longitude == 0)
                                    {
                                        geo_city = "Unknown";
                                        geo_country = "Unknown";
                                    }

                                    string line = string.Format("Date/Time: {0}, Country: {1}, URL: {2}, pageViews: {3}, visits: {4}", runDateTime.ToString(""), name, url, count0, count1);
                                    Console.WriteLine(line);

                                    omniture_raw_count row = new omniture_raw_count();
                                    row.created_date = runDateTime;                                // Not Run all
                                    //row.created_date = runDateTime.AddHours(runHour);                // Run all
                                    row.City = city;
                                    row.Country = country;
                                    row.pageViews = count0;
                                    row.visits = count1;
                                    row.uniquevisitors = count2;
                                    row.latitude = latitude;
                                    row.longitude = longitude;
                                    row.Geo_City = geo_city;
                                    row.Geo_Country = geo_country;
                                    dc.omniture_raw_counts.InsertOnSubmit(row);
                                }
                            }
                        }
                    }
                    dc.SubmitChanges();
                    update_totals();         // not Run all
                //}                              // Run all
                //update_totals();             // Run all
            }
            //runDateTime = runDateTime.AddDays(1);      // Run all
            //}                                          // Run all
        }
 partial void Updateomniture_raw_count(omniture_raw_count instance);
 partial void Deleteomniture_raw_count(omniture_raw_count instance);
 partial void Insertomniture_raw_count(omniture_raw_count instance);