From 4b357a57599ed623abbe094838d22895dc22fb5a Mon Sep 17 00:00:00 2001
From: gaoruoshu <gaoruoshu@huawei.com>
Date: Wed, 9 Aug 2023 21:41:10 +0800
Subject: [PATCH 3/3] bugfix: collection res can only save file to specified
 dir

---
 Makefile                                     |  3 +++
 api/profile/profile.pb.go                    |  6 ++---
 api/profile/profile.proto                    |  2 +-
 common/config/config.go                      |  1 +
 modules/client/profile/profile_collection.go | 24 ++++++--------------
 modules/server/profile/profile.go            | 11 +++++----
 6 files changed, 21 insertions(+), 26 deletions(-)

diff --git a/Makefile b/Makefile
index 5d9d317..bcac447 100755
--- a/Makefile
+++ b/Makefile
@@ -53,6 +53,7 @@ cleanall: clean
 	rm -rf $(DESTDIR)/var/lib/atuned/
 	rm -rf $(DESTDIR)/var/run/atuned/
 	rm -rf $(DESTDIR)/var/atuned/
+	rm -rf $(DESTDIR)/var/atune_data/
 
 db:
 	sqlite3 database/atuned.db ".read database/init.sql"
@@ -78,6 +79,7 @@ libinstall:
 	rm -rf $(DESTDIR)/var/lib/atuned/
 	rm -rf $(DESTDIR)/var/run/atuned/
 	rm -rf $(DESTDIR)/var/atuned/
+	rm -rf $(DESTDIR)/var/atune_data/
 	mkdir -p $(DESTDIR)/etc/atuned/tuning
 	mkdir -p $(DESTDIR)/etc/atuned/rules
 	mkdir -p $(DESTDIR)/etc/atuned/training
@@ -90,6 +92,7 @@ libinstall:
 	mkdir -p $(DESTDIR)/var/lib/atuned
 	mkdir -p $(DESTDIR)/var/run/atuned
 	mkdir -p $(DESTDIR)/var/atuned
+	mkdir -p $(DESTDIR)/var/atune_data/collection
 	mkdir -p $(DESTDIR)$(PREFIX)/share/bash-completion/completions
 	install -m 640 pkg/daemon_profile_server.so $(DESTDIR)$(PREFIX)/lib/atuned/modules
 	install -m 750 pkg/atune-adm $(BINDIR)
