reworked some code parts

This commit is contained in:
Paul 2019-06-26 02:10:35 +02:00
parent 0083207235
commit a92d119b9d
3 changed files with 69 additions and 69 deletions

View File

@ -1,67 +1,29 @@
package main package main
import ( import (
"archive/zip"
"flag" "flag"
"fmt"
"time"
_ "github.com/influxdata/influxdb1-client"
client "github.com/influxdata/influxdb1-client/v2"
) )
var szip Srcfile
var sxml Srcfile
var ofile Outfile
var err error
var zipfile *zip.Reader
var prices *[]Price
var now = time.Now()
var fpc FuelPricesConfig
var configpath string
func main() { func main() {
var configpath string
var fpc FuelPricesConfig
var err error
flag.StringVar(&configpath, "configfile", "common.ini", "config file to use with fuelprices section") flag.StringVar(&configpath, "configfile", "common.ini", "config file to use with fuelprices section")
flag.Parse() flag.Parse()
GetConfig(configpath, &fpc) GetConfig(configpath, &fpc)
sxml.Filename = fpc.RemoteFilename var szip Srcfile
var output []byte
httpClient, err := client.NewHTTPClient(client.HTTPConfig{ err = DownloadFile(&fpc, &szip)
Addr: fmt.Sprintf("http://%s:%d", fpc.InfluxHost, fpc.InfluxPort),
Username: fpc.InfluxUser,
Password: fpc.InfluxPass,
})
HandleFatalError(err) HandleFatalError(err)
bp, err := client.NewBatchPoints(client.BatchPointsConfig{ err = ExtractZip(&fpc, &szip, &output)
Database: fpc.InfluxDB,
})
HandleFatalError(err) HandleFatalError(err)
err = DownloadFile(fpc.RemoteURL) var prices *[]Price
HandleFatalError(err) GetPrices(&prices, &fpc, &output)
err = ExtractZip() SendToInflux(&fpc, prices)
HandleFatalError(err)
GetPrices(&prices, fpc.Pos, fpc.Types, fpc.XPathBase)
for _, p := range *prices {
tags := map[string]string{"pdv": p.ID, "fuel": p.Fuel}
fields := map[string]interface{}{"value": p.Amount}
point, _ := client.NewPoint(
fpc.Table,
tags,
fields,
now,
)
bp.AddPoint(point)
err = httpClient.Write(bp)
HandleError(err)
}
} }

View File

@ -10,8 +10,11 @@ import (
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
"time"
"github.com/antchfx/xmlquery" "github.com/antchfx/xmlquery"
_ "github.com/influxdata/influxdb1-client"
client "github.com/influxdata/influxdb1-client/v2"
"gopkg.in/ini.v1" "gopkg.in/ini.v1"
) )
@ -54,26 +57,32 @@ func GetConfig(configfile string, fuelpricesconfig *FuelPricesConfig) error {
} }
// DownloadFile fetch file from webserver // DownloadFile fetch file from webserver
func DownloadFile(url string) error { func DownloadFile(fpc *FuelPricesConfig, szip *Srcfile) error {
resp, err := http.Get(url) pollTo := 30 * time.Millisecond
c := &http.Client{Timeout: pollTo * time.Second, Transport: &http.Transport{
IdleConnTimeout: pollTo,
DisableCompression: false,
}}
//resp, err := http.Get(url)
resp, err := c.Get(fpc.RemoteURL)
HandleError(err) HandleError(err)
defer resp.Body.Close()
szip.Content, err = ioutil.ReadAll(resp.Body) szip.Content, err = ioutil.ReadAll(resp.Body)
HandleError(err) HandleError(err)
err = resp.Body.Close() time.Sleep(pollTo)
HandleError(err)
return err return err
} }
// ExtractZip get the XML file to be processed // ExtractZip get the XML file to be processed
func ExtractZip() error { func ExtractZip(fpc *FuelPricesConfig, szip *Srcfile, output *[]byte) error {
zipfile, err = zip.NewReader(bytes.NewReader(szip.Content), int64(len(szip.Content))) zipfile, err := zip.NewReader(bytes.NewReader(szip.Content), int64(len(szip.Content)))
if err != nil { HandleFatalError(err)
log.Fatal("Unable to open zipfile")
}
for _, f := range zipfile.File { for _, f := range zipfile.File {
if f.Name == fpc.RemoteFilename { if f.Name == fpc.RemoteFilename {
@ -81,7 +90,7 @@ func ExtractZip() error {
if err != nil { if err != nil {
return err return err
} }
ofile.Content, err = ioutil.ReadAll(rc) *output, err = ioutil.ReadAll(rc)
rc.Close() rc.Close()
} else { } else {
log.Fatal("File not found") log.Fatal("File not found")
@ -91,17 +100,17 @@ func ExtractZip() error {
} }
// GetPrices parses the XML file and get values of prices // GetPrices parses the XML file and get values of prices
func GetPrices(prices **[]Price, spos []string, stypes []string, sxpathbase string) error { func GetPrices(prices **[]Price, fpc *FuelPricesConfig, output *[]byte) error {
var pr []Price var pr []Price
var xml *xmlquery.Node var xml *xmlquery.Node
f := bytes.NewReader(ofile.Content) f := bytes.NewReader(*output)
xml, err = xmlquery.Parse(f) xml, err := xmlquery.Parse(f)
HandleError(err) HandleError(err)
for _, station := range spos { for _, station := range fpc.Pos {
for _, fuel := range stypes { for _, fuel := range fpc.Types {
query := fmt.Sprintf(sxpathbase, station, fuel) query := fmt.Sprintf(fpc.XPathBase, station, fuel)
list := xmlquery.FindOne(xml, query) list := xmlquery.FindOne(xml, query)
for _, i := range list.Attr { for _, i := range list.Attr {
@ -119,6 +128,40 @@ func GetPrices(prices **[]Price, spos []string, stypes []string, sxpathbase stri
return err return err
} }
// SendToInflux sends time series data to influxdb
func SendToInflux(fpc *FuelPricesConfig, prices *[]Price) {
httpClient, err := client.NewHTTPClient(client.HTTPConfig{
Addr: fmt.Sprintf("http://%s:%d", fpc.InfluxHost, fpc.InfluxPort),
Username: fpc.InfluxUser,
Password: fpc.InfluxPass,
})
HandleFatalError(err)
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: fpc.InfluxDB,
})
HandleFatalError(err)
for _, p := range *prices {
tags := map[string]string{"pdv": p.ID, "fuel": p.Fuel}
fields := map[string]interface{}{"value": p.Amount}
point, _ := client.NewPoint(
fpc.Table,
tags,
fields,
time.Now(),
)
log.Println(point)
bp.AddPoint(point)
err = httpClient.Write(bp)
HandleError(err)
}
}
// HandleError handles errors to return err // HandleError handles errors to return err
func HandleError(err error) error { func HandleError(err error) error {
if err != nil { if err != nil {

View File

@ -7,11 +7,6 @@ type Srcfile struct {
Content []byte Content []byte
} }
// Outfile is the output XML file
type Outfile struct {
Content []byte
}
// FuelPricesConfig is the main configuration // FuelPricesConfig is the main configuration
type FuelPricesConfig struct { type FuelPricesConfig struct {
RemoteURL string RemoteURL string