Improve statistic

This commit is contained in:
2025-05-30 12:04:54 +08:00
parent 46e4ffc9d8
commit 5e6d202acd
3 changed files with 21 additions and 22 deletions

View File

@@ -293,6 +293,10 @@ func init() {
for id, stats := range cachedResourcesStats { for id, stats := range cachedResourcesStats {
var count int64 var count int64
if err := tx.Model(&model.Resource{}).Where("id = ?", id).Count(&count).Error; err != nil { if err := tx.Model(&model.Resource{}).Where("id = ?", id).Count(&count).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
log.Warnf("Resource with ID %d not found, skipping stats update", id)
continue
}
return err return err
} }
if count == 0 { if count == 0 {
@@ -322,15 +326,6 @@ func init() {
} }
func AddResourceViewCount(id uint) error { func AddResourceViewCount(id uint) error {
// 检查资源是否存在
exists, err := ExistsResource(id)
if err != nil {
return err
}
if !exists {
return model.NewNotFoundError("Resource not found")
}
cacheMutex.RLock() cacheMutex.RLock()
stats, exists := cachedResourcesStats[id] stats, exists := cachedResourcesStats[id]
cacheMutex.RUnlock() cacheMutex.RUnlock()
@@ -350,15 +345,6 @@ func AddResourceViewCount(id uint) error {
} }
func AddResourceDownloadCount(id uint) error { func AddResourceDownloadCount(id uint) error {
// 检查资源是否存在
exists, err := ExistsResource(id)
if err != nil {
return err
}
if !exists {
return model.NewNotFoundError("Resource not found")
}
cacheMutex.RLock() cacheMutex.RLock()
stats, exists := cachedResourcesStats[id] stats, exists := cachedResourcesStats[id]
cacheMutex.RUnlock() cacheMutex.RUnlock()

View File

@@ -437,7 +437,10 @@ func DownloadFile(ip, fid, cfToken string) (string, string, error) {
if file.StorageID == nil { if file.StorageID == nil {
if file.RedirectUrl != "" { if file.RedirectUrl != "" {
_ = dao.AddResourceDownloadCount(file.ResourceID) err := dao.AddResourceDownloadCount(file.ResourceID)
if err != nil {
log.Errorf("failed to add resource download count: %v", err)
}
return file.RedirectUrl, file.Filename, nil return file.RedirectUrl, file.Filename, nil
} }
return "", "", model.NewRequestError("file is not available") return "", "", model.NewRequestError("file is not available")
@@ -454,10 +457,17 @@ func DownloadFile(ip, fid, cfToken string) (string, string, error) {
} }
path, err := iStorage.Download(file.StorageKey, file.Filename) path, err := iStorage.Download(file.StorageKey, file.Filename)
if err != nil {
log.Error("failed to download file from storage: ", err)
return "", "", model.NewInternalServerError("failed to download file from storage")
}
_ = dao.AddResourceDownloadCount(file.ResourceID) err = dao.AddResourceDownloadCount(file.ResourceID)
if err != nil {
log.Errorf("failed to add resource download count: %v", err)
}
return path, file.Filename, err return path, file.Filename, nil
} }
func testFileUrl(url string) (int64, error) { func testFileUrl(url string) (int64, error) {

View File

@@ -113,10 +113,13 @@ func parseResourceIfPresent(line string, host string) *model.ResourceView {
func GetResource(id uint, host string) (*model.ResourceDetailView, error) { func GetResource(id uint, host string) (*model.ResourceDetailView, error) {
r, err := dao.GetResourceByID(id) r, err := dao.GetResourceByID(id)
_ = dao.AddResourceViewCount(id)
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = dao.AddResourceViewCount(id)
if err != nil {
log.Error("AddResourceViewCount error: ", err)
}
v := r.ToDetailView() v := r.ToDetailView()
if host != "" { if host != "" {
related := findRelatedResources(r, host) related := findRelatedResources(r, host)