Skip to content

Commit

Permalink
Fixed influxdb example
Browse files Browse the repository at this point in the history
  • Loading branch information
oscaromeu committed Dec 5, 2021
1 parent 848cc6d commit 2fde2ad
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 468 deletions.
4 changes: 2 additions & 2 deletions examples/influxdb/.env
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ export INFLUX_SERVER="localhost"
export INFLUX_PORT=8086
export INFLUX_DB=meteocat
export INFLUX_MEASUREMENT=meteocat
export INFLUX_USERNAME=admin
export INFLUX_PASSWORD=admin
export INFLUX_USERNAME=""
export INFLUX_PASSWORD=""
export LOG_DEBUG=true
export EXTRA_TAGS=group:my_group1
export DOCKER_BUILDKIT=1
15 changes: 5 additions & 10 deletions examples/influxdb/config.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
general:
meteocat_api_key: "uVCjZoDF7P3i6pCfRh1aI9YoQxFX6eRr1cjHypzs"
meteocat_codi_estacio:
- D5
- X2
- X4
- X8
- Y7
meteocat_codi_variable: ""
meteocat_api_key: "API_KEY"
database:
influx_server: "192.168.1.69"
influx_server: "localhost"
influx_port: "8086"
influx_bucket: "meteocat-testing"
influx_measurement: "meteocat_all_by_station"
influx_org: "Testing"
influx_token: "5dYxum3y8db1ZZ4uCYkEjpt4C0AIqJ8oVwLW6IJcQ6O1h5olYMDv9GJewEHiLF6FjCQnqTDjrS-90ZLwFCLQBg=="
influx_username: ""
influx_password: ""
influx_token: "random"


22 changes: 0 additions & 22 deletions examples/influxdb/go.mod

This file was deleted.

388 changes: 0 additions & 388 deletions examples/influxdb/go.sum

This file was deleted.

116 changes: 84 additions & 32 deletions examples/influxdb/main.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package main

import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"github.com/ilyakaznacheev/cleanenv"
client "github.com/influxdata/influxdb-client-go/v2"
"github.com/oscaromeu/meteocat"
"github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
"os"
"strconv"
"time"

"github.com/ilyakaznacheev/cleanenv"
client "github.com/influxdata/influxdb-client-go/v2"
"github.com/oscaromeu/meteocat"
"github.com/sirupsen/logrus"
)

var (
Expand All @@ -24,10 +28,10 @@ var (
)

var (
timeApiBaseURL = "https://www.timeapi.io/api/Time/current/zone?timeZone=Europe/Madrid"
timeApiBaseURL = "https://www.timeapi.io/api/Time"
)

type Time struct {
type CalculationResult struct {
Year int `json:"year"`
Month int `json:"month"`
Day int `json:"day"`
Expand All @@ -43,6 +47,13 @@ type Time struct {
DstActive bool `json:"dstActive"`
}

type Calculation struct {
TimeZone string `json:"timeZone"`
OriginalDateTime string
UsedTimeSpan string
CalculationResult CalculationResult
}

type General struct {
ApiKey string `yaml:"meteocat_api_key" env:"METEOCAT_API_KEY" env-description:"Meteocat Api Key"`
Estacio []string `yaml:"meteocat_codi_estacio" env:"METEOCAT_CODI_ESTACIO" env-description:"Meteocat Codi Estacio"`
Expand Down Expand Up @@ -119,17 +130,15 @@ func ProcessArgs(cfg interface{}) Args {

func InitDB(conf Config) (client.Client, error) {

fmt.Sprintf("http://%s:%s", conf.InfluxServer, conf.InfluxPort)

c := client.NewClient(fmt.Sprintf("http://%s:%s", conf.InfluxServer, conf.InfluxPort), conf.InfluxToken)
c := client.NewClient("http://localhost:8086", fmt.Sprintf("%s:%s", "admin", "admin"))

return c, nil

}

func (t *Time) getTime() {
func (t *CalculationResult) currentByZone(tz string) {

url := timeApiBaseURL
url := timeApiBaseURL + fmt.Sprintf("/current/zone?timeZone=%s", tz)
method := "GET"

client := &http.Client{}
Expand All @@ -153,6 +162,54 @@ func (t *Time) getTime() {

}

func (c *Calculation) currentDecrecment(ts string, tz string) {

//url := timeApiBaseURL + "/Calculation/current/decrement"
url := "https://www.timeapi.io/api/Calculation/current/decrement"
method := "POST"

type Payload struct {
TimeZone string `json:"timeZone"`
TimeSpan string `json:"timeSpan"`
}

payload := Payload{
TimeZone: tz,
TimeSpan: ts,
}
p, err := json.Marshal(payload)
client := &http.Client{}
req, err := http.NewRequest(method, url, bytes.NewBuffer(p))

req.Header.Add("accept", "application/json")
req.Header.Add("Content-Type", "application/json")

if err != nil {
fmt.Println(err)
return
}
res, err := client.Do(req)

fmt.Println(res)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()

body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}

if err := json.Unmarshal([]byte(body), &c); err != nil {
fmt.Println(err)
return
}
fmt.Println(c)
}

func main() {

c, err := InitDB(cfg)
Expand All @@ -161,28 +218,22 @@ func main() {
os.Exit(1)
}

var t Time
var Tags = make(map[string]string)
var cal Calculation
//var Tags = make(map[string]string)

t.getTime()
cal.currentDecrecment("00:02:30:00", "Europe/Madrid")

d, _ := meteocat.NewOpenDataMesurades()

data := meteocat.Data{
Any: strconv.Itoa(t.Year),
Mes: strconv.Itoa(t.Month),
Dia: strconv.Itoa(t.Day),
Any: strconv.Itoa(cal.CalculationResult.Year),
Mes: strconv.Itoa(cal.CalculationResult.Month),
Dia: strconv.Itoa(cal.CalculationResult.Day),
}

// timeDate := meteocat.TimeDate{
// Hour: strconv.Itoa(t.Hour),
// Minute: strconv.Itoa(t.Minute),
// Seconds: "00",
// }

timeDate := meteocat.TimeDate{
Hour: "05",
Minute: "00",
Hour: strconv.Itoa(cal.CalculationResult.Hour),
Minute: strconv.Itoa(cal.CalculationResult.Minute),
Seconds: "00",
}
fmt.Println(data)
Expand All @@ -193,22 +244,23 @@ func main() {

d.OpenDataMeasurementAllByStation(p)

writeAPI := c.WriteAPI(cfg.InfluxOrg, cfg.InfluxBucket)
str := fmt.Sprintf("%s-%s-%sT%s:%s:%s", p.Any, p.Mes, p.Dia, p.Hour, p.Minute, p.Seconds)

tt, err := time.Parse(layout, str)
writeAPI := c.WriteAPIBlocking("", "test/autogen")

for _, v := range d.OpenData {
Tags["codi_estacio"] = v.CodiEstacio
Tags["nom_estacio"] = meteocat.CodisEstacions[v.CodiEstacio]
Tags["codi_variable"] = v.CodiVariable
Tags["nom_variable"] = meteocat.CodisVariables[v.CodiVariable]
t, _ = time.Parse(layout, data.Any, data.Mes, data.Dia)
Fields["valor"] = l.Valor
Fields["valor"] = v.ValorLectura
p := client.NewPoint(cfg.InfluxMeasurement,
Tags,
Fields, fmt.Sprintf("?data_lectura=%s-%s-%sT%s:%s:%s", p.Any, p.Mes, p.Dia, p.Hour, p.Minute, p.Seconds))
Fields, tt)
// write point asynchronously
writeAPI.WritePoint(p)
writeAPI.WritePoint(context.Background(), p)
}
// Flush writes
writeAPI.Flush()
// always close client at the end
defer c.Close()

Expand Down
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ go 1.13

require (
github.com/fatih/color v1.9.0
github.com/go-co-op/gocron v0.5.0
github.com/ilyakaznacheev/cleanenv v1.2.5
github.com/influxdata/influxdb-client-go/v2 v2.6.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/mattn/go-colorable v0.1.7 // indirect
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.7.0
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.6.1 // indirect
golang.org/x/sys v0.0.0-20201118182958-a01c418693c7 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
)
Loading

0 comments on commit 2fde2ad

Please sign in to comment.