Skip to content

Commit

Permalink
refactor: refactor the classroom service
Browse files Browse the repository at this point in the history
  • Loading branch information
SchwarzSail committed Sep 29, 2024
1 parent 7cf5c99 commit 9d3fafa
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 276 deletions.
52 changes: 2 additions & 50 deletions cmd/classroom/README.md
Original file line number Diff line number Diff line change
@@ -1,59 +1,11 @@
# 空教室模块

**此模块用于研究生获取空教室信息** create by 李梓玄 2023.10.17
**此模块用于研究生获取空教室信息** create by 李梓玄 2023.10.17 update by 朱胤帆 2024.9.29

此模块基于kitex框架(微服务框架):https://github.com/cloudwego/kitex

此模块基于jwch库(教务处的信息爬取库):https://github.com/west2-online/jwch

## 接口文档
飞书文档:

TODOOOOOOOOOOOOOOOOOOO!

## 接口的传入参数与返回值

参阅项目根目录下的`idl/empty_room.thrift`文件

如需修改接口的传入参数与返回值,参照thrift语法修改`empty_room.thrift`后,在`cmd/empty_room`执行

```shell
# 更多信息参照 cmd/classroom/Makefile
make gen
```

其中请求中的参数`building`需要传入一个教学楼Map的key,代码如下

```go
var (
buildingMap = map[string]string{
"x3": "公共教学楼西3",
"x2": "公共教学楼西2",
"x1": "公共教学楼西1",
"zl": "公共教学楼中楼",
"d1": "公共教学楼东1",
"d2": "公共教学楼东2",
"d3": "公共教学楼东3",
"wkl": "公共教学楼文科楼",
"wk": "公共教学楼文科楼",
}
)

```

该Map定义于jwch库中: https://github.com/west2-online/jwch

## 接口逻辑

**因为研究生的教学管理系统没有获取空教室功能,所以本接口基于一个默认的本科生账号密码去本科教学管理系统获取**

此模块暂时只能获取旗山校区的空教室信息

### `GetEmptyRoom`

1. 验证token handler
2. 当未传入账号密码时,使用默认账号密码 handler
3. 根据request信息拼接key,从`redis`中寻找缓存
4. 如果命中缓存,则返回空教室信息
5. 如果未命中缓存,调用`jwch`库进行登录、获取空教室信息
6. 返回空教室信息,同时开启一个Goroutine 将空教室信息写入

23 changes: 21 additions & 2 deletions cmd/classroom/dal/cache/empty_room.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,32 @@ package cache
import (
"context"
"encoding/json"
"fmt"
"github.com/west2-online/fzuhelper-server/pkg/constants"
"github.com/west2-online/fzuhelper-server/pkg/logger"
"strings"
)

