diff --git a/README.md b/README.md index 577b6a6da78997c585e4a51815c6011f41151825..429fc581e6756760457c15726991208d03711da7 100644 --- a/README.md +++ b/README.md @@ -2,14 +2,9 @@ Used to serve gdd, cfs, and gefs data, and can be expanded to more data -Still need to add CFS/GEFS, and confidence intervals - -In the future, the errors folder might be moved to a common package when more go services are built to have shared error -handling across all services - To run: `go run main.go` Build to executable: `go build` -To rebuild swagger: `swag init` +To rebuild swagger: `swag init --parseDependency --parseDepth 2` ## Testing @@ -20,4 +15,4 @@ Swagger [Here](http://localhost:8080/api/weather/gdd-swagger/index.html#/) ## Docker -To build the image: `docker build -t dawn/gitlab.cs.umd.edu/dawn/go-backend/dawn-gdd .` \ No newline at end of file +To build the image: `dawn-cli build-image -p .` \ No newline at end of file diff --git a/config/error_codes.go b/config/error_codes.go index 815d1a5c50b02806bc480fab743dcb597bafa782..e0eac4adaa4633b3f20e43906b0c76dc6f251f71 100644 --- a/config/error_codes.go +++ b/config/error_codes.go @@ -32,6 +32,12 @@ var NO_DATA_FOUND = &common.DawnError{ // 500s +var DATABSE_CONNECTION_ERROR = &common.DawnError{ + Name: "DATABSE_CONNECTION_ERROR", + Description: "Cannot connect to database", + Code: 500, +} + var INTERNAL_SERVER_STANDARD_ERROR = &common.DawnError{ Name: "INTERNAL_SERVER_ERROR", Description: "Unkown internal server error occurred", diff --git a/controllers/routes.go b/controllers/routes.go index 2df6c3bc8261237d7d99dc4b5a25ee4a256fca3b..727996df6ba7d9ae1b3160d6cd1756672d3ef86c 100644 --- a/controllers/routes.go +++ b/controllers/routes.go @@ -16,6 +16,6 @@ func GddRoutes(route fiber.Router) { route.Get("gdd/csv", GetCSVFile) - route.Get("gdd/seeds/corn", GetCornSeedMaturityDate) + route.Get("gdd/maturity/corn", GetCornSeedMaturityDate) route.Get("gdd/seeds", GetSeedList) } diff --git a/controllers/seed_controllers.go b/controllers/seed_controllers.go index 9e5ff9a1292bf68742eac25e64ba02a36a3ea039..d6e28eed1fdec3d5833df7a71cdcf0dd5129cfb7 100644 --- a/controllers/seed_controllers.go +++ b/controllers/seed_controllers.go @@ -8,16 +8,6 @@ import ( "github.com/gofiber/fiber/v2" ) -// GetSeedList godoc -// @Summary Get list of seeds in database -// @Tags GDD Seed Data -// @Description Get list of seeds in database -// @Accept json -// @Produce json -// @Success 200 {object} models.SeedListResponse -// @Failure 400 {object} common.StandardError -// @Param product query string true "Crop type to use" Enums(corn, soybean) -// @Router /api/weather/gdd/seeds [get] func GetSeedList(c *fiber.Ctx) error { request := models.SeedListRequest{Product: c.Query("product")} return c.Status(fiber.StatusOK).JSON( @@ -26,19 +16,19 @@ func GetSeedList(c *fiber.Ctx) error { } // GetCornSeedMaturityDate godoc -// @Summary Get estimated maturity date from corn seed (uses CFS data if current GDUs are less than the crop maturity) -// @Tags GDD Seed Data -// @Description Get estimated maturity date from corn seed +// @Summary Get estimated maturity date from given gdd (uses CFS data if current GDUs are less than the crop maturity) +// @Tags GDD Maturity Data +// @Description Get estimated maturity date from given gdd // @Accept json // @Produce json // @Success 200 {object} models.CornMaturityResponse // @Failure 400 {object} common.StandardError // @Param latitude query number true "Latitude to search for" // @Param longitude query number true "Longitude to search for" -// @Param seed query string true "Corn seed to use" +// @Param gdds query number true "number of gdds given" // @Param month query number true "month planted" // @Param date query number true "date planted" -// @Router /api/weather/gdd/seeds/corn [get] +// @Router /api/weather/gdd/maturity/corn [get] func GetCornSeedMaturityDate(c *fiber.Ctx) error { ctx := common.DawnCtx{FiberCtx: c} request := models.BuildCornMaturityRequest(c) diff --git a/docs/docs.go b/docs/docs.go index 3645be82c166cf180c813a7dfad3d58cd327939c..f978dfd0ffc16758b6b88ca45dd764a94b7a7862 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -581,9 +581,9 @@ var doc = `{ } } }, - "/api/weather/gdd/normals": { + "/api/weather/gdd/maturity/corn": { "get": { - "description": "get gdd normals", + "description": "Get estimated maturity date from given gdd", "consumes": [ "application/json" ], @@ -591,33 +591,10 @@ var doc = `{ "application/json" ], "tags": [ - "Gdd" + "GDD Maturity Data" ], - "summary": "Get gdd normals", + "summary": "Get estimated maturity date from given gdd (uses CFS data if current GDUs are less than the crop maturity)", "parameters": [ - { - "enum": [ - "corn", - "soybean", - "sunflower", - "tomato", - "sugar_beet", - "peanut", - "cotton", - "potato", - "wheat", - "pea", - "oat", - "spring_wheat", - "rice", - "sorghum" - ], - "type": "string", - "description": "Crop to calculate gdd for", - "name": "product", - "in": "query", - "required": true - }, { "type": "number", "description": "Latitude to search for", @@ -633,9 +610,23 @@ var doc = `{ "required": true }, { - "type": "boolean", - "description": "Accumulate gdd values", - "name": "accumulate", + "type": "number", + "description": "number of gdds given", + "name": "gdds", + "in": "query", + "required": true + }, + { + "type": "number", + "description": "month planted", + "name": "month", + "in": "query", + "required": true + }, + { + "type": "number", + "description": "date planted", + "name": "date", "in": "query", "required": true } @@ -644,7 +635,7 @@ var doc = `{ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.GddResponse" + "$ref": "#/definitions/models.CornMaturityResponse" } }, "400": { @@ -656,9 +647,9 @@ var doc = `{ } } }, - "/api/weather/gdd/seeds": { + "/api/weather/gdd/normals": { "get": { - "description": "Get list of seeds in database", + "description": "get gdd normals", "consumes": [ "application/json" ], @@ -666,52 +657,33 @@ var doc = `{ "application/json" ], "tags": [ - "GDD Seed Data" + "Gdd" ], - "summary": "Get list of seeds in database", + "summary": "Get gdd normals", "parameters": [ { "enum": [ "corn", - "soybean" + "soybean", + "sunflower", + "tomato", + "sugar_beet", + "peanut", + "cotton", + "potato", + "wheat", + "pea", + "oat", + "spring_wheat", + "rice", + "sorghum" ], "type": "string", - "description": "Crop type to use", + "description": "Crop to calculate gdd for", "name": "product", "in": "query", "required": true - } - ], - "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/models.SeedListResponse" - } }, - "400": { - "description": "Bad Request", - "schema": { - "$ref": "#/definitions/common.StandardError" - } - } - } - } - }, - "/api/weather/gdd/seeds/corn": { - "get": { - "description": "Get estimated maturity date from corn seed", - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "GDD Seed Data" - ], - "summary": "Get estimated maturity date from corn seed (uses CFS data if current GDUs are less than the crop maturity)", - "parameters": [ { "type": "number", "description": "Latitude to search for", @@ -727,23 +699,9 @@ var doc = `{ "required": true }, { - "type": "string", - "description": "Corn seed to use", - "name": "seed", - "in": "query", - "required": true - }, - { - "type": "number", - "description": "month planted", - "name": "month", - "in": "query", - "required": true - }, - { - "type": "number", - "description": "date planted", - "name": "date", + "type": "boolean", + "description": "Accumulate gdd values", + "name": "accumulate", "in": "query", "required": true } @@ -752,7 +710,7 @@ var doc = `{ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.CornMaturityResponse" + "$ref": "#/definitions/models.GddResponse" } }, "400": { @@ -961,17 +919,6 @@ var doc = `{ } } } - }, - "models.SeedListResponse": { - "type": "object", - "properties": { - "seeds": { - "type": "array", - "items": { - "type": "string" - } - } - } } } }` diff --git a/docs/swagger.json b/docs/swagger.json index a110e77c39d9d6eb870da3c578e08881ff69cfe0..8907929551416f5070d48765dc85f671f9f42afe 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -567,9 +567,9 @@ } } }, - "/api/weather/gdd/normals": { + "/api/weather/gdd/maturity/corn": { "get": { - "description": "get gdd normals", + "description": "Get estimated maturity date from given gdd", "consumes": [ "application/json" ], @@ -577,33 +577,10 @@ "application/json" ], "tags": [ - "Gdd" + "GDD Maturity Data" ], - "summary": "Get gdd normals", + "summary": "Get estimated maturity date from given gdd (uses CFS data if current GDUs are less than the crop maturity)", "parameters": [ - { - "enum": [ - "corn", - "soybean", - "sunflower", - "tomato", - "sugar_beet", - "peanut", - "cotton", - "potato", - "wheat", - "pea", - "oat", - "spring_wheat", - "rice", - "sorghum" - ], - "type": "string", - "description": "Crop to calculate gdd for", - "name": "product", - "in": "query", - "required": true - }, { "type": "number", "description": "Latitude to search for", @@ -619,9 +596,23 @@ "required": true }, { - "type": "boolean", - "description": "Accumulate gdd values", - "name": "accumulate", + "type": "number", + "description": "number of gdds given", + "name": "gdds", + "in": "query", + "required": true + }, + { + "type": "number", + "description": "month planted", + "name": "month", + "in": "query", + "required": true + }, + { + "type": "number", + "description": "date planted", + "name": "date", "in": "query", "required": true } @@ -630,7 +621,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.GddResponse" + "$ref": "#/definitions/models.CornMaturityResponse" } }, "400": { @@ -642,9 +633,9 @@ } } }, - "/api/weather/gdd/seeds": { + "/api/weather/gdd/normals": { "get": { - "description": "Get list of seeds in database", + "description": "get gdd normals", "consumes": [ "application/json" ], @@ -652,52 +643,33 @@ "application/json" ], "tags": [ - "GDD Seed Data" + "Gdd" ], - "summary": "Get list of seeds in database", + "summary": "Get gdd normals", "parameters": [ { "enum": [ "corn", - "soybean" + "soybean", + "sunflower", + "tomato", + "sugar_beet", + "peanut", + "cotton", + "potato", + "wheat", + "pea", + "oat", + "spring_wheat", + "rice", + "sorghum" ], "type": "string", - "description": "Crop type to use", + "description": "Crop to calculate gdd for", "name": "product", "in": "query", "required": true - } - ], - "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/models.SeedListResponse" - } }, - "400": { - "description": "Bad Request", - "schema": { - "$ref": "#/definitions/common.StandardError" - } - } - } - } - }, - "/api/weather/gdd/seeds/corn": { - "get": { - "description": "Get estimated maturity date from corn seed", - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "GDD Seed Data" - ], - "summary": "Get estimated maturity date from corn seed (uses CFS data if current GDUs are less than the crop maturity)", - "parameters": [ { "type": "number", "description": "Latitude to search for", @@ -713,23 +685,9 @@ "required": true }, { - "type": "string", - "description": "Corn seed to use", - "name": "seed", - "in": "query", - "required": true - }, - { - "type": "number", - "description": "month planted", - "name": "month", - "in": "query", - "required": true - }, - { - "type": "number", - "description": "date planted", - "name": "date", + "type": "boolean", + "description": "Accumulate gdd values", + "name": "accumulate", "in": "query", "required": true } @@ -738,7 +696,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.CornMaturityResponse" + "$ref": "#/definitions/models.GddResponse" } }, "400": { @@ -947,17 +905,6 @@ } } } - }, - "models.SeedListResponse": { - "type": "object", - "properties": { - "seeds": { - "type": "array", - "items": { - "type": "string" - } - } - } } } } \ No newline at end of file diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 8310da733a31b92aa675906d92a15e4f3a805a45..ad514eb42d91813d948961d2d17415f93d9e1bb6 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -128,13 +128,6 @@ definitions: type: number type: array type: object - models.SeedListResponse: - properties: - seeds: - items: - type: string - type: array - type: object host: localhost:8080 info: contact: @@ -539,32 +532,12 @@ paths: summary: Get GDD values calculated from GEFS tags: - Gdd - /api/weather/gdd/normals: + /api/weather/gdd/maturity/corn: get: consumes: - application/json - description: get gdd normals + description: Get estimated maturity date from given gdd parameters: - - description: Crop to calculate gdd for - enum: - - corn - - soybean - - sunflower - - tomato - - sugar_beet - - peanut - - cotton - - potato - - wheat - - pea - - oat - - spring_wheat - - rice - - sorghum - in: query - name: product - required: true - type: string - description: Latitude to search for in: query name: latitude @@ -575,59 +548,62 @@ paths: name: longitude required: true type: number - - description: Accumulate gdd values + - description: number of gdds given in: query - name: accumulate + name: gdds required: true - type: boolean + type: number + - description: month planted + in: query + name: month + required: true + type: number + - description: date planted + in: query + name: date + required: true + type: number produces: - application/json responses: "200": description: OK schema: - $ref: '#/definitions/models.GddResponse' + $ref: '#/definitions/models.CornMaturityResponse' "400": description: Bad Request schema: $ref: '#/definitions/common.StandardError' - summary: Get gdd normals + summary: Get estimated maturity date from given gdd (uses CFS data if current + GDUs are less than the crop maturity) tags: - - Gdd - /api/weather/gdd/seeds: + - GDD Maturity Data + /api/weather/gdd/normals: get: consumes: - application/json - description: Get list of seeds in database + description: get gdd normals parameters: - - description: Crop type to use + - description: Crop to calculate gdd for enum: - corn - soybean + - sunflower + - tomato + - sugar_beet + - peanut + - cotton + - potato + - wheat + - pea + - oat + - spring_wheat + - rice + - sorghum in: query name: product required: true type: string - produces: - - application/json - responses: - "200": - description: OK - schema: - $ref: '#/definitions/models.SeedListResponse' - "400": - description: Bad Request - schema: - $ref: '#/definitions/common.StandardError' - summary: Get list of seeds in database - tags: - - GDD Seed Data - /api/weather/gdd/seeds/corn: - get: - consumes: - - application/json - description: Get estimated maturity date from corn seed - parameters: - description: Latitude to search for in: query name: latitude @@ -638,34 +614,23 @@ paths: name: longitude required: true type: number - - description: Corn seed to use - in: query - name: seed - required: true - type: string - - description: month planted - in: query - name: month - required: true - type: number - - description: date planted + - description: Accumulate gdd values in: query - name: date + name: accumulate required: true - type: number + type: boolean produces: - application/json responses: "200": description: OK schema: - $ref: '#/definitions/models.CornMaturityResponse' + $ref: '#/definitions/models.GddResponse' "400": description: Bad Request schema: $ref: '#/definitions/common.StandardError' - summary: Get estimated maturity date from corn seed (uses CFS data if current - GDUs are less than the crop maturity) + summary: Get gdd normals tags: - - GDD Seed Data + - Gdd swagger: "2.0" diff --git a/models/seeds.go b/models/seeds.go index 3c87ed445a07782019a8728d080ba95dd1ff50a5..c837463c28e7a3590080ec163cf4fd36a2b7fe25 100644 --- a/models/seeds.go +++ b/models/seeds.go @@ -21,7 +21,7 @@ type SeedListResponse struct { type CornMaturityRequest struct { Latitude float64 `json:"latitude"` Longitude float64 `json:"longitude"` - Seed string `json:"seed"` + Gdds float64 `json:"gdds"` Month int `json:"month"` Date int `json:"date"` } @@ -38,6 +38,7 @@ func (r CornMaturityRequest) Validate() error { return validation.ValidateStruct(&r, 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)), + validation.Field(&r.Gdds, validation.Required, validation.Min(0.0), validation.Max(6000.0)), validation.Field(&r.Month, validation.Required, validation.Min(1), validation.Max(12)), validation.Field(&r.Date, validation.Required, validation.Min(1), validation.Max(31)), ) @@ -49,12 +50,12 @@ func BuildCornMaturityRequest(c *fiber.Ctx) CornMaturityRequest { lon, _ := strconv.ParseFloat(c.Query("longitude", "-10000.0"), 64) month, _ := strconv.Atoi(c.Query("month", "0")) date, _ := strconv.Atoi(c.Query("date", "0")) - seed := c.Query("seed", "-10000.0") + gdds, _ := strconv.ParseFloat(c.Query("gdds", "-10000.0"), 64) newRequest := CornMaturityRequest{ Latitude: lat, Longitude: lon, - Seed: seed, + Gdds: gdds, Month: month, Date: date, } diff --git a/persistence/mongodb.go b/persistence/mongodb.go index 3eb8b9cb06784f24bdfb179fe683f053745aa554..fc489a23afdc20c999c71e5ae9a0c1425f24c4b7 100644 --- a/persistence/mongodb.go +++ b/persistence/mongodb.go @@ -28,11 +28,11 @@ func CreateDBSession() error { Conn, err = mongo.Connect(Ctx, options.Client(). ApplyURI(ConnString)) if err != nil { - return err + panic(config.DATABSE_CONNECTION_ERROR) } err = Conn.Ping(Ctx, nil) if err != nil { - return err + panic(config.DATABSE_CONNECTION_ERROR) } return nil } diff --git a/services/seed_service.go b/services/seed_service.go index 4d57c7cb8062bb08c12c8e2f8e1b5660fdbcfb17..d1a9eedf80ced15a1cb0460db1987ef55a336dc1 100644 --- a/services/seed_service.go +++ b/services/seed_service.go @@ -29,11 +29,6 @@ func GetSeedList(c *fiber.Ctx, request models.SeedListRequest) models.SeedListRe func GetCornMaturityDate(ctx common.DawnCtx, request models.CornMaturityRequest) models.CornMaturityResponse { - seed := persistence.FindSeed(request.Seed) - if seed.Type != "corn" { - panic(config.SEED_TYPE_MISMATCH(request.Seed, "corn")) - } - gddRequest := models.GddRequest{ Year: time.Now().Year(), Product: "corn", @@ -57,14 +52,14 @@ func GetCornMaturityDate(ctx common.DawnCtx, request models.CornMaturityRequest) for i := startingIdxVal; i < len(gdds.GddValues); i++ { value := gdds.GddValues[i] gdus += value - if math.Abs(gdus-seed.BlackLayerGdus) < math.Abs(closestValue-seed.BlackLayerGdus) { + if math.Abs(gdus-request.Gdds) < math.Abs(closestValue-request.Gdds) { closestValue = gdus closestIdx = i } } response := models.CornMaturityResponse{ Date: utils.ConvertDateIdxToDate(closestIdx), - GDD: seed.BlackLayerGdus, + GDD: request.Gdds, ClosestGDD: closestValue, ClosestLatitude: gdds.ClosestLatitude, ClosestLongitude: gdds.ClosestLongitude,