From ab280f20aef71b1f750ea3ca6d2748c418d47213 Mon Sep 17 00:00:00 2001 From: Don Mennerich Date: Mon, 30 Sep 2024 07:21:02 -0400 Subject: [PATCH 01/13] updating api and database package filenames --- .gitignore | 3 ++- api/v0/{API-Accessions.go => apiAccessions.go} | 0 api/v0/{API-Entries.go => apiEntries.go} | 0 api/v0/{API-Repositories.go => apiRepositories.go} | 0 api/v0/{API-Resources.go => apiResources.go} | 0 api/v0/{API-V0.go => apiRoot.go} | 0 database/{Database-Accessions.go => databaseAccessions.go} | 0 database/{Database-Entries.go => databaseEntries.go} | 0 database/{Database-Migrations.go => databaseMigrations.go} | 0 database/{Database-Repositories.go => databaseRepositories.go} | 0 database/{Database-Resources.go => databaseResources.go} | 0 database/{database.go => databaseRoot.go} | 0 database/{Database-Sessions.go => databaseSessions.go} | 0 database/{Database-Tokens.go => databaseTokens.go} | 0 database/{Database-Users.go => databaseUsers.go} | 0 15 files changed, 2 insertions(+), 1 deletion(-) rename api/v0/{API-Accessions.go => apiAccessions.go} (100%) rename api/v0/{API-Entries.go => apiEntries.go} (100%) rename api/v0/{API-Repositories.go => apiRepositories.go} (100%) rename api/v0/{API-Resources.go => apiResources.go} (100%) rename api/v0/{API-V0.go => apiRoot.go} (100%) rename database/{Database-Accessions.go => databaseAccessions.go} (100%) rename database/{Database-Entries.go => databaseEntries.go} (100%) rename database/{Database-Migrations.go => databaseMigrations.go} (100%) rename database/{Database-Repositories.go => databaseRepositories.go} (100%) rename database/{Database-Resources.go => databaseResources.go} (100%) rename database/{database.go => databaseRoot.go} (100%) rename database/{Database-Sessions.go => databaseSessions.go} (100%) rename database/{Database-Tokens.go => databaseTokens.go} (100%) rename database/{Database-Users.go => databaseUsers.go} (100%) diff --git a/.gitignore b/.gitignore index 071a894..07ac242 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ bin/* .vscode/* .idea/* *.log -*.conf \ No newline at end of file +*.conf +tmp/* \ No newline at end of file diff --git a/api/v0/API-Accessions.go b/api/v0/apiAccessions.go similarity index 100% rename from api/v0/API-Accessions.go rename to api/v0/apiAccessions.go diff --git a/api/v0/API-Entries.go b/api/v0/apiEntries.go similarity index 100% rename from api/v0/API-Entries.go rename to api/v0/apiEntries.go diff --git a/api/v0/API-Repositories.go b/api/v0/apiRepositories.go similarity index 100% rename from api/v0/API-Repositories.go rename to api/v0/apiRepositories.go diff --git a/api/v0/API-Resources.go b/api/v0/apiResources.go similarity index 100% rename from api/v0/API-Resources.go rename to api/v0/apiResources.go diff --git a/api/v0/API-V0.go b/api/v0/apiRoot.go similarity index 100% rename from api/v0/API-V0.go rename to api/v0/apiRoot.go diff --git a/database/Database-Accessions.go b/database/databaseAccessions.go similarity index 100% rename from database/Database-Accessions.go rename to database/databaseAccessions.go diff --git a/database/Database-Entries.go b/database/databaseEntries.go similarity index 100% rename from database/Database-Entries.go rename to database/databaseEntries.go diff --git a/database/Database-Migrations.go b/database/databaseMigrations.go similarity index 100% rename from database/Database-Migrations.go rename to database/databaseMigrations.go diff --git a/database/Database-Repositories.go b/database/databaseRepositories.go similarity index 100% rename from database/Database-Repositories.go rename to database/databaseRepositories.go diff --git a/database/Database-Resources.go b/database/databaseResources.go similarity index 100% rename from database/Database-Resources.go rename to database/databaseResources.go diff --git a/database/database.go b/database/databaseRoot.go similarity index 100% rename from database/database.go rename to database/databaseRoot.go diff --git a/database/Database-Sessions.go b/database/databaseSessions.go similarity index 100% rename from database/Database-Sessions.go rename to database/databaseSessions.go diff --git a/database/Database-Tokens.go b/database/databaseTokens.go similarity index 100% rename from database/Database-Tokens.go rename to database/databaseTokens.go diff --git a/database/Database-Users.go b/database/databaseUsers.go similarity index 100% rename from database/Database-Users.go rename to database/databaseUsers.go From 0427ee91f2552d32350757de6c1ec015aa32a833 Mon Sep 17 00:00:00 2001 From: Don Mennerich Date: Mon, 30 Sep 2024 07:24:14 -0400 Subject: [PATCH 02/13] updating version --- api/v0/apiRoot.go | 2 +- medialog.go | 2 +- templates/global/footer.html | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/api/v0/apiRoot.go b/api/v0/apiRoot.go index 6061b63..a2e002d 100644 --- a/api/v0/apiRoot.go +++ b/api/v0/apiRoot.go @@ -43,7 +43,7 @@ type SummaryTotalsAccession struct { const UNAUTHORIZED = "Please authenticate to access this service" const apiVersion = "v0.1.4" -const medialogVersion = "v1.0.7" +const medialogVersion = "v0.1.8" var ACCESS_DENIED = map[string]string{"error": "access denied"} diff --git a/medialog.go b/medialog.go index d927885..6e99226 100644 --- a/medialog.go +++ b/medialog.go @@ -25,7 +25,7 @@ var ( createAdmin bool ) -const version = "v1.0.7" +const version = "v1.0.8" func init() { flag.StringVar(&environment, "environment", "", "") diff --git a/templates/global/footer.html b/templates/global/footer.html index a4fac7c..0ef353b 100644 --- a/templates/global/footer.html +++ b/templates/global/footer.html @@ -4,7 +4,7 @@ From 854de5d55fdfe7e66ba26663d2a4836a28fd5782 Mon Sep 17 00:00:00 2001 From: Don Mennerich Date: Mon, 30 Sep 2024 07:48:01 -0400 Subject: [PATCH 03/13] creating route for report csv files --- controllers/reportsController.go | 6 +++++- router/routes.go | 7 ++++--- templates/reports/reports-menu.html | 1 + templates/reports/reports-range.html | 16 +++++++++++++++- templates/reports/reports-result.html | 1 + 5 files changed, 26 insertions(+), 5 deletions(-) diff --git a/controllers/reportsController.go b/controllers/reportsController.go index 8be66d3..945b9fc 100644 --- a/controllers/reportsController.go +++ b/controllers/reportsController.go @@ -45,7 +45,7 @@ func ReportsIndex(c *gin.Context) { }) } -func ReportRange(c *gin.Context) { +func ReportsRange(c *gin.Context) { if err := isLoggedIn(c); err != nil { ThrowError(http.StatusUnauthorized, err.Error(), c, false) @@ -100,6 +100,10 @@ func ReportRange(c *gin.Context) { } +func ReportsCSV(c *gin.Context) { + c.JSON(http.StatusOK, "Hello csv") +} + var months = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} var years = []int{2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024} //make s range from 2014 to current year var days = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31} diff --git a/router/routes.go b/router/routes.go index 81ea4cf..a7cf4b9 100644 --- a/router/routes.go +++ b/router/routes.go @@ -83,9 +83,10 @@ func LoadRoutes(router *gin.Engine) { userRoutes.GET(":id/revoke_api", func(c *gin.Context) { controllers.RevokeAPI(c) }) //Report Group - reportRoutes := router.Group("/reports") - reportRoutes.GET("", func(c *gin.Context) { controllers.ReportsIndex(c) }) - reportRoutes.POST("/range", func(c *gin.Context) { controllers.ReportRange(c) }) + reportsRoutes := router.Group("/reports") + reportsRoutes.GET("", func(c *gin.Context) { controllers.ReportsIndex(c) }) + reportsRoutes.POST("/range", func(c *gin.Context) { controllers.ReportsRange(c) }) + reportsRoutes.POST("/csv", func(c *gin.Context) { controllers.ReportsCSV(c) }) //Search Group searchRoutes := router.Group("/search") diff --git a/templates/reports/reports-menu.html b/templates/reports/reports-menu.html index 800b086..6fac504 100644 --- a/templates/reports/reports-menu.html +++ b/templates/reports/reports-menu.html @@ -52,6 +52,7 @@
Reports
repository
+ +
+
{{ template "summary-table.html" . }} diff --git a/templates/reports/reports-result.html b/templates/reports/reports-result.html index 2440c55..d1e5015 100644 --- a/templates/reports/reports-result.html +++ b/templates/reports/reports-result.html @@ -76,6 +76,7 @@
Reports
repository
+ + + + + + + +
From 68431e221cc66e6fbab97361170ef6e8121c9096 Mon Sep 17 00:00:00 2001 From: Don Mennerich Date: Mon, 30 Sep 2024 15:16:08 -0400 Subject: [PATCH 06/13] adding csv generation to reports interface --- controllers/accessionsController.go | 1 + controllers/reportsController.go | 11 +++++------ controllers/resourcesController.go | 1 + models/models.go | 6 +++--- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/controllers/accessionsController.go b/controllers/accessionsController.go index f16d305..e3f314f 100644 --- a/controllers/accessionsController.go +++ b/controllers/accessionsController.go @@ -570,5 +570,6 @@ func AccessionGenCSV(c *gin.Context) { c.Header("content-type", "text/csv") c.Header("Content-Description", "File Transfer") c.Header("Content-Disposition", "attachment; filename="+csvFileName) + c.Status(http.StatusOK) c.Writer.Write([]byte(csvBuffer.String())) } diff --git a/controllers/reportsController.go b/controllers/reportsController.go index 4f4bcb8..92eab28 100644 --- a/controllers/reportsController.go +++ b/controllers/reportsController.go @@ -120,19 +120,18 @@ func ReportsCSV(c *gin.Context) { csvBuffer := new(strings.Builder) var csvWriter = csv.NewWriter(csvBuffer) csvWriter.Write(models.CSVHeader) + csvWriter.Flush() for _, entry := range entries { - record := entry.ToCSV() - csvWriter.Write(record) + csvWriter.Write(entry.ToCSV()) + csvWriter.Flush() } - csvWriter.Flush() - csvFileName := fmt.Sprintf("%s.csv", "report") // make astring formatter for dateRage struct + csvFileName := fmt.Sprintf("%s.csv", "report") // make a string formatter for dateRage struct c.Header("content-type", "text/csv") c.Header("Content-Description", "File Transfer") c.Header("Content-Disposition", "attachment; filename="+csvFileName) + c.Status(http.StatusOK) c.Writer.Write([]byte(csvBuffer.String())) - - c.JSON(http.StatusOK, entries) } var months = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} diff --git a/controllers/resourcesController.go b/controllers/resourcesController.go index 404419d..a5062dd 100644 --- a/controllers/resourcesController.go +++ b/controllers/resourcesController.go @@ -397,5 +397,6 @@ func ResourceGenCSV(c *gin.Context) { c.Header("content-type", "text/csv") c.Header("Content-Description", "File Transfer") c.Header("Content-Disposition", "attachment; filename="+csvFileName) + c.Status(http.StatusOK) c.Writer.Write([]byte(csvBuffer.String())) } diff --git a/models/models.go b/models/models.go index e668703..c92c36a 100644 --- a/models/models.go +++ b/models/models.go @@ -2,7 +2,7 @@ package models import ( "fmt" - "regexp" + "strings" "time" "github.com/google/uuid" @@ -86,8 +86,8 @@ type Entry struct { var CSVHeader = []string{"id", "media_id", "mediatype", "content_type", "label_text", "is_refreshed", "imaging_success", "repository", "resource", "accession", "storage_location"} func (e Entry) ToCSV() []string { - re := regexp.MustCompile(`\r?\n`) - labelText := re.ReplaceAllString(e.LabelText, " ") + + labelText := strings.ReplaceAll(e.LabelText, "\n", " ") csv := []string{ e.ID.String(), From 26aba81e303249f5a80245627172f591fe35620e Mon Sep 17 00:00:00 2001 From: Don Mennerich Date: Mon, 7 Oct 2024 14:13:40 -0400 Subject: [PATCH 07/13] adding password toggle to login screen --- public/medialog.js | 8 ++++++++ templates/users/users-login.html | 6 +++++- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 public/medialog.js diff --git a/public/medialog.js b/public/medialog.js new file mode 100644 index 0000000..629115e --- /dev/null +++ b/public/medialog.js @@ -0,0 +1,8 @@ +function togglePassword() { + var passwd = document.getElementById("password_1"); + if (passwd.type === "password") { + passwd.type = "text"; + } else { + passwd.type = "password"; + } + } \ No newline at end of file diff --git a/templates/users/users-login.html b/templates/users/users-login.html index d5c2c25..47cab3d 100644 --- a/templates/users/users-login.html +++ b/templates/users/users-login.html @@ -35,10 +35,14 @@

Login

+ +
+
+
@@ -51,5 +55,5 @@

Login

- + {{ template "footer.html" . }} \ No newline at end of file From 33ca875edea826b1866e97a0dcfb2b6dad4e0c3c Mon Sep 17 00:00:00 2001 From: Don Mennerich Date: Tue, 8 Oct 2024 09:40:49 -0400 Subject: [PATCH 08/13] adding range summary to api --- api/v0/apiReports.go | 65 ++++++++++++++++++++++++++++++++++++++++++++ api_test.go | 18 ++++++++++++ router/routes.go | 3 ++ 3 files changed, 86 insertions(+) create mode 100644 api/v0/apiReports.go diff --git a/api/v0/apiReports.go b/api/v0/apiReports.go new file mode 100644 index 0000000..d88f485 --- /dev/null +++ b/api/v0/apiReports.go @@ -0,0 +1,65 @@ +package api + +import ( + "net/http" + "strconv" + + "github.com/gin-gonic/gin" + "github.com/nyudlts/bytemath" + "github.com/nyudlts/go-medialog/database" +) + +type SummaryAndTotal struct { + RepositoryID int `json:"repository_id"` + Repository string `json:"repository` + Total int64 `json:"total_Size"` + TotalHuman string `json:"total_human_size"` + Summary database.Summaries `json:"summary"` +} + +func SummaryDateRange(c *gin.Context) { + if _, err := checkToken(c); err != nil { + c.JSON(http.StatusUnauthorized, err.Error()) + return + } + + //get the start and end dates + startDate := c.Query("start_date") + endDate := c.Query("end_date") + + var dr = database.DateRange{} + 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]) + + entries, err := database.GetSummaryByDateRange(dr) + if err != nil { + c.JSON(http.StatusInternalServerError, err.Error()) + return + } + + repository, err := database.FindRepository(uint(dr.RepositoryID)) + if err != nil { + c.JSON(http.StatusInternalServerError, err.Error()) + return + } + + var totalSize int64 = 0 + for _, hit := range entries.GetSlice() { + totalSize = totalSize + int64(hit.Size) + } + + f64 := float64(totalSize) + tsize := bytemath.ConvertToBytes(f64, bytemath.B) + humanSize := bytemath.ConvertBytesToHumanReadable(int64(tsize)) + + c.JSON(http.StatusOK, SummaryAndTotal{Repository: repository.Slug, Total: totalSize, Summary: entries, TotalHuman: humanSize, RepositoryID: dr.RepositoryID}) +} diff --git a/api_test.go b/api_test.go index dd98691..0e13d96 100644 --- a/api_test.go +++ b/api_test.go @@ -464,6 +464,24 @@ func TestAPI(t *testing.T) { assert.Equal(t, "application/json; charset=utf-8", recorder.Header().Get("content-type")) }) + //report functions + t.Run("test get summary of range", func(t *testing.T) { + recorder := httptest.NewRecorder() + c, _ := gin.CreateTestContext(recorder) + url := fmt.Sprintf("%s/reports/range?start_date=%s&end_date=%s&repository_id=%d", APIROOT, "20140101", "20241031", repository.ID) + req, err := http.NewRequestWithContext(c, "GET", url, nil) + if err != nil { + t.Error(err) + } + req.Header.Add("X-Medialog-Token", token) + r.ServeHTTP(recorder, req) + assert.Equal(t, 200, recorder.Code) + assert.Equal(t, "application/json; charset=utf-8", recorder.Header().Get("content-type")) + + body, _ := io.ReadAll(recorder.Body) + t.Logf("%s", string(body)) + }) + //delete functions t.Run("test delete an entry", func(t *testing.T) { diff --git a/router/routes.go b/router/routes.go index a7cf4b9..3eeda35 100644 --- a/router/routes.go +++ b/router/routes.go @@ -150,6 +150,9 @@ func LoadAPI(router *gin.Engine) { //sessions apiV0Routes.DELETE("delete_sessions", func(c *gin.Context) { api.DeleteSessionsV0(c) }) + + //reports + apiV0Routes.GET("reports/range", func(c *gin.Context) { api.SummaryDateRange(c) }) } func Test(c *gin.Context) { From 4249df4ae0cc69910cbb67a5cb005e5eef29ac58 Mon Sep 17 00:00:00 2001 From: Don Mennerich Date: Tue, 8 Oct 2024 11:23:41 -0400 Subject: [PATCH 09/13] adding is_refreshed bool to daterange type --- api/v0/apiReports.go | 49 +++++++++++++++++++++++++++++++------ api_test.go | 36 ++++++++++++++++++++++++++- database/databaseEntries.go | 27 ++++++++++++++------ database/databaseRoot.go | 1 + 4 files changed, 97 insertions(+), 16 deletions(-) diff --git a/api/v0/apiReports.go b/api/v0/apiReports.go index d88f485..730db6b 100644 --- a/api/v0/apiReports.go +++ b/api/v0/apiReports.go @@ -1,7 +1,9 @@ package api import ( + "fmt" "net/http" + "regexp" "strconv" "github.com/gin-gonic/gin" @@ -14,7 +16,7 @@ type SummaryAndTotal struct { Repository string `json:"repository` Total int64 `json:"total_Size"` TotalHuman string `json:"total_human_size"` - Summary database.Summaries `json:"summary"` + Summaries database.Summaries `json:"summaries"` } func SummaryDateRange(c *gin.Context) { @@ -24,15 +26,39 @@ func SummaryDateRange(c *gin.Context) { } //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, fmt.Errorf("invalid start_date")) + return + } + + if !datePattern.MatchString(endDate) { + c.JSON(http.StatusBadRequest, fmt.Errorf("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]) @@ -40,26 +66,33 @@ func SummaryDateRange(c *gin.Context) { dr.EndMonth, _ = strconv.Atoi(endDate[4:6]) dr.EndDay, _ = strconv.Atoi(endDate[6:8]) - entries, err := database.GetSummaryByDateRange(dr) + summaries, err := database.GetSummaryByDateRange(dr) if err != nil { c.JSON(http.StatusInternalServerError, err.Error()) return } - repository, err := database.FindRepository(uint(dr.RepositoryID)) - 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 - for _, hit := range entries.GetSlice() { + for _, hit := range summaries.GetSlice() { totalSize = totalSize + int64(hit.Size) } + //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, SummaryAndTotal{Repository: repository.Slug, Total: totalSize, Summary: entries, TotalHuman: humanSize, RepositoryID: dr.RepositoryID}) + c.JSON(http.StatusOK, SummaryAndTotal{Repository: slug, Total: totalSize, Summaries: summaries, TotalHuman: humanSize, RepositoryID: dr.RepositoryID}) } diff --git a/api_test.go b/api_test.go index 0e13d96..b2c047f 100644 --- a/api_test.go +++ b/api_test.go @@ -12,6 +12,7 @@ import ( "testing" "github.com/gin-gonic/gin" + "github.com/nyudlts/go-medialog/api/v0" "github.com/nyudlts/go-medialog/models" router "github.com/nyudlts/go-medialog/router" "github.com/stretchr/testify/assert" @@ -478,8 +479,41 @@ func TestAPI(t *testing.T) { assert.Equal(t, 200, recorder.Code) assert.Equal(t, "application/json; charset=utf-8", recorder.Header().Get("content-type")) + var summary = api.SummaryAndTotal{} body, _ := io.ReadAll(recorder.Body) - t.Logf("%s", string(body)) + + //ensure the summary has one entry + if err := json.Unmarshal(body, &summary); err != nil { + c.JSON(http.StatusInternalServerError, err.Error()) + } + + assert.Equal(t, 1, len(summary.Summaries)) + + }) + + t.Run("test get summary of range refreshed only", func(t *testing.T) { + recorder := httptest.NewRecorder() + c, _ := gin.CreateTestContext(recorder) + url := fmt.Sprintf("%s/reports/range?start_date=%s&end_date=%s&is_refreshed=true", APIROOT, "20140101", "20241031") + req, err := http.NewRequestWithContext(c, "GET", url, nil) + if err != nil { + t.Error(err) + } + req.Header.Add("X-Medialog-Token", token) + r.ServeHTTP(recorder, req) + assert.Equal(t, 200, recorder.Code) + assert.Equal(t, "application/json; charset=utf-8", recorder.Header().Get("content-type")) + + var summary = api.SummaryAndTotal{} + body, _ := io.ReadAll(recorder.Body) + + //ensure the summary has one entry + if err := json.Unmarshal(body, &summary); err != nil { + c.JSON(http.StatusInternalServerError, err.Error()) + } + + assert.Equal(t, 0, len(summary.Summaries)) + }) //delete functions diff --git a/database/databaseEntries.go b/database/databaseEntries.go index 8371524..a3ed104 100644 --- a/database/databaseEntries.go +++ b/database/databaseEntries.go @@ -266,21 +266,34 @@ func GetEntriesByDateRange(dr DateRange) ([]models.Entry, error) { } func GetSummaryByDateRange(dr DateRange) (Summaries, error) { + startDate := fmt.Sprintf("%d-%d-%dT00:00:00Z", dr.StartYear, dr.StartMonth, dr.StartDay) endDate := fmt.Sprintf("%d-%d-%dT23:59:59Z", dr.EndYear, dr.EndMonth, dr.EndDay) entries := []models.Entry{} - if dr.RepositoryID == 0 { - if err := db.Where("created_at BETWEEN ? AND ?", startDate, endDate).Find(&entries).Error; err != nil { - return Summaries{}, err + //this needs to be simplified + if dr.IsRefreshed { + if dr.RepositoryID == 0 { + if err := db.Where("created_at BETWEEN ? AND ?", startDate, endDate).Where("is_refreshed = true").Find(&entries).Error; err != nil { + return Summaries{}, err + } + } else { + if err := db.Where("repository_id = ?", dr.RepositoryID).Where("created_at BETWEEN ? AND ?", startDate, endDate).Where("is_refreshed = true").Find(&entries).Error; err != nil { + return Summaries{}, err + } } - return getSummary(entries), nil } else { - if err := db.Where("repository_id = ?", dr.RepositoryID).Where("created_at BETWEEN ? AND ?", startDate, endDate).Find(&entries).Error; err != nil { - return Summaries{}, err + if dr.RepositoryID == 0 { + if err := db.Where("created_at BETWEEN ? AND ?", startDate, endDate).Find(&entries).Error; err != nil { + return Summaries{}, err + } + } else { + if err := db.Where("repository_id = ?", dr.RepositoryID).Where("created_at BETWEEN ? AND ?", startDate, endDate).Find(&entries).Error; err != nil { + return Summaries{}, err + } } - return getSummary(entries), nil } + return getSummary(entries), nil } func summaryContains(summaries Summaries, mediatype string) bool { diff --git a/database/databaseRoot.go b/database/databaseRoot.go index 36452e0..f1b3e9e 100644 --- a/database/databaseRoot.go +++ b/database/databaseRoot.go @@ -24,6 +24,7 @@ type DateRange struct { EndMonth int `form:"end-month"` EndDay int `form:"end-day"` RepositoryID int `form:"repository-id"` + IsRefreshed bool } func ConnectMySQL(dbconfig models.DatabaseConfig, gormDebug bool) error { From 79f0dcc57e55513081082f018f4950b05e3bfb94 Mon Sep 17 00:00:00 2001 From: Don Mennerich Date: Tue, 8 Oct 2024 13:19:02 -0400 Subject: [PATCH 10/13] altering daterange regex --- api/v0/apiReports.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/v0/apiReports.go b/api/v0/apiReports.go index 730db6b..eda64d2 100644 --- a/api/v0/apiReports.go +++ b/api/v0/apiReports.go @@ -26,7 +26,7 @@ func SummaryDateRange(c *gin.Context) { } //get the start and end dates - datePattern := regexp.MustCompile("[0-9]{8}") + datePattern := regexp.MustCompile("^[0-9]{8}$") startDate := c.Query("start_date") endDate := c.Query("end_date") From d3a44df9ea0497e0d142757a49aa96125d647f9b Mon Sep 17 00:00:00 2001 From: Don Mennerich Date: Tue, 8 Oct 2024 13:27:36 -0400 Subject: [PATCH 11/13] adding total count to --- api/v0/apiReports.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/api/v0/apiReports.go b/api/v0/apiReports.go index eda64d2..a953725 100644 --- a/api/v0/apiReports.go +++ b/api/v0/apiReports.go @@ -11,10 +11,11 @@ import ( "github.com/nyudlts/go-medialog/database" ) -type SummaryAndTotal struct { +type SummaryAndTotals struct { RepositoryID int `json:"repository_id"` - Repository string `json:"repository` - Total int64 `json:"total_Size"` + 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"` } @@ -85,8 +86,10 @@ func SummaryDateRange(c *gin.Context) { } var totalSize int64 = 0 + var totalCount int = 0 for _, hit := range summaries.GetSlice() { totalSize = totalSize + int64(hit.Size) + totalCount++ } //convert the total size to a human readable format @@ -94,5 +97,12 @@ func SummaryDateRange(c *gin.Context) { tsize := bytemath.ConvertToBytes(f64, bytemath.B) humanSize := bytemath.ConvertBytesToHumanReadable(int64(tsize)) - c.JSON(http.StatusOK, SummaryAndTotal{Repository: slug, Total: totalSize, Summaries: summaries, TotalHuman: humanSize, RepositoryID: dr.RepositoryID}) + c.JSON(http.StatusOK, SummaryAndTotals{ + RepositoryID: dr.RepositoryID, + Repository: slug, + TotalCount: totalCount, + TotalSize: totalSize, + TotalHuman: humanSize, + Summaries: summaries, + }) } From 052f65db4ab15d68c9d5ab300d870ed8170e6efe Mon Sep 17 00:00:00 2001 From: Don Mennerich Date: Tue, 8 Oct 2024 13:35:36 -0400 Subject: [PATCH 12/13] fixing count --- api/v0/apiReports.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/v0/apiReports.go b/api/v0/apiReports.go index a953725..1a3b8ee 100644 --- a/api/v0/apiReports.go +++ b/api/v0/apiReports.go @@ -89,7 +89,7 @@ func SummaryDateRange(c *gin.Context) { var totalCount int = 0 for _, hit := range summaries.GetSlice() { totalSize = totalSize + int64(hit.Size) - totalCount++ + totalCount = totalCount + hit.Count } //convert the total size to a human readable format From 2567c570925e62a298597203177c3337c7f92440 Mon Sep 17 00:00:00 2001 From: Don Mennerich Date: Tue, 8 Oct 2024 13:53:17 -0400 Subject: [PATCH 13/13] changing error messages --- api/v0/apiReports.go | 5 ++--- api_test.go | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/api/v0/apiReports.go b/api/v0/apiReports.go index 1a3b8ee..ab10612 100644 --- a/api/v0/apiReports.go +++ b/api/v0/apiReports.go @@ -1,7 +1,6 @@ package api import ( - "fmt" "net/http" "regexp" "strconv" @@ -33,12 +32,12 @@ func SummaryDateRange(c *gin.Context) { //validate start and end dates if !datePattern.MatchString(startDate) { - c.JSON(http.StatusBadRequest, fmt.Errorf("invalid start_date")) + c.JSON(http.StatusBadRequest, "invalid start_date") return } if !datePattern.MatchString(endDate) { - c.JSON(http.StatusBadRequest, fmt.Errorf("invalid end_date")) + c.JSON(http.StatusBadRequest, "invalid end_date") return } diff --git a/api_test.go b/api_test.go index b2c047f..e42c1c4 100644 --- a/api_test.go +++ b/api_test.go @@ -479,7 +479,7 @@ func TestAPI(t *testing.T) { assert.Equal(t, 200, recorder.Code) assert.Equal(t, "application/json; charset=utf-8", recorder.Header().Get("content-type")) - var summary = api.SummaryAndTotal{} + var summary = api.SummaryAndTotals{} body, _ := io.ReadAll(recorder.Body) //ensure the summary has one entry @@ -504,7 +504,7 @@ func TestAPI(t *testing.T) { assert.Equal(t, 200, recorder.Code) assert.Equal(t, "application/json; charset=utf-8", recorder.Header().Get("content-type")) - var summary = api.SummaryAndTotal{} + var summary = api.SummaryAndTotals{} body, _ := io.ReadAll(recorder.Body) //ensure the summary has one entry