Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transform from uW to dbm for optical levels #311

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions transformer/auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,10 @@ func init() {
Help: "Remove single fields in a metric based on a regular expression criteria",
AutoMake: false,
})
Auto.Add(skogul.Module{
Name: "huwtodbm",
Aliases: []string{},
Alloc: func() interface{} { return &HuWtoDBM{} },
Help: "Translate from uW to DBM.",
})
}
84 changes: 84 additions & 0 deletions transformer/huw_to_dbm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2024 Telenor Norge AS
* Author(s):
* - Hans Rafaelsen <hans.rafaelsen@telenor.no>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/

package transformer

import (
"errors"
"fmt"
"math"
"sync"

"github.com/sirupsen/logrus"
"github.com/telenornms/skogul"
)

var uwdbmLog = skogul.Logger("transformer", "uw_to_dbm")

// Compute from mico-watt (uW) to desibel (DBM) for Huwaei routers
type HuWtoDBM struct {
Source string `doc:"Data key to read from."`
Destination string `doc:"Data key to write to. Default 'dbm'"`
Treshold float64 `doc:"Treshold for selecting which formula to use. Default 200"`
once sync.Once
err error
}

// Transform
func (uwdbm *HuWtoDBM) Transform(c *skogul.Container) error {
uwdbm.once.Do(func() {
if uwdbm.Destination == "" {
uwdbm.Destination = "dbm"
}
if uwdbm.Treshold == 0 {
uwdbm.Treshold = 200
}
})
for _, m := range c.Metrics {
v, ok := m.Data[uwdbm.Source].(float64)
if !ok {
uwdbmLog.Log(logrus.ErrorLevel, "Value is not valid float '", uwdbm.Source,
"': '", m.Data[uwdbm.Source], "'")
uwdbm.err = errors.New(fmt.Sprintf("Value '%s' is not valid float", uwdbm.Source))
continue
}

if v > uwdbm.Treshold {
v = 10 * 1 / math.Log10(10) * math.Log10(0.1+v/1000)
} else {
v = 10 * 1 / math.Log10(10) * math.Log10(0.01+v/100)
}
// In case we don't compute a valid value, set it to -40 to indicate a broken link
if math.IsNaN(v) || math.IsInf(v, 0) {
v = -40
}
m.Data[uwdbm.Destination] = v
}
return uwdbm.err
}

