Skip to content

Commit

Permalink
Added Commits History (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
emanuelef authored Aug 11, 2024
1 parent 8030c5a commit a360312
Show file tree
Hide file tree
Showing 6 changed files with 939 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21.6

require (
github.com/Code-Hex/go-generics-cache v1.5.1
github.com/emanuelef/github-repo-activity-stats v0.2.34
github.com/emanuelef/github-repo-activity-stats v0.2.35
github.com/gofiber/contrib/otelfiber v1.0.10
github.com/gofiber/fiber/v2 v2.52.5
github.com/joho/godotenv v1.5.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGX
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emanuelef/github-repo-activity-stats v0.2.34 h1:EonhG6aY11gLESYLUeNkaIALlgB5eNpk7scsdRonjPg=
github.com/emanuelef/github-repo-activity-stats v0.2.34/go.mod h1:NFcTD10p2U0MMITQUMG27rZspky9iuoLROol7c1Dikk=
github.com/emanuelef/github-repo-activity-stats v0.2.35 h1:HljnvA2kThqunHJmss/lnMU0RiLnJIAtAbkloOtgkL4=
github.com/emanuelef/github-repo-activity-stats v0.2.35/go.mod h1:NFcTD10p2U0MMITQUMG27rZspky9iuoLROol7c1Dikk=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down
114 changes: 114 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ type PRsWithStatsResponse struct {
PRs []stats.PRsPerDay `json:"prs"`
}

type CommitsWithStatsResponse struct {
Commits []stats.CommitsPerDay `json:"commits"`
DefaultBranch string `json:"defaultBranch"`
}

func getEnv(key, fallback string) string {
value, exists := os.LookupEnv(key)
if !exists {
Expand Down Expand Up @@ -118,6 +123,8 @@ func main() {
cacheIssues := cache.New[string, IssuesWithStatsResponse]()
cacheForks := cache.New[string, ForksWithStatsResponse]()
cachePRs := cache.New[string, PRsWithStatsResponse]()
cacheCommits := cache.New[string, CommitsWithStatsResponse]()

cacheHackerNews := cache.New[string, []news.Article]()
cacheReddit := cache.New[string, []news.ArticleData]()
cacheYouTube := cache.New[string, []news.YTVideoMetadata]()
Expand All @@ -126,6 +133,7 @@ func main() {
onGoingIssues := make(map[string]bool)
onGoingForks := make(map[string]bool)
onGoingPRs := make(map[string]bool)
onGoingCommits := make(map[string]bool)

ghStatClients := make(map[string]*repostats.ClientGQL)

Expand Down Expand Up @@ -776,6 +784,112 @@ func main() {
return c.JSON(res)
})

app.Get("/allCommits", func(c *fiber.Ctx) error {
param := c.Query("repo")
randomIndex := rand.Intn(len(maps.Keys(ghStatClients)))
clientKey := c.Query("client", maps.Keys(ghStatClients)[randomIndex])
forceRefetch := c.Query("forceRefetch", "false") == "true"

client, ok := ghStatClients[clientKey]
if !ok {
return c.Status(404).SendString("Resource not found")
}

repo, err := url.QueryUnescape(param)
if err != nil {
return err
}

// needed because c.Query cannot be used as a map key
repo = fmt.Sprintf("%s", repo)
repo = strings.ToLower(repo)

ip := c.Get("X-Forwarded-For")

// If X-Forwarded-For is empty, fallback to RemoteIP
if ip == "" {
ip = c.IP()
}

userAgent := c.Get("User-Agent")
log.Printf("Commits Request from IP: %s, Repo: %s User-Agent: %s\n", ip, repo, userAgent)

if strings.Contains(userAgent, "python-requests") {
return c.Status(404).SendString("Custom 404 Error: Resource not found")
}

span := trace.SpanFromContext(c.UserContext())
span.SetAttributes(attribute.String("github.repo", repo))
span.SetAttributes(attribute.String("caller.ip", ip))

if forceRefetch {
cacheCommits.Delete(repo)
}

if res, hit := cacheCommits.Get(repo); hit {
return c.JSON(res)
}

// if another request is already getting the data, skip and rely on SSE updates
if _, hit := onGoingCommits[repo]; hit {
return c.SendStatus(fiber.StatusNoContent)
}

onGoingCommits[repo] = true

updateChannel := make(chan int)
var allCommits []stats.CommitsPerDay
var branchName string

eg, ctx := errgroup.WithContext(ctx)

eg.Go(func() error {
allCommits, branchName, err = client.GetAllCommitsHistory(ctx, repo, updateChannel)
if err != nil {
return err
}
return nil
})

for progress := range updateChannel {
// fmt.Printf("Progress: %d\n", progress)

wg := &sync.WaitGroup{}

for _, s := range currentSessions.Sessions {
wg.Add(1)
go func(cs *session.Session) {
defer wg.Done()
if cs.Repo == repo {
cs.StateChannel <- progress
}
}(s)
}
wg.Wait()
}

if err := eg.Wait(); err != nil {
delete(onGoingCommits, repo)
return err
}

// defer close(updateChannel)

res := CommitsWithStatsResponse{
Commits: allCommits,
DefaultBranch: branchName,
}

now := time.Now()
nextDay := now.UTC().Truncate(24 * time.Hour).Add(DAY_CACHED * 24 * time.Hour)
durationUntilEndOfDay := nextDay.Sub(now)

cacheCommits.Set(repo, res, cache.WithExpiration(durationUntilEndOfDay))
delete(onGoingCommits, repo)

return c.JSON(res)
})

app.Get("/limits", func(c *fiber.Ctx) error {
client, ok := ghStatClients["PAT"]
if !ok {
Expand Down
18 changes: 17 additions & 1 deletion website/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import CompareChart from "./CompareChart";
import IssuesTimeSeriesChart from "./IssuesTimeSeriesChart";
import PRsTimeSeriesChart from "./PRsTimeSeriesChart";
import ForksTimeSeriesChart from "./ForksTimeSeriesChart";
import CommitsTimeSeriesChart from "./CommitsTimeSeriesChart";
import CalendarChart from "./CalendarChart";
import InfoPage from "./InfoPage";

Expand All @@ -22,6 +23,7 @@ import SsidChartRoundedIcon from "@mui/icons-material/SsidChartRounded";
import BugReportRoundedIcon from "@mui/icons-material/BugReportRounded";
import AltRouteOutlinedIcon from "@mui/icons-material/AltRouteOutlined";
import CallMergeRoundedIcon from '@mui/icons-material/CallMergeRounded';
import CommitRoundedIcon from '@mui/icons-material/CommitRounded';

import { ThemeProvider, createTheme } from "@mui/material/styles";
import CssBaseline from "@mui/material/CssBaseline";
Expand Down Expand Up @@ -87,7 +89,8 @@ function App() {
useLocation().pathname.includes("/info") ||
useLocation().pathname.includes("/issues") ||
useLocation().pathname.includes("/forks") ||
useLocation().pathname.includes("/prs")
useLocation().pathname.includes("/prs") ||
useLocation().pathname.includes("/commits")
)
}
>
Expand All @@ -104,6 +107,17 @@ function App() {
>
Compare
</MenuItem>
<MenuItem
component={<Link to="/commits" className="link" />}
icon={
<Tooltip title="Commits Timeline" placement="right">
<CommitRoundedIcon />
</Tooltip>
}
active={useLocation().pathname.includes("/commits")}
>
Commits
</MenuItem>
<MenuItem
component={<Link to="/prs" className="link" />}
icon={
Expand Down Expand Up @@ -191,6 +205,8 @@ function App() {
<Route path="/forks/:user/:repository" element={<ForksTimeSeriesChart />} />
<Route path="/prs" element={<PRsTimeSeriesChart />} />
<Route path="/prs/:user/:repository" element={<PRsTimeSeriesChart />} />
<Route path="/commits" element={<CommitsTimeSeriesChart />} />
<Route path="/commits/:user/:repository" element={<CommitsTimeSeriesChart />} />
</Routes>
</section>
</div>
Expand Down
Loading

0 comments on commit a360312

Please sign in to comment.