Skip to content

Commit

Permalink
feat: 增加AI数据查询功能以及工时填报时间限制(2024.11.1起实施)
Browse files Browse the repository at this point in the history
  • Loading branch information
yockii committed Oct 24, 2024
1 parent 4d3c8ed commit 762f44d
Show file tree
Hide file tree
Showing 13 changed files with 4,443 additions and 3,870 deletions.
14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ require (

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.0 // indirect
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/dlclark/regexp2 v1.10.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
Expand All @@ -37,14 +40,18 @@ require (
github.com/gofiber/template/html/v2 v2.1.1 // indirect
github.com/gofiber/utils v1.1.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/goph/emperror v0.17.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/huandu/xstrings v1.3.3 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
github.com/jackc/pgx/v5 v5.5.5 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.7 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/lestrrat/go-file-rotatelogs v0.0.0-20180223000712-d3151e2a480f // indirect
Expand All @@ -56,7 +63,12 @@ require (
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
github.com/minio/md5-simd v1.1.2 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/nikolalohinski/gonja v1.5.3 // indirect
github.com/pelletier/go-toml/v2 v2.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pkoukk/tiktoken-go v0.1.6 // indirect
Expand All @@ -76,7 +88,9 @@ require (
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.52.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
github.com/yargevad/filepathx v1.0.0 // indirect
github.com/yockii/snowflake_ext v0.1.0 // indirect
go.starlark.net v0.0.0-20230302034142-4b1e35fe2254 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect
golang.org/x/net v0.25.0 // indirect
Expand Down
265 changes: 265 additions & 0 deletions go.sum

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions internal/constant/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,8 @@ const (
ResourceWorkTimeDelete = ResourceWorkTime + ":delete"
ResourceWorkTimeStatistics = ResourceWorkTime + ":statistics"
)

// AI
const (
ResourceAI = "ai"
)
9 changes: 9 additions & 0 deletions internal/core/data/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,15 @@ func InitData() {
}
}

// AI
{
resources = append(resources, &model.Resource{
ResourceName: "AI智能查询",
ResourceCode: constant.ResourceAI,
Type: 1,
})
}

for _, resource := range resources {
//没有就添加资源
if err := database.DB.Where(resource).Attrs(&model.Resource{
Expand Down
20 changes: 11 additions & 9 deletions internal/module/ai/controller/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/tmc/langchaingo/chains"
"github.com/tmc/langchaingo/llms/openai"
"github.com/tmc/langchaingo/tools/sqldatabase"
_ "github.com/tmc/langchaingo/tools/sqldatabase/mysql"
"github.com/yockii/celestial/internal/constant"
"github.com/yockii/celestial/internal/core/middleware"
"github.com/yockii/ruomu-core/config"
"github.com/yockii/ruomu-core/server"
Expand All @@ -18,14 +20,14 @@ const (
)

func InitRouter() {
server.Get("/api/v1/aiSearch", middleware.NeedAuthorization("user"), controller.Generate)
server.Get("/api/v1/ai/dataSearch", middleware.NeedAuthorization(constant.ResourceAI), controller.Generate)
}

func init() {
client, err := openai.New(
openai.WithToken(openaiToken),
openai.WithBaseURL(openaiBaseURL),
openai.WithModel(modelName),
openai.WithToken(config.GetString("openai.openaiToken")),
openai.WithBaseURL(config.GetString("openai.openaiBaseURL")),
openai.WithModel(config.GetString("openai.modelName")),
)
if err != nil {
logrus.Fatal(err)
Expand All @@ -36,7 +38,7 @@ func init() {
config.GetString("database.readonlyPassword"),
config.GetString("database.host"),
config.GetInt("database.port"),
config.GetString("database.name"),
config.GetString("database.db"),
), nil)
if err != nil {
logrus.Fatal(err)
Expand All @@ -56,17 +58,17 @@ type aiController struct {
}

func (c *aiController) Generate(ctx *fiber.Ctx) error {
query := ctx.Query("query")
if query == "" {
question := ctx.Query("question")
if question == "" {
return ctx.JSON(&server.CommonResponse{
Code: server.ResponseCodeParamNotEnough,
Msg: server.ResponseMsgParamNotEnough + " query",
Msg: server.ResponseMsgParamNotEnough + " question",
})
}

sqlDatabaseChain := chains.NewSQLDatabaseChain(c.client, 100, c.db)
input := map[string]any{
"query": query,
"query": question,
}
result, err := chains.Predict(context.Background(), sqlDatabaseChain, input)
if err != nil {
Expand Down
24 changes: 24 additions & 0 deletions internal/module/log/controller/WorkTimeController.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ func (c *workTimeController) Add(ctx *fiber.Ctx) error {
})
}

// 判断填报的数据周期范围,周一到周日的数据,最晚应在下周一9点前填报完成,超时不允许填报
now := time.Now()
if now.UnixMilli() > 1730422800000 {
var earliestAllowedTime time.Time
// 判断now是否周一9点前
if now.Weekday() == time.Monday && now.Hour() < 9 {
// 允许填报上周工作
earliestAllowedTime = time.Date(now.Year(), now.Month(), now.Day()-7, 0, 0, 0, 0, time.Local)
// 最晚允许的时间是周天0点
} else {
// 最早允许当周周一0点
weekStart := now.AddDate(0, 0, -int(now.Weekday()))
earliestAllowedTime = time.Date(weekStart.Year(), weekStart.Month(), weekStart.Day(), 0, 0, 0, 0, time.Local)
}
latestAllowedTime := earliestAllowedTime.AddDate(0, 0, 7)

if !(instance.StartDate > earliestAllowedTime.UnixMilli() && instance.EndDate < latestAllowedTime.UnixMilli()) {
return ctx.JSON(&server.CommonResponse{
Code: server.ResponseCodeDataNotMatch,
Msg: "填报时间必须在本周 周一至下周一9点前",
})
}
}

duplicated, success, err := service.WorkTimeService.Add(instance)
if err != nil {
return ctx.JSON(&server.CommonResponse{
Expand Down
2 changes: 2 additions & 0 deletions page/source/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export {}

declare module '@vue/runtime-core' {
export interface GlobalComponents {
Assist: typeof import('./src/components/Assist.vue')['default']
DatePickerInTable: typeof import('./src/components/DatePickerInTable.vue')['default']
HelloWorld: typeof import('./src/components/HelloWorld.vue')['default']
IssueDetailDrawer: typeof import('./src/components/project/issue/IssueDetailDrawer.vue')['default']
Expand All @@ -33,6 +34,7 @@ declare module '@vue/runtime-core' {
NDropdown: typeof import('naive-ui')['NDropdown']
NEllipsis: typeof import('naive-ui')['NEllipsis']
NEmpty: typeof import('naive-ui')['NEmpty']
NFloatButton: typeof import('naive-ui')['NFloatButton']
NForm: typeof import('naive-ui')['NForm']
NFormItem: typeof import('naive-ui')['NFormItem']
NFormItemGi: typeof import('naive-ui')['NFormItemGi']
Expand Down
2 changes: 1 addition & 1 deletion page/source/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"eslint-config-prettier": "^8.10.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-vue": "^9.17.0",
"naive-ui": "^2.34.4",
"naive-ui": "^2.40.1",
"prettier": "^2.8.8",
"sass": "^1.65.1",
"sass-loader": "^13.3.2",
Expand Down
Loading

0 comments on commit 762f44d

Please sign in to comment.