// Verify checks that the required variables are set
func (uwdbm *HuWtoDBM) Verify() error {
if uwdbm.Source == "" {
return skogul.MissingArgument("Source")
}
return nil
}
183 changes: 183 additions & 0 deletions transformer/huw_to_dbm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/*
* Copyright (c) 2024 Telenor Norge AS
* Author(s):
* - Hans Rafaelsen <hans.rafaelsen@telenor.no>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/

package transformer_test

import (
"testing"

"github.com/telenornms/skogul"
"github.com/telenornms/skogul/transformer"
)

func TestHUWtoDBMDefault(t *testing.T) {
metric := skogul.Metric{}
metric.Metadata = make(map[string]interface{})
metric.Metadata["systemID"] = "xxx1"
metric.Metadata["if_name"] = "SNMPv2-SMI::enterprises.2011.5.25.31.1.1.3.1.8.67305550"
metric.Data = make(map[string]interface{})
metric.Data["uW"] = float64(35)
c := skogul.Container{}
c.Metrics = []*skogul.Metric{&metric}

conv := transformer.HuWtoDBM{
Source: "uW",
}

t.Logf("Container before transform:\n%v", c)
err := conv.Transform(&c)
if err != nil {
t.Errorf("UWtoDBM returned non-nil err: %v", err)
}

t.Logf("Container after transform:\n%v", c)

v, ok := c.Metrics[0].Data["dbm"].(float64)
if !ok {
t.Fatal("Failed to create 'dbm' field")
}
expect := -4.436974992327127
if v != expect {
t.Errorf("Failed to compute correct 'dbm' field. Got: %f. Expected: %f", v, expect)
}
}

func TestHUWtoDBMDest(t *testing.T) {
metric := skogul.Metric{}
metric.Metadata = make(map[string]interface{})
metric.Metadata["systemID"] = "xxx1"
metric.Metadata["if_name"] = "SNMPv2-SMI::enterprises.2011.5.25.31.1.1.3.1.8.67305550"
metric.Data = make(map[string]interface{})
metric.Data["uW"] = float64(35)
c := skogul.Container{}
c.Metrics = []*skogul.Metric{&metric}

conv := transformer.HuWtoDBM{
Source: "uW",
Destination: "foo",
}

t.Logf("Container before transform:\n%v", c)
err := conv.Transform(&c)
if err != nil {
t.Errorf("UWtoDBM returned non-nil err: %v", err)
}

t.Logf("Container after transform:\n%v", c)

v, ok := c.Metrics[0].Data["foo"].(float64)
if !ok {
t.Fatal("Failed to create destination field 'foo'")
}
expect := -4.436974992327127
if v != expect {
t.Errorf("Failed to compute correct 'foo' field. Got: %f. Expected: %f", v, expect)
}
}

func TestHUWtoDBMDestTreshold(t *testing.T) {
metric := skogul.Metric{}
metric.Metadata = make(map[string]interface{})
metric.Metadata["systemID"] = "xxx1"
metric.Metadata["if_name"] = "SNMPv2-SMI::enterprises.2011.5.25.31.1.1.3.1.8.67305550"
metric.Data = make(map[string]interface{})
metric.Data["uW"] = float64(35)
c := skogul.Container{}
c.Metrics = []*skogul.Metric{&metric}

conv := transformer.HuWtoDBM{
Source: "uW",
Destination: "foo",
Treshold: 10,
}

t.Logf("Container before transform:\n%v", c)
err := conv.Transform(&c)
if err != nil {
t.Errorf("UWtoDBM returned non-nil err: %v", err)
}

t.Logf("Container after transform:\n%v", c)

v, ok := c.Metrics[0].Data["foo"].(float64)
if !ok {
t.Fatal("Failed to create destination field 'foo'")
}
expect := -8.696662315049938
if v != expect {
t.Errorf("Failed to compute correct 'foo' field. Got: %f. Expected: %f", v, expect)
}
}

func TestHUWtoDBMNotFloat(t *testing.T) {
metric := skogul.Metric{}
metric.Metadata = make(map[string]interface{})
metric.Metadata["systemID"] = "xxx1"
metric.Metadata["if_name"] = "SNMPv2-SMI::enterprises.2011.5.25.31.1.1.3.1.8.67305550"
metric.Data = make(map[string]interface{})
metric.Data["uW"] = "foo"
c := skogul.Container{}
c.Metrics = []*skogul.Metric{&metric}

conv := transformer.HuWtoDBM{
Source: "uW",
}

t.Logf("Container before transform:\n%v", c)
err := conv.Transform(&c)
if err == nil {
t.Errorf("UWtoDBM check for error test failed")
}

t.Logf("Container after transform:\n%v", c)
}

func TestHUWtoDBMNegative(t *testing.T) {
metric := skogul.Metric{}
metric.Metadata = make(map[string]interface{})
metric.Metadata["systemID"] = "xxx1"
metric.Metadata["if_name"] = "SNMPv2-SMI::enterprises.2011.5.25.31.1.1.3.1.8.67305550"
metric.Data = make(map[string]interface{})
metric.Data["uW"] = float64(-35)
c := skogul.Container{}
c.Metrics = []*skogul.Metric{&metric}

conv := transformer.HuWtoDBM{
Source: "uW",
}

t.Logf("Container before transform:\n%v", c)
err := conv.Transform(&c)
if err != nil {
t.Errorf("UWtoDBM returned non-nil err: %v", err)
}

t.Logf("Container after transform:\n%v", c)

v, ok := c.Metrics[0].Data["dbm"].(float64)
if !ok {
t.Fatal("Failed to create 'dbm' field")
}
expect := -40.0
if v != expect {
t.Errorf("Failed to compute correct 'dbm' field. Got: %f. Expected: %f", v, expect)
}
}
Loading