Beispiel #1
0
 public GraphDefinition SaveGraphDefinition(GraphDefinition gd)
 {
     if (string.IsNullOrEmpty(gd.Id))
     {
         gd.OwnerId = UserSessionContext.CurrentUserRecordId;
         Db.GetCollection<GraphDefinition>().Save(gd);
     }
     else
     {
         var gd2 = Db.GetCollection<GraphDefinition>().FindOneById(gd.Id);
         if (gd2.OwnerId != UserSessionContext.CurrentUserRecordId) throw new Exception("You can't modify this graph");
         gd.OwnerId = UserSessionContext.CurrentUserRecordId;
         Db.GetCollection<GraphDefinition>().Save(gd);
     }
     return gd;
 }
Beispiel #2
0
 public GraphDefinition GetDefaultGraphDefinitionForDataSource(string dsId)
 {
     var ds = DSRepo.GetDataSeries(dsId, true);
     GraphDefinition gd = new GraphDefinition();
     gd.Defs = new List<DEF>();
     gd.CVDefs = new List<CVDEF>();
     gd.Elements = new List<GraphElement>();
     gd.ACL = new List<string>();
     gd.EventCategories = new List<string>();
     gd.OwnerId = UserSessionContext.CurrentUserInfo.Id;
     gd.Title = ds.Description;
     gd.AdditionalCmdParams = "--full-size";
     for (int i=0; i < ds.Fields.Count; i++)
     {
         var fld = ds.Fields[i];
         string vn = fld.Name;
         gd.Defs.Add(new DEF { DataSourceId = ds.Id, Field = fld.Name, Variable = vn, CF = ConsolidationFunction.AVERAGE });
         gd.CVDefs.Add(new CVDEF { CDEF = false, Expression = string.Format("{0},LAST", vn), Variable = vn + "_last" });
         gd.Elements.Add(new GraphElement { Op = GraphOperation.LINE2, Value = vn, Color = GetColor(i, 0xa0), Legend = string.IsNullOrEmpty(fld.Description) ? fld.Name : fld.Description, Params = "" });
         gd.Elements.Add(new GraphElement { Op = GraphOperation.GPRINT, Value = vn + "_last", Params = "%1.1lf \\l"});
     }
     return gd;
 }
Beispiel #3
0
 private Dictionary<string, object> PrepareGraphParams(GraphDefinition gd, DrawOptions opts)
 {
     var dic = new Dictionary<string, object>();
     dic["step"] = opts.Step.HasValue ? opts.Step.Value : 0;
     dic["width"] = opts.Width.HasValue ? opts.Width : 0;
     dic["height"] = opts.Height.HasValue ? opts.Height : 0;
     dic["graphDefinitionId"] = gd.Id;
     dic["graphTitle"] = gd.Title.Replace("\"", "\\\"");
     return dic;
 }
