MCPcopy
hub / github.com/wavetermdev/waveterm / updateActivityTEvent

Function updateActivityTEvent

pkg/telemetry/telemetry.go:167–198  ·  view source on GitHub ↗

ignores the timestamp in tevent, and uses the current time

(ctx context.Context, tevent *telemetrydata.TEvent)

Source from the content-addressed store, hash-verified

165
166// ignores the timestamp in tevent, and uses the current time
167func updateActivityTEvent(ctx context.Context, tevent *telemetrydata.TEvent) error {
168 eventTs := time.Now()
169 // compute to 1-hour boundary, and round up to next 1-hour boundary
170 eventTs = eventTs.Truncate(time.Hour).Add(time.Hour)
171
172 return wstore.WithTx(ctx, func(tx *wstore.TxWrap) error {
173 // find event that matches this timestamp with event name "app:activity"
174 var hasRow bool
175 var curActivity telemetrydata.TEventProps
176 uuidStr := tx.GetString(`SELECT uuid FROM db_tevent WHERE ts = ? AND event = ?`, eventTs.UnixMilli(), ActivityEventName)
177 if uuidStr != "" {
178 hasRow = true
179 rawProps := tx.GetString(`SELECT props FROM db_tevent WHERE uuid = ?`, uuidStr)
180 err := json.Unmarshal([]byte(rawProps), &curActivity)
181 if err != nil {
182 // ignore, curActivity will just be 0
183 log.Printf("error unmarshalling activity props: %v\n", err)
184 }
185 }
186 mergeActivity(&curActivity, tevent.Props)
187
188 if hasRow {
189 query := `UPDATE db_tevent SET props = ? WHERE uuid = ?`
190 tx.Exec(query, dbutil.QuickJson(curActivity), uuidStr)
191 } else {
192 query := `INSERT INTO db_tevent (uuid, ts, tslocal, event, props) VALUES (?, ?, ?, ?, ?)`
193 tsLocal := utilfn.ConvertToWallClockPT(eventTs).Format(time.RFC3339)
194 tx.Exec(query, uuid.New().String(), eventTs.UnixMilli(), tsLocal, ActivityEventName, dbutil.QuickJson(curActivity))
195 }
196 return nil
197 })
198}
199
200// aggregates wsh:run events per (cmd, haderror) key within the current 1-hour bucket
201func updateWshRunTEvent(ctx context.Context, tevent *telemetrydata.TEvent) error {

Callers 1

RecordTEventFunction · 0.85

Calls 6

WithTxFunction · 0.92
QuickJsonFunction · 0.92
ConvertToWallClockPTFunction · 0.92
mergeActivityFunction · 0.85
GetStringMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected