Example #1
0
 public Tvqs()
 {
     var t = new DateTime(2015, 01, 01, 0, 0, 0, 0);
     Tvq20150101 = new Tvq(t, 100, Quality.Ok);
     t = new DateTime(2015, 01, 05, 0, 0, 0, 0);
     Tvq20150105 = new Tvq(t, 105, Quality.Ok);
     t = new DateTime(2015, 01, 10, 0, 0, 0, 0);
     Tvq20150110 = new Tvq(t, 105, Quality.Ok);
     t = new DateTime(2015, 01, 31, 23, 59, 59);
     Tvq20150131 = new Tvq(t, 105, Quality.Ok);
     t = new DateTime(2015, 06, 01, 0, 0, 0, 0);
     Tvq20150601 = new Tvq(t, 200, Quality.Ok);
     t = new DateTime(2015, 07, 01, 0, 0, 0, 0);
     Tvq20150701 = new Tvq(t, 300, Quality.Ok);
     t = new DateTime(2015, 12, 31, 23, 59, 59);
     Tvq20151231 = new Tvq(t, 399, Quality.Ok);
     t = new DateTime(2016, 01, 01, 0, 0, 0, 0);
     Tvq20160101 = new Tvq(t, 400, Quality.Ok);
     t = new DateTime(2016, 02, 01, 0, 0, 0, 0);
     Tvq20160201 = new Tvq(t, 410, Quality.Ok);
     t = new DateTime(2016, 06, 01, 0, 0, 0, 0);
     Tvq20160601 = new Tvq(t, 500, Quality.Ok);
     t = new DateTime(2017, 06, 01, 0, 0, 0, 0);
     Tvq20170601 = new Tvq(t, 500, Quality.Ok);
 }
Example #2
0
        public void Apply_WhenFirstValueIsInsidePeriod_AssumesStepwise()
        {
            // Arrange
            var tvq1 = new Tvq(
                new DateTime(2015, 01, 10, 0, 0, 0, 0),
                110,
                Quality.Ok);
            var tvq2 = new Tvq(
                new DateTime(2015, 01, 20, 0, 0, 0, 0),
                120,
                Quality.Ok);
            var ts = new Timeseries { tvq1, tvq2 };

            // Act
            var t0 = new DateTime(2015, 01, 01, 0, 0, 0, 0);
            var t1 = new DateTime(2015, 01, 31, 23, 59, 59, 999);
            var result = new Average().Apply(t0, t1, ts);

            // Assert
            var area1 = tvq1.V*(tvq1.Time - t0).TotalSeconds;
            var area2 = (tvq2.Time - tvq1.Time).TotalSeconds*(tvq1.V + tvq2.V)/2;
            var area3 = (t1 - tvq2.Time).TotalSeconds*tvq2.V;
            var expected = (area1 + area2 + area3) / (t1 - t0).TotalSeconds;
            Assert.Equal(expected, result.V);
        }
Example #3
0
        public TvqEntity(string partitionKey, Tvq tvq)
        {
            PartitionKey = partitionKey;
            RowKey = tvq.Time.Ticks.ToString();

            V = tvq.V;
            Q = tvq.Q;
        }
        public DeltaTsOperatorTest()
        {
            m_Delta = new DeltaTsOperator();
            m_t0 = new DateTime(2015, 01, 01, 0, 0, 0, 0);
            m_t1 = new DateTime(2015, 02, 01, 0, 0, 0, 0);
            m_t2 = new DateTime(2015, 02, 02, 0, 0, 0, 0);

            m_Tvq5 = new Tvq(m_t0, 5, Quality.Ok);
            m_Tvq7 = new Tvq(m_t1, 7, Quality.Ok);
            m_Tvq11 = new Tvq(m_t2, 11, Quality.Ok);
        }
Example #5
0
        /// <summary>
        /// Linearly interpolates or extrapolates the value at t
        /// </summary>
        /// <param name="t"></param>
        /// <param name="tvq1"></param>
        /// <param name="tvq2"></param>
        /// <returns></returns>
        public static Tvq CalculateValueAt(DateTime t, Tvq tvq1, Tvq tvq2)
        {
            var dx = tvq2.Time - tvq1.Time;
            var dy = tvq2.V - tvq1.V;
            var k = dy / dx.TotalSeconds;

            var x = (t - tvq1.Time).TotalSeconds;
            var y = k * x + tvq1.V;

            var tvq = new Tvq(t, y, Quality.Interpolated);
            return tvq;
        }
Example #6
0
        public void AddRegistryEntry(Tvq tvq)
        {
            // Create the table client.
            var tableClient = m_StorageAccount.CreateCloudTableClient();

            // Create the table if it doesn't exist.
            var table = tableClient.GetTableReference("registryentries");
            table.CreateIfNotExists();

            var tvqEntity = new TvqEntity(m_PartitionKey, tvq);
            var insertOperation = TableOperation.Insert(tvqEntity);

            // Execute the insert operation.
            table.Execute(insertOperation);
        }
Example #7
0
        public Tvq Apply(Tvq o1, Tvq o2)
        {
            var v = o2.V - o1.V;
            var q = Quality.Ok;

            if (o1.Q == Quality.Suspect || o2.Q == Quality.Suspect)
            {
                q = Quality.Suspect;
            }
            else if (o1.Q == Quality.Interpolated || o2.Q == Quality.Interpolated)
            {
                q = Quality.Interpolated;
            }

            return new Tvq(o2.Time, v, q);
        }
