Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions controller/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,19 @@ func GetAllChannels(c *gin.Context) {
})
return
}
total, err := model.CountChannels()
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": channels,
"total": total,
})
return
}
Expand Down
18 changes: 18 additions & 0 deletions controller/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,19 @@ func GetAllLogs(c *gin.Context) {
})
return
}
total, err := model.CountAllLogs(logType, startTimestamp, endTimestamp, modelName, username, tokenName, channel)
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": logs,
"total": total,
})
return
}
Expand All @@ -56,10 +65,19 @@ func GetUserLogs(c *gin.Context) {
})
return
}
total, err := model.CountUserLogs(userId, logType, startTimestamp, endTimestamp, modelName, tokenName)
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": logs,
"total": total,
})
return
}
Expand Down
9 changes: 9 additions & 0 deletions controller/redemption.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,19 @@ func GetAllRedemptions(c *gin.Context) {
})
return
}
total, err := model.CountRedemptions()
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": redemptions,
"total": total,
})
return
}
Expand Down
11 changes: 11 additions & 0 deletions controller/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,21 @@ func GetAllTokens(c *gin.Context) {
})
return
}

total, err := model.CountUserTokens(userId)
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": tokens,
"total": total,
})
return
}
Expand Down
10 changes: 10 additions & 0 deletions controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,20 @@ func GetAllUsers(c *gin.Context) {
return
}

total, err := model.CountUsers()
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": users,
"total": total,
})
}

Expand Down
6 changes: 6 additions & 0 deletions model/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ func GetAllChannels(startIdx int, num int, scope string) ([]*Channel, error) {
return channels, err
}

func CountChannels() (int64, error) {
var count int64
err := DB.Model(&Channel{}).Count(&count).Error
return count, err
}

