Exemple #1
0
        /// <summary>
        /// A simple example of building graphics.
        /// </summary>
        private static List <double?[]> PointCalc(GrafanaArg grafArg)
        {
            long t1;
            long t0;

            if (grafArg.range == null)
            {
                t1 = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
                t0 = t1 - 900000; // 15 min
            }
            else
            {
                DateTime to = grafArg.range.to;
                to = DateTime.SpecifyKind(to, DateTimeKind.Local).ToUniversalTime();
                DateTimeOffset ofs      = new DateTimeOffset(to);
                long           ofsValTo = ofs.ToUnixTimeMilliseconds();

                DateTime from = grafArg.range.from;
                to = DateTime.SpecifyKind(from, DateTimeKind.Local).ToUniversalTime();
                DateTimeOffset ofs1       = new DateTimeOffset(from);
                long           ofsValFrom = ofs1.ToUnixTimeMilliseconds();

                t1 = ofsValTo;
                t0 = ofsValFrom;
            }

            List <double?[]> points = new List <double?[]>();

            int step = 1000;

            if (grafArg.intervalMs > 0)
            {
                step = grafArg.intervalMs;
            }

            while (t0 < t1)
            {
                DateTimeOffset offset = DateTimeOffset.FromUnixTimeMilliseconds(t0);
                int            day    = offset.Day;
                int            hour   = offset.Hour;

                double time = t0;
                //double y = 100 * Math.Sin(time * 0.0001);
                double y = day * 100 + hour + 0.1 * Math.Sin(time * 0.0001);

                points.Add(new double?[] { y, time });
                t0 += step;
            }

            return(points);
        }
Exemple #2
0
        /// <summary>
        /// Determine the type of archive, hourly or minute.
        /// </summary>
        private static void SelectArcType(GrafanaArg grafArg, out bool isHour, out int coef)
        {
            long diff = GetUnixTimeMs(grafArg.range.to) - GetUnixTimeMs(grafArg.range.from);

            // more than 24 h
            if (diff / 60000 > 1440)
            {
                coef   = 60;
                isHour = true;
            }
            else
            {
                coef   = 1;
                isHour = false;
            }
        }
Exemple #3
0
        /// <summary>
        /// A simple example of building graphics.
        /// </summary>
        private static List <double?[]> PointCalc(GrafanaArg grafArg)
        {
            long t1;
            long t0;

            if (grafArg.range == null)
            {
                t1 = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
                t0 = t1 - 900000; // 15 min
            }
            else
            {
                t1 = GetUnixTimeMs(grafArg.range.to);
                t0 = GetUnixTimeMs(grafArg.range.from);
            }

            List <double?[]> points = new List <double?[]>();

            int step = 1000;

            if (grafArg.intervalMs > 0)
            {
                step = grafArg.intervalMs;
            }

            while (t0 < t1)
            {
                DateTimeOffset offset = DateTimeOffset.FromUnixTimeMilliseconds(t0);
                int            day    = offset.Day;
                int            hour   = offset.Hour;

                double time = t0;
                //double y = 100 * Math.Sin(time * 0.0001);
                double y = day * 100 + hour + 0.1 * Math.Sin(time * 0.0001);

                points.Add(new double?[] { y, time });
                t0 += step;
            }

            return(points);
        }
