-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from OJ-lab/zztrans/main
perf README.md & Support problem package import
- Loading branch information
Showing
11 changed files
with
163 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io/fs" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
gormAgent "github.com/OJ-lab/oj-lab-services/src/core/agent/gorm" | ||
minioAgent "github.com/OJ-lab/oj-lab-services/src/core/agent/minio" | ||
"github.com/OJ-lab/oj-lab-services/src/service/mapper" | ||
"github.com/OJ-lab/oj-lab-services/src/service/model" | ||
yaml "gopkg.in/yaml.v2" | ||
|
||
"github.com/minio/minio-go/v7" | ||
) | ||
|
||
var ctx = context.Background() | ||
|
||
func main() { | ||
db := gormAgent.GetDefaultDB() | ||
minioClient := minioAgent.GetMinioClient() | ||
|
||
log.Printf("%#v\n", minioClient) // minioClient is now set up | ||
bucketName := minioAgent.GetBucketName() | ||
|
||
err := minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{}) | ||
if err != nil { | ||
// Check to see if we already own this bucket (which happens if you run this twice) | ||
exists, errBucketExists := minioClient.BucketExists(ctx, bucketName) | ||
if errBucketExists == nil && exists { | ||
log.Printf("We already own %s\n", bucketName) | ||
} else { | ||
log.Fatalln(err) | ||
} | ||
} else { | ||
log.Printf("Successfully created %s\n", bucketName) | ||
} | ||
|
||
// Read package files | ||
// Search Problem under packagePath | ||
// 1. parse problem path as `slug`, | ||
// parse problem.yaml's name as `title`, | ||
// parse problem.md as description. | ||
// 2. insert object into minio storage. | ||
packagePath := "tests/data/packages/icpc" | ||
title := "" | ||
slug := "" | ||
filepath.Walk(packagePath, func(path string, info fs.FileInfo, err error) error { | ||
if info == nil { | ||
return fmt.Errorf("file info is nil") | ||
} | ||
if info.IsDir() { | ||
return nil | ||
} | ||
relativePath := strings.Replace(path, packagePath, "", 1) | ||
println("relativePath: ", relativePath) | ||
if filepath.Base(relativePath) == "problem.yaml" { | ||
resultMap := make(map[string]interface{}) | ||
yamlFile, err := os.ReadFile(path) | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
err = yaml.Unmarshal(yamlFile, &resultMap) | ||
if err != nil { | ||
log.Printf("Unmarshal: %v\n", err) | ||
} | ||
title = resultMap["name"].(string) | ||
if title == "" { | ||
log.Fatal("name key not exist in problem.yaml") | ||
} | ||
slug = strings.Split(relativePath, "/")[1] | ||
log.Println("title: ", title) | ||
log.Println("slug: ", slug) | ||
} | ||
if filepath.Base(relativePath) == "problem.md" { | ||
content, err := os.ReadFile(path) | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
description := string(content) | ||
println("description: ", description) | ||
mapper.CreateProblem(db, model.Problem{ | ||
Slug: slug, | ||
Title: title, | ||
Description: &description, | ||
Tags: []*model.AlgorithmTag{ | ||
{Name: "to-be-add"}, | ||
}, | ||
}) | ||
} | ||
|
||
_, minioErr := minioClient.FPutObject(ctx, bucketName, | ||
relativePath, | ||
path, | ||
minio.PutObjectOptions{}) | ||
if minioErr != nil { | ||
log.Fatalln(minioErr) | ||
} | ||
return err | ||
}) | ||
|
||
log.Println("Read Problem Success!") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,9 @@ | ||
package core_test | ||
|
||
import ( | ||
"fmt" | ||
"io/fs" | ||
"log" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
minioAgent "github.com/OJ-lab/oj-lab-services/src/core/agent/minio" | ||
"github.com/minio/minio-go/v7" | ||
) | ||
|
||
func TestMinio(T *testing.T) { | ||
// Initialize minio client object. | ||
minioClient := minioAgent.GetMinioClient() | ||
|
||
log.Printf("%#v\n", minioClient) // minioClient is now set up | ||
bucketName := minioAgent.GetBucketName() | ||
|
||
err := minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{}) | ||
if err != nil { | ||
// Check to see if we already own this bucket (which happens if you run this twice) | ||
exists, errBucketExists := minioClient.BucketExists(ctx, bucketName) | ||
if errBucketExists == nil && exists { | ||
log.Printf("We already own %s\n", bucketName) | ||
} else { | ||
log.Fatalln(err) | ||
} | ||
} else { | ||
log.Printf("Successfully created %s\n", bucketName) | ||
} | ||
|
||
// Upload package files | ||
packagePath := "../data/packages/icpc/hello-world" | ||
filepath.Walk(packagePath, func(path string, info fs.FileInfo, err error) error { | ||
if info == nil { | ||
return fmt.Errorf("file info is nil") | ||
} | ||
if info.IsDir() { | ||
return nil | ||
} | ||
relativePath := filepath.Join(filepath.Base(packagePath), strings.Replace(path, packagePath, "", 1)) | ||
println(relativePath) | ||
_, minioErr := minioClient.FPutObject(ctx, bucketName, | ||
relativePath, | ||
path, | ||
minio.PutObjectOptions{}) | ||
if minioErr != nil { | ||
log.Fatalln(minioErr) | ||
} | ||
return minioErr | ||
}) | ||
} |
1 change: 1 addition & 0 deletions
1
tests/data/packages/icpc/hello-world/problem_statement/problem.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Write a program that prints "Hello World!". |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters