diff options
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 68 |
1 files changed, 48 insertions, 20 deletions
@@ -9,11 +9,6 @@ import ( "time" ) -// Define the structure for GitHub commit activity response -type CommitActivity struct { - Total int `json:"total"` -} - // Function to fetch the user's commit count func getCommitCount(username string) (int, error) { // Set up the request URL @@ -64,31 +59,64 @@ func getCommitCount(username string) (int, error) { return int(totalCommits), nil } -// API handler for fetching commit count -func commitCountHandler(w http.ResponseWriter, r *http.Request) { - // Get the username from query parameters - username := r.URL.Query().Get("username") - if username == "" { - http.Error(w, "username parameter is required", http.StatusBadRequest) +// API handler for fetching commit difference and daily average +func commitDifferenceHandler(w http.ResponseWriter, r *http.Request) { + // Get usernames from query parameters + username1 := r.URL.Query().Get("username1") + username2 := r.URL.Query().Get("username2") + + if username1 == "" || username2 == "" { + http.Error(w, "Both username1 and username2 parameters are required", http.StatusBadRequest) + return + } + + // Fetch commit counts for both users + count1, err := getCommitCount(username1) + if err != nil { + http.Error(w, fmt.Sprintf("Error fetching commits for %s: %v", username1, err), http.StatusInternalServerError) return } - // Fetch the commit count - count, err := getCommitCount(username) + count2, err := getCommitCount(username2) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + http.Error(w, fmt.Sprintf("Error fetching commits for %s: %v", username2, err), http.StatusInternalServerError) return } - // Return the commit count as JSON + // Calculate the difference and remaining days + difference := abs(count1 - count2) + remainingDays := daysUntilEndOfYear() + dailyAverage := difference / remainingDays + + // Return the results as JSON w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(map[string]int{"commit_count": count}) + json.NewEncoder(w).Encode(map[string]interface{}{ + "commit_count_user1": count1, + "commit_count_user2": count2, + "difference": difference, + "remaining_days": remainingDays, + "daily_average": dailyAverage, + }) +} + +// Utility function to calculate absolute value +func abs(x int) int { + if x < 0 { + return -x + } + return x +} + +// Utility function to calculate days remaining in the year +func daysUntilEndOfYear() int { + now := time.Now() + endOfYear := time.Date(now.Year(), time.December, 31, 23, 59, 59, 0, time.UTC) + return int(endOfYear.Sub(now).Hours() / 24) } func main() { // Set up the HTTP server - http.HandleFunc("/commit-count", commitCountHandler) - fmt.Println("Server is running on port 8080...") - log.Fatal(http.ListenAndServe(":8080", nil)) + http.HandleFunc("/commit-difference", commitDifferenceHandler) + fmt.Println("Server is running on port 3001...") + log.Fatal(http.ListenAndServe(":3001", nil)) } - |