/// <summary> /// Formats the timestamp to an integer string with the specified precision. /// </summary> /// <param name="timestamp">The timestamp to format.</param> /// <param name="precision">The precision to format the timestamp to.</param> /// <returns>The timestamp formatted to a string with the specified precision.</returns> public static String FormatTimestamp(DateTime timestamp, InfluxPrecision precision) { if (timestamp < unixEpoch) { throw new ArgumentOutOfRangeException(nameof(timestamp), "The timestamp cannot be earlier than the UNIX epoch (1970/1/1)."); } Int64 longTime = 0L; TimeSpan sinceEpoch = timestamp - unixEpoch; switch (precision) { case InfluxPrecision.Nanoseconds: longTime = sinceEpoch.Ticks * 100L; break; // 100ns per tick case InfluxPrecision.Microseconds: longTime = sinceEpoch.Ticks / 10L; break; // 10 ticks per us case InfluxPrecision.Milliseconds: longTime = sinceEpoch.Ticks / TimeSpan.TicksPerMillisecond; break; case InfluxPrecision.Seconds: longTime = sinceEpoch.Ticks / TimeSpan.TicksPerSecond; break; case InfluxPrecision.Minutes: longTime = sinceEpoch.Ticks / TimeSpan.TicksPerMinute; break; case InfluxPrecision.Hours: longTime = sinceEpoch.Ticks / TimeSpan.TicksPerHour; break; default: throw new ArgumentException(nameof(precision), $"Invalid timestamp precision: {precision}"); } return(String.Format(CultureInfo.InvariantCulture, "{0:d}", longTime)); }
/// <summary> /// Creates a URI for the specified hostname and database using authentication. Optionally uses the default retention policy (DEFAULT) and time precision (s). /// </summary> /// <param name="scheme">The URI scheme type, ie. http, https, net.udp</param> /// <param name="host">The hostname or IP address of the InfluxDB server.</param> /// <param name="port">The port number of the InfluxDB server. Set to zero to use the default of <see cref="InfluxConfig.Default.PortHttp"/>. This value is required for the UDP protocol.</param> /// <param name="database">The name of the database to write records to.</param> /// <param name="username">The username to use to authenticate to the InfluxDB server. Leave blank to skip authentication.</param> /// <param name="password">The password to use to authenticate to the InfluxDB server. Leave blank to skip authentication.</param> /// <param name="retentionPolicy">The retention policy to use. Leave blank to use the server default of "DEFAULT".</param> /// <param name="precision">The timestamp precision specifier used in the line protocol writes. Leave blank to use the default of <see cref="InfluxConfig.Default.Precision"/>.</param> /// <returns>A new InfluxDB URI using the specified parameters.</returns> public static Uri FormatInfluxUri(String scheme, String host, UInt16?port, String database, String username, String password, String retentionPolicy = null, InfluxPrecision?precision = null) { scheme = scheme ?? InfluxUtils.SchemeHttp; if ((port ?? 0) == 0 && (scheme == SchemeHttp || scheme == SchemeHttps)) { port = InfluxConfig.Default.PortHttp; } InfluxPrecision prec = precision ?? InfluxConfig.Default.Precision; String uriString = $@"{scheme}://{host}:{port}/write?db={database}"; if (!String.IsNullOrWhiteSpace(username)) { uriString += $@"&u={username}"; } if (!String.IsNullOrWhiteSpace(password)) { uriString += $@"&p={password}"; } if (!String.IsNullOrWhiteSpace(retentionPolicy)) { uriString += $@"&rp={retentionPolicy}"; } if (prec != InfluxPrecision.Nanoseconds) { uriString += $@"&precision={prec.ToShortName()}"; // only need to specify precision if it's not nanoseconds (the InfluxDB default) } return(new Uri(uriString)); //return new Uri($@"{scheme}://{host}:{port}/write?db={database}&u={username}&p={password}&rp={retentionPolicy}&precision={prec.ToShortName()}"); }
/// <summary> /// Gets the short name (n,u,ms,s,m,h) for the InfluxDB precision specifier to be used in the URI query string. /// </summary> /// <param name="precision">The precision to get the short name for.</param> /// <returns>The short name for the <see cref="InfluxPrecision"/> value.</returns> public static String ToShortName(this InfluxPrecision precision) { switch (precision) { case InfluxPrecision.Nanoseconds: return("n"); case InfluxPrecision.Microseconds: return("u"); case InfluxPrecision.Milliseconds: return("ms"); case InfluxPrecision.Seconds: return("s"); case InfluxPrecision.Minutes: return("m"); case InfluxPrecision.Hours: return("h"); default: throw new ArgumentException(nameof(precision), $"Invalid timestamp precision: {precision}"); } }
static Default() { PortHttp = 8086; Precision = InfluxPrecision.Seconds; }
/// <summary> /// Creates an HTTP JSON URI for InfluxDB using the values specified in the <see cref="InfluxConfig"/> object. /// </summary> /// <param name="config">The configuration object to get the relevant fields to build the HTTP URI from.</param> /// <returns>A new InfluxDB JSON URI using the configuration specified in the <paramref name="config"/> parameter.</returns> protected Uri FormatInfluxUri(InfluxConfig config) { InfluxPrecision prec = config.Precision ?? InfluxConfig.Default.Precision; return(new Uri($@"http://{config.Hostname}:{config.Port}/db/{config.Database}/series?u={config.Username}&p={config.Password}&time_precision={prec.ToShortName()}")); }