From a4ca71d0add61c576689a16e10e8c25ff9c0519b Mon Sep 17 00:00:00 2001 From: saji Date: Wed, 14 Feb 2024 02:15:21 -0600 Subject: [PATCH] make regex precompiled that made things fast lol also fix error output for skylabify --- cmd/skylabify/skylabify.go | 2 +- go.mod | 6 ++++++ go.sum | 11 +++++++++++ internal/db/db.go | 4 ++-- internal/logparsers/parsers.go | 4 ++-- socketcan/netlink.go | 15 +++++++++++++-- 6 files changed, 35 insertions(+), 7 deletions(-) diff --git a/cmd/skylabify/skylabify.go b/cmd/skylabify/skylabify.go index 83d4021..f4a83ad 100644 --- a/cmd/skylabify/skylabify.go +++ b/cmd/skylabify/skylabify.go @@ -120,7 +120,7 @@ func run(ctx *cli.Context) (err error) { continue } else if err != nil { // TODO: we should consider absorbing all errors. - fmt.Printf("got an error %v\n", err) + slog.Error("got an error", "err", err) n_err++ continue } diff --git a/go.mod b/go.mod index f36a42e..c665eef 100644 --- a/go.mod +++ b/go.mod @@ -19,9 +19,15 @@ require ( require ( github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect github.com/creack/goselect v0.1.2 // indirect + github.com/google/go-cmp v0.5.9 // indirect + github.com/josharian/native v1.1.0 // indirect github.com/klauspost/compress v1.10.3 // indirect + github.com/mdlayher/netlink v1.7.2 // indirect + github.com/mdlayher/socket v0.4.1 // indirect github.com/philhofer/fwd v1.1.2 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/stretchr/testify v1.8.0 // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect + golang.org/x/net v0.9.0 // indirect + golang.org/x/sync v0.1.0 // indirect ) diff --git a/go.sum b/go.sum index 1981130..6dbc64a 100644 --- a/go.sum +++ b/go.sum @@ -31,6 +31,8 @@ github.com/golang/protobuf v1.3.5 h1:F768QJ1E9tib+q5Sc8MkdJi1RxLTbRcTf8LJV56aRls github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -38,6 +40,8 @@ github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvK github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g= github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ= +github.com/josharian/native v1.1.0 h1:uuaP0hAbW7Y4l0ZRQ6C9zfb7Mg1mbFKry/xzDAfmtLA= +github.com/josharian/native v1.1.0/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w= github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/klauspost/compress v1.10.3 h1:OP96hzwJVBIHYU52pVTI6CczrxPvrGfgqF9N5eTO0Q8= @@ -51,6 +55,10 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= +github.com/mdlayher/netlink v1.7.2 h1:/UtM3ofJap7Vl4QWCPDGXY8d3GIY2UGSDbK+QWmY8/g= +github.com/mdlayher/netlink v1.7.2/go.mod h1:xraEF7uJbxLhc5fpHL4cPe221LI2bdttWlU+ZGLfQSw= +github.com/mdlayher/socket v0.4.1 h1:eM9y2/jlbs1M615oshPQOHZzj6R6wMT7bX5NPiQvn2U= +github.com/mdlayher/socket v0.4.1/go.mod h1:cAqeGjoufqdxWkD7DkpyS+wcefOtmu5OQ8KuoJGIReA= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= @@ -91,8 +99,11 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE= +golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM= +golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/internal/db/db.go b/internal/db/db.go index 0532e03..8eddb32 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -78,6 +78,7 @@ func (tdb *TelemDb) AddEventsCtx(ctx context.Context, events ...skylab.BusEvent) // n = 0 tx, err := tdb.db.BeginTx(ctx, nil) + defer tx.Rollback() if err != nil { return } @@ -104,13 +105,12 @@ func (tdb *TelemDb) AddEventsCtx(ctx context.Context, events ...skylab.BusEvent) // construct the full statement now sqlStmt = sqlStmt + strings.Join(inserts[:idx], ",") stmt, err := tx.PrepareContext(ctx, sqlStmt) + defer stmt.Close() if err != nil { - tx.Rollback() return } res, err := stmt.ExecContext(ctx, vals...) if err != nil { - tx.Rollback() return } n, err = res.RowsAffected() diff --git a/internal/logparsers/parsers.go b/internal/logparsers/parsers.go index 17515b9..24dce35 100644 --- a/internal/logparsers/parsers.go +++ b/internal/logparsers/parsers.go @@ -90,6 +90,7 @@ func parseCanDumpLine(dumpLine string) (b skylab.BusEvent, err error) { return } +var telemRegex = regexp.MustCompile(`^(\d+).(\d{3}) (\w{3})(\w+)$`) func parseTelemLogLine(line string) (b skylab.BusEvent, err error) { b = skylab.BusEvent{} // strip trailng newline since we rely on it being gone @@ -99,7 +100,6 @@ func parseTelemLogLine(line string) (b skylab.BusEvent, err error) { // the second part there is 3 nibbles (12 bits, 3 hex chars) for can ID, // the rest is data. // this regex does the processing. - r := regexp.MustCompile(`^(\d+).(\d{3}) (\w{3})(\w+)$`) // these files tend to get corrupted. there are all kinds of nasties that can happen. // defense against random panics @@ -108,7 +108,7 @@ func parseTelemLogLine(line string) (b skylab.BusEvent, err error) { err = NewFormatError("caught panic", nil) } }() - a := r.FindStringSubmatch(line) + a := telemRegex.FindStringSubmatch(line) if a == nil || len(a) != 5 { err = NewFormatError("no regex match", nil) return diff --git a/socketcan/netlink.go b/socketcan/netlink.go index 15e9a0c..451d11e 100644 --- a/socketcan/netlink.go +++ b/socketcan/netlink.go @@ -1,5 +1,16 @@ -//go:build linux +//go:build ignore +// +build ignore -package socketcan +package main // TODO: implement netlink support to set baud rate and other parameters. + +import ( + "github.com/mdlayher/netlink" + "golang.org/x/sys/unix" +) + +// this program demonstrates basic CAN stuff. +// i give up this shit is so hard + +