Skip to content

Commit

Permalink
+ [feat] add statistics api
Browse files Browse the repository at this point in the history
  • Loading branch information
ysicing committed Sep 18, 2024
1 parent 4c18f38 commit e3a7baf
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 1 deletion.
8 changes: 7 additions & 1 deletion cmd/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/easysoft/qcadmin/cmd/flags"
"github.com/easysoft/qcadmin/cmd/precheck"
"github.com/easysoft/qcadmin/internal/api/statistics"
"github.com/easysoft/qcadmin/internal/pkg/util/factory"
"github.com/easysoft/qcadmin/pkg/cluster"

Expand Down Expand Up @@ -57,7 +58,11 @@ func InitCommand(f factory.Factory) *cobra.Command {
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return myCluster.InitNode()
if err := myCluster.InitNode(); err != nil {
return err
}
// statistics.SendStatistics("install")
return nil
},
}
init.Flags().AddFlagSet(flags.ConvertFlags(init, myCluster.GetInitFlags()))
Expand Down Expand Up @@ -122,6 +127,7 @@ func CleanCommand(f factory.Factory) *cobra.Command {
return err
}
log.Donef("uninstall cluster success")
statistics.SendStatistics("uninstall")
return nil
}
log.Donef("cancel clean cluster")
Expand Down
2 changes: 2 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/easysoft/qcadmin/cmd/flags"
"github.com/easysoft/qcadmin/cmd/precheck"
"github.com/easysoft/qcadmin/common"
"github.com/easysoft/qcadmin/internal/api/statistics"
"github.com/easysoft/qcadmin/internal/app/config"
"github.com/easysoft/qcadmin/internal/pkg/k8s"
"github.com/easysoft/qcadmin/internal/pkg/types"
Expand Down Expand Up @@ -128,6 +129,7 @@ func newCmdInit(f factory.Factory) *cobra.Command {
return
}
cp.Show()
statistics.SendStatistics("install")
}
return initCmd
}
2 changes: 2 additions & 0 deletions cmd/quickon/quickon.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/spf13/cobra"

"github.com/easysoft/qcadmin/cmd/flags"
"github.com/easysoft/qcadmin/internal/api/statistics"
"github.com/easysoft/qcadmin/internal/app/config"
"github.com/easysoft/qcadmin/internal/pkg/types"
"github.com/easysoft/qcadmin/internal/pkg/util/factory"
Expand Down Expand Up @@ -97,6 +98,7 @@ func UninstallCommand(f factory.Factory) *cobra.Command {
return err
}
log.Done("uninstall platform success")
statistics.SendStatistics("uninstall")
return nil
}
log.Donef("cancel uninstall platform")
Expand Down
2 changes: 2 additions & 0 deletions cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/spf13/cobra"

"github.com/easysoft/qcadmin/internal/api/statistics"
"github.com/easysoft/qcadmin/internal/pkg/util/factory"

qcexec "github.com/easysoft/qcadmin/internal/pkg/util/exec"
Expand All @@ -36,6 +37,7 @@ func newCmdUninstall(f factory.Factory) *cobra.Command {
log.Errorf("uninstall platform failed, reason: %v", err)
return
}
statistics.SendStatistics("uninstall")
if cleanCluster {
// TODO 检查集群是否是quickon安装的
log.Debugf("start uninstall cluster")
Expand Down
63 changes: 63 additions & 0 deletions internal/api/statistics/statistics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) 2021-2024 北京渠成软件有限公司(Beijing Qucheng Software Co., Ltd. www.qucheng.com) All rights reserved.
// Use of this source code is covered by the following dual licenses:
// (1) Z PUBLIC LICENSE 1.2 (ZPL 1.2)
// (2) Affero General Public License 3.0 (AGPL 3.0)
// license that can be found in the LICENSE file.

package statistics

import (
"fmt"

"github.com/imroc/req/v3"

"github.com/easysoft/qcadmin/common"
"github.com/easysoft/qcadmin/internal/app/config"
)

type CollectData struct {
CliVersion string `json:"cli_version"`
ClusterID string `json:"cluster_id"`
Domain string `json:"domain,omitempty"`
Type string `json:"type"`
Action string `json:"action"`
Devops bool `json:"devops,omitempty"`
}

type Result struct {
Code int `json:"code"`
Message string `json:"message"`
}

func SendStatistics(action string) error {
cfg, _ := config.LoadConfig()

data := &CollectData{
CliVersion: common.Version,
ClusterID: cfg.Cluster.ID,
Domain: cfg.Domain,
Type: cfg.Quickon.Type.String(),
Action: action,
Devops: cfg.Quickon.DevOps,
}

// send statistics
client := req.C().SetLogger(nil).SetUserAgent(common.GetUG())
var result Result
resp, err := client.R().
SetHeader("accept", "application/json").
SetHeader("cluster.id", cfg.Cluster.ID).
SetBody(data).
SetSuccessResult(&result).
Post("https://api.qucheng.com/api/qoss/collect")
if err != nil {
return fmt.Errorf("send failed, reason: %v", err)
}
if !resp.IsSuccessState() {
return fmt.Errorf("send failed, reason: bad response status %v", resp.Status)
}
if result.Code != 200 {
return fmt.Errorf("send failed, reason: %s", result.Message)
}
return nil
}
33 changes: 33 additions & 0 deletions internal/api/statistics/statistics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2021-2024 北京渠成软件有限公司(Beijing Qucheng Software Co., Ltd. www.qucheng.com) All rights reserved.
// Use of this source code is covered by the following dual licenses:
// (1) Z PUBLIC LICENSE 1.2 (ZPL 1.2)
// (2) Affero General Public License 3.0 (AGPL 3.0)
// license that can be found in the LICENSE file.

package statistics

import "testing"

func TestSendStatistics(t *testing.T) {
type args struct {
action string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "TestSendStatistics",
args: args{action: "install"},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := SendStatistics(tt.args.action); (err != nil) != tt.wantErr {
t.Errorf("SendStatistics() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

0 comments on commit e3a7baf

Please sign in to comment.