diff --git a/cmd/main-server.go b/cmd/main-server.go index 5f9878a4..25e29105 100644 --- a/cmd/main-server.go +++ b/cmd/main-server.go @@ -20,6 +20,7 @@ import ( "github.com/gorilla/mux" "github.com/scripthaus-dev/sh2-server/pkg/cmdrunner" + "github.com/scripthaus-dev/sh2-server/pkg/pcloud" "github.com/scripthaus-dev/sh2-server/pkg/remote" "github.com/scripthaus-dev/sh2-server/pkg/scbase" "github.com/scripthaus-dev/sh2-server/pkg/scpacket" @@ -405,6 +406,10 @@ func test() error { return nil } +func doBeforeClose() { + pcloud.SendTelemetry() +} + // watch stdin, kill server if stdin is closed func stdinReadWatch() { buf := make([]byte, 1024) @@ -412,6 +417,7 @@ func stdinReadWatch() { _, err := os.Stdin.Read(buf) if err != nil { log.Printf("stdin closed/error, shutting down: %v\n", err) + doBeforeClose() time.Sleep(1 * time.Second) syscall.Kill(syscall.Getpid(), syscall.SIGINT) } diff --git a/db/migrations/000002_activity.up.sql b/db/migrations/000002_activity.up.sql index c2ccd32e..a143305a 100644 --- a/db/migrations/000002_activity.up.sql +++ b/db/migrations/000002_activity.up.sql @@ -1,7 +1,7 @@ CREATE TABLE activity ( day varchar(20) PRIMARY KEY, uploaded boolean NOT NULL, - numlines int NOT NULL, + numcommands int NOT NULL, activeminutes int NOT NULL, fgminutes int NOT NULL, openminutes int NOT NULL, diff --git a/pkg/cmdrunner/cmdrunner.go b/pkg/cmdrunner/cmdrunner.go index 319794e6..13e120c3 100644 --- a/pkg/cmdrunner/cmdrunner.go +++ b/pkg/cmdrunner/cmdrunner.go @@ -365,6 +365,13 @@ func EvalCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore. if len(pk.Args[0]) > MaxCommandLen { return nil, fmt.Errorf("command length too long len:%d, max:%d", len(pk.Args[0]), MaxCommandLen) } + if pk.Interactive { + err := sstore.UpdateCurrentActivity(ctx, sstore.ActivityUpdate{NumCommands: 1}) + if err != nil { + log.Printf("[error] incrementing activity numcommands: %v\n", err) + // fall through (non-fatal error) + } + } var historyContext historyContextType ctxWithHistory := context.WithValue(ctx, historyContextKey, &historyContext) var update sstore.UpdatePacket @@ -376,7 +383,7 @@ func EvalCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore. err := addToHistory(ctx, pk, historyContext, (newPk.MetaCmd != "run"), (rtnErr != nil)) if err != nil { log.Printf("[error] adding to history: %v\n", err) - // continue... + // fall through (non-fatal error) } } return update, rtnErr diff --git a/pkg/pcloud/pcloud.go b/pkg/pcloud/pcloud.go new file mode 100644 index 00000000..33df9a2b --- /dev/null +++ b/pkg/pcloud/pcloud.go @@ -0,0 +1,5 @@ +package pcloud + +func SendTelemetry() error { + return nil +} diff --git a/pkg/sstore/dbops.go b/pkg/sstore/dbops.go index d02a5301..2a501904 100644 --- a/pkg/sstore/dbops.go +++ b/pkg/sstore/dbops.go @@ -1844,16 +1844,22 @@ func UpdateCurrentActivity(ctx context.Context, update ActivityUpdate) error { txErr := WithTx(ctx, func(tx *TxWrap) error { query := `SELECT day FROM activity WHERE day = ?` if !tx.Exists(query, dayStr) { - query = `INSERT INTO activity (day, uploaded, numlines, fgminutes, activeminutes, openminutes, tzname, tzoffset, clientversion, clientarch) - VALUES (?, 0, 0, 0, 0, 0, ?, ?, ?, ?)` + query = `INSERT INTO activity (day, uploaded, numcommands, fgminutes, activeminutes, openminutes, tzname, tzoffset, clientversion, clientarch) + VALUES (?, 0, 0, 0, 0, 0, ?, ?, ?, ?)` tzName, tzOffset := now.Zone() if len(tzName) > MaxTzNameLen { tzName = tzName[0:MaxTzNameLen] } tx.ExecWrap(query, dayStr, tzName, tzOffset, scbase.PromptVersion, scbase.ClientArch()) } - query = `UPDATE activity SET numlines = numlines + ?, fgminutes = fgminutes + ?, activeminutes = activeminutes + ?, openminutes = openminutes + ? WHERE day = ?` - tx.ExecWrap(query, update.NumLines, update.FgMinutes, update.ActiveMinutes, update.OpenMinutes, dayStr) + query = `UPDATE activity + SET numcommands = numcommands + ?, + fgminutes = fgminutes + ?, + activeminutes = activeminutes + ?, + openminutes = openminutes + ?, + clientversion = ? + WHERE day = ?` + tx.ExecWrap(query, update.NumCommands, update.FgMinutes, update.ActiveMinutes, update.OpenMinutes, scbase.PromptVersion, dayStr) return nil }) if txErr != nil { diff --git a/pkg/sstore/sstore.go b/pkg/sstore/sstore.go index ecea27bb..be68bdd2 100644 --- a/pkg/sstore/sstore.go +++ b/pkg/sstore/sstore.go @@ -116,13 +116,13 @@ type ActivityUpdate struct { FgMinutes int ActiveMinutes int OpenMinutes int - NumLines int + NumCommands int } type ActivityType struct { Day string `json:"day"` Uploaded bool `json:"-"` - NumLines int `json:"numlines"` + NumCommands int `json:"numcommands"` ActiveMinutes int `json:"activeminutes"` FgMinutes int `json:"fgminutes"` OpenMinutes int `json:"openminutes"`