influxx

package module
v1.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2025 License: MIT Imports: 9 Imported by: 0

README

influxx

Influx helper

Install

go get github.com/innotechdevops/influxx@v1.2.0

How to use

  • Define struct
type MinewSensor struct {
    Timestamp    time.Time `influxdb:"Timestamp"`
    Temperature  float64   `influxdb:"Temperature"`
    Humidity     float64   `influxdb:"Humidity"`
    Battery      float64   `influxdb:"Battery"`
    RSSI         float64   `influxdb:"RSSI"`
    Code         string    `influxdb:"Code"`
}
  • Try parse value to struct (Recommended)
var values []influxdb1.Result
dataStruct := influxx.TryParser[MinewSensor](values, func(element []MinewSensor) Struct {
    return MinewSensor {
        Timestamp:   element[0],
        Temperature: element[1],
        Humidity:    element[2],
        Battery:     element[3],
        RSSI:        element[4],
        Code:        element[5],
    }
})
  • Parse value to struct
query := `
    SELECT
        time,
        temperature,
        humidity,
        battery,
        rssi,
        code
    FROM (
        SELECT
            mean(temperature) as temperature,
            mean(humidity) as humidity,
            mean(battery) as battery,
            mean(rssi) as rssi
        FROM minew_sensor_indoor
        WHERE code =~ /A|B/ AND time >= now() and time <= now()
        GROUP BY time(15m), code fill(previous) tz('Asia/Bangkok')
    )
`

values := [][]any{{"Timestamp", "Temperature", "Humidity", "Battery", "RSSI", "Code"}}
values = append(values, response.Results[0].Series[0].Values...)
sensors := influxx.Parser[MinewSensor](values)
  • Try Mapping value null-safety
tags := map[string]string{}
fields := map[string]any{}

influxx.TryMapping("tag1", "1", tags)
influxx.TryMapping("tag2", "C001", tags)
influxx.TryMapping("tag3", "", tags)
influxx.TryMapping("field1", influxx.AnyToPointer(99.99), fields)
influxx.TryMapping("field2", 100, fields)
influxx.TryMapping[*string, any]("field3", nil, fields)

fmt.Println(tags)   // map[tag1:1 tag2:C001]
fmt.Println(fields) // map[field1:99.99 field2:100]
  • Convert struct to influx pattern
data := []MyStruct{
    {
        Timestamp: time.Now().Unix(),
        ID:        "1",
        Code:      "C01",
        Field1:    influxx.AnyToPointer(9.9),
        Field2:    influxx.AnyToPointer(10),
        Field3:    nil,
    },
    {
        Timestamp: time.Now().Unix(),
        ID:        "2",
        Code:      "C02",
        Field1:    influxx.AnyToPointer(11.9),
        Field2:    influxx.AnyToPointer(22),
        Field3:    nil,
    },
}

_ = influxx.Convert(data, func(timestamp time.Time, tags map[string]string, fields map[string]any) {
    fmt.Println("timestamp:", timestamp)
    fmt.Println("tags:", tags)
    fmt.Println("fields:", fields)
})
  • New points by struct
data := []MyStruct{
    {
        Timestamp: time.Now().Unix(),
        ID:        "1",
        Code:      "C01",
        Field1:    influxx.AnyToPointer(9.9),
        Field2:    influxx.AnyToPointer(10),
        Field3:    nil,
    },
    {
        Timestamp: time.Now().Unix(),
        ID:        "2",
        Code:      "C02",
        Field1:    influxx.AnyToPointer(11.9),
        Field2:    influxx.AnyToPointer(22),
        Field3:    nil,
    },
}

bp, _ := influxdb1.NewBatchPoints(influxdb1.BatchPointsConfig{
    Database:  "my-database",
    Precision: "s",
})
_ = influxx.NewPoint(data, "my_name", bp)

Documentation

Index

Constants

View Source
const (
	FnRaw        = "raw"
	FnMean       = "mean"
	FnLast       = "last"
	FnFirst      = "first"
	FnSum        = "sum"
	FillPrevious = "previous"
	FillLinear   = "linear"
	FillNone     = "none"
	FillZero     = "0"
	FillNull     = "null"
)

