Skip to content

Commit 4755c0d

Browse files
replace Map with sync.Map to handle concurrent reads/writes (changes generated by Cursor);
1 parent f5a20a4 commit 4755c0d

File tree

3 files changed

+93
-59
lines changed

3 files changed

+93
-59
lines changed

dev/pyRevitTelemetryServer/persistence/models.go

Lines changed: 77 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package persistence
22

33
import (
44
"fmt"
5+
"sync"
56

67
"pyrevittelemetryserver/cli"
78

@@ -21,26 +22,37 @@ type TraceInfoV1 struct {
2122
}
2223

2324
type ScriptTelemetryRecordV1 struct {
24-
Date string `json:"date" bson:"date" valid:"-"`
25-
Time string `json:"time" bson:"time" valid:"-"`
26-
UserName string `json:"username" bson:"username" valid:"-"`
27-
RevitVersion string `json:"revit" bson:"revit" valid:"numeric~Invalid revit version"`
28-
RevitBuild string `json:"revitbuild" bson:"revitbuild" valid:"matches(\\d{8}_\\d{4}\\(x\\d{2}\\))~Invalid revit build number"`
29-
SessionId string `json:"sessionid" bson:"sessionid" valid:"uuidv4~Invalid session id"`
30-
PyRevitVersion string `json:"pyrevit" bson:"pyrevit" valid:"-"`
31-
IsDebugMode bool `json:"debug" bson:"debug"`
32-
IsConfigMode bool `json:"config" bson:"config"`
33-
CommandName string `json:"commandname" bson:"commandname" valid:"-"`
34-
CommandUniqueName string `json:"commanduniquename" bson:"commanduniquename" valid:"-"`
35-
BundleName string `json:"commandbundle" bson:"commandbundle" valid:"-"`
36-
ExtensionName string `json:"commandextension" bson:"commandextension" valid:"-"`
37-
ResultCode int `json:"resultcode" bson:"resultcode" valid:"numeric~Invalid result code"`
38-
CommandResults map[string]string `json:"commandresults" bson:"commandresults" valid:"-"`
39-
ScriptPath string `json:"scriptpath" bson:"scriptpath" valid:"-"`
40-
TraceInfo TraceInfoV1 `json:"trace" bson:"trace"`
25+
Date string `json:"date" bson:"date" valid:"-"`
26+
Time string `json:"time" bson:"time" valid:"-"`
27+
UserName string `json:"username" bson:"username" valid:"-"`
28+
RevitVersion string `json:"revit" bson:"revit" valid:"numeric~Invalid revit version"`
29+
RevitBuild string `json:"revitbuild" bson:"revitbuild" valid:"matches(\\d{8}_\\d{4}\\(x\\d{2}\\))~Invalid revit build number"`
30+
SessionId string `json:"sessionid" bson:"sessionid" valid:"uuidv4~Invalid session id"`
31+
PyRevitVersion string `json:"pyrevit" bson:"pyrevit" valid:"-"`
32+
IsDebugMode bool `json:"debug" bson:"debug"`
33+
IsConfigMode bool `json:"config" bson:"config"`
34+
CommandName string `json:"commandname" bson:"commandname" valid:"-"`
35+
CommandUniqueName string `json:"commanduniquename" bson:"commanduniquename" valid:"-"`
36+
BundleName string `json:"commandbundle" bson:"commandbundle" valid:"-"`
37+
ExtensionName string `json:"commandextension" bson:"commandextension" valid:"-"`
38+
ResultCode int `json:"resultcode" bson:"resultcode" valid:"numeric~Invalid result code"`
39+
CommandResults sync.Map `json:"commandresults" bson:"commandresults" valid:"-"`
40+
ScriptPath string `json:"scriptpath" bson:"scriptpath" valid:"-"`
41+
TraceInfo TraceInfoV1 `json:"trace" bson:"trace"`
4142
}
4243

4344
func (logrec ScriptTelemetryRecordV1) PrintRecordInfo(logger *cli.Logger, message string) {
45+
// Convert sync.Map to regular map for printing
46+
results := make(map[string]string)
47+
logrec.CommandResults.Range(func(key, value interface{}) bool {
48+
if k, ok := key.(string); ok {
49+
if v, ok := value.(string); ok {
50+
results[k] = v
51+
}
52+
}
53+
return true
54+
})
55+
4456
logger.Print(fmt.Sprintf(
4557
"%s %s-%s %q @ %s:%s [%s.%s] code=%d info=%v",
4658
message,
@@ -52,7 +64,7 @@ func (logrec ScriptTelemetryRecordV1) PrintRecordInfo(logger *cli.Logger, messag
5264
logrec.ExtensionName,
5365
logrec.CommandName,
5466
logrec.ResultCode,
55-
logrec.CommandResults,
67+
results,
5668
))
5769
}
5870

@@ -66,10 +78,10 @@ func (logrec ScriptTelemetryRecordV1) Validate() error {
6678

6779
// v2.0
6880
type EngineInfoV2 struct {
69-
Type string `json:"type" bson:"type" valid:"engine~Invalid executor engine type"`
70-
Version string `json:"version" bson:"version" valid:"-"`
71-
SysPaths []string `json:"syspath" bson:"syspath" valid:"-"`
72-
Configs map[string]interface{} `json:"configs" bson:"configs" valid:"-"`
81+
Type string `json:"type" bson:"type" valid:"engine~Invalid executor engine type"`
82+
Version string `json:"version" bson:"version" valid:"-"`
83+
SysPaths []string `json:"syspath" bson:"syspath" valid:"-"`
84+
Configs sync.Map `json:"configs" bson:"configs" valid:"-"`
7385
}
7486

7587
type TraceInfoV2 struct {
@@ -82,33 +94,42 @@ type RecordMetaV2 struct {
8294
}
8395

8496
type ScriptTelemetryRecordV2 struct {
85-
RecordMeta RecordMetaV2 `json:"meta" bson:"meta"`
86-
TimeStamp string `json:"timestamp" bson:"timestamp" valid:"rfc3339~Invalid timestamp"`
87-
UserName string `json:"username" bson:"username" valid:"-"`
88-
HostUserName string `json:"host_user" bson:"host_user" valid:"-"`
89-
RevitVersion string `json:"revit" bson:"revit" valid:"numeric~Invalid revit version"`
90-
RevitBuild string `json:"revitbuild" bson:"revitbuild" valid:"matches(\\d{8}_\\d{4}\\(x\\d{2}\\))~Invalid revit build number"`
91-
SessionId string `json:"sessionid" bson:"sessionid" valid:"uuidv4~Invalid session id"`
92-
PyRevitVersion string `json:"pyrevit" bson:"pyrevit" valid:"-"`
93-
Clone string `json:"clone" bson:"clone" valid:"-"`
94-
IsDebugMode bool `json:"debug" bson:"debug"`
95-
IsConfigMode bool `json:"config" bson:"config"`
96-
IsExecFromGUI bool `json:"from_gui" bson:"from_gui"`
97-
ExecId string `json:"exec_id" bson:"exec_id" valid:"-"`
98-
ExecTimeStamp string `json:"exec_timestamp" bson:"exec_timestamp" valid:"-"`
99-
CommandName string `json:"commandname" bson:"commandname" valid:"-"`
100-
CommandUniqueName string `json:"commanduniquename" bson:"commanduniquename" valid:"-"`
101-
BundleName string `json:"commandbundle" bson:"commandbundle" valid:"-"`
102-
ExtensionName string `json:"commandextension" bson:"commandextension" valid:"-"`
103-
DocumentName string `json:"docname" bson:"docname" valid:"-"`
104-
DocumentPath string `json:"docpath" bson:"docpath" valid:"-"`
105-
ResultCode int `json:"resultcode" bson:"resultcode" valid:"numeric~Invalid result code"`
106-
CommandResults map[string]interface{} `json:"commandresults" bson:"commandresults" valid:"-"`
107-
ScriptPath string `json:"scriptpath" bson:"scriptpath" valid:"-"`
108-
TraceInfo TraceInfoV2 `json:"trace" bson:"trace"`
97+
RecordMeta RecordMetaV2 `json:"meta" bson:"meta"`
98+
TimeStamp string `json:"timestamp" bson:"timestamp" valid:"rfc3339~Invalid timestamp"`
99+
UserName string `json:"username" bson:"username" valid:"-"`
100+
HostUserName string `json:"host_user" bson:"host_user" valid:"-"`
101+
RevitVersion string `json:"revit" bson:"revit" valid:"numeric~Invalid revit version"`
102+
RevitBuild string `json:"revitbuild" bson:"revitbuild" valid:"matches(\\d{8}_\\d{4}\\(x\\d{2}\\))~Invalid revit build number"`
103+
SessionId string `json:"sessionid" bson:"sessionid" valid:"uuidv4~Invalid session id"`
104+
PyRevitVersion string `json:"pyrevit" bson:"pyrevit" valid:"-"`
105+
Clone string `json:"clone" bson:"clone" valid:"-"`
106+
IsDebugMode bool `json:"debug" bson:"debug"`
107+
IsConfigMode bool `json:"config" bson:"config"`
108+
IsExecFromGUI bool `json:"from_gui" bson:"from_gui"`
109+
ExecId string `json:"exec_id" bson:"exec_id" valid:"-"`
110+
ExecTimeStamp string `json:"exec_timestamp" bson:"exec_timestamp" valid:"-"`
111+
CommandName string `json:"commandname" bson:"commandname" valid:"-"`
112+
CommandUniqueName string `json:"commanduniquename" bson:"commanduniquename" valid:"-"`
113+
BundleName string `json:"commandbundle" bson:"commandbundle" valid:"-"`
114+
ExtensionName string `json:"commandextension" bson:"commandextension" valid:"-"`
115+
DocumentName string `json:"docname" bson:"docname" valid:"-"`
116+
DocumentPath string `json:"docpath" bson:"docpath" valid:"-"`
117+
ResultCode int `json:"resultcode" bson:"resultcode" valid:"numeric~Invalid result code"`
118+
CommandResults sync.Map `json:"commandresults" bson:"commandresults" valid:"-"`
119+
ScriptPath string `json:"scriptpath" bson:"scriptpath" valid:"-"`
120+
TraceInfo TraceInfoV2 `json:"trace" bson:"trace"`
109121
}
110122

111123
func (logrec ScriptTelemetryRecordV2) PrintRecordInfo(logger *cli.Logger, message string) {
124+
// Convert sync.Map to regular map for printing
125+
results := make(map[string]interface{})
126+
logrec.CommandResults.Range(func(key, value interface{}) bool {
127+
if k, ok := key.(string); ok {
128+
results[k] = value
129+
}
130+
return true
131+
})
132+
112133
logger.Print(fmt.Sprintf(
113134
"%s %s %q %s:%s (%s) [%s.%s] code=%d info=%v",
114135
message,
@@ -120,7 +141,7 @@ func (logrec ScriptTelemetryRecordV2) PrintRecordInfo(logger *cli.Logger, messag
120141
logrec.ExtensionName,
121142
logrec.CommandName,
122143
logrec.ResultCode,
123-
logrec.CommandResults,
144+
results,
124145
))
125146
}
126147

@@ -158,15 +179,15 @@ func (logrec ScriptTelemetryRecordV2) Validate() error {
158179

159180
// introduced with api v2
160181
type EventTelemetryRecordV2 struct {
161-
RecordMeta RecordMetaV2 `json:"meta" bson:"meta"`
162-
TimeStamp string `json:"timestamp" bson:"timestamp" valid:"rfc3339~Invalid timestamp"`
163-
HandlerId string `json:"handler_id" bson:"handler_id" valid:"-"`
164-
EventType string `json:"type" bson:"type" valid:"-"`
165-
EventArgs map[string]interface{} `json:"args" bson:"args" valid:"-"`
166-
UserName string `json:"username" bson:"username" valid:"-"`
167-
HostUserName string `json:"host_user" bson:"host_user" valid:"-"`
168-
RevitVersion string `json:"revit" bson:"revit" valid:"numeric~Invalid revit version"`
169-
RevitBuild string `json:"revitbuild" bson:"revitbuild" valid:"matches(\\d{8}_\\d{4}\\(x\\d{2}\\))~Invalid revit build number"`
182+
RecordMeta RecordMetaV2 `json:"meta" bson:"meta"`
183+
TimeStamp string `json:"timestamp" bson:"timestamp" valid:"rfc3339~Invalid timestamp"`
184+
HandlerId string `json:"handler_id" bson:"handler_id" valid:"-"`
185+
EventType string `json:"type" bson:"type" valid:"-"`
186+
EventArgs sync.Map `json:"args" bson:"args" valid:"-"`
187+
UserName string `json:"username" bson:"username" valid:"-"`
188+
HostUserName string `json:"host_user" bson:"host_user" valid:"-"`
189+
RevitVersion string `json:"revit" bson:"revit" valid:"numeric~Invalid revit version"`
190+
RevitBuild string `json:"revitbuild" bson:"revitbuild" valid:"matches(\\d{8}_\\d{4}\\(x\\d{2}\\))~Invalid revit build number"`
170191

171192
// general
172193
Cancellable bool `json:"cancellable" bson:"cancellable"`

dev/pyRevitTelemetryServer/server/events.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"net/http"
7+
"sync"
78

89
"pyrevittelemetryserver/cli"
910
"pyrevittelemetryserver/persistence"
@@ -37,7 +38,9 @@ func RouteEvents(router *mux.Router, opts *cli.Options, dbConn persistence.Conne
3738
// https://stackoverflow.com/a/26212073
3839
router.HandleFunc("/api/v2/events/", func(w http.ResponseWriter, r *http.Request) {
3940
// parse given json data into a new record
40-
logrec := persistence.EventTelemetryRecordV2{}
41+
logrec := persistence.EventTelemetryRecordV2{
42+
EventArgs: sync.Map{},
43+
}
4144
decodeErr := json.NewDecoder(r.Body).Decode(&logrec)
4245
if decodeErr != nil {
4346
logger.Debug(decodeErr)

dev/pyRevitTelemetryServer/server/scripts.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"net/http"
7+
"sync"
78

89
"pyrevittelemetryserver/cli"
910
"pyrevittelemetryserver/persistence"
@@ -47,7 +48,9 @@ func RouteScripts(router *mux.Router, opts *cli.Options, dbConn persistence.Conn
4748
// https://stackoverflow.com/a/26212073
4849
router.HandleFunc("/api/v1/scripts/", func(w http.ResponseWriter, r *http.Request) {
4950
// parse given json data into a new record
50-
logrec := persistence.ScriptTelemetryRecordV1{}
51+
logrec := persistence.ScriptTelemetryRecordV1{
52+
CommandResults: sync.Map{},
53+
}
5154
decodeErr := json.NewDecoder(r.Body).Decode(&logrec)
5255
if decodeErr != nil {
5356
logger.Debug(decodeErr)
@@ -78,7 +81,14 @@ func RouteScripts(router *mux.Router, opts *cli.Options, dbConn persistence.Conn
7881

7982
router.HandleFunc("/api/v2/scripts/", func(w http.ResponseWriter, r *http.Request) {
8083
// parse given json data into a new record
81-
logrec := persistence.ScriptTelemetryRecordV2{}
84+
logrec := persistence.ScriptTelemetryRecordV2{
85+
CommandResults: sync.Map{},
86+
TraceInfo: persistence.TraceInfoV2{
87+
EngineInfo: persistence.EngineInfoV2{
88+
Configs: sync.Map{},
89+
},
90+
},
91+
}
8292
decodeErr := json.NewDecoder(r.Body).Decode(&logrec)
8393
if decodeErr != nil {
8494
logger.Debug(decodeErr)

0 commit comments

Comments
 (0)