XU XUv0.16.0

#github.com/xulang-org/packages/influxdb

导入方式: influxdb #github.com/xulang-org/packages/influxdb

公开 API: 6 函数, 25 方法, 6 类型, 0 常量, 0 方法集

扩展包 `influxdb #github.com/xulang-org/packages/influxdb`,由模块 github.com/xulang-org/packages 提供。

github.com/xulang-org/packages · 1.17.2 · 安装与使用

示例代码 · API 声明

示例代码

以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。

32_standalone_influxdb/main.xu

所需 SDK:Xu 0.8.3 · 完整工程 · 下载源码

Xu本地 SDK 示例
@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 声明

open

XuAPI
pub fn open(endpoint string, token string, org string, bucket string) Client

用途

打开资源或建立网络连接。

参数

endpoint string
endpoint(string)
token string
token(string)
org string
org(string)
bucket string
bucket(string)

返回值

Client — 该类型的值

查看用法示例

open_pool_client

XuAPI
pub fn open_pool_client(endpoint string, token string, org string, bucket string, max_idle int) Pool

用途

执行 open pool client 操作。

参数

endpoint string
endpoint(string)
token string
token(string)
org string
org(string)
bucket string
bucket(string)
max_idle int
max_idle(int)

返回值

Pool — 该类型的值

struct Response

XuAPI
pub struct Response {
    pub status_code int
    pub body string
}

用途

Influxdb 服务端响应结果,包含状态码、响应头与正文数据。

公开字段

status_code int
字段(int)
body string
正文

struct Client

XuAPI
pub struct Client {
    pub host string
    pub port int
    pub secure bool
    pub token string
    pub org string
    pub bucket string
}

用途

Influxdb 客户端会话实例,维护与服务端的连接并提供操作接口。

公开字段

host string
主机
port int
端口
secure bool
字段(bool)
token string
字段(string)
org string
字段(string)
bucket string
字段(string)

new_client

XuAPI
pub fn new_client(endpoint string, token string, org string, bucket string) Client

用途

构造 client。

参数

endpoint string
endpoint(string)
token string
token(string)
org string
org(string)
bucket string
bucket(string)

返回值

Client — 该类型的值

($&Client) request

XuAPI
pub fn (c $&Client) request(method string, path string, body string, content_type string, accept string) Response

用途

$&Client 上的操作:请求。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
method string
method(string)
path string
文件系统路径(string)
body string
正文(string)
content_type string
content_type(string)
accept string
accept(string)

返回值

Response — 该类型的值

struct Tag

XuAPI
pub struct Tag {
    pub key string
    pub val string
}

用途

承载 key、val 等属性的结构体。

公开字段

key string
键
val string
值

struct Field

XuAPI
pub struct Field {
    pub key string
    pub val string
}

用途

承载 key、val 等属性的结构体。

公开字段

key string
键
val string
值

struct Point

XuAPI
pub struct Point {
    pub measurement string
    pub tags []Tag
    pub fields []Field
    pub timestamp int
}

用途

承载 measurement、tags、fields、timestamp 等属性的结构体。

公开字段

measurement string
字段(string)
tags []Tag
字段([]Tag)
fields []Field
字段([]Field)
timestamp int
字段(int)

new_point

XuAPI
pub fn new_point(measurement string) Point

用途

构造 point。

参数

measurement string
measurement(string)

返回值

Point — 该类型的值

查看用法示例

($&Point) add_tag

XuAPI
pub fn (p $&Point) add_tag(key string, val string)

用途

$&Point 上的操作:增加标签。

参数

p $&Point
本次调用使用的 Point 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
key string
键(string)
val string
值(string)

返回值

不返回值。

($&Point) add_field_string

XuAPI
pub fn (p $&Point) add_field_string(key string, val string)

用途

$&Point 上的操作:执行 add field string 操作。

参数

p $&Point
本次调用使用的 Point 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
key string
键(string)
val string
值(string)

返回值

不返回值。

($&Point) add_field_int

XuAPI
pub fn (p $&Point) add_field_int(key string, val int)

用途

$&Point 上的操作:执行 add field int 操作。

参数

p $&Point
本次调用使用的 Point 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
key string
键(string)
val int
值(int)

返回值

不返回值。

($&Point) add_field_float

XuAPI
pub fn (p $&Point) add_field_float(key string, val string)

用途

$&Point 上的操作:执行 add field float 操作。

参数

p $&Point
本次调用使用的 Point 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
key string
键(string)
val string
值(string)

返回值

不返回值。

($&Point) add_field_bool

XuAPI
pub fn (p $&Point) add_field_bool(key string, val bool)

用途

$&Point 上的操作:执行 add field bool 操作。

参数

p $&Point
本次调用使用的 Point 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
key string
键(string)
val bool
值(bool)

返回值

不返回值。

($&Point) set_timestamp

XuAPI
pub fn (p $&Point) set_timestamp(ts int)

用途

$&Point 上的操作:设置 timestamp。

