Skip to content

Commit

Permalink
Add more stats in the main page (#81)
Browse files Browse the repository at this point in the history
* Adding new stats

* fix JSON download

* add peaks
  • Loading branch information
emanuelef committed Feb 17, 2024
1 parent 47c575e commit 3a36926
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 41 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.3.1
github.com/emanuelef/github-repo-activity-stats v0.2.9
github.com/emanuelef/github-repo-activity-stats v0.2.12
github.com/gofiber/contrib/otelfiber v1.0.10
github.com/gofiber/fiber/v2 v2.52.0
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 @@ -6,8 +6,8 @@ github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqy
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
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.9 h1:hdPv+l3zW/25KxNGj3qFjf4LL89uv98hy/4WRis9zhM=
github.com/emanuelef/github-repo-activity-stats v0.2.9/go.mod h1:DS1QpHyhmItr6PtYZjoXgclhqOJP8nAg7nIXNZ6c16c=
github.com/emanuelef/github-repo-activity-stats v0.2.12 h1:6N6Oj4hfTI+4JfThpWBW2WWufnDV+bX6O3sXIfA9ccM=
github.com/emanuelef/github-repo-activity-stats v0.2.12/go.mod h1:DS1QpHyhmItr6PtYZjoXgclhqOJP8nAg7nIXNZ6c16c=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
Expand Down
30 changes: 26 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ var currentSessions session.SessionsLock

const DAY_CACHED = 7

type StarsWithStatsResponse struct {
Stars []stats.StarsPerDay `json:"stars"`
NewLast10Days int `json:"newLast10Days"`
MaxPeriods []repostats.MaxPeriod `json:"maxPeriods"`
MaxPeaks []repostats.PeakDay `json:"maxPeaks"`
}

func getEnv(key, fallback string) string {
value, exists := os.LookupEnv(key)
if !exists {
Expand Down Expand Up @@ -91,7 +98,7 @@ func main() {
}

cacheOverall := cache.New[string, *stats.RepoStats]()
cacheStars := cache.New[string, []stats.StarsPerDay]()
cacheStars := cache.New[string, StarsWithStatsResponse]()

onGoingStars := make(map[string]bool)

Expand Down Expand Up @@ -295,14 +302,29 @@ func main() {
return err
}

maxPeriods, maxPeaks, err := repostats.FindMaxConsecutivePeriods(allStars, 10)

if err != nil {
return err
}

newLastNDays := repostats.NewStarsLastDays(allStars, 10)

res := StarsWithStatsResponse{
Stars: allStars,
NewLast10Days: newLastNDays,
MaxPeriods: maxPeriods,
MaxPeaks: maxPeaks,
}

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

cacheStars.Set(repo, allStars, cache.WithExpiration(durationUntilEndOfDay))
cacheStars.Set(repo, res, cache.WithExpiration(durationUntilEndOfDay))
delete(onGoingStars, repo)

return c.JSON(allStars)
return c.JSON(res)
})

app.Get("/limits", func(c *fiber.Ctx) error {
Expand Down Expand Up @@ -377,7 +399,7 @@ func main() {
// Check if the data is cached
if res, hit := cacheStars.Get(repo); hit {
// Generate CSV data from the cached data
csvData, err := generateCSVData(repo, res)
csvData, err := generateCSVData(repo, res.Stars)
if err != nil {
log.Printf("Error generating CSV data: %v", err)
return c.Status(500).SendString("Internal Server Error")
Expand Down
34 changes: 17 additions & 17 deletions website/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
"react-fusioncharts": "^4.0.0",
"react-github-btn": "^1.4.0",
"react-pro-sidebar": "^1.1.0",
"react-router-dom": "^6.22.0",
"react-router-dom": "^6.22.1",
"react-toastify": "^10.0.4",
"react18-json-view": "^0.2.7"
},
"devDependencies": {
"@types/react": "^18.2.55",
"@types/react": "^18.2.56",
"@types/react-dom": "^18.2.19",
"@typescript-eslint/eslint-plugin": "^7.0.1",
"@typescript-eslint/parser": "^7.0.1",
Expand Down
10 changes: 5 additions & 5 deletions website/src/CompareChart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -206,18 +206,18 @@ function CompareChart() {
.then((results) => {
const [data1, data2] = results;

data1.forEach((subarray) => {
data1.stars.forEach((subarray) => {
subarray.push(repo);
});

data2.forEach((subarray) => {
data2.stars.forEach((subarray) => {
subarray.push(repo2);
});

removeUncompleteDay(data1);
removeUncompleteDay(data2);
removeUncompleteDay(data1.stars);
removeUncompleteDay(data2.stars);

handleCombinedData(data1.concat(data2));
handleCombinedData(data1.stars.concat(data2.stars));
setLoading(false);
})
.catch((error) => {
Expand Down
49 changes: 39 additions & 10 deletions website/src/TimeSeriesChart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ const chart_props = {
],
},
],
xAxis: {
plot: "Time",
timemarker: [],
},
chart: {
animation: "0",
theme: "candy",
Expand Down Expand Up @@ -162,14 +166,16 @@ function TimeSeriesChart() {
})
.then((data) => {
setLoading(false);
console.log(data);
const starHistory = data.stars;

// check if last element is today
if (data.length > 1) {
const lastElement = data[data.length - 1];
if (starHistory.length > 1) {
const lastElement = starHistory[starHistory.length - 1];
console.log(lastElement[0]);
console.log(data);
console.log(starHistory);
const isLastElementToday = isToday(lastElement[0]);
data.pop(); // remove last element as the current day is not complete
starHistory.pop(); // remove last element as the current day is not complete
console.log("isLastElementToday", isLastElementToday);
setShowForceRefetch(!isLastElementToday);
setForceRefetch(false);
Expand All @@ -178,22 +184,43 @@ function TimeSeriesChart() {
}

const fusionTable = new FusionCharts.DataStore().createDataTable(
data,
starHistory,
schema
);
const options = { ...ds };
options.timeseriesDs.dataSource.caption = { text: `Stars ${repo}` };
options.timeseriesDs.dataSource.data = fusionTable;
options.timeseriesDs.dataSource.yAxis[0].plot[0].value =
"Cumulative Stars";
options.timeseriesDs.dataSource.chart.theme = theme;

const maxPeriods = data.maxPeriods.map((period) => ({
start: period.StartDay,
end: period.EndDay,
label: `${period.TotalStars} is the highest number of new stars in a 10 day period`,
timeformat: "%d-%m-%Y",
type: "full",
}));

const maxPeaks = data.maxPeaks.map((peak) => ({
start: peak.Day,
timeformat: "%d-%m-%Y",
label: `${peak.Stars} is the maximum number of new stars in one day`,
style: {
marker: {
fill: "#30EE47",
},
},
}));

const timemarkers = maxPeriods.concat(maxPeaks);

(options.timeseriesDs.dataSource.xAxis.timemarker = timemarkers),
(options.timeseriesDs.dataSource.chart.theme = theme);
options.timeseriesDs.dataSource.chart.exportFileName = `${selectedRepo.replace(
"/",
"_"
)}-stars-history`;
setds(options);

console.log(data);
})
.catch((e) => {
console.error(`An error occurred: ${e}`);
Expand Down Expand Up @@ -226,8 +253,10 @@ function TimeSeriesChart() {
const downloadUrl = `${HOST}/allStars?repo=${repoParsed}`;

fetch(downloadUrl)
.then((response) => response.blob())
.then((blob) => {
.then((response) => response.json())
.then((data) => {
const starsContent = JSON.stringify(data.stars);
const blob = new Blob([starsContent], { type: "application/json" });
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
Expand Down

0 comments on commit 3a36926

Please sign in to comment.