Skip to content

Commit 262b562

Browse files
committed
some improvements
1 parent 1223793 commit 262b562

File tree

3 files changed

+10
-25
lines changed

3 files changed

+10
-25
lines changed

routers/web/repo/code_frequency.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package repo
55

66
import (
7-
"errors"
87
"net/http"
98

109
"code.gitea.io/gitea/modules/templates"
@@ -30,11 +29,7 @@ func CodeFrequency(ctx *context.Context) {
3029
// CodeFrequencyData returns JSON of code frequency data
3130
func CodeFrequencyData(ctx *context.Context) {
3231
if contributorStats, err := contributors_service.GetContributorStats(ctx, ctx.Cache, ctx.Repo.Repository, ctx.Repo.Repository.DefaultBranch); err != nil {
33-
if errors.Is(err, contributors_service.ErrAwaitGeneration) {
34-
ctx.Status(http.StatusAccepted)
35-
return
36-
}
37-
ctx.ServerError("GetContributorStats", err)
32+
ctx.ServerError("GetCodeFrequencyData", err)
3833
} else {
3934
ctx.JSON(http.StatusOK, contributorStats["total"].Weeks)
4035
}

routers/web/repo/contributors.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
package repo
55

66
import (
7-
std_ctx "context"
8-
"errors"
97
"net/http"
108

119
"code.gitea.io/gitea/modules/templates"
@@ -28,10 +26,6 @@ func Contributors(ctx *context.Context) {
2826
// ContributorsData renders JSON of contributors along with their weekly commit statistics
2927
func ContributorsData(ctx *context.Context) {
3028
if contributorStats, err := contributors_service.GetContributorStats(ctx, ctx.Cache, ctx.Repo.Repository, ctx.Repo.Repository.DefaultBranch); err != nil {
31-
if errors.Is(err, contributors_service.ErrAwaitGeneration) || errors.Is(err, std_ctx.DeadlineExceeded) {
32-
ctx.Status(http.StatusAccepted)
33-
return
34-
}
3529
ctx.ServerError("GetContributorStats", err)
3630
} else {
3731
ctx.JSON(http.StatusOK, contributorStats)

services/repository/contributors_graph.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ const (
2929
contributorStatsCacheTimeout int64 = 60 * 10
3030
)
3131

32-
var (
33-
ErrAwaitGeneration = errors.New("generation took longer than ")
34-
awaitGenerationTime = time.Second * 60
35-
)
32+
var ErrAwaitGeneration = errors.New("generation took longer than ")
3633

3734
type WeekData struct {
3835
Week int64 `json:"week"` // Starting day of the week as Unix timestamp
@@ -95,19 +92,18 @@ func GetContributorStats(ctx context.Context, cache cache.StringCache, repo *rep
9592
}
9693
defer releaser()
9794

98-
// set a timeout for the generation
99-
ctx, cancel := context.WithTimeout(ctx, awaitGenerationTime)
100-
defer cancel()
95+
// check if generation is already completed by other request
96+
if cache.IsExist(cacheKey) {
97+
var res map[string]*ContributorData
98+
if _, cacheErr := cache.GetJSON(cacheKey, &res); cacheErr != nil {
99+
return nil, fmt.Errorf("cached error: %w", cacheErr.ToError())
100+
}
101+
return res, nil
102+
}
101103

102104
// run generation async
103105
res, err := generateContributorStats(ctx, cache, cacheKey, repo, revision)
104106
if err != nil {
105-
switch {
106-
case errors.Is(err, context.DeadlineExceeded):
107-
_ = cache.PutJSON(cacheKey, fmt.Errorf("generateContributorStats: %w", ErrAwaitGeneration), contributorStatsCacheTimeout)
108-
default:
109-
_ = cache.PutJSON(cacheKey, fmt.Errorf("generateContributorStats: %w", err), contributorStatsCacheTimeout)
110-
}
111107
return nil, err
112108
}
113109

0 commit comments

Comments
 (0)