change numlines to numcommands. log all interactive eval commands to activity table. stub pcloud telemetry call

This commit is contained in:
sawka
2023-01-17 16:02:44 -08:00
parent 85c943f65a
commit 353bfad434
6 changed files with 32 additions and 8 deletions
+6
View File
@@ -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)
}
+1 -1
View File
@@ -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,
+8 -1
View File
@@ -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
+5
View File
@@ -0,0 +1,5 @@
package pcloud
func SendTelemetry() error {
return nil
}
+10 -4
View File
@@ -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 {
+2 -2
View File
@@ -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"`