コード例 #1
0
ファイル: RetentionPolicy.cs プロジェクト: johnmetta/statsify
        public static RetentionPolicy Parse(string text)
        {
            var retentionPolicy = new RetentionPolicy();
            var parts           = text.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim());

            foreach (var part in parts)
            {
                var subparts = part.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                if (subparts.Length != 2)
                {
                    continue;
                }

                var precision = TimeSpanParser.ParseTimeSpan(subparts[0]);
                var history   = TimeSpanParser.ParseTimeSpan(subparts[1]);

                if (!precision.HasValue || !history.HasValue)
                {
                    continue;
                }

                retentionPolicy.Add(precision.Value, history.Value);
            } // foreach

            return(retentionPolicy);
        }
コード例 #2
0
        public static void EnsureRetentionPolicyValid(RetentionPolicy retentionPolicy)
        {
            if (retentionPolicy.Count == 0)
            {
                throw new RetentionPolicyValidationException("A Statsify database requires at least one Retention");
            }

            var retentions = retentionPolicy.OrderBy(r => r.Precision).ToList();

            for (var i = 1; i < retentions.Count; ++i)
            {
                var previousRetention = retentions[i - 1];
                var retention         = retentions[i];

                if (previousRetention.Precision == retention.Precision)
                {
                    throw new RetentionPolicyValidationException(
                              string.Format("A Statsify database may not be created having two Retentions (#{0}, #{1}) with the same precision", i - 1, i));
                }

                if (retention.Precision % previousRetention.Precision != 0)
                {
                    throw new RetentionPolicyValidationException(
                              string.Format("Higher precision Retention (#{0}, {1}) must evenly divide lower precision Retention (#{2}, {3})",
                                            i - 1, previousRetention.Precision, i, retention.Precision));
                }

                if ((TimeSpan)retention.History < previousRetention.History)
                {
                    throw new RetentionPolicyValidationException(
                              string.Format("Lower precision Retention (#{0}, {1}) must cover larger time intervals than higher precision Retention (#{2}, {3})",
                                            i, retention.History, i - 1, previousRetention.History));
                }

                var pointsPerDownsample = (long)retention.Precision / previousRetention.Precision;

                if (previousRetention.History < pointsPerDownsample)
                {
                    throw new RetentionPolicyValidationException(
                              string.Format("Retention (#{0}) must have at least enough points to consolidate to the next Retention", i - 1));
                }
            } // for
        }
コード例 #3
0
        public static DatapointDatabase Create(string path, float downsamplingFactor, DownsamplingMethod downsamplingMethod, RetentionPolicy retentionPolicy, Func <DateTime> currentTimeProvider = null)
        {
            EnsureValidRetentionPolicy(retentionPolicy);

            using (var fileStream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.Read, 8192, FileOptions.WriteThrough))
                return(Create(path, fileStream, downsamplingFactor, downsamplingMethod, retentionPolicy, currentTimeProvider));
        }
コード例 #4
0
 private static void EnsureValidRetentionPolicy(RetentionPolicy retentionPolicy)
 {
     RetentionPolicyValidator.EnsureRetentionPolicyValid(retentionPolicy);
 }
コード例 #5
0
        private static DatapointDatabase Create(string path, FileStream stream, float downsamplingFactor, DownsamplingMethod downsamplingMethod, RetentionPolicy retentionPolicy, Func <DateTime> currentTimeProvider = null)
        {
            var maxRetention = retentionPolicy.Select(h => h.Precision * h.History).Max();

            var headerSize =
                sizeof(byte) * Signature.Length +
                sizeof(byte) * Version.Length +
                sizeof(int) +
                sizeof(float) +
                sizeof(int) +
                sizeof(int) +
                ArchiveHeaderSize * retentionPolicy.Count;

            var archives = new List <Archive>();

            using (var binaryWriter = new BinaryWriter(stream))
            {
                binaryWriter.Write(Signature);
                binaryWriter.Write(Version);

                // ReSharper disable RedundantCast
                binaryWriter.Write((int)downsamplingMethod);
                binaryWriter.Write((float)downsamplingFactor);
                binaryWriter.Write((int)maxRetention);
                binaryWriter.Write((int)retentionPolicy.Count);
                // ReSharper restore RedundantCast

                var offset = headerSize;

                foreach (var retention in retentionPolicy)
                {
                    // ReSharper disable RedundantCast
                    binaryWriter.Write((int)offset);
                    binaryWriter.Write((int)retention.Precision);
                    binaryWriter.Write((int)retention.History);
                    // ReSharper restore RedundantCast

                    archives.Add(new Archive(offset, retention.History * DatapointSize, retention));

                    offset += retention.History * DatapointSize;
                } // foreach

                stream.SetLength(offset);
            } // using

            return(new DatapointDatabase(path, downsamplingFactor, downsamplingMethod, maxRetention, archives, currentTimeProvider));
        }