feat(parser/log): new parser
This commit is contained in:
		
							parent
							
								
									1bbc589897
								
							
						
					
					
						commit
						290b614159
					
				|  | @ -9,6 +9,7 @@ import ( | |||
| 	_ "github.com/joint-online-judge/JOJ3/internal/parser/dummy" | ||||
| 	_ "github.com/joint-online-judge/JOJ3/internal/parser/healthcheck" | ||||
| 	_ "github.com/joint-online-judge/JOJ3/internal/parser/keyword" | ||||
| 	_ "github.com/joint-online-judge/JOJ3/internal/parser/log" | ||||
| 	_ "github.com/joint-online-judge/JOJ3/internal/parser/resultdetail" | ||||
| 	_ "github.com/joint-online-judge/JOJ3/internal/parser/resultstatus" | ||||
| 	_ "github.com/joint-online-judge/JOJ3/internal/parser/sample" | ||||
|  |  | |||
							
								
								
									
										9
									
								
								internal/parser/log/meta.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								internal/parser/log/meta.go
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,9 @@ | |||
| package log | ||||
| 
 | ||||
| import "github.com/joint-online-judge/JOJ3/internal/stage" | ||||
| 
 | ||||
| var name = "log" | ||||
| 
 | ||||
| func init() { | ||||
| 	stage.RegisterParser(name, &Log{}) | ||||
| } | ||||
							
								
								
									
										52
									
								
								internal/parser/log/parser.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								internal/parser/log/parser.go
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,52 @@ | |||
| package log | ||||
| 
 | ||||
| import ( | ||||
| 	"encoding/json" | ||||
| 	"fmt" | ||||
| 	"log/slog" | ||||
| 
 | ||||
| 	"github.com/joint-online-judge/JOJ3/internal/stage" | ||||
| ) | ||||
| 
 | ||||
| type Conf struct { | ||||
| 	FileName string `default:"stdout"` | ||||
| 	Msg      string `default:"log msg"` | ||||
| } | ||||
| 
 | ||||
| type Log struct{} | ||||
| 
 | ||||
| func Parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult { | ||||
| 	content := executorResult.Files[conf.FileName] | ||||
| 	var data map[string]any | ||||
| 	err := json.Unmarshal([]byte(content), &data) | ||||
| 	if err != nil { | ||||
| 		slog.Error(conf.Msg, "error", err) | ||||
| 		return stage.ParserResult{ | ||||
| 			Score:   0, | ||||
| 			Comment: fmt.Sprintf("Failed to parse content: %s", err), | ||||
| 		} | ||||
| 	} | ||||
| 	args := make([]any, 0, len(data)*2) | ||||
| 	for key, value := range data { | ||||
| 		args = append(args, key, value) | ||||
| 	} | ||||
| 	slog.Info(conf.Msg, args...) | ||||
| 	return stage.ParserResult{ | ||||
| 		Score:   0, | ||||
| 		Comment: "", | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func (*Log) Run(results []stage.ExecutorResult, confAny any) ( | ||||
| 	[]stage.ParserResult, bool, error, | ||||
| ) { | ||||
| 	conf, err := stage.DecodeConf[Conf](confAny) | ||||
| 	if err != nil { | ||||
| 		return nil, true, err | ||||
| 	} | ||||
| 	var res []stage.ParserResult | ||||
| 	for _, result := range results { | ||||
| 		res = append(res, Parse(result, *conf)) | ||||
| 	} | ||||
| 	return res, false, nil | ||||
| } | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user