Skip to content

Commit 5578177

Browse files
authored
Add default window to wasm playground (#34)
Co-authored-by: Tony Meehan <tonymeehan@users.noreply.github.com>
1 parent f35fc04 commit 5578177

3 files changed

Lines changed: 48 additions & 9 deletions

File tree

cmd/wasm/wasm.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import (
77
"encoding/json"
88
"errors"
99
"syscall/js"
10+
"time"
1011

12+
"github.com/prequel-dev/preq/internal/pkg/config"
1113
"github.com/prequel-dev/preq/internal/pkg/ux"
1214
"github.com/prequel-dev/preq/internal/pkg/verz"
1315
"github.com/prequel-dev/preq/pkg/eval"
@@ -79,8 +81,10 @@ func detectWrapper(ctx context.Context) js.Func {
7981
inputData = args[0].String()
8082
ruleData = args[1].String()
8183

82-
reportDoc, stats, err = eval.Detect(ctx, cfg, inputData, ruleData)
83-
if err != nil {
84+
// Permit events to arrive out of order within a 1 hour window by default
85+
cfg = config.Marshal(config.WithWindow(time.Hour))
86+
87+
if reportDoc, stats, err = eval.Detect(ctx, cfg, inputData, ruleData); err != nil {
8488
return errJson(err)
8589
}
8690

internal/pkg/config/config.go

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"fmt"
45
"os"
56
"path/filepath"
67
"strings"
@@ -12,7 +13,7 @@ import (
1213
)
1314

1415
var (
15-
DefaultConfig = `timestamps:
16+
defaultConfig = `timestamps:
1617
1718
# Example: {"level":"error","error":"context deadline exceeded","time":1744570895480541,"caller":"server.go:462"}
1819
- format: epochany
@@ -164,6 +165,8 @@ var (
164165
pattern: |
165166
/Date\((\d+)\)
166167
`
168+
169+
windowConfig = `window: %s`
167170
)
168171

169172
type NotificationWebhook struct {
@@ -193,7 +196,39 @@ type Regex struct {
193196
Format string `yaml:"format"`
194197
}
195198

196-
func LoadConfig(dir, file string) (*Config, error) {
199+
type OptT func(*optsT)
200+
201+
type optsT struct {
202+
window time.Duration
203+
}
204+
205+
func WithWindow(window time.Duration) func(*optsT) {
206+
return func(o *optsT) {
207+
o.window = window
208+
}
209+
}
210+
211+
func parseOpts(opts ...OptT) *optsT {
212+
o := &optsT{}
213+
for _, opt := range opts {
214+
opt(o)
215+
}
216+
return o
217+
}
218+
219+
func Marshal(opts ...OptT) string {
220+
o := parseOpts(opts...)
221+
222+
if o.window > 0 {
223+
wcfg := fmt.Sprintf(windowConfig, o.window)
224+
return fmt.Sprintf("%s\n%s\n", defaultConfig, wcfg)
225+
}
226+
227+
return defaultConfig
228+
}
229+
230+
func LoadConfig(dir, file string, opts ...OptT) (*Config, error) {
231+
197232
var config Config
198233

199234
if _, err := os.Stat(dir); os.IsNotExist(err) {
@@ -203,7 +238,7 @@ func LoadConfig(dir, file string) (*Config, error) {
203238
}
204239

205240
if _, err := os.Stat(filepath.Join(dir, file)); os.IsNotExist(err) {
206-
if err := WriteDefaultConfig(filepath.Join(dir, file)); err != nil {
241+
if err := WriteDefaultConfig(filepath.Join(dir, file), opts...); err != nil {
207242
log.Error().Err(err).Msg("Failed to write default config")
208243
return nil, err
209244
}
@@ -221,8 +256,9 @@ func LoadConfig(dir, file string) (*Config, error) {
221256
return &config, nil
222257
}
223258

224-
func WriteDefaultConfig(path string) error {
225-
return os.WriteFile(path, []byte(DefaultConfig), 0644)
259+
func WriteDefaultConfig(path string, opts ...OptT) error {
260+
cfg := Marshal(opts...)
261+
return os.WriteFile(path, []byte(cfg), 0644)
226262
}
227263

228264
func LoadConfigFromBytes(data string) (*Config, error) {

pkg/eval/eval.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ func Detect(ctx context.Context, cfg, data, rule string) (ux.ReportDocT, ux.Stat
2626
)
2727

2828
if len(cfg) == 0 {
29-
log.Warn().Msg("No config provided, using default")
30-
cfg = config.DefaultConfig
29+
cfg = config.Marshal()
3130
}
3231

3332
if c, err = config.LoadConfigFromBytes(cfg); err != nil {

0 commit comments

Comments
 (0)