moonflux

    Pure MoonBit InfluxDB v2 client, Line Protocol toolkit, annotated CSV parser, and CLI.

    influxdb
    line-protocol
    flux
    time-series
    Download zip
    Author
    Version
    0.1.2
    License
    Apache-2.0
    Last updated
    6 hours ago
    Downloads
    11

    #MoonFlux

    MoonFlux is a pure MoonBit InfluxDB v2 toolkit:

    • typed Point builder and InfluxDB Line Protocol encoder
    • Line Protocol parser with escape and quoted-string handling
    • annotated CSV parser for Flux query responses
    • multi-table Flux result model with typed cells and group keys
    • small HTTP client for /health, /api/v2/write, and /api/v2/query
    • command-line interface for ping, write, query, and encode
    • bounded batch splitting for large Line Protocol payloads
    • reproducible benchmarks and boundary tests
    • Docker-based integration smoke test against a real InfluxDB v2 instance

    #Install

    moon add zkc-long/moonflux@0.1.2

    #Library

    ///|
    let point = @moonflux.new_point("weather")
    .add_tag("location", "shanghai")
    .add_field("temperature", @moonflux.DoubleValue(21.5))
    .add_field("ok", @moonflux.BoolValue(true))

    ///|
    let line = @moonflux.encode_point(point).unwrap()

    ///|
    let parsed = @moonflux.parse_line(line).unwrap()

    For HTTP access:

    ///|
    let config = @client.ClientConfig(
    url="http://localhost:8086",
    token="my-token",
    org="my-org",
    bucket="my-bucket",
    )

    ///|
    let client = @client.new_client(config)

    ///|
    let status = client.write(line)

    The client also exposes write_with_precision for ns, us, ms, or s timestamp precision.

    For full Flux result semantics:

    ///|
    match client.query_flux("from(bucket: \"my-bucket\") |> range(start: -1h)") {
    Ok(result) => println(result.table_count())
    Err(error) => println(error)
    }

    #CLI

    moon run --target native cmd/main -- ping --url http://localhost:8086 moon run --target native cmd/main -- write --url http://localhost:8086 --token my-token --org my-org --bucket my-bucket --data "weather,location=shanghai temperature=21.5" moon run --target native cmd/main -- write --url http://localhost:8086 --token my-token --org my-org --bucket my-bucket --file points.lp --precision ns moon run --target native cmd/main -- write --url http://localhost:8086 --token my-token --org my-org --bucket my-bucket --file points.lp --batch-size 5000 --batch-bytes 1048576 moon run --target native cmd/main -- query --url http://localhost:8086 --token my-token --org my-org --query "from(bucket: \"my-bucket\")" moon run --target native cmd/main -- query --format json --url http://localhost:8086 --token my-token --org my-org --query "from(bucket: \"my-bucket\") |> group(columns: [\"host\"])" moon run --target native cmd/main -- encode --measurement weather --field 21.5

    CLI options fall back to INFLUX_URL, INFLUX_TOKEN, INFLUX_ORG, and INFLUX_BUCKET.

    #InfluxDB Smoke Test

    The MVP scope, acceptance criteria, and known boundaries are documented in docs/MVP.zh-CN.md.

    The Chinese project application draft is available at docs/APPLICATION.zh-CN.md.

    Multi-table query semantics are documented in docs/FLUX_QUERY_SEMANTICS.zh-CN.md, and the official requirement map is in docs/OFFICIAL_REQUIREMENTS.zh-CN.md.

    docker compose up -d bash scripts/integration.sh docker compose down

    #Benchmarks

    moon run --target native bench

    The deterministic benchmark covers 10,000 point encodings, 10,000 Line Protocol rows, and 10,000 multi-table annotated CSV rows. Measured data is in docs/BENCHMARKS.zh-CN.md.

    #Development

    moon check moon test

    Native builds require a system C compiler. Library tests run on the default backend.

    AnnotatedColumn

    pub(all) struct AnnotatedColumn {
    name : String
    data_type : String
    group : Bool
    default : String
    }

    Column metadata from an InfluxDB annotated CSV response.

    AnnotatedCsv

    pub(all) struct AnnotatedCsv {
    columns : Array[AnnotatedColumn]
    rows : Array[AnnotatedRow]
    }

    Parsed annotated CSV response.

    AnnotatedRow

    pub(all) struct AnnotatedRow {
    values : Array[String]
    }

    One data row from an annotated CSV response.

    BatchLimits

    pub(all) struct BatchLimits {
    max_points : Int
    max_bytes : Int
    }

    Limits used when splitting a Line Protocol payload for transport.

    BatchLimits::new

    fn BatchLimits::new(max_points : Int, max_bytes : Int) -> BatchLimits

    Create explicit batch limits.

    FieldValue

    pub(all) enum FieldValue {
    IntValue(Int64)
    UIntValue(UInt64)
    DoubleValue(Double)
    StringValue(String)
    BoolValue(Bool)
    }

    InfluxDB field value variants.

    FluxCell

    pub(all) struct FluxCell {
    raw : String
    value : FluxValue
    }

    One annotated CSV cell with its raw text and parsed value.

    FluxColumn

    pub(all) struct FluxColumn {
    name : String
    data_type : String
    group : Bool
    default : String
    }

    Column metadata for one Flux result table.

    FluxQueryResult

    pub(all) struct FluxQueryResult {
    tables : Array[FluxTable]
    }

    Multi-table Flux query result.

    FluxQueryResult::table_count

    fn FluxQueryResult::table_count(self : FluxQueryResult) -> Int

    Number of tables in a Flux result.

    FluxTable

    pub(all) struct FluxTable {
    columns : Array[FluxColumn]
    rows : Array[Array[FluxCell]]
    }

    One table in a Flux annotated CSV response.

    FluxTable::column_index

    fn FluxTable::column_index(self : FluxTable, name : String) -> Int?

    Find a column index by name.

    FluxTable::column_names

    fn FluxTable::column_names(self : FluxTable) -> Array[String]

    Return column names in table order.

    FluxTable::field

    fn FluxTable::field(self : FluxTable) -> String?

    Return the first row's field.

    FluxTable::group_key_string

    fn FluxTable::group_key_string(self : FluxTable, row_index : Int) -> String

    Render group keys as a stable display string.

    FluxTable::group_pairs

    fn FluxTable::group_pairs(self : FluxTable, row_index : Int) -> Array[(String, String)]

    Return the group-key columns for one row.

    FluxTable::measurement

    fn FluxTable::measurement(self : FluxTable) -> String?

    Return the first row's measurement.

    FluxTable::row_count

    fn FluxTable::row_count(self : FluxTable) -> Int

    Number of data rows in a Flux table.

    FluxTable::string_value

    fn FluxTable::string_value(self : FluxTable, row_index : Int, column_name : String) -> String?

    Return a named string value from one row.

    FluxTable::table_id

    fn FluxTable::table_id(self : FluxTable) -> String?

    Return the first row's table identifier.

    FluxValue

    pub(all) enum FluxValue {
    StringValue(String)
    LongValue(Int64)
    UnsignedLongValue(UInt64)
    DoubleValue(Double)
    BooleanValue(Bool)
    DateTimeValue(String)
    DurationValue(String)
    Base64BinaryValue(String)
    UnknownValue(String)
    }

    A typed value from an InfluxDB annotated CSV cell.

    Point

    pub(all) struct Point {
    measurement : String
    tags : Array[(String, String)]
    fields : Array[(String, FieldValue)]
    timestamp : Int64?
    }

    A point ready for encoding as InfluxDB Line Protocol.

    Point::add_field

    fn Point::add_field(self : Point, key : String, value : FieldValue) -> Point

    Add one field and return the updated point.

    Point::add_tag

    fn Point::add_tag(self : Point, key : String, value : String) -> Point

    Add one tag and return the updated point.

    Point::set_timestamp

    fn Point::set_timestamp(self : Point, timestamp : Int64) -> Point

    Set a nanosecond timestamp and return the updated point.

    encode_point

    fn encode_point(point : Point) -> Result[String, String]

    Encode one point as a line-protocol row.

    encode_point_batches

    fn encode_point_batches(points : Array[Point], limits : BatchLimits) -> Result[Array[String], String]

    Encode points into bounded Line Protocol batches.

    encode_points

    fn encode_points(points : Array[Point]) -> Result[String, String]

    Encode many points, separated by newlines.

    escape_key_like

    fn escape_key_like(value : String) -> String

    Escape a tag key, tag value, or field key.

    escape_measurement

    fn escape_measurement(value : String) -> String

    Escape a measurement.

    escape_string_field

    fn escape_string_field(value : String) -> String

    Escape a string field value.

    flux_query_result_json

    fn flux_query_result_json(result : FluxQueryResult) -> String

    Render a complete multi-table Flux result as JSON.

    flux_value_display

    fn flux_value_display(value : FluxValue) -> String

    Render a Flux value for terminal output.

    flux_value_json

    fn flux_value_json(value : FluxValue) -> String

    Render a Flux value as a JSON token.

    new_point

    fn new_point(measurement : String) -> Point

    Create an empty point for a measurement.

    parse_annotated_csv

    fn parse_annotated_csv(text : String) -> Result[AnnotatedCsv, String]

    Parse an InfluxDB annotated CSV response.

    parse_flux_query_result

    fn parse_flux_query_result(text : String) -> Result[FluxQueryResult, String]

    Parse a multi-table Flux annotated CSV response.

    parse_line

    fn parse_line(line : String) -> Result[Point, String]

    Parse one line-protocol row.

    parse_lines

    fn parse_lines(text : String) -> Result[Array[Point], String]

    Parse a newline-delimited line-protocol payload.

    split_line_protocol

    fn split_line_protocol(text : String, limits : BatchLimits) -> Result[Array[String], String]

    Split a newline-delimited payload without exceeding point or byte limits.

    unescape_key_like

    fn unescape_key_like(value : String) -> String

    Remove InfluxDB key-like escapes.

    unescape_measurement

    fn unescape_measurement(value : String) -> String

    Remove InfluxDB measurement escapes.

    unescape_string_field

    fn unescape_string_field(value : String) -> String

    Remove string-field escapes.

    validate_point

    fn validate_point(point : Point) -> Result[Unit, String]

    Validate point invariants required by InfluxDB Line Protocol.

    Powered by MoonBit

    Site sourceReport issueModulesSkillsBuild queueStatistics

    © 2026 mooncakes.io