参数

p $&Point
本次调用使用的 Point 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
ts int
ts(int)

返回值

不返回值。

(&Point) to_line

XuAPI
pub fn (p &Point) to_line() string

用途

&Point 上的操作:转换为 line。

参数

p &Point
本次调用使用的 Point 实例;以只读借用传入,调用方仍持有实例。

返回值

string — 结果字符串

($&Client) write_point

XuAPI
pub fn (c $&Client) write_point(point Point) bool

用途

$&Client 上的操作:执行 write point 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
point Point
point(Point)

返回值

bool — 是否成立

($&Client) write_line

XuAPI
pub fn (c $&Client) write_line(line string) bool

用途

$&Client 上的操作:执行 write line 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
line string
line(string)

返回值

bool — 是否成立

($&Client) write_raw

XuAPI
pub fn (c $&Client) write_raw(lines string) bool

用途

$&Client 上的操作:执行 write raw 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
lines string
lines(string)

返回值

bool — 是否成立

struct Pool

XuAPI
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
}

用途

InfluxDB 连接池管理器,维护复用套接字连接。

公开字段

host string
主机
port int
端口
secure bool
字段(bool)
token string
字段(string)
org string
字段(string)
bucket string
字段(string)
state chan pool.State
字段(通道;类型 chan pool.State)
max_idle int
字段(int)

new_pool

XuAPI
pub fn new_pool(endpoint string, token string, org string, bucket string, max_idle int) Pool

用途

构造 pool。

参数

endpoint string
endpoint(string)
token string
token(string)
org string
org(string)
bucket string
bucket(string)
max_idle int
max_idle(int)

返回值

Pool — 该类型的值

open_pool

XuAPI
pub fn open_pool(endpoint string, token string, org string, bucket string, max_idle int) Pool

用途

执行 open pool 操作。

参数

endpoint string
endpoint(string)
token string
token(string)
org string
org(string)
bucket string
bucket(string)
max_idle int
max_idle(int)

返回值

Pool — 该类型的值

查看用法示例

(&Pool) get

XuAPI
pub fn (p &Pool) get() Client

用途

在 &Pool 上执行 get 操作。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

Client — 该类型的值

(&Pool) dup

XuAPI
pub fn (p &Pool) dup() Pool

用途

创建并返回持有独立状态或底层的副本对象。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

Pool — 该类型的值

(&Pool) get_conn

XuAPI
pub fn (p &Pool) get_conn() int

用途

&Pool 上的操作:读取 conn。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

int — 该类型的值

(&Pool) put_conn

XuAPI
pub fn (p &Pool) put_conn(fd int)

用途

&Pool 上的操作:执行 put conn 操作。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
fd int
文件描述符(int)

返回值

不返回值。

(&Pool) discard_conn

XuAPI
pub fn (p &Pool) discard_conn(fd int)

用途

&Pool 上的操作:丢弃连接。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
fd int
文件描述符(int)

返回值

不返回值。

(&Pool) write_point

XuAPI
pub fn (p &Pool) write_point(point Point) bool

用途

&Pool 上的操作:执行 write point 操作。失败时抛出错误,调用方需处理或继续传播。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
point Point
point(Point)

返回值

bool — 是否成立

(&Pool) write_line

XuAPI
pub fn (p &Pool) write_line(line string) bool

用途

&Pool 上的操作:执行 write line 操作。失败时抛出错误,调用方需处理或继续传播。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
line string
line(string)

返回值

bool — 是否成立

(&Pool) write_raw

XuAPI
pub fn (p &Pool) write_raw(lines string) bool

用途

&Pool 上的操作:执行 write raw 操作。失败时抛出错误,调用方需处理或继续传播。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
lines string
lines(string)

返回值

bool — 是否成立

(&Pool) query_flux

XuAPI
pub fn (p &Pool) query_flux(flux string) string

用途

&Pool 上的操作:执行 query flux 操作。失败时抛出错误,调用方需处理或继续传播。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
flux string
flux(string)

返回值

string — 结果字符串

(&Pool) ping

XuAPI
pub fn (p &Pool) ping() bool

用途

发送心跳保活探测请求,检测连接活性与服务可用性。失败时抛出错误,调用方需处理或继续传播。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

bool — 是否成立

(&Pool) close

XuAPI
pub fn (p &Pool) close()

用途

&Pool 上的操作:关闭资源。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

不返回值。

($&Client) health

XuAPI
pub fn (c $&Client) health() bool

用途

在 $&Client 上执行 health 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。

返回值

bool — 是否成立

($&Client) ping

XuAPI
pub fn (c $&Client) ping() bool

用途

发送心跳保活探测请求,检测连接活性与服务可用性。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。

返回值

bool — 是否成立

($&Client) query_flux

XuAPI
pub fn (c $&Client) query_flux(flux string) string

用途

$&Client 上的操作:执行 query flux 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
flux string
flux(string)

返回值

string — 结果字符串