@@ -27,6 +27,7 @@ import (
2727
2828const MaxTzNameLen = 50
2929const ActivityEventName = "app:activity"
30+ const WshRunEventName = "wsh:run"
3031
3132var cachedTosAgreedTs atomic.Int64
3233
@@ -196,6 +197,44 @@ func updateActivityTEvent(ctx context.Context, tevent *telemetrydata.TEvent) err
196197 })
197198}
198199
200+ // aggregates wsh:run events per (cmd, haderror) key within the current 1-hour bucket
201+ func updateWshRunTEvent (ctx context.Context , tevent * telemetrydata.TEvent ) error {
202+ eventTs := time .Now ().Truncate (time .Hour ).Add (time .Hour )
203+ incomingCount := tevent .Props .WshCount
204+ if incomingCount <= 0 {
205+ incomingCount = 1
206+ }
207+ return wstore .WithTx (ctx , func (tx * wstore.TxWrap ) error {
208+ uuidStr := tx .GetString (
209+ `SELECT uuid FROM db_tevent WHERE ts = ? AND event = ? AND json_extract(props, '$."wsh:cmd"') IS ?` ,
210+ eventTs .UnixMilli (), WshRunEventName , tevent .Props .WshCmd ,
211+ )
212+ if uuidStr != "" {
213+ var curProps telemetrydata.TEventProps
214+ rawProps := tx .GetString (`SELECT props FROM db_tevent WHERE uuid = ?` , uuidStr )
215+ if rawProps != "" {
216+ if err := json .Unmarshal ([]byte (rawProps ), & curProps ); err != nil {
217+ log .Printf ("error unmarshalling wsh:run props: %v\n " , err )
218+ }
219+ }
220+ curCount := curProps .WshCount
221+ if curCount <= 0 {
222+ curCount = 1
223+ }
224+ curProps .WshCount = curCount + incomingCount
225+ curProps .WshErrorCount += tevent .Props .WshErrorCount
226+ tx .Exec (`UPDATE db_tevent SET props = ? WHERE uuid = ?` , dbutil .QuickJson (curProps ), uuidStr )
227+ } else {
228+ newProps := tevent .Props
229+ newProps .WshCount = incomingCount
230+ tsLocal := utilfn .ConvertToWallClockPT (eventTs ).Format (time .RFC3339 )
231+ tx .Exec (`INSERT INTO db_tevent (uuid, ts, tslocal, event, props) VALUES (?, ?, ?, ?, ?)` ,
232+ uuid .New ().String (), eventTs .UnixMilli (), tsLocal , WshRunEventName , dbutil .QuickJson (newProps ))
233+ }
234+ return nil
235+ })
236+ }
237+
199238func TruncateActivityTEventForShutdown (ctx context.Context ) error {
200239 nowTs := time .Now ()
201240 eventTs := nowTs .Truncate (time .Hour ).Add (time .Hour )
@@ -259,6 +298,9 @@ func RecordTEvent(ctx context.Context, tevent *telemetrydata.TEvent) error {
259298 if tevent .Event == ActivityEventName {
260299 return updateActivityTEvent (ctx , tevent )
261300 }
301+ if tevent .Event == WshRunEventName {
302+ return updateWshRunTEvent (ctx , tevent )
303+ }
262304 return insertTEvent (ctx , tevent )
263305}
264306
0 commit comments