Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dawn-gdd
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
dawn
go-backend
dawn-gdd
Commits
1492c739
Commit
1492c739
authored
2 years ago
by
Tucker Gary Siegel
Browse files
Options
Downloads
Patches
Plain Diff
add planting date parameter for gdds
parent
95ba8786
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
controllers/gdd_controller.go
+50
-49
50 additions, 49 deletions
controllers/gdd_controller.go
models/gdd.go
+100
-92
100 additions, 92 deletions
models/gdd.go
services/gdd_service.go
+7
-0
7 additions, 0 deletions
services/gdd_service.go
with
157 additions
and
141 deletions
controllers/gdd_controller.go
+
50
−
49
View file @
1492c739
package
controllers
import
(
"github.com/tgs266/dawn-go-common/common"
"gitlab.cs.umd.edu/dawn/go-backend/dawn-gdd/models"
"gitlab.cs.umd.edu/dawn/go-backend/dawn-gdd/services"
"github.com/gofiber/fiber/v2"
)
var
GetGddValues
=
services
.
GetGddValues
// GetDailyGdd godoc
// @Summary Get gdd values
// @Tags Gdd
// @Description get gdd values
// @Accept json
// @Produce json
// @Success 200 {object} models.GddResponse
// @Failure 400 {object} common.StandardError
// @Param year query int true "Year to get gdd for"
// @Param product query string true "Crop to calculate gdd for" Enums(corn, soybean, sunflower, tomato, sugar_beet, peanut, cotton, potato, wheat, pea, oat, spring_wheat, rice, sorghum)
// @Param latitude query number true "Latitude to search for"
// @Param longitude query number true "Longitude to search for"
// @Param accumulate query boolean true "Accumulate gdd values"
// @Router /gdd/daily [get]
func
GetDailyGdd
(
c
*
fiber
.
Ctx
)
error
{
ctx
:=
common
.
DawnCtx
{
FiberCtx
:
c
}
request
:=
models
.
BuildGddRequest
(
ctx
.
FiberCtx
)
return
c
.
Status
(
fiber
.
StatusOK
)
.
JSON
(
GetGddValues
(
ctx
,
request
))
}
// GetNormalGdd godoc
// @Summary Get gdd normals
// @Tags Gdd
// @Description get gdd normals
// @Accept json
// @Produce json
// @Success 200 {object} models.GddResponse
// @Failure 400 {object} common.StandardError
// @Param product query string true "Crop to calculate gdd for" Enums(corn, soybean, sunflower, tomato, sugar_beet, peanut, cotton, potato, wheat, pea, oat, spring_wheat, rice, sorghum)
// @Param latitude query number true "Latitude to search for"
// @Param longitude query number true "Longitude to search for"
// @Param accumulate query boolean true "Accumulate gdd values"
// @Router /gdd/normals [get]
func
GetNormalGdd
(
c
*
fiber
.
Ctx
)
error
{
request
:=
models
.
BuildGddRequest
(
c
)
return
c
.
Status
(
fiber
.
StatusOK
)
.
JSON
(
services
.
GetNormalValues
(
request
))
}
package
controllers
import
(
"github.com/tgs266/dawn-go-common/common"
"gitlab.cs.umd.edu/dawn/go-backend/dawn-gdd/models"
"gitlab.cs.umd.edu/dawn/go-backend/dawn-gdd/services"
"github.com/gofiber/fiber/v2"
)
var
GetGddValues
=
services
.
GetGddValues
// GetDailyGdd godoc
// @Summary Get gdd values
// @Tags Gdd
// @Description get gdd values
// @Accept json
// @Produce json
// @Success 200 {object} models.GddResponse
// @Failure 400 {object} common.StandardError
// @Param year query int true "Year to get gdd for"
// @Param product query string true "Crop to calculate gdd for" Enums(corn, soybean, sunflower, tomato, sugar_beet, peanut, cotton, potato, wheat, pea, oat, spring_wheat, rice, sorghum)
// @Param latitude query number true "Latitude to search for"
// @Param longitude query number true "Longitude to search for"
// @Param accumulate query boolean true "Accumulate gdd values"
// @Param plantingDate query string true "Plant date, ISO8601 or RFC3339 format"
// @Router /gdd/daily [get]
func
GetDailyGdd
(
c
*
fiber
.
Ctx
)
error
{
ctx
:=
common
.
DawnCtx
{
FiberCtx
:
c
}
request
:=
models
.
BuildGddRequest
(
ctx
.
FiberCtx
)
return
c
.
Status
(
fiber
.
StatusOK
)
.
JSON
(
GetGddValues
(
ctx
,
request
))
}
// GetNormalGdd godoc
// @Summary Get gdd normals
// @Tags Gdd
// @Description get gdd normals
// @Accept json
// @Produce json
// @Success 200 {object} models.GddResponse
// @Failure 400 {object} common.StandardError
// @Param product query string true "Crop to calculate gdd for" Enums(corn, soybean, sunflower, tomato, sugar_beet, peanut, cotton, potato, wheat, pea, oat, spring_wheat, rice, sorghum)
// @Param latitude query number true "Latitude to search for"
// @Param longitude query number true "Longitude to search for"
// @Param accumulate query boolean true "Accumulate gdd values"
// @Router /gdd/normals [get]
func
GetNormalGdd
(
c
*
fiber
.
Ctx
)
error
{
request
:=
models
.
BuildGddRequest
(
c
)
return
c
.
Status
(
fiber
.
StatusOK
)
.
JSON
(
services
.
GetNormalValues
(
request
))
}
This diff is collapsed.
Click to expand it.
models/gdd.go
+
100
−
92
View file @
1492c739
package
models
import
(
"strconv"
"time"
"gitlab.cs.umd.edu/dawn/go-backend/dawn-gdd/config"
"gitlab.cs.umd.edu/dawn/go-backend/dawn-gdd/persistence/entities"
validation
"github.com/go-ozzo/ozzo-validation"
"github.com/gofiber/fiber/v2"
)
type
GddResponse
struct
{
Product
string
`json:"product"`
ClosestLatitude
float64
`json:"closest_latitude"`
ClosestLongitude
float64
`json:"closest_longitude"`
GddValues
[]
float64
`json:"gdd_values"`
LastDate
time
.
Time
`json:"last_date"`
}
type
GddRequest
struct
{
Year
int
`json:"year"`
Product
string
`json:"product"`
Latitude
float64
`json:"latitude"`
Longitude
float64
`json:"longitude"`
Accumulate
bool
`json:"accumulate"`
}
func
(
r
GddRequest
)
Validate
()
error
{
return
validation
.
ValidateStruct
(
&
r
,
validation
.
Field
(
&
r
.
Year
,
validation
.
Required
,
validation
.
Min
(
1981
),
validation
.
Max
(
time
.
Now
()
.
Year
())),
validation
.
Field
(
&
r
.
Product
,
validation
.
Required
,
validation
.
Length
(
1
,
100
)),
validation
.
Field
(
&
r
.
Latitude
,
validation
.
Required
,
validation
.
Min
(
-
90.0
),
validation
.
Max
(
90.0
)),
validation
.
Field
(
&
r
.
Longitude
,
validation
.
Required
,
validation
.
Min
(
-
180.0
),
validation
.
Max
(
180.0
)),
)
}
func
(
r
GddRequest
)
ValidateNoYear
()
error
{
return
validation
.
ValidateStruct
(
&
r
,
validation
.
Field
(
&
r
.
Product
,
validation
.
Required
,
validation
.
Length
(
1
,
100
)),
validation
.
Field
(
&
r
.
Latitude
,
validation
.
Required
,
validation
.
Min
(
-
90.0
),
validation
.
Max
(
90.0
)),
validation
.
Field
(
&
r
.
Longitude
,
validation
.
Required
,
validation
.
Min
(
-
180.0
),
validation
.
Max
(
180.0
)),
)
}
var
BuildGddRequest
=
func
(
c
*
fiber
.
Ctx
)
GddRequest
{
year
,
errYear
:=
strconv
.
Atoi
(
c
.
Query
(
"year"
,
"0"
))
product
:=
c
.
Query
(
"product"
)
latitude
,
errLat
:=
strconv
.
ParseFloat
(
c
.
Query
(
"latitude"
),
64
)
longitude
,
errLon
:=
strconv
.
ParseFloat
(
c
.
Query
(
"longitude"
),
64
)
accumulate
,
errBool
:=
strconv
.
ParseBool
(
c
.
Query
(
"accumulate"
,
"false"
))
rNew
:=
GddRequest
{
Year
:
year
,
Product
:
product
,
Latitude
:
latitude
,
Longitude
:
longitude
,
Accumulate
:
accumulate
,
}
if
errYear
!=
nil
||
errLat
!=
nil
||
errLon
!=
nil
||
errBool
!=
nil
{
panic
(
config
.
BAD_REQUEST
)
}
if
rNew
.
Year
!=
0
{
if
e
:=
rNew
.
Validate
();
e
!=
nil
{
panic
(
config
.
BAD_REQUEST
.
AddLogDetails
(
e
.
Error
()))
}
}
else
{
if
e
:=
rNew
.
ValidateNoYear
();
e
!=
nil
{
panic
(
config
.
BAD_REQUEST
.
AddLogDetails
(
e
.
Error
()))
}
}
return
rNew
}
func
(
r
GddRequest
)
BuildLocation
()
entities
.
Location
{
l
:=
entities
.
Location
{
Type
:
"Point"
,
Coordinates
:
[]
float64
{
r
.
Longitude
,
r
.
Latitude
},
}
return
l
}
func
BuildLocation
(
lat
float64
,
lng
float64
)
entities
.
Location
{
l
:=
entities
.
Location
{
Type
:
"Point"
,
Coordinates
:
[]
float64
{
lng
,
lat
},
}
return
l
}
package
models
import
(
"strconv"
"time"
"gitlab.cs.umd.edu/dawn/go-backend/dawn-gdd/config"
"gitlab.cs.umd.edu/dawn/go-backend/dawn-gdd/persistence/entities"
validation
"github.com/go-ozzo/ozzo-validation"
"github.com/gofiber/fiber/v2"
)
type
GddResponse
struct
{
Product
string
`json:"product"`
ClosestLatitude
float64
`json:"closest_latitude"`
ClosestLongitude
float64
`json:"closest_longitude"`
GddValues
[]
float64
`json:"gdd_values"`
LastDate
time
.
Time
`json:"last_date"`
}
type
GddRequest
struct
{
Year
int
`json:"year"`
Product
string
`json:"product"`
Latitude
float64
`json:"latitude"`
Longitude
float64
`json:"longitude"`
Accumulate
bool
`json:"accumulate"`
PlantingDate
time
.
Time
`json:"plantingDate"`
}
func
(
r
GddRequest
)
Validate
()
error
{
return
validation
.
ValidateStruct
(
&
r
,
validation
.
Field
(
&
r
.
Year
,
validation
.
Required
,
validation
.
Min
(
1981
),
validation
.
Max
(
time
.
Now
()
.
Year
())),
validation
.
Field
(
&
r
.
Product
,
validation
.
Required
,
validation
.
Length
(
1
,
100
)),
validation
.
Field
(
&
r
.
Latitude
,
validation
.
Required
,
validation
.
Min
(
-
90.0
),
validation
.
Max
(
90.0
)),
validation
.
Field
(
&
r
.
Longitude
,
validation
.
Required
,
validation
.
Min
(
-
180.0
),
validation
.
Max
(
180.0
)),
)
}
func
(
r
GddRequest
)
ValidateNoYear
()
error
{
return
validation
.
ValidateStruct
(
&
r
,
validation
.
Field
(
&
r
.
Product
,
validation
.
Required
,
validation
.
Length
(
1
,
100
)),
validation
.
Field
(
&
r
.
Latitude
,
validation
.
Required
,
validation
.
Min
(
-
90.0
),
validation
.
Max
(
90.0
)),
validation
.
Field
(
&
r
.
Longitude
,
validation
.
Required
,
validation
.
Min
(
-
180.0
),
validation
.
Max
(
180.0
)),
)
}
var
BuildGddRequest
=
func
(
c
*
fiber
.
Ctx
)
GddRequest
{
year
,
errYear
:=
strconv
.
Atoi
(
c
.
Query
(
"year"
,
strconv
.
Itoa
(
time
.
Now
()
.
Year
())))
product
:=
c
.
Query
(
"product"
)
pd
:=
c
.
Query
(
"plantingDate"
)
latitude
,
errLat
:=
strconv
.
ParseFloat
(
c
.
Query
(
"latitude"
),
64
)
longitude
,
errLon
:=
strconv
.
ParseFloat
(
c
.
Query
(
"longitude"
),
64
)
accumulate
,
errBool
:=
strconv
.
ParseBool
(
c
.
Query
(
"accumulate"
,
"false"
))
plantingDate
,
errDate
:=
time
.
Parse
(
time
.
RFC3339
,
pd
)
rNew
:=
GddRequest
{
Year
:
year
,
Product
:
product
,
Latitude
:
latitude
,
Longitude
:
longitude
,
Accumulate
:
accumulate
,
PlantingDate
:
plantingDate
,
}
if
errYear
!=
nil
||
errLat
!=
nil
||
errLon
!=
nil
||
errBool
!=
nil
{
panic
(
config
.
BAD_REQUEST
)
}
if
errDate
!=
nil
&&
pd
!=
""
{
panic
(
config
.
BAD_REQUEST
.
PutDetail
(
"reason"
,
"date must be ISO8601 or RFC3339 format"
))
}
if
rNew
.
Year
!=
0
{
if
e
:=
rNew
.
Validate
();
e
!=
nil
{
panic
(
config
.
BAD_REQUEST
.
AddLogDetails
(
e
.
Error
()))
}
}
else
{
if
e
:=
rNew
.
ValidateNoYear
();
e
!=
nil
{
panic
(
config
.
BAD_REQUEST
.
AddLogDetails
(
e
.
Error
()))
}
}
return
rNew
}
func
(
r
GddRequest
)
BuildLocation
()
entities
.
Location
{
l
:=
entities
.
Location
{
Type
:
"Point"
,
Coordinates
:
[]
float64
{
r
.
Longitude
,
r
.
Latitude
},
}
return
l
}
func
BuildLocation
(
lat
float64
,
lng
float64
)
entities
.
Location
{
l
:=
entities
.
Location
{
Type
:
"Point"
,
Coordinates
:
[]
float64
{
lng
,
lat
},
}
return
l
}
This diff is collapsed.
Click to expand it.
services/gdd_service.go
+
7
−
0
View file @
1492c739
...
...
@@ -41,6 +41,13 @@ func GetGddValues(ctx common.DawnCtx, request models.GddRequest) models.GddRespo
}
else
{
gdds
=
persistence
.
GddFindFirstByYearAndLocation
(
request
.
Year
,
request
.
BuildLocation
())
}
if
!
request
.
PlantingDate
.
IsZero
()
&&
request
.
Year
>=
time
.
Now
()
.
Year
()
{
pdInt
:=
request
.
PlantingDate
.
YearDay
()
-
1
gdds
.
MaxTemps
=
gdds
.
MaxTemps
[
pdInt
:
]
gdds
.
MinTemps
=
gdds
.
MinTemps
[
pdInt
:
]
}
returnGdds
:=
models
.
GddResponse
{
Product
:
product
.
Name
,
ClosestLatitude
:
gdds
.
Location
.
Coordinates
[
1
],
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment