feat(healthcheck): remove joj3-check
This commit is contained in:
		
							parent
							
								
									1bc539a80f
								
							
						
					
					
						commit
						1bbc589897
					
				|  | @ -9,7 +9,6 @@ import ( | ||||||
| 	"strings" | 	"strings" | ||||||
| 
 | 
 | ||||||
| 	"github.com/joint-online-judge/JOJ3/cmd/joj3/env" | 	"github.com/joint-online-judge/JOJ3/cmd/joj3/env" | ||||||
| 	"github.com/joint-online-judge/JOJ3/internal/conf" |  | ||||||
| 	"github.com/joint-online-judge/JOJ3/pkg/healthcheck" | 	"github.com/joint-online-judge/JOJ3/pkg/healthcheck" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
|  | @ -57,19 +56,7 @@ func init() { | ||||||
| 	flag.StringVar(&checkFileNameList, "checkFileNameList", "", "comma-separated list of files to check") | 	flag.StringVar(&checkFileNameList, "checkFileNameList", "", "comma-separated list of files to check") | ||||||
| 	flag.StringVar(&checkFileSumList, "checkFileSumList", "", "comma-separated list of expected checksums") | 	flag.StringVar(&checkFileSumList, "checkFileSumList", "", "comma-separated list of expected checksums") | ||||||
| 	parseMultiValueFlag(&metaFile, "meta", "meta files to check") | 	parseMultiValueFlag(&metaFile, "meta", "meta files to check") | ||||||
| 	flag.StringVar(&confPath, "confPath", "", "path to conf file for teapot check") | 	flag.StringVar(&confPath, "confPath", "", "path to conf file for teapot check") // TODO: remove me
 | ||||||
| } |  | ||||||
| 
 |  | ||||||
| func prepareTeapotCheck() ( |  | ||||||
| 	confObj *conf.Conf, groups []string, err error, |  | ||||||
| ) { |  | ||||||
| 	confObj, err = conf.ParseConfFile(confPath) |  | ||||||
| 	if err != nil { |  | ||||||
| 		slog.Error("parse conf", "error", err) |  | ||||||
| 		return |  | ||||||
| 	} |  | ||||||
| 	groups = strings.Split(os.Getenv(env.Groups), ",") |  | ||||||
| 	return |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func main() { | func main() { | ||||||
|  | @ -86,14 +73,9 @@ func main() { | ||||||
| 		"checkFileSumList", checkFileSumList, | 		"checkFileSumList", checkFileSumList, | ||||||
| 		"meta", metaFile, | 		"meta", metaFile, | ||||||
| 	) | 	) | ||||||
| 	var err error | 	groups := strings.Split(os.Getenv(env.Groups), ",") | ||||||
| 	confObj, groups, err := prepareTeapotCheck() |  | ||||||
| 	if err != nil { |  | ||||||
| 		slog.Error("prepare teapot check", "error", err) |  | ||||||
| 		confObj = nil |  | ||||||
| 	} |  | ||||||
| 	res := healthcheck.All( | 	res := healthcheck.All( | ||||||
| 		confObj, rootDir, checkFileNameList, checkFileSumList, | 		rootDir, checkFileNameList, checkFileSumList, | ||||||
| 		groups, metaFile, repoSize, | 		groups, metaFile, repoSize, | ||||||
| 	) | 	) | ||||||
| 	jsonRes, err := json.Marshal(res) | 	jsonRes, err := json.Marshal(res) | ||||||
|  |  | ||||||
|  | @ -2,8 +2,6 @@ package healthcheck | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 
 |  | ||||||
| 	"github.com/joint-online-judge/JOJ3/internal/conf" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| type Result struct { | type Result struct { | ||||||
|  | @ -12,21 +10,11 @@ type Result struct { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func All( | func All( | ||||||
| 	confObj *conf.Conf, |  | ||||||
| 	rootDir, checkFileNameList, checkFileSumList string, | 	rootDir, checkFileNameList, checkFileSumList string, | ||||||
| 	groups, metaFile []string, | 	groups, metaFile []string, | ||||||
| 	repoSize float64, | 	repoSize float64, | ||||||
| ) (res Result) { | ) (res Result) { | ||||||
| 	var err error | 	var err error | ||||||
| 	if confObj != nil { |  | ||||||
| 		output, err := TeapotCheck(confObj, groups) |  | ||||||
| 		if err != nil { |  | ||||||
| 			res.Msg += fmt.Sprintf("### Teapot Check Failed:\n%s\n", output) |  | ||||||
| 			res.Failed = true |  | ||||||
| 		} else { |  | ||||||
| 			res.Msg += fmt.Sprintf("### Teapot Check Result:\n%s\n", output) |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| 	err = RepoSize(repoSize) | 	err = RepoSize(repoSize) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		res.Msg += fmt.Sprintf("### Repo Size Check Failed:\n%s\n", err.Error()) | 		res.Msg += fmt.Sprintf("### Repo Size Check Failed:\n%s\n", err.Error()) | ||||||
|  |  | ||||||
|  | @ -1,107 +0,0 @@ | ||||||
| package healthcheck |  | ||||||
| 
 |  | ||||||
| import ( |  | ||||||
| 	"bytes" |  | ||||||
| 	"encoding/json" |  | ||||||
| 	"fmt" |  | ||||||
| 	"log/slog" |  | ||||||
| 	"os" |  | ||||||
| 	"os/exec" |  | ||||||
| 	"strings" |  | ||||||
| 
 |  | ||||||
| 	"github.com/joint-online-judge/JOJ3/internal/conf" |  | ||||||
| ) |  | ||||||
| 
 |  | ||||||
| type CheckResult struct { |  | ||||||
| 	Name        string `json:"name"` |  | ||||||
| 	SubmitCount int    `json:"submit_count"` |  | ||||||
| 	MaxCount    int    `json:"max_count"` |  | ||||||
| 	TimePeriod  int    `json:"time_period"` |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| func runTeapot(conf *conf.Conf) (checkResults []CheckResult, err error) { |  | ||||||
| 	os.Setenv("LOG_FILE_PATH", conf.Teapot.LogPath) |  | ||||||
| 	var formattedGroups []string |  | ||||||
| 	for _, group := range conf.Teapot.Groups { |  | ||||||
| 		groupConfig := fmt.Sprintf("%s=%d:%d", |  | ||||||
| 			group.Name, group.MaxCount, group.TimePeriodHour) |  | ||||||
| 		formattedGroups = append(formattedGroups, groupConfig) |  | ||||||
| 	} |  | ||||||
| 	args := []string{ |  | ||||||
| 		"joj3-check", conf.Teapot.EnvFilePath, |  | ||||||
| 		conf.Teapot.GradingRepoName, |  | ||||||
| 		conf.Teapot.ScoreboardPath, |  | ||||||
| 		"--group-config", strings.Join(formattedGroups, ","), |  | ||||||
| 	} |  | ||||||
| 	var stdoutBuf, stderrBuf bytes.Buffer |  | ||||||
| 	cmd := exec.Command("joint-teapot", args...) // #nosec G204
 |  | ||||||
| 	cmd.Stdout = &stdoutBuf |  | ||||||
| 	cmd.Stderr = &stderrBuf |  | ||||||
| 	err = cmd.Run() |  | ||||||
| 	if err != nil { |  | ||||||
| 		slog.Error("teapot check args", "args", args) |  | ||||||
| 		slog.Error("teapot check exec", |  | ||||||
| 			"stdout", stdoutBuf.String(), "stderr", stderrBuf.String()) |  | ||||||
| 		slog.Error("teapot check exec", "error", err) |  | ||||||
| 		return |  | ||||||
| 	} |  | ||||||
| 	if err = json.Unmarshal(stdoutBuf.Bytes(), &checkResults); err != nil { |  | ||||||
| 		slog.Error("unmarshal teapot check result", "error", err, |  | ||||||
| 			"stdout", stdoutBuf.String()) |  | ||||||
| 		return |  | ||||||
| 	} |  | ||||||
| 	slog.Info("teapot check result", "result", checkResults) |  | ||||||
| 	return |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| func generateOutput( |  | ||||||
| 	checkResults []CheckResult, |  | ||||||
| 	groups []string, |  | ||||||
| ) (comment string, err error) { |  | ||||||
| 	if len(checkResults) == 0 { |  | ||||||
| 		return |  | ||||||
| 	} |  | ||||||
| 	for _, checkResult := range checkResults { |  | ||||||
| 		useGroup := false |  | ||||||
| 		if checkResult.Name != "" { |  | ||||||
| 			comment += fmt.Sprintf("keyword `%s` ", checkResult.Name) |  | ||||||
| 		} else { |  | ||||||
| 			useGroup = true |  | ||||||
| 		} |  | ||||||
| 		for _, group := range groups { |  | ||||||
| 			if strings.EqualFold(group, checkResult.Name) { |  | ||||||
| 				useGroup = true |  | ||||||
| 				break |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 		comment += fmt.Sprintf( |  | ||||||
| 			"in last %d hour(s): submit count %d, max count %d", |  | ||||||
| 			checkResult.TimePeriod, |  | ||||||
| 			checkResult.SubmitCount, |  | ||||||
| 			checkResult.MaxCount, |  | ||||||
| 		) |  | ||||||
| 		if useGroup && checkResult.SubmitCount+1 > checkResult.MaxCount { |  | ||||||
| 			err = fmt.Errorf( |  | ||||||
| 				"keyword `%s` submit count exceeded", |  | ||||||
| 				checkResult.Name, |  | ||||||
| 			) |  | ||||||
| 			comment += ", exceeded" |  | ||||||
| 		} |  | ||||||
| 		comment += "\n" |  | ||||||
| 	} |  | ||||||
| 	return |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| func TeapotCheck(conf *conf.Conf, groups []string) (output string, err error) { |  | ||||||
| 	checkResults, err := runTeapot(conf) |  | ||||||
| 	if err != nil { |  | ||||||
| 		slog.Error("teapot check", "error", err) |  | ||||||
| 		return |  | ||||||
| 	} |  | ||||||
| 	output, err = generateOutput(checkResults, groups) |  | ||||||
| 	if err != nil { |  | ||||||
| 		slog.Error("generate output", "error", err) |  | ||||||
| 		return |  | ||||||
| 	} |  | ||||||
| 	return |  | ||||||
| } |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user