|
| 1 | + |
| 2 | +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 3 | +;; |
| 4 | +;; MODULE : telemetry-track.scm |
| 5 | +;; DESCRIPTION : Telemetry event tracking with memory queue and flush |
| 6 | +;; COPYRIGHT : (C) 2026 Yuki Lu |
| 7 | +;; |
| 8 | +;; This software falls under the GNU general public license version 3 or later. |
| 9 | +;; It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE |
| 10 | +;; in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>. |
| 11 | +;; |
| 12 | +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 13 | + |
| 14 | +(texmacs-module (telemetry telemetry-track) |
| 15 | + (:use (telemetry telemetry-utils))) |
| 16 | + |
| 17 | +(import (scheme base) |
| 18 | + (liii base) |
| 19 | + (liii json) |
| 20 | + (liii os) |
| 21 | + (liii path) |
| 22 | + (liii string) |
| 23 | + (liii list) |
| 24 | +) |
| 25 | + |
| 26 | +(define-public *telemetry-event-queue* '()) |
| 27 | + |
| 28 | +(define-public (track-event event-type properties) |
| 29 | + (if (not (telemetry-enabled?)) |
| 30 | + #f |
| 31 | + (if (and (string? event-type) (not (string-null? event-type))) |
| 32 | + (begin |
| 33 | + (set! *telemetry-event-queue* |
| 34 | + (cons (telemetry-make-event event-type properties) |
| 35 | + *telemetry-event-queue*)) |
| 36 | + (let ((len (length *telemetry-event-queue*))) |
| 37 | + (display (string-append "[telemetry] track: " event-type |
| 38 | + " (queue: " (number->string len) |
| 39 | + "/" (number->string (telemetry-get-buffer-size)) ")\n")) |
| 40 | + (if (> len telemetry-max-queue-size) |
| 41 | + (set! *telemetry-event-queue* |
| 42 | + (list-head *telemetry-event-queue* telemetry-max-queue-size))) |
| 43 | + (if (>= len (telemetry-get-buffer-size)) |
| 44 | + (telemetry-flush))) |
| 45 | + #t) |
| 46 | + #f))) |
| 47 | + |
| 48 | +(define-public (telemetry-queue-length) |
| 49 | + (length *telemetry-event-queue*)) |
| 50 | + |
| 51 | +(define-public (telemetry-flush-if-needed) |
| 52 | + (if (not (telemetry-enabled?)) |
| 53 | + #t |
| 54 | + (if (not (null? *telemetry-event-queue*)) |
| 55 | + (telemetry-flush) |
| 56 | + #t))) |
| 57 | + |
| 58 | +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 59 | +;; Flush implementation: lightweight append-only file writes |
| 60 | +;; Complex logic (size limits, stale filtering) handled by liii subprocess |
| 61 | +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 62 | + |
| 63 | +(define telemetry-lock-timeout-seconds 30) |
| 64 | + |
| 65 | +(define (telemetry-lock-info owner now) |
| 66 | + `(("owner" . ,owner) ("created_at" . ,now))) |
| 67 | + |
| 68 | +(define (telemetry-read-lock-info) |
| 69 | + (catch #t |
| 70 | + (lambda () |
| 71 | + (let ((text (string-load (system->url (telemetry-lock-info-path))))) |
| 72 | + (if (and (string? text) (> (string-length text) 0)) |
| 73 | + (string->json text) |
| 74 | + #f))) |
| 75 | + (lambda args #f))) |
| 76 | + |
| 77 | +(define (telemetry-lock-expired? now) |
| 78 | + (let ((info (telemetry-read-lock-info))) |
| 79 | + (if info |
| 80 | + (let ((created (json-ref-number info "created_at" 0))) |
| 81 | + (> (- now created) telemetry-lock-timeout-seconds)) |
| 82 | + #t))) |
| 83 | + |
| 84 | +(define (telemetry-remove-lock) |
| 85 | + (catch #t |
| 86 | + (lambda () |
| 87 | + (path-unlink (telemetry-lock-info-path) #t) |
| 88 | + (rmdir (telemetry-lock-path))) |
| 89 | + (lambda args #f))) |
| 90 | + |
| 91 | +(define (telemetry-acquire-lock) |
| 92 | + (telemetry-ensure-dir) |
| 93 | + (let ((owner (telemetry-lock-owner)) |
| 94 | + (now (inexact->exact (truncate (current-time))))) |
| 95 | + (catch #t |
| 96 | + (lambda () |
| 97 | + (mkdir (telemetry-lock-path)) |
| 98 | + (string-save |
| 99 | + (json->string (telemetry-lock-info owner now)) |
| 100 | + (system->url (telemetry-lock-info-path))) |
| 101 | + owner) |
| 102 | + (lambda args |
| 103 | + (if (telemetry-lock-expired? now) |
| 104 | + (begin |
| 105 | + (telemetry-remove-lock) |
| 106 | + (catch #t |
| 107 | + (lambda () |
| 108 | + (mkdir (telemetry-lock-path)) |
| 109 | + (string-save |
| 110 | + (json->string (telemetry-lock-info owner now)) |
| 111 | + (system->url (telemetry-lock-info-path))) |
| 112 | + owner) |
| 113 | + (lambda args2 #f))) |
| 114 | + #f))))) |
| 115 | + |
| 116 | +(define (telemetry-release-lock owner) |
| 117 | + (let ((info (telemetry-read-lock-info))) |
| 118 | + (if (and info |
| 119 | + (string=? (json-ref-string info "owner" "") owner)) |
| 120 | + (telemetry-remove-lock) |
| 121 | + (begin |
| 122 | + (display (string-append "[telemetry] warn: lock owner mismatch, skipping release " |
| 123 | + "(expected " owner ", got " |
| 124 | + (if info (json-ref-string info "owner" "") "none") ")\n")) |
| 125 | + #f)))) |
| 126 | + |
| 127 | +(define-public (telemetry-write-pending events) |
| 128 | + (if (null? events) |
| 129 | + #t |
| 130 | + (let ((path (telemetry-pending-path)) |
| 131 | + (lines (map json->string events))) |
| 132 | + (catch #t |
| 133 | + (lambda () |
| 134 | + (let ((text (string-append (string-join lines "\n") "\n"))) |
| 135 | + (string-append-to-file text (system->url path)) |
| 136 | + (display (string-append "[telemetry] flush: " |
| 137 | + (number->string (length events)) |
| 138 | + " events -> " |
| 139 | + path "\n")) |
| 140 | + #t)) |
| 141 | + (lambda args |
| 142 | + (display (string-append "[telemetry] error: write failed: " |
| 143 | + (object->string args) "\n")) |
| 144 | + #f))))) |
| 145 | + |
| 146 | +(define-public (telemetry-flush) |
| 147 | + (if (null? *telemetry-event-queue*) |
| 148 | + #t |
| 149 | + (let ((owner (telemetry-acquire-lock))) |
| 150 | + (if owner |
| 151 | + (let ((ok? (telemetry-write-pending (reverse *telemetry-event-queue*)))) |
| 152 | + (if ok? |
| 153 | + (begin |
| 154 | + (set! *telemetry-event-queue* '()) |
| 155 | + (telemetry-release-lock owner) |
| 156 | + #t) |
| 157 | + (begin |
| 158 | + (display (string-append "[telemetry] error: flush failed, keeping " |
| 159 | + (number->string (length *telemetry-event-queue*)) |
| 160 | + " events in memory queue\n")) |
| 161 | + (telemetry-release-lock owner) |
| 162 | + #f))) |
| 163 | + #f)))) |
0 commit comments