public void WriteAnnotation(DateTime timestamp, string title, string message, params string[] tags) { using (var fileStream = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.Read)) using (var binaryWriter = new Util.BinaryWriter(fileStream, Encoding.UTF8, true)) { fileStream.Seek(0, SeekOrigin.End); var ts = ConvertToTimestamp(timestamp.ToUniversalTime()); binaryWriter.Write(ts); var buffer = Encoding.UTF8.GetBytes(title); binaryWriter.Write(buffer.Length); binaryWriter.Write(buffer); buffer = Encoding.UTF8.GetBytes(message); binaryWriter.Write(buffer.Length); binaryWriter.Write(buffer); var tagCount = tags == null ? 0 : tags.Length; binaryWriter.Write(tagCount); if (tags != null) { foreach (var tag in tags) { buffer = Encoding.UTF8.GetBytes(tag); binaryWriter.Write(buffer.Length); binaryWriter.Write(buffer); } // foreach } binaryWriter.Flush(); }// using }
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 }
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 }