func SearchChannels(keyword string) (channels []*Channel, err error) {
err = DB.Omit("key").Where("id = ? or name LIKE ?", helper.String2Int(keyword), keyword+"%").Find(&channels).Error
return channels, err
Expand Down
54 changes: 54 additions & 0 deletions model/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,36 @@ func GetAllLogs(logType int, startTimestamp int64, endTimestamp int64, modelName
return logs, err
}

func CountAllLogs(logType int, startTimestamp int64, endTimestamp int64, modelName string, username string, tokenName string, channel int) (int64, error) {
var tx *gorm.DB
if logType == LogTypeUnknown {
tx = LOG_DB.Model(&Log{})
} else {
tx = LOG_DB.Model(&Log{}).Where("type = ?", logType)
}
if modelName != "" {
tx = tx.Where("model_name = ?", modelName)
}
if username != "" {
tx = tx.Where("username = ?", username)
}
if tokenName != "" {
tx = tx.Where("token_name = ?", tokenName)
}
if startTimestamp != 0 {
tx = tx.Where("created_at >= ?", startTimestamp)
}
if endTimestamp != 0 {
tx = tx.Where("created_at <= ?", endTimestamp)
}
if channel != 0 {
tx = tx.Where("channel_id = ?", channel)
}
var count int64
err := tx.Count(&count).Error
return count, err
}

func GetUserLogs(userId int, logType int, startTimestamp int64, endTimestamp int64, modelName string, tokenName string, startIdx int, num int) (logs []*Log, err error) {
var tx *gorm.DB
if logType == LogTypeUnknown {
Expand All @@ -145,6 +175,30 @@ func GetUserLogs(userId int, logType int, startTimestamp int64, endTimestamp int
return logs, err
}

func CountUserLogs(userId int, logType int, startTimestamp int64, endTimestamp int64, modelName string, tokenName string) (int64, error) {
var tx *gorm.DB
if logType == LogTypeUnknown {
tx = LOG_DB.Model(&Log{}).Where("user_id = ?", userId)
} else {
tx = LOG_DB.Model(&Log{}).Where("user_id = ? and type = ?", userId, logType)
}
if modelName != "" {
tx = tx.Where("model_name = ?", modelName)
}
if tokenName != "" {
tx = tx.Where("token_name = ?", tokenName)
}
if startTimestamp != 0 {
tx = tx.Where("created_at >= ?", startTimestamp)
}
if endTimestamp != 0 {
tx = tx.Where("created_at <= ?", endTimestamp)
}
var count int64
err := tx.Count(&count).Error
return count, err
}

func SearchAllLogs(keyword string) (logs []*Log, err error) {
err = LOG_DB.Where("type = ? or content LIKE ?", keyword, keyword+"%").Order("id desc").Limit(config.MaxRecentItems).Find(&logs).Error
return logs, err
Expand Down
6 changes: 6 additions & 0 deletions model/redemption.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ func GetAllRedemptions(startIdx int, num int) ([]*Redemption, error) {
return redemptions, err
}

func CountRedemptions() (int64, error) {
var count int64
err := DB.Model(&Redemption{}).Count(&count).Error
return count, err
}

func SearchRedemptions(keyword string) (redemptions []*Redemption, err error) {
err = DB.Where("id = ? or name LIKE ?", keyword, keyword+"%").Find(&redemptions).Error
return redemptions, err
Expand Down
6 changes: 6 additions & 0 deletions model/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ func GetAllUserTokens(userId int, startIdx int, num int, order string) ([]*Token
return tokens, err
}

func CountUserTokens(userId int) (int64, error) {
var count int64
err := DB.Model(&Token{}).Where("user_id = ?", userId).Count(&count).Error
return count, err
}

func SearchUserTokens(userId int, keyword string) (tokens []*Token, err error) {
err = DB.Where("user_id = ?", userId).Where("name LIKE ?", keyword+"%").Find(&tokens).Error
return tokens, err
Expand Down
6 changes: 6 additions & 0 deletions model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ func GetAllUsers(startIdx int, num int, order string) (users []*User, err error)
return users, err
}

func CountUsers() (int64, error) {
var count int64
err := DB.Model(&User{}).Where("status != ?", UserStatusDeleted).Count(&count).Error
return count, err
}

func SearchUsers(keyword string) (users []*User, err error) {
if !common.UsingPostgreSQL {
err = DB.Omit("password").Where("id = ? or username LIKE ? or email LIKE ? or display_name LIKE ?", keyword, keyword+"%", keyword+"%", keyword+"%").Find(&users).Error
Expand Down
21 changes: 14 additions & 7 deletions web/berry/src/views/Channel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default function ChannelPage() {
const [activePage, setActivePage] = useState(0);
const [searching, setSearching] = useState(false);
const [searchKeyword, setSearchKeyword] = useState('');
const [channelCount, setChannelCount] = useState(0);
const theme = useTheme();
const matchUpMd = useMediaQuery(theme.breakpoints.up('sm'));
const [openModal, setOpenModal] = useState(false);
Expand All @@ -36,7 +37,7 @@ export default function ChannelPage() {
const loadChannels = async (startIdx) => {
setSearching(true);
const res = await API.get(`/api/channel/?p=${startIdx}`);
const { success, message, data } = res.data;
const { success, message, data, total } = res.data;
if (success) {
if (startIdx === 0) {
setChannels(data);
Expand All @@ -45,19 +46,24 @@ export default function ChannelPage() {
newChannels.splice(startIdx * ITEMS_PER_PAGE, data.length, ...data);
setChannels(newChannels);
}
if (total !== undefined) {
setChannelCount(total);
}
} else {
showError(message);
}
setSearching(false);
};

const onPaginationChange = (event, activePage) => {
const onPaginationChange = (event, newPage) => {
(async () => {
if (activePage === Math.ceil(channels.length / ITEMS_PER_PAGE)) {
// In this case we have to load more data and then append them.
await loadChannels(activePage);
const pageStart = newPage * ITEMS_PER_PAGE;
const pageEnd = pageStart + ITEMS_PER_PAGE;
if (channels.slice(pageStart, pageEnd).length < ITEMS_PER_PAGE && pageStart < channelCount) {
// Page data not yet loaded, fetch it from the backend.
await loadChannels(newPage);
}
setActivePage(activePage);
setActivePage(newPage);
})();
};

Expand All @@ -73,6 +79,7 @@ export default function ChannelPage() {
const { success, message, data } = res.data;
if (success) {
setChannels(data);
setChannelCount(data.length);
setActivePage(0);
} else {
showError(message);
Expand Down Expand Up @@ -274,7 +281,7 @@ export default function ChannelPage() {
<TablePagination
page={activePage}
component="div"
count={channels.length + (channels.length % ITEMS_PER_PAGE === 0 ? 1 : 0)}
count={channelCount}
rowsPerPage={ITEMS_PER_PAGE}
onPageChange={onPaginationChange}
rowsPerPageOptions={[ITEMS_PER_PAGE]}
Expand Down
20 changes: 13 additions & 7 deletions web/berry/src/views/Log/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default function Log() {
const [activePage, setActivePage] = useState(0);
const [searching, setSearching] = useState(false);
const [searchKeyword, setSearchKeyword] = useState(originalKeyword);
const [logCount, setLogCount] = useState(0);
const [initPage, setInitPage] = useState(true);
const userIsAdmin = isAdmin();

Expand All @@ -48,7 +49,7 @@ export default function Log() {
delete query.channel;
}
const res = await API.get(url, { params: query });
const { success, message, data } = res.data;
const { success, message, data, total } = res.data;
if (success) {
if (startIdx === 0) {
setLogs(data);
Expand All @@ -57,19 +58,24 @@ export default function Log() {
newLogs.splice(startIdx * ITEMS_PER_PAGE, data.length, ...data);
setLogs(newLogs);
}
if (total !== undefined) {
setLogCount(total);
}
} else {
showError(message);
}
setSearching(false);
};

const onPaginationChange = (event, activePage) => {
const onPaginationChange = (event, newPage) => {
(async () => {
if (activePage === Math.ceil(logs.length / ITEMS_PER_PAGE)) {
// In this case we have to load more data and then append them.
await loadLogs(activePage);
const pageStart = newPage * ITEMS_PER_PAGE;
const pageEnd = pageStart + ITEMS_PER_PAGE;
if (logs.slice(pageStart, pageEnd).length < ITEMS_PER_PAGE && pageStart < logCount) {
// Page data not yet loaded, fetch it from the backend.
await loadLogs(newPage);
}
setActivePage(activePage);
setActivePage(newPage);
})();
};

Expand Down Expand Up @@ -146,7 +152,7 @@ export default function Log() {
<TablePagination
page={activePage}
component="div"
count={logs.length + (logs.length % ITEMS_PER_PAGE === 0 ? 1 : 0)}
count={logCount}
rowsPerPage={ITEMS_PER_PAGE}
onPageChange={onPaginationChange}
rowsPerPageOptions={[ITEMS_PER_PAGE]}
Expand Down
Loading