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

Remove timeDuration #81

Merged
merged 5 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 6 additions & 1 deletion assets/templates/aws.vpcflow/schema-a/gotext.tpl
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
{{generate "Version"}} {{generate "AccountID"}} {{generate "InterfaceID"}} {{generate "SrcAddr"}} {{generate "DstAddr"}} {{generate "SrcPort"}} {{generate "DstPort"}} {{generate "Protocol"}}{{ $packets := generate "Packets" }} {{ $packets }} {{mul $packets 15 }} {{$startOffset := generate "StartOffset" }}{{$startOffsetInSecond := mul -1 1000000000 $startOffset }}{{$startOffsetDuration := timeDuration $startOffsetInSecond}}{{$end := generate "End" }}{{$start := $end.Add $startOffsetDuration}}{{$start.Format "2006-01-02T15:04:05.999999Z07:00" }} {{$end.Format "2006-01-02T15:04:05.999999Z07:00"}} {{generate "Action"}}{{ if eq $packets 0 }} NODATA {{ else }} {{generate "LogStatus"}} {{ end }}
{{- $startOffset := generate "StartOffset" }}
{{- $startOffsetInSecond := mul -1 1000000000 $startOffset }}
{{- $startOffsetDuration := $startOffsetInSecond | int64 | duration}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$startOffsetInSecond was indeed $startOffsetInNanosecond (my bad :))
$startOffset was multiplied 1000000000 because our timeDuration expected nanoseconds, duration from sprig expects seconds: so we need to use directly the value in $startOffset (still multiplied by -1 since we want to subtract the value) is already the correct one.
Not sure if you prefer a oneliner or not, but this might work as well:

{{- $start := mul -1 $startOffset | int64 | duration | $end.Add }}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for noticing, I completely missed that.
I updated the template.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my oneliner was wrong:
time.Time.Add takes a time.Duration, but duration returns a string, so we must use dateModify from sprig

{{- $start := $end | dateModify  (mul -1 $startOffset | int64 | duration) }}

todo: let's add to CI to generate at least one event for all the templates

{{- $end := generate "End" }}
{{- $start := $end.Add $startOffsetDuration}}
{{generate "Version"}} {{generate "AccountID"}} {{generate "InterfaceID"}} {{generate "SrcAddr"}} {{generate "DstAddr"}} {{generate "SrcPort"}} {{generate "DstPort"}} {{generate "Protocol"}}{{ $packets := generate "Packets" }} {{ $packets }} {{mul $packets 15 }} {{$start.Format "2006-01-02T15:04:05.999999Z07:00" }} {{$end.Format "2006-01-02T15:04:05.999999Z07:00"}} {{generate "Action"}}{{ if eq $packets 0 }} NODATA {{ else }} {{generate "LogStatus"}} {{ end }}
6 changes: 0 additions & 6 deletions docs/writing-templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,3 @@ This is equivalent to the following when using the `placeholder` template type:

#### sprig functions
The template loads the functions provided by sprig (https://masterminds.github.io/sprig/) with the exclusion of the functions are not guaranteed to evaluate to the same result for given input (https://github.com/Masterminds/sprig/blob/581758eb7d96ae4d113649668fa96acc74d46e7f/functions.go#L68-L95)

#### "timeDuration" function
The template provides a function named "timeDuration" that accept an int64 and return equivalent `time.Duration`, for example the following will render `5s`:
```text
{{$timeDuration := timeDuration 5000000000}}{{$timeDuration}}
```
5 changes: 0 additions & 5 deletions pkg/genlib/generator_with_text_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"io"
"math/rand"
"text/template"
"time"

"github.com/Masterminds/sprig/v3"
)
Expand Down Expand Up @@ -127,10 +126,6 @@ func NewGeneratorWithTextTemplate(tpl []byte, cfg Config, fields Fields, totSize

templateFns := sprig.TxtFuncMap()

templateFns["timeDuration"] = func(duration int64) time.Duration {
return time.Duration(duration)
}

templateFns["awsAZFromRegion"] = func(region string) string {
azs, ok := awsAZs[region]
if !ok {
Expand Down