-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from smlx/batman
feat: add batman metrics
- Loading branch information
Showing
11 changed files
with
795 additions
and
359 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package mitm | ||
|
||
import ( | ||
"math" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
) | ||
|
||
type timeFunc func() time.Time | ||
|
||
func timeNow() time.Time { | ||
return time.Now().In(time.FixedZone("+08", 8*60*60)) | ||
} | ||
|
||
// setupBatsignal enables the prometheus metrics which display the bat insignia | ||
// on the time series graph | ||
func setupBatsignal() { | ||
promauto.NewUntypedFunc(prometheus.UntypedOpts{ | ||
Name: "batsignal_top", | ||
Help: "Top of the batsignal", | ||
}, batsignalTop(timeNow)) | ||
promauto.NewUntypedFunc(prometheus.UntypedOpts{ | ||
Name: "batsignal_bottom", | ||
Help: "Bottom of the batsignal", | ||
}, batsignalBottom(timeNow)) | ||
} | ||
|
||
// Taking the time returned by tf as an x value zeroed to midday, calculates | ||
// the positive y value of the Batman function. | ||
// https://www.pacifict.com/Examples/Batman/ | ||
func batsignalTop(tf timeFunc) func() float64 { | ||
return func() float64 { | ||
now := tf() | ||
switch x := float64(now.Hour()) + float64(now.Minute())/60 - 12; { | ||
case x > -7 && x < -3: | ||
fallthrough | ||
case x > 3 && x < 7: | ||
return math.Sqrt(1-math.Pow(x/7, 2)) * 3 | ||
case x >= -3 && x < -1: | ||
fallthrough | ||
case x > 1 && x <= 3: | ||
return 6*math.Sqrt(10)/7 - 0.5*math.Abs(x) + 1.5 - | ||
(3*math.Sqrt(10)/7)*math.Sqrt(4-math.Pow(math.Abs(x)-1, 2)) | ||
case x >= -1 && x < -0.75: | ||
fallthrough | ||
case x > 0.75 && x <= 1: | ||
return 9 - 8*math.Abs(x) | ||
case x >= -0.75 && x < -0.5: | ||
fallthrough | ||
case x > 0.5 && x <= 0.75: | ||
return 3*math.Abs(x) + 0.75 | ||
case x >= -0.5 && x <= 0.5: | ||
return 2.25 | ||
default: | ||
return 0 | ||
} | ||
} | ||
} | ||
|
||
// Taking the time returned by tf as an x value zeroed to midday, calculates | ||
// the negative y value of the Batman function. | ||
// https://www.pacifict.com/Examples/Batman/ | ||
func batsignalBottom(tf timeFunc) func() float64 { | ||
return func() float64 { | ||
now := tf() | ||
switch x := float64(now.Hour()) + float64(now.Minute())/60 - 12; { | ||
case x > -7 && x < -4: | ||
fallthrough | ||
case x > 4 && x < 7: | ||
return -math.Sqrt(1-math.Pow(x/7, 2)) * 3 | ||
case x >= -4 && x <= 4: | ||
return math.Abs(x/2) - (3*math.Sqrt(33)-7)/112*math.Pow(x, 2) - 3 + | ||
math.Sqrt(1-math.Pow(math.Abs(math.Abs(x)-2)-1, 2)) | ||
default: | ||
return 0 | ||
} | ||
} | ||
} | ||
|
||
// batsignal takes data assumed to be an outbound packet. If it is a metrics | ||
// packet, it mutates the metrics values of the data in order to draw the | ||
// Batman function on the SEMS portal plot. | ||
// On any kind of error, it returns the original data. | ||
func batsignal(metrics *OutboundMetricsPacket) ([]byte, error) { | ||
metrics.PowerGenerationWatts = int32(1000 * batsignalTop(timeNow)()) | ||
metrics.PowerExportWatts = int32(1000 * batsignalBottom(timeNow)()) | ||
return metrics.MarshalBinary() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.