Class for exporting time series data
Exemple #1
0
        public TimeSeriesData GetTimeSeries(string seriesId, DateTime start, DateTime end, string step)
        {
            var tsd = new TimeSeriesData();
            tsd.SeriesId = seriesId;
            tsd.StartTime = start;
            tsd.EndTime = end;

            var ds = Db.GetCollection<DataSeries>().FindOneById(seriesId);
            if (ds == null) throw new Exception("Invalid series: " + seriesId);
            char stp = string.IsNullOrEmpty(step) ? CalcDefaultStep(end - start, ds) : step[0];
            string st = FormatDatePartForStep(start, stp);
            string ed = FormatDatePartForStep(end, stp);
            st = string.Format("{0}{1}#{2}", stp, ds.Id, st);
            ed = string.Format("{0}{1}#{2}", stp, ds.Id, ed);
            tsd.DataColumns = new List<string>();
            tsd.Columns = new List<TimeSeriesData.Column>();
            tsd.Rows = new List<TimeSeriesData.Row>();
            foreach (var f in ds.Fields)
            {
                tsd.DataColumns.Add(f.Name);
                tsd.Columns.Add(new TimeSeriesData.Column { Name = f.Name, Color = null });
            }
            var lst = Db.GetCollection(ds.AggDataCollection).FindAs<reduce_sums>(MongoQueryBuilder.DynQuery(x => x._id >= st && x._id <= ed)).SetSortOrder("_id").ToList();
            var ret = new List<object>();
            Dictionary<string, reduce_sums> d = new Dictionary<string, reduce_sums>();
            foreach (var s in lst)
            {
                d[s._id] = s;
            }

            DateTime st1 = RoundToStepBoundary(start, stp, false);
            while (st1 < end)
            {
                string k = GetKey(ds.Id, stp, st1);
                reduce_sums s;
                if (d.ContainsKey(k))
                {
                    s = d[k];
                    tsd.Rows.Add(new TimeSeriesData.Row
                    {
                        T = RRD.RrdUtil.ToUnixTime(st1),
                        Timestamp = st1,
                        V = s.value.V.Select(x => (double) x).ToArray()
                    });

                }
                st1 = Increment(st1, stp, 1);
            }

            return tsd;
        }
Exemple #2
0
 public TimeSeriesData ExportGraphData(GraphDefinition gd, DrawOptions opts)
 {
     var cmdtext = BuildRrdGraphCmdline(gd, opts, null, null, true);
     string ret = RunRrdWithCommandline(cmdtext);
     var doc = new XmlDocument();
     doc.LoadXml(ret);
     var x = new TimeSeriesData();
     x.DataColumns = new List<string>();
     x.Rows = new List<TimeSeriesData.Row>();
     x.StartTime = FromUnixTime(int.Parse(doc.DocumentElement.SelectSingleNode("meta/start").InnerText));
     x.EndTime = FromUnixTime(int.Parse(doc.DocumentElement.SelectSingleNode("meta/end").InnerText));
     x.StepSecs = int.Parse(doc.DocumentElement.SelectSingleNode("meta/step").InnerText);
     foreach (XmlElement el in doc.DocumentElement.SelectNodes("meta/legend/entry"))
     {
         x.DataColumns.Add(el.InnerText);
     }
     foreach (XmlElement el in doc.DocumentElement.SelectNodes("data/row"))
     {
         var r = new TimeSeriesData.Row();
         r.T = Int32.Parse(el.SelectSingleNode("t").InnerText);
         r.Timestamp = FromUnixTime(r.T);
         var v = new List<double>();
         foreach (XmlElement xv in el.SelectNodes("v"))
         {
             v.Add(ParseDouble(xv.InnerText));
         }
         r.V = v.ToArray();
         x.Rows.Add(r);
     }
     return x;
 }