@@ -2,6 +2,7 @@ package analytic
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
5
6
"fmt"
6
7
"net/http"
7
8
"sync"
@@ -650,11 +651,12 @@ func nodeAnalyticRecord(env *model.Environment, ctx context.Context) error {
650
651
_ = c .Close ()
651
652
}()
652
653
653
- var nodeStat NodeStat
654
654
messageCount := 0
655
655
656
656
for {
657
- err = c .ReadJSON (& nodeStat )
657
+ // Use json.RawMessage to handle both NodeStat and Node types
658
+ var rawMsg json.RawMessage
659
+ err = c .ReadJSON (& rawMsg )
658
660
if err != nil {
659
661
if helper .IsUnexpectedWebsocketError (err ) {
660
662
logger .Debugf ("nodeAnalyticRecord: Unexpected WebSocket error for environment ID: %d, error: %v" , env .ID , err )
@@ -667,15 +669,42 @@ func nodeAnalyticRecord(env *model.Environment, ctx context.Context) error {
667
669
messageCount ++
668
670
logger .Debugf ("nodeAnalyticRecord: Received message #%d from environment ID: %d" , messageCount , env .ID )
669
671
670
- // set online
671
- nodeStat .Status = true
672
- nodeStat .ResponseAt = time .Now ()
673
-
674
672
mutex .Lock ()
675
673
if NodeMap [env .ID ] != nil {
676
- NodeMap [env .ID ].NodeStat = nodeStat
677
- logger .Debugf ("nodeAnalyticRecord: Updated NodeStat for environment ID: %d, Status: %t, ResponseAt: %v" ,
678
- env .ID , nodeStat .Status , nodeStat .ResponseAt )
674
+ // Try to unmarshal as complete Node first (contains both NodeInfo and NodeStat)
675
+ var fullNode Node
676
+ if err := json .Unmarshal (rawMsg , & fullNode ); err == nil && fullNode .Version != "" {
677
+ // Check if version has changed
678
+ oldVersion := NodeMap [env .ID ].Version
679
+ if oldVersion != "" && oldVersion != fullNode .Version {
680
+ logger .Infof ("nodeAnalyticRecord: Version updated for environment ID: %d, from %s to %s" ,
681
+ env .ID , oldVersion , fullNode .Version )
682
+ }
683
+
684
+ // This is a complete Node with version info - update everything
685
+ NodeMap [env .ID ].NodeInfo = fullNode .NodeInfo
686
+ NodeMap [env .ID ].NodeStat = fullNode .NodeStat
687
+ // Ensure status and response time are set
688
+ NodeMap [env .ID ].NodeStat .Status = true
689
+ NodeMap [env .ID ].NodeStat .ResponseAt = time .Now ()
690
+
691
+ logger .Debugf ("nodeAnalyticRecord: Updated complete Node info for environment ID: %d, Version: %s, Status: %t, ResponseAt: %v" ,
692
+ env .ID , fullNode .Version , NodeMap [env .ID ].NodeStat .Status , NodeMap [env .ID ].NodeStat .ResponseAt )
693
+ } else {
694
+ // Fall back to NodeStat only
695
+ var nodeStat NodeStat
696
+ if err := json .Unmarshal (rawMsg , & nodeStat ); err == nil {
697
+ // set online
698
+ nodeStat .Status = true
699
+ nodeStat .ResponseAt = time .Now ()
700
+
701
+ NodeMap [env .ID ].NodeStat = nodeStat
702
+ logger .Debugf ("nodeAnalyticRecord: Updated NodeStat for environment ID: %d, Status: %t, ResponseAt: %v" ,
703
+ env .ID , nodeStat .Status , nodeStat .ResponseAt )
704
+ } else {
705
+ logger .Debugf ("nodeAnalyticRecord: Failed to unmarshal message for environment ID: %d, error: %v" , env .ID , err )
706
+ }
707
+ }
679
708
} else {
680
709
logger .Debugf ("nodeAnalyticRecord: Warning - Node not found in NodeMap for environment ID: %d" , env .ID )
681
710
}
0 commit comments