diff --git a/api/profile/profile.pb.go b/api/profile/profile.pb.go
index dba166d..4e70c41 100644
--- a/api/profile/profile.pb.go
+++ b/api/profile/profile.pb.go
@@ -408,7 +408,7 @@ type CollectFlag struct {
 	Interval             int64    `protobuf:"varint,1,opt,name=Interval,proto3" json:"Interval,omitempty"`
 	Duration             int64    `protobuf:"varint,2,opt,name=Duration,proto3" json:"Duration,omitempty"`
 	Workload             string   `protobuf:"bytes,3,opt,name=Workload,proto3" json:"Workload,omitempty"`
-	OutputPath           string   `protobuf:"bytes,4,opt,name=OutputPath,proto3" json:"OutputPath,omitempty"`
+	OutputDir            string   `protobuf:"bytes,4,opt,name=OutputDir,proto3" json:"OutputDir,omitempty"`
 	Block                string   `protobuf:"bytes,5,opt,name=Block,proto3" json:"Block,omitempty"`
 	Network              string   `protobuf:"bytes,6,opt,name=Network,proto3" json:"Network,omitempty"`
 	Type                 string   `protobuf:"bytes,7,opt,name=Type,proto3" json:"Type,omitempty"`
@@ -463,9 +463,9 @@ func (m *CollectFlag) GetWorkload() string {
 	return ""
 }
 
-func (m *CollectFlag) GetOutputPath() string {
+func (m *CollectFlag) GetOutputDir() string {
 	if m != nil {
-		return m.OutputPath
+		return m.OutputDir
 	}
 	return ""
 }
diff --git a/api/profile/profile.proto b/api/profile/profile.proto
index 2a6e751..71d8ceb 100755
--- a/api/profile/profile.proto
+++ b/api/profile/profile.proto
@@ -76,7 +76,7 @@ message CollectFlag {
     int64 Interval = 1;
     int64 Duration = 2;
     string Workload = 3;
-    string OutputPath = 4;
+    string OutputDir = 4;
     string Block = 5;
     string Network = 6;
     string Type = 7;
diff --git a/common/config/config.go b/common/config/config.go
index 4bd70a6..b9d2b66 100644
--- a/common/config/config.go
+++ b/common/config/config.go
@@ -55,6 +55,7 @@ const (
 	DefaultCheckerPath      = "/usr/share/atuned/checker/"
 	DefaultBackupPath       = "/usr/share/atuned/backup/"
 	DefaultTuningLogPath    = "/var/atuned"
+	DefaultCollectionPath	= "/var/atune_data/collection/"
 )
 
 // log config
diff --git a/modules/client/profile/profile_collection.go b/modules/client/profile/profile_collection.go
index 26b8d65..2873dac 100644
--- a/modules/client/profile/profile_collection.go
+++ b/modules/client/profile/profile_collection.go
@@ -20,7 +20,6 @@ import (
 	"gitee.com/openeuler/A-Tune/common/utils"
 	"fmt"
 	"io"
-	"path/filepath"
 
 	"github.com/urfave/cli"
 	CTX "golang.org/x/net/context"
@@ -48,8 +47,8 @@ var collectionCommand = cli.Command{
 			Value: 1200,
 		},
 		cli.StringFlag{
-			Name:  "output_path,o",
-			Usage: "the output path of the collecting data",
+			Name:  "output_dir,o",
+			Usage: "the output dir name of the collecting data, full dir path would be /var/atune_data/collection/{output_dir}",
 			Value: "",
 		},
 		cli.StringFlag{
@@ -71,8 +70,8 @@ var collectionCommand = cli.Command{
 	Description: func() string {
 		desc := `
 	 collect data for train machine learning model, you must set the command options
-	 which has no default value, the output_path must be a absolute path.
-	     example: atune-adm collection -f mysql -i 5 -d 1200 -o /home -b sda -n eth0 -t mysql`
+	 which has no default value.
+	     example: atune-adm collection -f mysql -i 5 -d 1200 -o tmp -b sda -n eth0 -t mysql`
 		return desc
 	}(),
 	Action: collection,
@@ -118,9 +117,9 @@ func checkCollectionCtx(ctx *cli.Context) error {
 		return fmt.Errorf("error: app type must be specified")
 	}
 
-	if ctx.String("output_path") == "" {
+	if ctx.String("output_dir") == "" {
 		_ = cli.ShowCommandHelp(ctx, "collection")
-		return fmt.Errorf("error: output_path must be specified")
+		return fmt.Errorf("error: output_dir must be specified")
 	}
 
 	if ctx.Int64("interval") < 1 || ctx.Int64("interval") > 60 {
@@ -131,10 +130,6 @@ func checkCollectionCtx(ctx *cli.Context) error {
 		return fmt.Errorf("error: collection duration value must be bigger than interval*10")
 	}
 
-	if !filepath.IsAbs(ctx.String("output_path")) {
-		return fmt.Errorf("error: output path must be absolute path")
-	}
-
 	return nil
 }
 
@@ -149,16 +144,11 @@ func collection(ctx *cli.Context) error {
 	}
 	defer c.Close()
 
-	outputPath := ctx.String("output_path")
-	outputPath, err = filepath.Abs(outputPath)
-	if err != nil {
-		return err
-	}
 	message := PB.CollectFlag{
 		Interval:   ctx.Int64("interval"),
 		Duration:   ctx.Int64("duration"),
 		Workload:   ctx.String("filename"),
-		OutputPath: outputPath,
+		OutputDir:  ctx.String("output_dir"),
 		Block:      ctx.String("disk"),
 		Network:    ctx.String("network"),
 		Type:       ctx.String("app_type"),
diff --git a/modules/server/profile/profile.go b/modules/server/profile/profile.go
index 3264167..ec1e739 100644
--- a/modules/server/profile/profile.go
+++ b/modules/server/profile/profile.go
@@ -1019,8 +1019,8 @@ func (s *ProfileServer) Collection(message *PB.CollectFlag, stream PB.ProfileMgr
 		return fmt.Errorf("input:%s is invalid", message.GetWorkload())
 	}
 
-	if valid := utils.IsInputStringValid(message.GetOutputPath()); !valid {
-		return fmt.Errorf("input:%s is invalid", message.GetOutputPath())
+	if valid, _ := regexp.MatchString("^[a-zA-Z]+(_[a-zA-Z0-9]+)*$", message.GetOutputDir()); !valid {
+		return fmt.Errorf("output dir:%s is invalid", message.GetOutputDir())
 	}
 
 	if valid := utils.IsInputStringValid(message.GetType()); !valid {
@@ -1049,12 +1049,13 @@ func (s *ProfileServer) Collection(message *PB.CollectFlag, stream PB.ProfileMgr
 		return err
 	}
 
-	exist, err := utils.PathExist(message.GetOutputPath())
+	outputDir := path.Join(config.DefaultCollectionPath, message.GetOutputDir())
+	exist, err := utils.PathExist(message.GetOutputDir())
 	if err != nil {
 		return err
 	}
 	if !exist {
-		return fmt.Errorf("output_path %s is not exist", message.GetOutputPath())
+		_ = os.MkdirAll(outputDir, 0755)
 	}
 
 	if err = utils.InterfaceByName(message.GetNetwork()); err != nil {
@@ -1106,7 +1107,7 @@ func (s *ProfileServer) Collection(message *PB.CollectFlag, stream PB.ProfileMgr
 	collectorBody.Monitors = monitors
 	nowTime := time.Now().Format("20060702-150405")
 	fileName := fmt.Sprintf("%s-%s.csv", message.GetWorkload(), nowTime)
-	collectorBody.File = path.Join(message.GetOutputPath(), fileName)
+	collectorBody.File = path.Join(outputDir, fileName)
 	if include == "" {
 		include = "default"
 	}
-- 
2.27.0

