code.gitea.io/gitea@v1.22.3/routers/web/repo/code_frequency.go (about)

     1  // Copyright 2023 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package repo
     5  
     6  import (
     7  	"errors"
     8  	"net/http"
     9  
    10  	"code.gitea.io/gitea/modules/base"
    11  	"code.gitea.io/gitea/services/context"
    12  	contributors_service "code.gitea.io/gitea/services/repository"
    13  )
    14  
    15  const (
    16  	tplCodeFrequency base.TplName = "repo/activity"
    17  )
    18  
    19  // CodeFrequency renders the page to show repository code frequency
    20  func CodeFrequency(ctx *context.Context) {
    21  	ctx.Data["Title"] = ctx.Tr("repo.activity.navbar.code_frequency")
    22  
    23  	ctx.Data["PageIsActivity"] = true
    24  	ctx.Data["PageIsCodeFrequency"] = true
    25  	ctx.PageData["repoLink"] = ctx.Repo.RepoLink
    26  
    27  	ctx.HTML(http.StatusOK, tplCodeFrequency)
    28  }
    29  
    30  // CodeFrequencyData returns JSON of code frequency data
    31  func CodeFrequencyData(ctx *context.Context) {
    32  	if contributorStats, err := contributors_service.GetContributorStats(ctx, ctx.Cache, ctx.Repo.Repository, ctx.Repo.CommitID); err != nil {
    33  		if errors.Is(err, contributors_service.ErrAwaitGeneration) {
    34  			ctx.Status(http.StatusAccepted)
    35  			return
    36  		}
    37  		ctx.ServerError("GetCodeFrequencyData", err)
    38  	} else {
    39  		ctx.JSON(http.StatusOK, contributorStats["total"].Weeks)
    40  	}
    41  }