This is an automated email from the ASF dual-hosted git repository.

zhongxjian pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-kubernetes.git


The following commit(s) were added to refs/heads/master by this push:
     new 6d60bb16 [horus] Optimization and repair testing (#337)
6d60bb16 is described below

commit 6d60bb167849b8e246cc6a4160d58aed78d06a28
Author: mfordjody <[email protected]>
AuthorDate: Thu Sep 12 13:43:50 2024 +0800

    [horus] Optimization and repair testing (#337)
    
    fix typo
---
 app/horus/basic/db/db.go              | 33 +++++++++++++++++++--
 app/horus/basic/db/db_test.go         | 56 +++++++++++++++++++++++++++++++++++
 app/horus/core/alert/dingtalk_test.go |  5 ++--
 app/horus/core/alert/slack_test.go    |  5 ++--
 4 files changed, 92 insertions(+), 7 deletions(-)

diff --git a/app/horus/basic/db/db.go b/app/horus/basic/db/db.go
index cb087714..6b58f72d 100644
--- a/app/horus/basic/db/db.go
+++ b/app/horus/basic/db/db.go
@@ -19,6 +19,7 @@ import (
        "fmt"
        "github.com/apache/dubbo-kubernetes/app/horus/basic/config"
        _ "github.com/go-sql-driver/mysql"
+       "time"
        "xorm.io/xorm"
        xlog "xorm.io/xorm/log"
 )
@@ -74,12 +75,24 @@ func InitDataBase(mc *config.MysqlConfiguration) error {
 }
 
 func (n *NodeDataInfo) Add() (int64, error) {
-       exist, _ := db.Exist(n)
+       exist, err := db.Exist(n)
+       if err != nil {
+               return 0, err
+       }
        if exist {
                return n.Id, nil
        }
-       absent, err := n.Add()
-       return absent, err
+
+       affected, err := db.Insert(n)
+       if err != nil {
+               return 0, err
+       }
+
+       if affected > 0 {
+               return n.Id, nil
+       }
+
+       return 0, fmt.Errorf("failed to insert record")
 }
 
 func (n *NodeDataInfo) Get() (*NodeDataInfo, error) {
@@ -93,6 +106,20 @@ func (n *NodeDataInfo) Get() (*NodeDataInfo, error) {
        return n, nil
 }
 
+func (n *NodeDataInfo) Update() (bool, error) {
+       firstDate := time.Now().Format("2006-01-02 15:04:05")
+       n.FirstDate = firstDate
+
+       row, err := db.Where(fmt.Sprintf("id=%d", n.Id)).Update(n)
+       if err != nil {
+               return false, err
+       }
+       if row > 0 {
+               return true, nil
+       }
+       return false, nil
+}
+
 func (n *NodeDataInfo) Check() (bool, error) {
        exist, err := db.Exist(n)
        return exist, err
diff --git a/app/horus/basic/db/db_test.go b/app/horus/basic/db/db_test.go
new file mode 100644
index 00000000..b102d681
--- /dev/null
+++ b/app/horus/basic/db/db_test.go
@@ -0,0 +1,56 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package db_test
+
+import (
+       "github.com/apache/dubbo-kubernetes/app/horus/basic/config"
+       "github.com/apache/dubbo-kubernetes/app/horus/basic/db"
+       "testing"
+       "time"
+)
+
+func TestDataBase(t *testing.T) {
+       mc := &config.MysqlConfiguration{
+               Addr:  
"root:root@tcp(127.0.0.1:3306)/horus?charset=utf8&parseTime=True",
+               Debug: true,
+       }
+
+       err := db.InitDataBase(mc)
+       if err != nil {
+               t.Fatalf("Failed to initialize database: %v", err)
+               return
+       }
+
+       today := time.Now().Format("2006-01-02 15:04:05")
+       data := db.NodeDataInfo{
+               NodeName:    "db-test",
+               NodeIP:      "1.1.1.1",
+               Sn:          "123",
+               ClusterName: "beijing01",
+               ModuleName:  "model",
+               Reason:      "time wrong",
+               Restart:     0,
+               Repair:      0,
+               FirstDate:   today,
+       }
+       data.NodeName = "002"
+       id, err := data.Add()
+       t.Logf("test.add id:%v err:%v", id, err)
+       query, err := data.Get()
+       t.Logf("test.get query:%v err:%v", query, err)
+       update, err := data.Update()
+       t.Logf("test.update %v err: %v", update, err)
+}
diff --git a/app/horus/core/alert/dingtalk_test.go 
b/app/horus/core/alert/dingtalk_test.go
index 2e22b39d..adfd62c4 100644
--- a/app/horus/core/alert/dingtalk_test.go
+++ b/app/horus/core/alert/dingtalk_test.go
@@ -13,10 +13,11 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package alert
+package alert_test
 
 import (
        "github.com/apache/dubbo-kubernetes/app/horus/basic/config"
+       "github.com/apache/dubbo-kubernetes/app/horus/core/alert"
        "testing"
 )
 
@@ -26,5 +27,5 @@ func TestDingTalkSend(t *testing.T) {
                Title:      "",
                AtMobiles:  nil,
        }
-       DingTalkSend(im, "工单通知处理")
+       alert.DingTalkSend(im, "test")
 }
diff --git a/app/horus/core/alert/slack_test.go 
b/app/horus/core/alert/slack_test.go
index 833bd442..d254a043 100644
--- a/app/horus/core/alert/slack_test.go
+++ b/app/horus/core/alert/slack_test.go
@@ -13,10 +13,11 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package alert
+package alert_test
 
 import (
        "github.com/apache/dubbo-kubernetes/app/horus/basic/config"
+       "github.com/apache/dubbo-kubernetes/app/horus/core/alert"
        "testing"
 )
 
@@ -24,5 +25,5 @@ func TestSlackSend(t *testing.T) {
        im := &config.SlackConfiguration{
                WebhookUrl: 
"https://oapi.dingtalk.com/robot/send?access_token=aa2f3f74d7a2504653ca89b7a673707ba1d04b6d9d320c3572e5464d2f81471x";,
        }
-       SlackSend(im, "接入成功")
+       alert.SlackSend(im, "test")
 }

Reply via email to