Example #8
0
        /// <summary>
        /// Linearly interpolates or extrapolates the value at t
        /// </summary>
        /// <param name="t"></param>
        /// <param name="tvq1"></param>
        /// <param name="tvq2"></param>
        /// <returns></returns>
        public static Tvq CalculateValueAt(DateTime t, Tvq tvq1, Tvq tvq2)
        {
            double y;

            if (tvq2 == null)
            {
                y = tvq1.V;
            }
            else
            {
                var dx = tvq2.Time - tvq1.Time;
                var dy = tvq2.V - tvq1.V;
                var k  = dy / dx.TotalSeconds;

                var x = (t - tvq1.Time).TotalSeconds;
                y = k * x + tvq1.V;
            }

            var tvq = new Tvq(t, y, Quality.Interpolated);

            return(tvq);
        }
        public ActionResult SubmitRegisterEntry(RegisterEntryModel model)
        {
            try
            {
                bool ok;
                DateTime t;
                int v;
                ok = DateTime.TryParse(model.DateString, out t);
                if (!ok)
                {
                    ViewBag.Title = "Fel vid registrering";
                    ViewBag.SubTitle = "Datum kunde ej tolkas";
                    return View(model);
                } // TODO better error handling

                ok = int.TryParse(model.RegisterValue, out v);
                if (!ok)
                {
                    ViewBag.Title = "Fel vid registrering";
                    ViewBag.SubTitle = "Mätarställning kunde ej tolkas";
                    return View(model);
                } // TODO better error handling

                var now = DateTime.Now;
                var isToday =
                    t.Year == now.Year
                    && t.Month == now.Month
                    && t.Day == now.Day;
                if (isToday)
                {
                    t = now;
                }

                var tvq = new Tvq(t, v, Quality.Ok);

                var repo = new RegistryEntryRepoFactory().GetRegistryEntryRepo();
                repo.AddRegistryEntry(tvq);
                ViewBag.Title = "Mätarställning registrerad";
                ViewBag.SubTitle = "Mätarställningen blev registrerad";
                return View(model);
            }
            catch (Exception e)
            {
                _logger.TrackException(e);
                ViewBag.Title = "Ett fel uppstod";
                ViewBag.SubTitle = "Ett fel uppstod vid registrering av mätarställning";
                return View(model);
            }
        }
Example #10
0
 private static Tvq CalculateDelta(Tvq current, Tvq previous)
 {
     return DeltaOp.Apply(previous, current);
 }
 public void Apply_WhenSecondIsSuspect_ResultIsSuspect()
 {
     var suspect = new Tvq(m_t0, 0, Quality.Suspect);
     var result = m_Delta.Apply(m_Tvq5, suspect);
     Assert.Equal(Quality.Suspect, result.Q);
 }
 public void Apply_WhenSecondIsInterpolated_ResultIsInterpolated()
 {
     var interpolated = new Tvq(m_t0, 0, Quality.Interpolated);
     var result = m_Delta.Apply(m_Tvq5, interpolated);
     Assert.Equal(Quality.Interpolated, result.Q);
 }
Example #13
0
        /// <summary>
        /// Inserts point at the specified interval by attempting to interpolate and/or 
        /// extrapolate values. Useful when splitting up a period into several series
        /// and displaying calculated start/end values in a graph.
        /// </summary>
        /// <param name="ts"></param>
        /// <param name="intervalLength"></param>
        /// <returns>A new timeseries that has values at the start and end of the interval</returns>
        public Timeseries InsertPoints(Timeseries ts, Interval intervalLength)
        {
            if (ReferenceEquals(ts, null)) throw new ArgumentException("ts");

            var result = new Timeseries();
            Tvq previous = null;
            for (var i = 0; i < ts.Count; i++)
            {
                var current = ts[i];
                var isNewInterval =
                    i == 0 ||
                    (previous != null && previous.Time.Year != current.Time.Year);

                //
                // Previous interval end?
                //
                var doInsertPreviousEnd = isNewInterval && previous != null ; // && not last second
                if (doInsertPreviousEnd)
                {
                    var lastSecondOfPrevious = new DateTime(previous.Time.Year, 12, 31, 23, 59, 59);
                    var tvq = Tvq.CalculateValueAt(lastSecondOfPrevious, previous, current);
                    result.Add(tvq);
                }

                //
                // Current interval start?
                //
                var t = current.Time;
                var firstSecond = new DateTime(t.Year, 01, 01, 0, 0, 0);
                if (isNewInterval && t > firstSecond)
                {
                    var newTvq = previous == null ?
                        new Tvq(firstSecond, current.V, Quality.Interpolated) : Tvq.CalculateValueAt(firstSecond, previous, current);
                    result.Add(newTvq);
                }

                result.Add(current);

                previous = current;
            }

            if (ts.Any())
            {
                if (previous != null)
                {
                    var lastSecond = new DateTime(previous.Time.Year, 12, 31, 23, 59, 59);

                    var iLast = ts.Count - 1;
                    var lastTvq = ts.Last();

                    if (ts.Count <= 1 && lastTvq.Time < lastSecond)
                    {
                        var newTvq = new Tvq(lastSecond, lastTvq.V, Quality.Interpolated);
                        result.Add(newTvq);
                    }
                    else if (lastTvq.Time < lastSecond)
                    {
                        var newTvq = Tvq.CalculateValueAt(lastSecond, ts[iLast - 1], lastTvq);
                        result.Add(newTvq);
                    }
                }
            }
            return result;
        }