-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from nyudlts/report-csv
CSV Exports for reports; API Endpoints for reports
- Loading branch information
Showing
30 changed files
with
300 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ bin/* | |
.vscode/* | ||
.idea/* | ||
*.log | ||
*.conf | ||
*.conf | ||
tmp/* |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
"regexp" | ||
"strconv" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/nyudlts/bytemath" | ||
"github.com/nyudlts/go-medialog/database" | ||
) | ||
|
||
type SummaryAndTotals struct { | ||
RepositoryID int `json:"repository_id"` | ||
Repository string `json:"repository"` | ||
TotalCount int `json:"total_count"` | ||
TotalSize int64 `json:"total_Size"` | ||
TotalHuman string `json:"total_human_size"` | ||
Summaries database.Summaries `json:"summaries"` | ||
} | ||
|
||
func SummaryDateRange(c *gin.Context) { | ||
if _, err := checkToken(c); err != nil { | ||
c.JSON(http.StatusUnauthorized, err.Error()) | ||
return | ||
} | ||
|
||
//get the start and end dates | ||
datePattern := regexp.MustCompile("^[0-9]{8}$") | ||
startDate := c.Query("start_date") | ||
endDate := c.Query("end_date") | ||
|
||
//validate start and end dates | ||
if !datePattern.MatchString(startDate) { | ||
c.JSON(http.StatusBadRequest, "invalid start_date") | ||
return | ||
} | ||
|
||
if !datePattern.MatchString(endDate) { | ||
c.JSON(http.StatusBadRequest, "invalid end_date") | ||
return | ||
} | ||
|
||
var isRefreshed bool | ||
//parse is refreshed | ||
if c.Query("is_refreshed") == "true" { | ||
isRefreshed = true | ||
} else { | ||
isRefreshed = false | ||
} | ||
|
||
//create a date range variable | ||
var dr = database.DateRange{} | ||
dr.IsRefreshed = isRefreshed | ||
//parse the repository id | ||
var err error | ||
dr.RepositoryID, err = strconv.Atoi(c.Query("repository_id")) | ||
if err != nil { | ||
dr.RepositoryID = 0 | ||
} | ||
|
||
dr.StartYear, _ = strconv.Atoi(startDate[0:4]) | ||
dr.StartMonth, _ = strconv.Atoi(startDate[4:6]) | ||
dr.StartDay, _ = strconv.Atoi(startDate[6:8]) | ||
dr.EndYear, _ = strconv.Atoi(endDate[0:4]) | ||
dr.EndMonth, _ = strconv.Atoi(endDate[4:6]) | ||
dr.EndDay, _ = strconv.Atoi(endDate[6:8]) | ||
|
||
summaries, err := database.GetSummaryByDateRange(dr) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
|
||
var slug string | ||
if dr.RepositoryID == 0 { | ||
slug = "all" | ||
} else { | ||
repository, err := database.FindRepository(uint(dr.RepositoryID)) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
slug = repository.Slug | ||
} | ||
|
||
var totalSize int64 = 0 | ||
var totalCount int = 0 | ||
for _, hit := range summaries.GetSlice() { | ||
totalSize = totalSize + int64(hit.Size) | ||
totalCount = totalCount + hit.Count | ||
} | ||
|
||
//convert the total size to a human readable format | ||
f64 := float64(totalSize) | ||
tsize := bytemath.ConvertToBytes(f64, bytemath.B) | ||
humanSize := bytemath.ConvertBytesToHumanReadable(int64(tsize)) | ||
|
||
c.JSON(http.StatusOK, SummaryAndTotals{ | ||
RepositoryID: dr.RepositoryID, | ||
Repository: slug, | ||
TotalCount: totalCount, | ||
TotalSize: totalSize, | ||
TotalHuman: humanSize, | ||
Summaries: summaries, | ||
}) | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
module github.com/nyudlts/go-medialog | ||
|
||
go 1.22.3 | ||
go 1.23.1 | ||
|
||
|
||
require ( | ||
github.com/gin-contrib/sessions v1.0.0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.