[go: up one dir, main page]

Skip to content

Commit

Permalink
feat: able to fix channels
Browse files Browse the repository at this point in the history
  • Loading branch information
Calcium-Ion committed Jan 10, 2024
1 parent 3e13810 commit 6a24e89
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
16 changes: 16 additions & 0 deletions controller/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ func GetAllChannels(c *gin.Context) {
return
}

func FixChannelsAbilities(c *gin.Context) {
count, err := model.FixAbility()
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
"data": count,
})
}

func SearchChannels(c *gin.Context) {
keyword := c.Query("keyword")
group := c.Query("group")
Expand Down
43 changes: 43 additions & 0 deletions model/ability.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package model

import (
"fmt"
"one-api/common"
"strings"
)
Expand Down Expand Up @@ -118,3 +119,45 @@ func (channel *Channel) UpdateAbilities() error {
func UpdateAbilityStatus(channelId int, status bool) error {
return DB.Model(&Ability{}).Where("channel_id = ?", channelId).Select("enabled").Update("enabled", status).Error
}

func FixAbility() (int, error) {
var channelIds []int
count := 0
// Find all channel ids from channel table
err := DB.Model(&Channel{}).Pluck("id", &channelIds).Error
if err != nil {
common.SysError(fmt.Sprintf("Get channel ids from channel table failed: %s", err.Error()))
return 0, err
}
// Delete abilities of channels that are not in channel table
err = DB.Where("channel_id NOT IN (?)", channelIds).Delete(&Ability{}).Error
if err != nil {
common.SysError(fmt.Sprintf("Delete abilities of channels that are not in channel table failed: %s", err.Error()))
return 0, err
}
common.SysLog(fmt.Sprintf("Delete abilities of channels that are not in channel table successfully, ids: %v", channelIds))
count += len(channelIds)

// Use channelIds to find channel not in abilities table
var abilityChannelIds []int
err = DB.Model(&Ability{}).Pluck("channel_id", &abilityChannelIds).Error
if err != nil {
common.SysError(fmt.Sprintf("Get channel ids from abilities table failed: %s", err.Error()))
return 0, err
}
var channels []Channel
err = DB.Where("id NOT IN (?)", abilityChannelIds).Find(&channels).Error
if err != nil {
return 0, err
}
for _, channel := range channels {
err := channel.UpdateAbilities()
if err != nil {
common.SysError(fmt.Sprintf("Update abilities of channel %d failed: %s", channel.Id, err.Error()))
} else {
common.SysLog(fmt.Sprintf("Update abilities of channel %d successfully", channel.Id))
count++
}
}
return count, nil
}
1 change: 1 addition & 0 deletions router/api-router.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func SetApiRouter(router *gin.Engine) {
channelRoute.DELETE("/disabled", controller.DeleteDisabledChannel)
channelRoute.DELETE("/:id", controller.DeleteChannel)
channelRoute.POST("/batch", controller.DeleteChannelBatch)
channelRoute.POST("/fix", controller.FixChannelsAbilities)
}
tokenRoute := apiRouter.Group("/token")
tokenRoute.Use(middleware.UserAuth())
Expand Down
20 changes: 20 additions & 0 deletions web/src/components/ChannelsTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,17 @@ const ChannelsTable = () => {
setLoading(false);
}

const fixChannelsAbilities = async () => {
const res = await API.post(`/api/channel/fix`);
const {success, message, data} = res.data;
if (success) {
showSuccess(`已修复 ${data} 个通道!`);
await refresh();
} else {
showError(message);
}
}

const sortChannel = (key) => {
if (channels.length === 0) return;
setLoading(true);
Expand Down Expand Up @@ -722,6 +733,15 @@ const ChannelsTable = () => {
>
<Button disabled={!enableBatchDelete} theme='light' type='danger' style={{marginRight: 8}}>删除所选通道</Button>
</Popconfirm>
<Popconfirm
title="确定是否要修复数据库一致性?"
content="进行该操作时,可能导致渠道访问错误,请仅在数据库出现问题时使用"
okType={'warning'}
onConfirm={fixChannelsAbilities}
position={'top'}
>
<Button theme='light' type='secondary' style={{marginRight: 8}}>修复数据库一致性</Button>
</Popconfirm>
</Space>
</div>
</>
Expand Down

0 comments on commit 6a24e89

Please sign in to comment.