using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics; using System.Numerics; using InfluxDB.Client.Api.Domain; using InfluxDB.Client.Core; using NodaTime; namespace InfluxDB.Client.Writes { public partial class PointData { public sealed class Builder { private readonly string _measurementName; private readonly Dictionary _tags = new Dictionary(); private readonly Dictionary _fields = new Dictionary(); private WritePrecision _precision; private BigInteger? _time; private Builder(string measurementName) { Arguments.CheckNonEmptyString(measurementName, "Measurement name"); _measurementName = measurementName; _precision = WritePrecision.Ns; } /// /// Create a new Point withe specified a measurement name. /// /// the measurement name /// the new Point public static Builder Measurement(string measurementName) { return new Builder(measurementName); } /// /// Adds or replaces a tag value for a point. /// /// the tag name /// the tag value /// this public Builder Tag(string name, string value) { var isEmptyValue = string.IsNullOrEmpty(value); if (isEmptyValue) { if (_tags.ContainsKey(name)) { Trace.TraceWarning( $"Empty tags will cause deletion of, tag [{name}], measurement [{_measurementName}]"); _tags.Remove(name); } else { Trace.TraceWarning($"Empty tags has no effect, tag [{name}], measurement [{_measurementName}]"); } } else { _tags[name] = value; } return this; } /// /// Add a field with a value. /// /// the field name /// the field value /// this public Builder Field(string name, byte value) { return PutField(name, value); } /// /// Add a field with a value. /// /// the field name /// the field value /// this public Builder Field(string name, float value) { return PutField(name, value); } /// /// Add a field with a value. /// /// the field name /// the field value /// this public Builder Field(string name, double value) { return PutField(name, value); } /// /// Add a field with a value. /// /// the field name /// the field value /// this public Builder Field(string name, decimal value) { return PutField(name, value); } /// /// Add a field with a value. /// /// the field name /// the field value /// this public Builder Field(string name, long value) { return PutField(name, value); } /// /// Add a field with a value. /// /// the field name /// the field value /// this public Builder Field(string name, ulong value) { return PutField(name, value); } /// /// Add a field with a value. /// /// the field name /// the field value /// this public Builder Field(string name, uint value) { return PutField(name, value); } /// /// Add a field with a value. /// /// the field name /// the field value /// this public Builder Field(string name, string value) { return PutField(name, value); } /// /// Add a field with a value. /// /// the field name /// the field value /// this public Builder Field(string name, bool value) { return PutField(name, value); } /// /// Add a field with an value. /// /// the field name /// the field value /// this public Builder Field(string name, object value) { return PutField(name, value); } /// /// Updates the timestamp for the point. /// /// the timestamp /// the timestamp precision /// public Builder Timestamp(long timestamp, WritePrecision timeUnit) { _precision = timeUnit; _time = timestamp; return this; } /// /// Updates the timestamp for the point represented by . /// /// the timestamp /// the timestamp precision /// public Builder Timestamp(TimeSpan timestamp, WritePrecision timeUnit) { _time = TimeSpanToBigInteger(timestamp, timeUnit); _precision = timeUnit; return this; } /// /// Updates the timestamp for the point represented by . /// /// the timestamp /// the timestamp precision /// public Builder Timestamp(DateTime timestamp, WritePrecision timeUnit) { if (timestamp != null && timestamp.Kind != DateTimeKind.Utc) { throw new ArgumentException("Timestamps must be specified as UTC", nameof(timestamp)); } var timeSpan = timestamp.Subtract(EpochStart); return Timestamp(timeSpan, timeUnit); } /// /// Updates the timestamp for the point represented by . /// /// the timestamp /// the timestamp precision /// public Builder Timestamp(DateTimeOffset timestamp, WritePrecision timeUnit) { return Timestamp(timestamp.UtcDateTime, timeUnit); } /// /// Updates the timestamp for the point represented by . /// /// the timestamp /// the timestamp precision /// public Builder Timestamp(Instant timestamp, WritePrecision timeUnit) { _time = InstantToBigInteger(timestamp, timeUnit); _precision = timeUnit; return this; } /// /// Has point any fields? /// /// true, if the point contains any fields, false otherwise. public bool HasFields() { return _fields.Count > 0; } /// /// The PointData /// /// public PointData ToPointData() { return new PointData(_measurementName, _precision, _time, ImmutableSortedDictionary.CreateRange(_tags), ImmutableSortedDictionary.CreateRange(_fields)); } private Builder PutField(string name, object value) { Arguments.CheckNonEmptyString(name, "Field name"); _fields[name] = value; return this; } } } }