/// <summary> /// Requests data each time the period specified by Interval has elapsed. /// </summary> public async override Task RequestData() { try { Google.Apis.Analytics.v3.Data.RealtimeData rt = GetRequest.Execute(); List <Dictionary <string, string> > data = RealtimeData.GetData(rt); // Gets the date of the latest dataset in order to only add newer data. List <IInfluxSeries> last = await InfluxClient.QueryMultiSeriesAsync(DatabaseName, "SELECT last(*) FROM " + MeasurementName); DateTime now = DateTime.UtcNow; DateTime New = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0); DateTime?latest = last.FirstOrDefault()?.Entries[0].Time; List <InfluxDatapoint <InfluxValueField> > list = new List <InfluxDatapoint <InfluxValueField> >(); foreach (Dictionary <string, string> dict in data) { if (latest == null) { list.Add((InfluxDatapoint <InfluxValueField>) typeof(T).GetMethod("CreateInfluxDatapoint").Invoke(null, new object[] { MeasurementName, dict })); } else { // Necessary since nanoseconds cannot be set and will cause one date to be greater than the other although all (visible) parameters are equal. if (New.Subtract(new TimeSpan(0, Convert.ToInt32(dict["rt:minutesAgo"]), 0)) > latest) { list.Add((InfluxDatapoint <InfluxValueField>) typeof(T).GetMethod("CreateInfluxDatapoint").Invoke(null, new object[] { MeasurementName, dict })); } } await WriteToDatabase(list); } } catch (Exception ex) { Console.WriteLine("Error trying to request Google Analytics data: " + ex.Message); } }
/// <summary> /// Extracts the data of a Google Realtime request. /// </summary> /// <param name="rt">The RealtimeData response to a realtime request.</param> /// <returns>A List(Dictionary(string, string)) describing the data at a particular time relative to the time of the request.</returns> public static List <Dictionary <string, string> > GetData(Google.Apis.Analytics.v3.Data.RealtimeData rt) { List <Dictionary <string, string> > data = new List <Dictionary <string, string> >(); if (!(rt.Rows == null)) { foreach (List <string> r in rt.Rows) { data.Add(new Dictionary <string, string>()); for (int i = 0; i < r.Count; i++) { data[data.Count - 1].Add(rt.ColumnHeaders[i].Name, r[i]); } } } return(data); }