github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/controllers/component/component.go (about)

     1  package component
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  	"time"
     8  
     9  	"github.com/gin-gonic/gin"
    10  	"github.com/mdaxf/iac/com"
    11  	"github.com/mdaxf/iac/controllers/common"
    12  	"github.com/mdaxf/iac/logger"
    13  )
    14  
    15  type IACComponentController struct {
    16  }
    17  
    18  type HeartBeat struct {
    19  	Node          map[string]interface{} `json:Node"`
    20  	Result        map[string]interface{} `json:Result"`
    21  	ServiceStatus map[string]interface{} `json:ServiceStatus"`
    22  	Timestamp     time.Time              `json:timestamp"`
    23  }
    24  
    25  func (f *IACComponentController) ComponentHeartbeat(c *gin.Context) {
    26  	iLog := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "Component.heartbeat"}
    27  	startTime := time.Now()
    28  	defer func() {
    29  		elapsed := time.Since(startTime)
    30  		iLog.PerformanceWithDuration("controllers.component.heartbeat", elapsed)
    31  	}()
    32  
    33  	body, clientid, user, err := common.GetRequestBodyandUser(c)
    34  	if err != nil {
    35  		iLog.Error(fmt.Sprintf("Error reading body: %v", err))
    36  		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
    37  		return
    38  	}
    39  	iLog.ClientID = clientid
    40  	iLog.User = user
    41  
    42  	var data HeartBeat
    43  	err = json.Unmarshal(body, &data)
    44  
    45  	if err != nil {
    46  		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
    47  		return
    48  	}
    49  	// update the component status dataset
    50  	//iLog.Debug(fmt.Sprintf("Component.heartbeat: %v", data))
    51  
    52  	com.NodeHeartBeats[data.Node["AppID"].(string)] = data
    53  
    54  	removeNotResponseComponentNodeHeartBeats(iLog)
    55  
    56  	c.JSON(http.StatusOK, gin.H{"Status": "Success"})
    57  }
    58  
    59  func (f *IACComponentController) ComponentClose(c *gin.Context) {
    60  	iLog := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "Component.close"}
    61  	startTime := time.Now()
    62  	defer func() {
    63  		elapsed := time.Since(startTime)
    64  		iLog.PerformanceWithDuration("controllers.component.close", elapsed)
    65  	}()
    66  
    67  	body, clientid, user, err := common.GetRequestBodyandUser(c)
    68  	if err != nil {
    69  		iLog.Error(fmt.Sprintf("Error reading body: %v", err))
    70  		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
    71  		return
    72  	}
    73  	iLog.ClientID = clientid
    74  	iLog.User = user
    75  
    76  	var data HeartBeat
    77  	err = json.Unmarshal(body, &data)
    78  
    79  	if err != nil {
    80  		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
    81  		return
    82  	}
    83  	// update the component status dataset
    84  	iLog.Debug("Component close")
    85  
    86  	if com.NodeHeartBeats[data.Node["AppID"].(string)] != nil {
    87  		data.Node["CloseTime"] = data.Timestamp
    88  		data.Node["Status"] = "Closed"
    89  	}
    90  
    91  	c.JSON(http.StatusOK, gin.H{"data": "Component closed"})
    92  
    93  }
    94  
    95  func removeNotResponseComponentNodeHeartBeats(iLog logger.Log) {
    96  	for key, value := range com.NodeHeartBeats {
    97  
    98  		//	iLog.Debug(fmt.Sprintf("NodeHeartBeats[%s]: %v", key, value))
    99  
   100  		data, ok := value.(HeartBeat)
   101  		if !ok {
   102  			iLog.Error(fmt.Sprintf("NodeHeartBeats[%s] is not HeartBeat", key))
   103  			delete(com.NodeHeartBeats, key)
   104  			continue
   105  		}
   106  		//	iLog.Debug(fmt.Sprintf("NodeHeartBeats[%s]: %v", key, data))
   107  
   108  		lasteHeartBeatTime := data.Timestamp
   109  
   110  		//	iLog.Debug(fmt.Sprintf("NodeHeartBeats[%s][timestamp]: %v", key, lasteHeartBeatTime))
   111  
   112  		if lasteHeartBeatTime.IsZero() {
   113  			iLog.Error(fmt.Sprintf("NodeHeartBeats[%s][timestamp] is zero", key))
   114  			delete(com.NodeHeartBeats, key)
   115  			continue
   116  		}
   117  
   118  		if lasteHeartBeatTime.Add(time.Minute * 30).Before(time.Now().UTC()) {
   119  			iLog.Debug(fmt.Sprintf("NodeHeartBeats[%s][timestamp] is not response", key))
   120  			delete(com.NodeHeartBeats, key)
   121  		}
   122  
   123  		node := data.Node
   124  		if node == nil {
   125  			iLog.Error(fmt.Sprintf("NodeHeartBeats[%s][Node] is nil", key))
   126  			delete(com.NodeHeartBeats, key)
   127  			continue
   128  		}
   129  		//	iLog.Debug(fmt.Sprintf("NodeHeartBeats[%s][Node]: %v", key, node))
   130  
   131  		if node["Status"] == "Closed" {
   132  			iLog.Debug(fmt.Sprintf("NodeHeartBeats[%s][Status] is Closed", key))
   133  			delete(com.NodeHeartBeats, key)
   134  		}
   135  	}
   136  }