// SetEmptyRoomCache 设置空教室缓存 key 为 date + campus + startTime + endTime,value直接采用bytes存储
func SetEmptyRoomCache(ctx context.Context, key string, emptyRoomList []string) {
// SetXiaMenEmptyRoomCache 设置厦门工艺美院的空教室缓存
// 因为前端给的数据只有鼓浪屿校区和集美校区的数据,所以这里需要单独对这两份数据进行处理
func SetXiaMenEmptyRoomCache(ctx context.Context, date, start, end string, emptyRoomList []string) {
//分别整理两个校区的结果
guLangYuEmptyRooms := make([]string, 0)
jiMeiEmptyRooms := make([]string, 0)
for _, room := range emptyRoomList {
if strings.Contains(room, "鼓浪屿") {
guLangYuEmptyRooms = append(guLangYuEmptyRooms, room)
} else if strings.Contains(room, "集美") {
jiMeiEmptyRooms = append(jiMeiEmptyRooms, room)
}
}
guLangYuKey := fmt.Sprintf("%s.%s.%s.%s", date, "鼓浪屿校区", start, end)
jiMeiKey := fmt.Sprintf("%s.%s.%s.%s", date, "集美校区", start, end)
SetEmptyRoomCache(ctx, guLangYuKey, guLangYuEmptyRooms)
SetEmptyRoomCache(ctx, jiMeiKey, jiMeiEmptyRooms)
}

func SetEmptyRoomCache(ctx context.Context, key string, emptyRoomList []string) {
emptyRoomJson, err := json.Marshal(emptyRoomList)
// 两天过期
err = RedisClient.Set(ctx, key, emptyRoomJson, constants.ClassroomKeyExpire).Err()
Expand Down
65 changes: 65 additions & 0 deletions cmd/classroom/dal/cache/schedule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cache

import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/west2-online/fzuhelper-server/config"
"github.com/west2-online/fzuhelper-server/pkg/constants"
"github.com/west2-online/fzuhelper-server/pkg/logger"
"github.com/west2-online/jwch"
"strconv"
"time"
)

// ScheduledGetClassrooms 定期获取空教室信息
func ScheduledGetClassrooms() error {
ctx := context.Background()
//定义jwch的stu客户端
stu := jwch.NewStudent().WithUser(config.DefaultUser.Account, config.DefaultUser.Password)
//登录,id和cookies会自动保存在client中
stu.Login()

var dates []string
currentTime := time.Now()
//设定一周时间
for i := 0; i < 7; i++ {
date := currentTime.AddDate(0, 0, i).Format("2006-01-02")
dates = append(dates, date)
}
//构建jwch的请求参数
for _, date := range dates {
for _, campus := range constants.CampusArray {
for startTime := 1; startTime <= 11; startTime++ {
for endTime := startTime; endTime <= 11; endTime++ {
args := jwch.EmptyRoomReq{
Campus: campus,
Time: date,
Start: strconv.Itoa(startTime),
End: strconv.Itoa(endTime),
}
var res []string
var err error
if campus == "旗山校区" {
res, err = stu.GetQiShanEmptyRoom(args)
} else {
res, err = stu.GetEmptyRoom(args)
}
if err != nil {
logger.LoggerObj.Errorf("classroom.cache.GetClassrooms: %v", err)
return errors.Wrap(err, "classroom.cache.GetClassrooms failed")
}
if campus == "厦门工艺美院" {
go SetXiaMenEmptyRoomCache(ctx, date, args.Start, args.End, res)
} else {
key := fmt.Sprintf("%s.%s.%s.%s", args.Time, args.Campus, args.Start, args.End)
go SetEmptyRoomCache(ctx, key, res)
}
logger.LoggerObj.Debugf("classroom.cache.GetClassrooms add task %v", args)
}
}
logger.LoggerObj.Infof("classroom.cache.CGetClassrooms add all tasks of campus %v in the day %v", campus, date)
}
}
return nil
}
4 changes: 2 additions & 2 deletions cmd/classroom/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type ClassroomServiceImpl struct{}
// GetEmptyRoom implements the ClassroomServiceImpl interface.
func (s *ClassroomServiceImpl) GetEmptyRoom(ctx context.Context, req *classroom.EmptyRoomRequest) (resp *classroom.EmptyRoomResponse, err error) {
resp = classroom.NewEmptyRoomResponse()
l := service.NewClassroomServiceInDefault(ctx)
res, err := l.GetEmptyRooms(req)
l := service.NewClassroomService(ctx)
res, err := l.GetEmptyRoom(req)
if err != nil {
resp.Base = pack.BuildBaseResp(err)
return resp, nil
Expand Down
9 changes: 8 additions & 1 deletion cmd/classroom/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/west2-online/fzuhelper-server/pkg/logger"
"github.com/west2-online/fzuhelper-server/pkg/utils"
"net"
"time"
)

var (
Expand Down Expand Up @@ -73,7 +74,13 @@ func main() {
}),
)
//提前缓存空教室数据
go CacheEmptyRooms()
go func() {
for {
WorkQueue.Add("signal")
time.Sleep(constants.ScheduledTime)
}
}()

if err = svr.Run(); err != nil {
panic(err)
}
Expand Down
96 changes: 0 additions & 96 deletions cmd/classroom/schedule.go

This file was deleted.

Loading

0 comments on commit 9d3fafa

Please sign in to comment.