Exemple #4
0
        public IEnumerable <TrendData> GetDataForGrafana([FromBody] GrafanaArg grafanaArg)
        {
            if (grafanaArg == null)
            {
                return(GetEmptyTrend());
            }
            else
            {
                if (grafanaArg.targets == null)
                {
                    Log.WriteError("It is not possible to receive data");
                    return(GetEmptyTrend());
                }
                else
                {
                    SelectArcType(grafanaArg, out bool isHour, out int timeCoef);
                    TrendData[] trends = new TrendData[grafanaArg.targets.Length];
                    long        fromMs = GetUnixTimeMs(grafanaArg.range.from);
                    long        toMs   = GetUnixTimeMs(grafanaArg.range.to);

                    for (int i = 0; i < grafanaArg.targets.Length; i++)
                    {
                        List <double?[]> points = new List <double?[]>();
                        if (!int.TryParse(grafanaArg.targets[i].target.Trim(), out int cnlNum))
                        {
                            Log.WriteError("It is not possible to read the dates for the channel " + cnlNum);
                            trends[i] = new TrendData {
                                target = "-1", datapoints = null
                            };
                        }
                        else
                        {
                            foreach (DateTime date in EachDay(grafanaArg.range.from, grafanaArg.range.to))
                            {
                                Trend trend = GetTrend(UtcToLocalTime(date), cnlNum, isHour);

                                for (int i1 = 0; i1 < trend.Points.Count; i1++)
                                {
                                    long pointMs = GetUnixTimeMs(LocalToUtcTime(trend.Points[i1].DateTime));

                                    if (pointMs >= fromMs && pointMs <= toMs)
                                    {
                                        if (i1 > 0)
                                        {
                                            long prevMs = GetUnixTimeMs(LocalToUtcTime(trend.Points[i1 - 1].DateTime));

                                            if (pointMs - prevMs > timeCoef * 60000)
                                            {
                                                points.Add(new double?[] { null, prevMs + timeCoef * 60000 });
                                            }
                                            else
                                            {
                                                if (trend.Points[i1].Stat > 0)
                                                {
                                                    points.Add(new double?[] { trend.Points[i1].Val, pointMs });
                                                }
                                                else
                                                {
                                                    points.Add(new double?[] { null, pointMs });
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if (trend.Points[i1].Stat > 0)
                                            {
                                                points.Add(new double?[] { trend.Points[i1].Val, pointMs });
                                            }
                                            else
                                            {
                                                points.Add(new double?[] { null, pointMs });
                                            }
                                        }
                                    }
                                }
                            }

                            InCnlProps inCnlProps = dataAccess.GetCnlProps(cnlNum);
                            string     cnlName    = inCnlProps == null ? "" : inCnlProps.CnlName;

                            trends[i] = new TrendData {
                                target = "[" + cnlNum + "] " + cnlName, datapoints = points
                            };
                            Log.WriteAction("Channel data received " + cnlNum);
                        }
                    }

                    return(trends);
                }
            }
        }
Exemple #5
0
        public IEnumerable <TrendData> GetDataForGrafana([FromBody] GrafanaArg grafanaArg)
        {
            if (grafanaArg == null)
            {
                return(GetEmptyTrend());
            }
            else
            {
                if (grafanaArg.targets == null)
                {
                    Log.WriteError("It is not possible to receive data");
                    return(GetEmptyTrend());
                }
                else
                {
                    List <double?[]> points = new List <double?[]>();

                    SelectArcType(grafanaArg, out bool isHour, out int timeCoef);
                    TrendData[] trends = new TrendData[grafanaArg.targets.Length];

                    for (int i = 0; i < grafanaArg.targets.Length; i++)
                    {
                        points = new List <double?[]>();
                        if (!int.TryParse(grafanaArg.targets[i].target.Trim(), out int cnlNum))
                        {
                            Log.WriteError("It is not possible to read the dates for the channel " + cnlNum);
                            trends[i] = new TrendData {
                                target = "-1", datapoints = null
                            };
                        }
                        else
                        {
                            foreach (DateTime date in EachDay(grafanaArg.range.from, grafanaArg.range.to))
                            {
                                Trend trend = GetTrend(date, cnlNum, isHour);
                                for (int i1 = 0; i1 < trend.Points.Count; i1++)
                                {
                                    long ofsVal = GetUnixTimeMs(trend.Points[i1].DateTime);

                                    if (i1 > 0)
                                    {
                                        long ofsValP = GetUnixTimeMs(trend.Points[i1 - 1].DateTime);

                                        if ((ofsVal - ofsValP) > timeCoef * 60000)
                                        {
                                            points.Add(new double?[] { null, ofsValP + timeCoef * 60000 });
                                        }
                                        else
                                        {
                                            points.Add(new double?[] { trend.Points[i1].Val, ofsVal });
                                        }
                                    }
                                    else
                                    {
                                        points.Add(new double?[] { trend.Points[i1].Val, ofsVal });
                                    }
                                }
                            }
                            trends[i] = new TrendData {
                                target = cnlNum.ToString(), datapoints = points
                            };
                            Log.WriteAction("Channel data received " + cnlNum);
                        }
                    }

                    return(trends);
                }
            }
        }