Variables

This section is empty.

Functions

func AnyToPointer

func AnyToPointer[T any](data T) *T

func Convert

func Convert[T any](data []T, onCompute func(timestamp time.Time, tags map[string]string, fields map[string]any)) error

func FieldMapping added in v1.0.3

func FieldMapping[T any](key string, value *T, mapping map[string]any)

func FloatDecimal

func FloatDecimal(num float64, precision int) float64

func FloatDecimalPtr added in v1.2.1

func FloatDecimalPtr(num *float64, precision int) *float64

func Get added in v1.2.2

func Get[T any](value interface{}, def T) T

func GetFloat32 added in v1.2.0

func GetFloat32(value any, decimal ...int) float32

func GetFloat32Ptr added in v1.2.0

func GetFloat32Ptr(value any, decimal ...int) *float32

func GetFloat64

func GetFloat64(value any, decimal ...int) float64

func GetFloat64Ptr added in v1.1.0

func GetFloat64Ptr(value any, decimal ...int) *float64

func GetInt added in v1.2.0

func GetInt(value any) int

func GetInt64

func GetInt64(value any) int64

func GetInt64Ptr added in v1.1.0

func GetInt64Ptr(value any) *int64

func GetIntPtr added in v1.2.0

func GetIntPtr(value any) *int

func GetPtr added in v1.2.2

func GetPtr[T any](value interface{}) *T

func GetString

func GetString(value any) string

func GetStringPtr added in v1.1.0

func GetStringPtr(value any) *string

func GetTime added in v1.0.3

func GetTime(value any) time.Time

func GetTimePtr added in v1.1.0

func GetTimePtr(value any) *time.Time

func GetTimeString

func GetTimeString(timestamp int64) string

func GetTimeStringFormat added in v1.0.5

func GetTimeStringFormat(timestamp int64, format string) string

func GetTimeStringFormatPtr added in v1.2.1

func GetTimeStringFormatPtr(timestamp *int64, format string) *string

func InOr

func InOr(where string, list []string) string

InOr example: code = A OR code = B OR code = C

func InRegex

func InRegex(list []string) string

InRegex example: code =~ /A|B|C/

func NewPoint

func NewPoint[T any](data []T, name string, batchPoint influxdb1.BatchPoints) error

func Parser

func Parser[T any](rows [][]any) []T

Parser data to Struct How to use query := `

 SELECT
		time,
	    temperature,
	    humidity,
	    battery,
	    rssi,
	    code
	FROM (
	    SELECT
			mean(temperature) as temperature,
			mean(humidity) as humidity,
			mean(battery) as battery,
			mean(rssi) as rssi
		FROM minew_sensor_indoor
		WHERE code =~ /A|B/ AND time >= now() and time <= now()
		GROUP BY time(15m), code fill(previous) tz('Asia/Bangkok')
	)`

values := [][]any{{"Timestamp", "Temperature", "Humidity", "Battery", "RSSI", "Code"}} values = append(values, response.Results[0].Series[0].Values...) sensors = influxx.Parser[MinewSensor](values)

func Round

func Round(num float64) int

func SafetyMapping added in v1.0.3

func SafetyMapping[T any](value *T, onCompute func(value T))

func TagMapping added in v1.0.3

func TagMapping(key string, value string, mapping map[string]string)

func TryMapping

func TryMapping[V any, M any](key string, value V, mapping map[string]M)

func TryParser

func TryParser[T any](results []influxdb1.Result, onCompute func(element []any) T) []T

Types

type Params

type Params struct {
	Start int64  `json:"start"`
	End   int64  `json:"end"`
	Scale string `json:"scale"`
	Last  string `json:"last"`
	Fill  string `json:"fill"`
	Fn    string `json:"fn"`
}

type Query

type Query struct {
	Start string `json:"start"`
	Fn    string `json:"fn"`
	End   string `json:"end"`
	Group string `json:"group"`
	Fill  string `json:"fill"`
}

func TimeRangeConvert

func TimeRangeConvert(data Params) Query

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL