#github.com/xulang-org/packages/influxdb
Xu 0.16.0 API
import: influxdb #github.com/xulang-org/packages/influxdb
counts: 6 functions, 25 methods, 6 types, 0 constants, 0 method sets
InfluxDB v2 Time-Series Database Driver for Xu Features Native HTTP/1.1 REST communication via Xu standard library. Full support for InfluxDB Line Protocol point creation and batch writing. Flux script query support with annotated CSV output. Server health check & ping endpoints. Reusable HTTP connections with shared borrow/return and close state. Full compatibility with InfluxDB 2.x+.
github.com/xulang-org/packages · 1.17.2 · Install and use
Examples · API declarations
Examples
These sources come from the official examples repository and can be copied or downloaded. Run them with a local Xu SDK in the complete project; network, database, and desktop examples require the corresponding services or assets.
32_standalone_influxdb/main.xu
Required SDK: Xu 0.8.3 · Complete project · Download source
@main
influxdb #github.com/xulang-org/packages/influxdb
#errors
fn verify_influxdb_health() {
println("--- 1. Testing InfluxDB Health & Ping ---")
$client := influxdb.open(
"http://127.0.0.1:8086",
"my-super-secret-token",
"xu-org",
"xu-bucket"
)
is_ping := client.ping() >>> (err) {
println(" [FAIL] Ping failed: " + err.msg())
<<
}
if is_ping {
println(" [PASS] Ping succeeded.")
}
is_health := client.health() >>> (err) {
println(" [FAIL] Health check failed: " + err.msg())
<<
}
if is_health {
println(" [PASS] Health check passed.")
}
}
fn verify_influxdb_write_query() {
println("\n--- 2. Testing InfluxDB Line Protocol & Flux Query ---")
$client := influxdb.open(
"http://127.0.0.1:8086",
"my-super-secret-token",
"xu-org",
"xu-bucket"
)
// 1. Write single point using Point builder
println(" Constructing point 'cpu_load' with tags & fields...")
$pt := influxdb.new_point("cpu_load")
pt.add_tag("host", "worker-01")
pt.add_tag("env", "prod")
pt.add_field_float("usage", "45.8")
pt.add_field_int("cores", 16)
pt.add_field_bool("alert", false)
client.write_point(pt) >>> (err) {
println(" [FAIL] write_point failed: " + err.msg())
<<
}
println(" [PASS] Point written successfully.")
// 2. Write raw batch lines
println(" Writing batch metrics via raw line protocol...")
$raw_lines := "mem_metrics,host=worker-01 total=32768i,used=16384i\n"
raw_lines = raw_lines + "mem_metrics,host=worker-02 total=65536i,used=20480i\n"
client.write_raw(raw_lines) >>> (err) {
println(" [FAIL] write_raw failed: " + err.msg())
<<
}
println(" [PASS] Batch lines written successfully.")
// 3. Query using Flux
println(" Executing Flux query...")
flux := "from(bucket: \"xu-bucket\") |> range(start: -1h) |> filter(fn: (r) => r._measurement == \"cpu_load\") |> limit(n: 5)"
csv := client.query_flux(flux) >>> (err) {
println(" [FAIL] Flux query failed: " + err.msg())
<<
}
println(" Flux result preview:")
println(csv)
println(" [PASS] Flux query executed successfully.")
}
fn verify_influxdb_pool() {
println("\n--- 3. Testing InfluxDB Connection Pool ---")
$p := influxdb.open_pool(
"http://127.0.0.1:8086",
"my-super-secret-token",
"xu-org",
"xu-bucket",
5
)
// 1. Connection Reuse Test
fd1 := p.get_conn()
println(" Borrow 1 fd: " + str(fd1))
p.put_conn(fd1)
fd2 := p.get_conn()
println(" Borrow 2 (after return) fd: " + str(fd2))
if fd1 == fd2 {
println(" [PASS] Connection reused successfully (fd " + str(fd1) + " == " + str(fd2) + ")")
} else {
println(" [WARN] Different fd allocated: " + str(fd1) + " vs " + str(fd2))
}
// 2. Multi-connection concurrent borrow
fd3 := p.get_conn()
println(" Borrow 3 (concurrently) fd: " + str(fd3))
if fd2 != fd3 {
println(" [PASS] Concurrently allocated distinct fds (" + str(fd2) + " != " + str(fd3) + ")")
} else {
println(" [FAIL] Expected distinct fds for concurrent connections!")
}
p.put_conn(fd2)
p.put_conn(fd3)
// 3. Delegated operations via pool
println(" Testing delegated operations via pool...")
$pt := influxdb.new_point("pool_sensor")
pt.add_tag("room", "lab-01")
pt.add_field_float("humidity", "55.2")
p.write_point(pt) >>> (err) {
println(" [FAIL] Pool write_point error: " + err.msg())
<<
}
println(" [PASS] Pool write_point succeeded.")
p.write_raw("pool_sensor,room=lab-02 humidity=60.1\n") >>> (err) {
println(" [FAIL] Pool write_raw error: " + err.msg())
<<
}
println(" [PASS] Pool write_raw succeeded.")
flux := "from(bucket: \"xu-bucket\") |> range(start: -5m) |> filter(fn: (r) => r._measurement == \"pool_sensor\") |> limit(n: 5)"
csv := p.query_flux(flux) >>> (err) {
println(" [FAIL] Pool query_flux error: " + err.msg())
<<
}
println(" Retrieved flux data preview length: " + str(csv.len) + " bytes")
if csv.len > 0 {
println(" [PASS] Pool delegated query_flux succeeded.")
} else {
println(" [FAIL] Pool query returned empty.")
}
p.close()
println(" [PASS] InfluxDB Pool closed cleanly.")
}
fn main() {
println("=== Xu InfluxDB v2 Time-Series Database Driver Verification ===\n")
verify_influxdb_health()
verify_influxdb_write_query()
verify_influxdb_pool()
println("\n[INFLUXDB VERIFICATION 100% COMPLETED]")
}
API declarations
open
pub fn open(endpoint string, token string, org string, bucket string) Client
Purpose
Open resource or establish connection.
Parameters
endpoint string- endpoint (string)
token string- token (string)
org string- org (string)
bucket string- bucket (string)
Returns
Client — a value of this type
See usage example
open_pool_client
pub fn open_pool_client(endpoint string, token string, org string, bucket string, max_idle int) Pool
Purpose
Open pool client.
Parameters
endpoint string- endpoint (string)
token string- token (string)
org string- org (string)
bucket string- bucket (string)
max_idle int- max_idle (int)
Returns
Pool — a value of this type
struct Response
pub struct Response {
pub status_code int
pub body string
}
Purpose
Influxdb Server response payload containing status, headers, and body.
Public fields
status_code int- field (int)
body string- body
struct Client
pub struct Client {
pub host string
pub port int
pub secure bool
pub token string
pub org string
pub bucket string
}
Purpose
Influxdb Client session maintaining connection and operations.
Public fields
host string- host
port int- port
secure bool- field (bool)
token string- field (string)
org string- field (string)
bucket string- field (string)
new_client
pub fn new_client(endpoint string, token string, org string, bucket string) Client
Purpose
Construct client.
Parameters
endpoint string- endpoint (string)
token string- token (string)
org string- org (string)
bucket string- bucket (string)
Returns
Client — a value of this type
($&Client) request
pub fn (c $&Client) request(method string, path string, body string, content_type string, accept string) Response
Purpose
Execute request on $&Client. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&Client- The
Client instance used by this call (exclusive borrow).
method string- method (string)
path string- filesystem path (string)
body string- body (string)
content_type string- content_type (string)
accept string- accept (string)
Returns
Response — a value of this type
struct Tag
pub struct Tag {
pub key string
pub val string
}
Purpose
Structure encapsulating key, val.
Public fields
key string- key
val string- value
struct Field
pub struct Field {
pub key string
pub val string
}
Purpose
Structure encapsulating key, val.
Public fields
key string- key
val string- value
struct Point
pub struct Point {
pub measurement string
pub tags []Tag
pub fields []Field
pub timestamp int
}
Purpose
Structure encapsulating measurement, tags, fields, timestamp.
Public fields
measurement string- field (string)
tags []Tag- field ([]Tag)
fields []Field- field ([]Field)
timestamp int- field (int)
new_point
pub fn new_point(measurement string) Point
Purpose
Construct point.
Parameters
measurement string- measurement (string)
Returns
Point — a value of this type
See usage example
($&Point) add_tag
pub fn (p $&Point) add_tag(key string, val string)
Purpose
Operation on $&Point: add tag.
Parameters
p $&Point- The
Point instance used by this call (exclusive borrow).
key string- key (string)
val string- value (string)
Returns
This call does not return a value.
($&Point) add_field_string
pub fn (p $&Point) add_field_string(key string, val string)
Purpose
Operation on $&Point: add field string.
Parameters
p $&Point- The
Point instance used by this call (exclusive borrow).
key string- key (string)
val string- value (string)
Returns
This call does not return a value.
($&Point) add_field_int
pub fn (p $&Point) add_field_int(key string, val int)
Purpose
Operation on $&Point: add field int.
Parameters
p $&Point- The
Point instance used by this call (exclusive borrow).
key string- key (string)
val int- value (int)
Returns
This call does not return a value.
($&Point) add_field_float
pub fn (p $&Point) add_field_float(key string, val string)
Purpose
Operation on $&Point: add field float.
Parameters
p $&Point- The
Point instance used by this call (exclusive borrow).
key string- key (string)
val string- value (string)
Returns
This call does not return a value.
($&Point) add_field_bool
pub fn (p $&Point) add_field_bool(key string, val bool)
Purpose
Operation on $&Point: add field bool.
Parameters
p $&Point- The
Point instance used by this call (exclusive borrow).
key string- key (string)
val bool- value (bool)
Returns
This call does not return a value.
($&Point) set_timestamp
pub fn (p $&Point) set_timestamp(ts int)
Purpose
Operation on $&Point: set timestamp.
Parameters
p $&Point- The
Point instance used by this call (exclusive borrow).
ts int- ts (int)
Returns
This call does not return a value.
(&Point) to_line
pub fn (p &Point) to_line() string
Purpose
Operation on &Point: convert to line.
Parameters
p &Point- The
Point instance used by this call (borrow).
Returns
string — result string
($&Client) write_point
pub fn (c $&Client) write_point(point Point) bool
Purpose
Operation on $&Client: write point. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&Client- The
Client instance used by this call (exclusive borrow).
point Point- point (Point)
Returns
bool — true or false
($&Client) write_line
pub fn (c $&Client) write_line(line string) bool
Purpose
Operation on $&Client: write line. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&Client- The
Client instance used by this call (exclusive borrow).
line string- line (string)
Returns
bool — true or false
($&Client) write_raw
pub fn (c $&Client) write_raw(lines string) bool
Purpose
Operation on $&Client: write raw. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&Client- The
Client instance used by this call (exclusive borrow).
lines string- lines (string)
Returns
bool — true or false
struct Pool
pub struct Pool {
pub host string
pub port int
pub secure bool
pub token string
pub org string
pub bucket string
pub state chan pool.State
pub max_idle int
}
Purpose
Connection Pool for InfluxDB v2. Manages reusable TCP socket connections and provides Client instances.
Public fields
host string- host
port int- port
secure bool- field (bool)
token string- field (string)
org string- field (string)
bucket string- field (string)
state chan pool.State- field (channel; type chan pool.State)
max_idle int- field (int)
new_pool
pub fn new_pool(endpoint string, token string, org string, bucket string, max_idle int) Pool
Purpose
Construct pool.
Parameters
endpoint string- endpoint (string)
token string- token (string)
org string- org (string)
bucket string- bucket (string)
max_idle int- max_idle (int)
Returns
Pool — a value of this type
open_pool
pub fn open_pool(endpoint string, token string, org string, bucket string, max_idle int) Pool
Purpose
Open pool.
Parameters
endpoint string- endpoint (string)
token string- token (string)
org string- org (string)
bucket string- bucket (string)
max_idle int- max_idle (int)
Returns
Pool — a value of this type
See usage example
(&Pool) get
pub fn (p &Pool) get() Client
Purpose
Execute get on &Pool.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
Returns
Client — a value of this type
(&Pool) dup
pub fn (p &Pool) dup() Pool
Purpose
Shares sockets and close state; clones configuration strings.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
Returns
Pool — a value of this type
(&Pool) get_conn
pub fn (p &Pool) get_conn() int
Purpose
Operation on &Pool: read conn.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
Returns
int — a value of this type
(&Pool) put_conn
pub fn (p &Pool) put_conn(fd int)
Purpose
Operation on &Pool: put connection.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
fd int- file descriptor (int)
Returns
This call does not return a value.
(&Pool) discard_conn
pub fn (p &Pool) discard_conn(fd int)
Purpose
Operation on &Pool: discard connection.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
fd int- file descriptor (int)
Returns
This call does not return a value.
(&Pool) write_point
pub fn (p &Pool) write_point(point Point) bool
Purpose
Operation on &Pool: write point. Throws on failure; handle the error or propagate it to the caller.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
point Point- point (Point)
Returns
bool — true or false
(&Pool) write_line
pub fn (p &Pool) write_line(line string) bool
Purpose
Operation on &Pool: write line. Throws on failure; handle the error or propagate it to the caller.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
line string- line (string)
Returns
bool — true or false
(&Pool) write_raw
pub fn (p &Pool) write_raw(lines string) bool
Purpose
Operation on &Pool: write raw. Throws on failure; handle the error or propagate it to the caller.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
lines string- lines (string)
Returns
bool — true or false
(&Pool) query_flux
pub fn (p &Pool) query_flux(flux string) string
Purpose
Operation on &Pool: query flux. Throws on failure; handle the error or propagate it to the caller.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
flux string- flux (string)
Returns
string — result string
(&Pool) ping
pub fn (p &Pool) ping() bool
Purpose
Send a ping heartbeat request to verify connection liveness. Throws on failure; handle the error or propagate it to the caller.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
Returns
bool — true or false
(&Pool) close
Purpose
Operation on &Pool: close the resource.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
Returns
This call does not return a value.
($&Client) health
pub fn (c $&Client) health() bool
Purpose
Execute health on $&Client. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&Client- The
Client instance used by this call (exclusive borrow).
Returns
bool — true or false
($&Client) ping
pub fn (c $&Client) ping() bool
Purpose
Send a ping heartbeat request to verify connection liveness. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&Client- The
Client instance used by this call (exclusive borrow).
Returns
bool — true or false
($&Client) query_flux
pub fn (c $&Client) query_flux(flux string) string
Purpose
Operation on $&Client: query flux. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&Client- The
Client instance used by this call (exclusive borrow).
flux string- flux (string)
Returns
string — result string