Ejemplo n.º 1
0
        public static TightCapability Deserialize(Stream stream)
        {
            var reader = new Util.BinaryReader(stream);

            return(new TightCapability()
            {
                Code = reader.ReadInt32(),
                Vendor = reader.ReadString(4),
                Signature = reader.ReadString(8)
            });
        }
Ejemplo n.º 2
0
        public void WriteDatapoints(IList <Datapoint> datapoints)
        {
            Timestamp now = currentTimeProvider();

            var archiveIndex = 0;
            var archive      = archives[archiveIndex];

            var currentPoints = new List <Datapoint>();

            using (var fileStream = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
                using (var binaryWriter = new Util.BinaryWriter(fileStream, Encoding.UTF8, true))
                    using (var binaryReader = new Util.BinaryReader(fileStream, Encoding.UTF8, true))
                    {
                        foreach (var point in datapoints.OrderByDescending(d => d.Timestamp))
                        {
                            var age = TimeSpan.FromSeconds(now - (Timestamp)point.Timestamp);

                            while (archive.Retention.History < age)
                            {
                                if (currentPoints.Count > 0)
                                {
                                    WriteDatapoints(binaryReader, binaryWriter, archive, Enumerable.Reverse(currentPoints).ToList());
                                    currentPoints.Clear();
                                } // if

                                if (archiveIndex == archives.Count - 1)
                                {
                                    break;
                                }

                                archive = archives[++archiveIndex];
                            } // while

                            if (archive == null)
                            {
                                break;
                            }

                            currentPoints.Add(point);
                        } // foreach

                        if (archive != null && currentPoints.Count > 0)
                        {
                            WriteDatapoints(binaryReader, binaryWriter, archive, Enumerable.Reverse(currentPoints).ToList());
                        }

                        fileStream.Flush(true);
                    }// using
        }
Ejemplo n.º 3
0
        public static ServerInit Deserialize(Util.BinaryReader reader)
        {
            var serverInit = new ServerInit()
            {
                FrameBufferWidth  = reader.ReadUInt16(),
                FrameBufferHeight = reader.ReadUInt16(),
                PixelFormat       = PixelFormat.Deserialize(reader)
            };

            var nameSize = reader.ReadUInt32();

            serverInit.Name = reader.ReadString((int)nameSize);

            return(serverInit);
        }
Ejemplo n.º 4
0
        public IList <Annotation> ReadAnnotations(DateTime from, DateTime until)
        {
            using (var fileStream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                using (var binaryReader = new Util.BinaryReader(fileStream, Encoding.UTF8, true))
                {
                    fileStream.Seek(HeaderSize, SeekOrigin.Begin);

                    var annotations =
                        ReadAnnotations(binaryReader).
                        Where(a => from <= a.Timestamp && a.Timestamp <= until).
                        ToList();

                    return(annotations);
                }// using
        }
Ejemplo n.º 5
0
 public static PixelFormat Deserialize(Util.BinaryReader reader)
 {
     return(new PixelFormat()
     {
         BitsPerPixel = reader.ReadByte(),
         Depth = reader.ReadByte(),
         BigEndianFlag = reader.ReadByte(),
         TrueColorFlag = reader.ReadByte(),
         RedMax = reader.ReadUInt16(),
         GreenMax = reader.ReadUInt16(),
         BlueMax = reader.ReadUInt16(),
         RedShift = reader.ReadByte(),
         GreenShift = reader.ReadByte(),
         BlueShift = reader.ReadByte(),
         Padding = reader.ReadBytes(3),
     });
 }
Ejemplo n.º 6
0
        public void WriteDatapoint(DateTime dateTime, double value)
        {
            Timestamp timestamp = dateTime.ToUniversalTime();
            Timestamp now       = currentTimeProvider();

            var diff = now - timestamp;

            if (diff < 0)
            {
                throw new Exception();          // TODO
            }
            if (diff >= maxRetention)
            {
                throw new Exception();                      // TODO
            }
            var archive = archives.FirstOrDefault(a => a.Retention.History >= diff);

            if (archive == null)
            {
                throw new Exception(); // TODO: Exception
            }
            var lowerArchives = archives.Skip(archives.IndexOf(archive) + 1);

            timestamp = timestamp.RoundDownModulo(archive.Retention.Precision);

            using (var fileStream = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
                using (var binaryWriter = new Util.BinaryWriter(fileStream, Encoding.UTF8, true))
                    using (var binaryReader = new Util.BinaryReader(fileStream, Encoding.UTF8, true))
                    {
                        WriteDatapoint(binaryReader, binaryWriter, archive, timestamp, value);

                        var higher = archive;
                        foreach (var lower in lowerArchives)
                        {
                            if (!Downsample(binaryReader, binaryWriter, timestamp, higher, lower))
                            {
                                break;
                            }

                            higher = lower;
                        } // foreach

                        fileStream.Flush(true);
                    }// using
        }
Ejemplo n.º 7
0
        private static AnnotationDatabase Open(string path, FileStream stream)
        {
            using (var binaryReader = new Util.BinaryReader(stream, Encoding.UTF8, true))
            {
                var signature = binaryReader.ReadBytes(Signature.Length);
                if (!signature.SequenceEqual(Signature))
                {
                    throw new DatabaseException("Incompatible Statsify Database format");
                }

                var major   = binaryReader.ReadByte();
                var minor   = binaryReader.ReadByte();
                var version = new Version(major, minor);
                if (version != new Version(1, 0))
                {
                    throw new DatabaseException("Incompatible Statsify Database version");
                }
            } // using

            return(new AnnotationDatabase(path));
        }
Ejemplo n.º 8
0
        private static DatapointDatabase Open(string path, Stream stream, Func <DateTime> currentTimeProvider = null)
        {
            using (var binaryReader = new Util.BinaryReader(stream, Encoding.UTF8, true))
            {
                var signature = binaryReader.ReadBytes(Signature.Length);
                if (!signature.SequenceEqual(Signature))
                {
                    throw new DatabaseException("Incompatible Statsify Database format");
                }

                var major   = binaryReader.ReadByte();
                var minor   = binaryReader.ReadByte();
                var version = new Version(major, minor);
                if (version != new Version(1, 0))
                {
                    throw new DatabaseException("Incompatible Statsify Database version");
                }

                var dowsamplingMethod  = (DownsamplingMethod)binaryReader.ReadInt32();
                var downsamplingFactor = binaryReader.ReadSingle();
                var maxRetention       = binaryReader.ReadInt32();
                var archivesLength     = binaryReader.ReadInt32();

                var archives = new List <Archive>();

                for (var i = 0; i < archivesLength; ++i)
                {
                    var offset    = binaryReader.ReadInt32();
                    var precision = binaryReader.ReadInt32();
                    var history   = binaryReader.ReadInt32();

                    archives.Add(new Archive(offset, history * DatapointSize, new Retention(TimeSpan.FromSeconds(precision), history)));
                } // for

                return(new DatapointDatabase(path, downsamplingFactor, dowsamplingMethod, maxRetention, archives, currentTimeProvider));
            } // using
        }
Ejemplo n.º 9
0
        public Series ReadSeries(DateTime from, DateTime until, TimeSpan?precision = null)
        {
            Timestamp fromTimestamp  = from;
            Timestamp untilTimestamp = until;
            Timestamp nowTimestamp   = currentTimeProvider();

            if (fromTimestamp > untilTimestamp)
            {
                throw new Exception();                                // TODO: Exception class
            }
            Timestamp oldestTime = nowTimestamp - maxRetention;

            if (fromTimestamp > nowTimestamp)
            {
                return(null);
            }
            if (untilTimestamp < oldestTime)
            {
                return(null);
            }

            if (fromTimestamp < oldestTime)
            {
                fromTimestamp = oldestTime;
            }

            if (untilTimestamp > nowTimestamp)
            {
                untilTimestamp = nowTimestamp;
            }

            TimeSpan diff = TimeSpan.FromSeconds(nowTimestamp - fromTimestamp);

            var archive = archives.First(a => (TimeSpan)a.Retention.History >= diff && (precision == null || a.Retention.Precision >= precision.Value));

            var fromInterval  = fromTimestamp.RoundUpModulo(archive.Retention.Precision);
            var untilInterval = untilTimestamp.RoundUpModulo(archive.Retention.Precision);
            var step          = archive.Retention.Precision;

            double?[] values = null;

            using (var fileStream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                using (var binaryReader = new Util.BinaryReader(fileStream, Encoding.UTF8, true))
                {
                    Timestamp baseInterval = binaryReader.ReadInt64(archive.Offset);
                    if (baseInterval == 0)
                    {
                        var points = (untilInterval - fromInterval) / step;
                        values = new double?[points];
                    } // if
                    else
                    {
                        var fromOffset  = GetTimestampOffset(archive, fromInterval, baseInterval);
                        var untilOffset = GetTimestampOffset(archive, untilInterval, baseInterval);

                        var buffer = ReadBuffer(binaryReader, fromOffset, untilOffset, archive);

                        int knownValues;
                        UnpackDatapoints(archive, buffer, fromInterval, out values, out knownValues);
                    } // else
                }// using

            var timestamps =
                Enumerable.Range(0, values.Length).
                Select(i => new Timestamp(fromInterval + step * i));

            return(new Series(fromInterval, untilInterval, TimeSpan.FromSeconds(step),
                              timestamps.Zip(values, (ts, v) => new Datapoint(ts, v))));
        }