fix: splited temperatures

Changes:
- Split temperatures from log file into blocks.
  Every block has a size of 500 entries. Every block would be send to
  the remote host
This commit is contained in:
Markus Pesch 2018-12-07 22:50:28 +01:00
parent 81600154f0
commit 500d1a5823
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
8 changed files with 175 additions and 102 deletions

View File

@ -11,8 +11,9 @@ import (
var quiet bool
var listRemoteCmd = &cobra.Command{
Use: "list",
Short: "List Remove Servers",
Use: "list",
Short: "List Remove Servers",
Aliases: []string{"ls"},
Run: func(cmd *cobra.Command, args []string) {
if err := remote.Print(os.Stdout, configDir, quiet); err != nil {

View File

@ -10,9 +10,10 @@ import (
var all bool
var rmRemoteCmd = &cobra.Command{
Use: "rm",
Short: "Remove Remote Server",
Args: cobra.RangeArgs(0, 1),
Use: "rm",
Short: "Remove Remote Server",
Aliases: []string{"remove"},
Args: cobra.RangeArgs(0, 1),
Run: func(cmd *cobra.Command, args []string) {
if all {

View File

@ -10,8 +10,9 @@ import (
var force bool
var syncRemoteCmd = &cobra.Command{
Use: "sync",
Short: "Synchronise Device Values with Remote Servers",
Use: "sync",
Aliases: []string{"synchronize"},
Short: "Synchronise Device Values with Remote Servers",
Run: func(cmd *cobra.Command, args []string) {
if err := httpcall.SyncDevice(configDir, force); err != nil {

View File

@ -11,8 +11,9 @@ import (
var quiet bool
var listSensorCmd = &cobra.Command{
Use: "list",
Short: "List Sensors",
Use: "ls",
Short: "List Sensors",
Aliases: []string{"list"},
Run: func(cmd *cobra.Command, args []string) {
if err := sensor.Print(os.Stdout, configDir, quiet); err != nil {
log.Fatal(err)

View File

@ -8,9 +8,10 @@ import (
)
var rmSensorCmd = &cobra.Command{
Use: "rm",
Short: "Remove Sensor",
Args: cobra.ExactArgs(1),
Use: "rm",
Short: "Remove Sensor",
Aliases: []string{"remove"},
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if err := sensor.Remove(args[0], configDir); err != nil {
log.Fatal(err)

View File

@ -10,8 +10,9 @@ import (
var force bool
var syncSensorCmd = &cobra.Command{
Use: "sync",
Short: "Synchronise Sensors with Remote Servers",
Use: "sync",
Short: "Synchronise Sensors with Remote Servers",
Aliases: []string{"synchronize"},
Run: func(cmd *cobra.Command, args []string) {
if err := httpcall.SyncSensors(configDir, force); err != nil {

View File

@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"math"
"net/http"
"git.cryptic.systems/fh-trier/go-flucky/pkg/config"
@ -20,34 +21,54 @@ func SendTemperatures(temperatures []*stypes.Temperature, configDir string) erro
return err
}
temperaturesAsBytes, err := json.Marshal(temperatures)
if err != nil {
return fmt.Errorf("Can not marshal temperatures to JSON: %v", err)
// split temperatures in blocks to reduce server data size in single request
blockSize := 500
temperatureBlocks := make(map[int][]*stypes.Temperature)
length := int(math.Ceil(float64(len(temperatures)) / float64(blockSize)))
for i := 0; i < length; i++ {
start := i * blockSize
end := (i + 1) * blockSize
if end > len(temperatures) {
end = len(temperatures)
}
temperatureBlocks[i] = temperatures[start:end]
}
temperaturesAsReader := bytes.NewReader(temperaturesAsBytes)
for _, remote := range cnf.Remotes {
if remote.Enabled {
requestURL := fmt.Sprintf("%s%s", remote.Address, "/temperatures")
req, err := http.NewRequest("POST", requestURL, temperaturesAsReader)
if err != nil {
return err
}
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
for _, temperatureBlock := range temperatureBlocks {
if resp.StatusCode < 200 || resp.StatusCode > 299 {
b, err := ioutil.ReadAll(resp.Body)
temperaturesAsBytes, err := json.Marshal(temperatureBlock)
if err != nil {
return fmt.Errorf("Invalid HTTP-Statuscode - expected 200, got %d - can not read response body: %v", resp.StatusCode, err)
return fmt.Errorf("Can not marshal temperatures to JSON: %v", err)
}
temperaturesAsReader := bytes.NewReader(temperaturesAsBytes)
requestURL := fmt.Sprintf("%s%s", remote.Address, "/temperatures")
req, err := http.NewRequest("POST", requestURL, temperaturesAsReader)
if err != nil {
return err
}
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("Invalid HTTP-Statuscode - expected 200, got %d - can not read response body: %v", resp.StatusCode, err)
}
return fmt.Errorf("Invalid HTTP-Statuscode - expected 200, got %d: %v", resp.StatusCode, string(b))
}
return fmt.Errorf("Invalid HTTP-Statuscode - expected 200, got %d: %v", resp.StatusCode, string(b))
}
}
}

View File

@ -12,6 +12,7 @@ import (
stypes "git.cryptic.systems/fh-trier/go-flucky-server/pkg/types"
"git.cryptic.systems/fh-trier/go-flucky/pkg/httpcall"
"git.cryptic.systems/fh-trier/go-flucky/pkg/logs"
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
"git.cryptic.systems/fh-trier/go-flucky/pkg/config"
@ -30,49 +31,20 @@ func Get(sensorNames []string, push bool, configDir string, w io.Writer) error {
return err
}
// get sensors
sensors, err := getSensors(sensorNames, cnf)
if err != nil {
return err
}
// get temperatures
temperatures, err := getTemperatures(sensors)
if err != nil {
return err
}
tw := tabwriter.NewWriter(w, 0, 0, 5, ' ', 0)
// filter sensors
var sensors []*stypes.Sensor
if len(sensorNames) > 0 {
for _, sensorName := range sensorNames {
for _, sensor := range cnf.Sensors {
if sensorName == *sensor.SensorName && *sensor.SensorEnabled ||
sensorName == sensor.SensorID && *sensor.SensorEnabled ||
sensorName == *sensor.WireID && *sensor.SensorEnabled {
sensors = append(sensors, sensor)
}
}
}
} else {
for _, sensor := range cnf.Sensors {
if *sensor.SensorEnabled {
sensors = append(sensors, sensor)
}
}
}
if len(sensors) == 0 {
return fmt.Errorf("No matched Sensors. Check if your sensor is configured or enabled")
}
// check if sensor exists in cnf and has a wire id
for _, filterdSensor := range sensors {
if filterdSensor.WireID == nil || *filterdSensor.WireID == "" {
return fmt.Errorf("Sensor %v has no wire id", filterdSensor.SensorID)
}
var found bool
for _, cnfSensor := range cnf.Sensors {
if filterdSensor.SensorID == cnfSensor.SensorID {
found = true
}
}
if !found {
return fmt.Errorf("Can not found sensor %v in config", filterdSensor.SensorID)
}
}
// headlines
for _, sensor := range sensors {
if sensor.SensorName != nil && *sensor.SensorName != "" {
@ -84,10 +56,6 @@ func Get(sensorNames []string, push bool, configDir string, w io.Writer) error {
fmt.Fprint(tw, "\n")
// body
temperatures, err := getTemperatures(sensors)
if err != nil {
return err
}
for _, temperature := range temperatures {
fmt.Fprintf(tw, "%v\t", temperature.TemperatureValue)
}
@ -110,6 +78,43 @@ func Get(sensorNames []string, push bool, configDir string, w io.Writer) error {
}
// Fetch ...
func Fetch(sensorNames []string, push bool, configDir string) error {
// get cnf
cnf, err := config.Read(configDir)
if err != nil {
return err
}
// get sensors
sensors, err := getSensors(sensorNames, cnf)
if err != nil {
return err
}
// get temperatures
temperatures, err := getTemperatures(sensors)
if err != nil {
return err
}
// write logfiles or push
if !push {
if err := logs.AppendTemperature(temperatures, cnf); err != nil {
return err
}
} else {
if err := httpcall.SendTemperatures(temperatures, configDir); err != nil {
return err
}
}
return nil
}
// Follow ...
func Follow(sensorNames []string, push bool, configDir string, w io.Writer) error {
// get cnf
@ -118,39 +123,29 @@ func Follow(sensorNames []string, push bool, configDir string, w io.Writer) erro
return err
}
var temperatures []*stypes.Temperature
// get sensors
sensors, err := getSensors(sensorNames, cnf)
if err != nil {
return err
}
// tabwriter
tw := tabwriter.NewWriter(w, 0, 0, 5, ' ', 0)
// headlines
var sensors []*stypes.Sensor
numOfSensors := len(cnf.Sensors)
for _, sensor := range cnf.Sensors {
switch {
case sensor.SensorName != nil && numOfSensors <= 1:
sensors = append(sensors, sensor)
case sensor.SensorName == nil && numOfSensors <= 1:
sensors = append(sensors, sensor)
case sensor.SensorName != nil && numOfSensors > 1:
for _, sensor := range sensors {
if sensor.SensorName != nil && *sensor.SensorName != "" {
fmt.Fprintf(tw, "%v\t", *sensor.SensorName)
sensors = append(sensors, sensor)
break
case sensor.SensorName == nil && numOfSensors > 1:
} else {
fmt.Fprintf(tw, "%v\t", sensor.SensorID)
sensors = append(sensors, sensor)
break
}
}
if numOfSensors > 1 {
fmt.Fprint(tw, "\n")
}
fmt.Fprint(tw, "\n")
// body
temperatures := []*stypes.Temperature{}
ticker := time.NewTicker(1 * time.Second)
// TODO: create channel for own sensor
go func() {
for {
select {
@ -160,8 +155,16 @@ func Follow(sensorNames []string, push bool, configDir string, w io.Writer) erro
}
// get temperatures from sensors and write them into writer
temperatures, err = getTemperatures(sensors)
for _, temperature := range temperatures {
t, err := getTemperatures(sensors)
// TODO: get error back over channel
if err != nil {
return
}
// append temperatures to temperature list
temperatures = append(temperatures, t...)
for _, temperature := range t {
fmt.Fprintf(tw, "%3.3f\t", temperature.TemperatureValue)
}
@ -244,3 +247,46 @@ func getTemperatures(sensors []*stypes.Sensor) ([]*stypes.Temperature, error) {
return temperatures, nil
}
func getSensors(sensorNames []string, cnf *types.Config) ([]*stypes.Sensor, error) {
sensors := []*stypes.Sensor{}
if len(sensorNames) > 0 {
for _, sensorName := range sensorNames {
for _, sensor := range cnf.Sensors {
if sensorName == *sensor.SensorName && *sensor.SensorEnabled ||
sensorName == sensor.SensorID && *sensor.SensorEnabled ||
sensorName == *sensor.WireID && *sensor.SensorEnabled {
sensors = append(sensors, sensor)
}
}
}
} else {
for _, sensor := range cnf.Sensors {
if *sensor.SensorEnabled {
sensors = append(sensors, sensor)
}
}
}
if len(sensors) == 0 {
return nil, fmt.Errorf("No matched Sensors. Check if your sensor is configured or enabled")
}
// check if sensor exists in cnf and has a wire id
for _, filterdSensor := range sensors {
if filterdSensor.WireID == nil || *filterdSensor.WireID == "" {
return nil, fmt.Errorf("Sensor %v has no wire id", filterdSensor.SensorID)
}
var found bool
for _, cnfSensor := range cnf.Sensors {
if filterdSensor.SensorID == cnfSensor.SensorID {
found = true
}
}
if !found {
return nil, fmt.Errorf("Can not found sensor %v in config", filterdSensor.SensorID)
}
}
return sensors, nil
}