Beispiel #4
0
        protected string BuildRrdGraphCmdline(GraphDefinition gd, DrawOptions opts, IEnumerable<RrdEventInfo> addEvents, string destinationFile, bool asXport)
        {
            StringWriter cmd = new StringWriter();
            if (asXport)
            {
                cmd.Write("xport ");
                if (opts.Width.HasValue) cmd.Write(" -m {0}", opts.Width.Value);
            }
            else
            {
                if (destinationFile != null) cmd.Write("graphv \"{0}\"", destinationFile);
                if (!string.IsNullOrEmpty(gd.AdditionalCmdParams)) cmd.Write(" " + gd.AdditionalCmdParams);
                if (opts.Width.HasValue) cmd.Write(" -w {0}", opts.Width.Value);
                if (opts.Height.HasValue) cmd.Write(" -h {0}", opts.Height.Value);
            }
            if (!string.IsNullOrEmpty(opts.StartTime)) cmd.Write(" -s \"{0}\"", opts.StartTime);
            if (!string.IsNullOrEmpty(opts.EndTime)) cmd.Write(" -e \"{0}\"", opts.EndTime);
            if (opts.Step.HasValue) cmd.Write(" --step {0}", opts.Step.Value);

            if (gd.Defs == null) throw new Exception("DEFs are required");

            foreach (var d in gd.Defs)
            {
                cmd.Write(" " + d.ToRRDString());
            }

            if (gd.CVDefs != null)
            {
                foreach (var cvd in gd.CVDefs)
                {
                    cmd.Write(" " + cvd.ToRRDString());
                }
            }
            for (int i = 0; i < gd.Elements.Count; i++)
            {
                var el = gd.Elements[i];
                if (el.Hide) continue;
                if (opts.SkipElements != null && opts.SkipElements.Contains(i)) continue;
                if (asXport)
                {
                    if (el.Op == GraphOperation.AREA || el.Op == GraphOperation.LINE1 || el.Op == GraphOperation.LINE2
                        || el.Op == GraphOperation.LINE3 || el.Op == GraphOperation.TICK)
                    {
                        cmd.Write(" XPORT:{0}{1}", el.Value, string.IsNullOrEmpty(el.Legend) ? "" : string.Format(":\"{0}\"", el.Legend));
                    }
                }
                else if (destinationFile != null) cmd.Write(" " + el.ToRRDString());
            }

            if (addEvents != null && destinationFile != null)
            {
                addEvents = addEvents.OrderBy(x => x.Time);
                foreach (var ev in addEvents)
                {
                    cmd.Write(" " + ev.ToRRDString());
                }
            }
            var cmdtext = cmd.ToString();
            var dic = PrepareGraphParams(gd, opts);
            return Utils.SubstValue(cmdtext, dic);
        }
Beispiel #5
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;
 }
Beispiel #6
0
        public RrdImageInfo DrawGraph(GraphDefinition gd, DrawOptions opts, IEnumerable<RrdEventInfo> addEvents, string destinationFile)
        {
            var cmdtext = BuildRrdGraphCmdline(gd, opts, addEvents, destinationFile, false);

            string ret = RunRrdWithCommandline(cmdtext);
            var ii = new RrdImageInfo();
            ii.FileName = destinationFile;
            log.Debug("Img info: {0}", ret);
            var dt = ParseRRDInfo(ret, true);
            if (dt.ContainsKey("graph_start"))
            {
                ii.GraphStartTime = Convert.ToInt32(dt["graph_start"]);
                ii.Start = FromUnixTime(ii.GraphStartTime);
            }
            if (dt.ContainsKey("graph_end"))
            {
                ii.GraphEndTime = Convert.ToInt32(dt["graph_end"]);
                ii.End = FromUnixTime(ii.GraphEndTime);
            }
            if (dt.ContainsKey("graph_top")) ii.GraphTop = Convert.ToInt32(dt["graph_top"]);
            if (dt.ContainsKey("graph_left")) ii.GraphLeft = Convert.ToInt32(dt["graph_left"]);
            if (dt.ContainsKey("graph_width")) ii.GraphWidth = Convert.ToInt32(dt["graph_width"]);
            if (dt.ContainsKey("graph_height")) ii.GraphHeight = Convert.ToInt32(dt["graph_height"]);
            if (dt.ContainsKey("value_min")) ii.MinValue = Convert.ToDouble(dt["value_min"]);
            if (dt.ContainsKey("value_max")) ii.MaxValue = Convert.ToDouble(dt["value_max"]);
            if (addEvents != null)
            {
                ii.Events = new List<RrdImageInfo.ImgEventInfo>();
                foreach (var ev in addEvents)
                {
                    if (ev.Time < ii.GraphStartTime || ev.Time > ii.GraphEndTime) continue;
                    ii.Events.Add(new RrdImageInfo.ImgEventInfo
                    {
                        EventId = ev.EventId,
                        Time = ev.Time,
                        GraphX = GetGraphXForTime(ev.Time, ii.GraphStartTime, ii.GraphEndTime, ii.GraphWidth)
                    });
                }
            }
            ii.UpdateSec = 300;
            return ii;
        }