Author: Paolo Lulli <paolo@lulli.net>
Add openapi endpoint
Makefile | 12 + openapi/swagger.json | 306 ++++++++++++++++++++++++++++++++++++++++ openapi/swagger.yaml | 196 +++++++++++++++++++++++++ server/docs/docs.go | 164 +++++++++++++++++--- server/docs/swagger.json | 160 +++++++++++++++++--- server/docs/swagger.yaml | 125 +++++++++++++--- server/main.go | 16 ++ server/rest/rest-event.go | 48 +++++ server/rest/rest-metric.go | 49 +++++ server/service-rest.go | 6
diff --git a/Makefile b/Makefile index 966a96b2c5d9f6f87c6ce1240fa9d9db2abff785..d61176f582836a581be52c76d571600a1e9aa655 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,17 @@ +.PHONY: all oldswagger openapi clean server + all: server -openapi: +oldswagger: cd server; swag init -server: openapi +openapi: + cd server; swag2op init --openapiOutputDir=../openapi + +server: oldswagger cd server; go clean; go build + +clean: + cd server; go clean; diff --git a/openapi/swagger.json b/openapi/swagger.json new file mode 100755 index 0000000000000000000000000000000000000000..331442d30dacaa5a8cf7af158e08dbf3355d3447 --- /dev/null +++ b/openapi/swagger.json @@ -0,0 +1,306 @@ +{ + "components": { + "schemas": { + "model.EventModel": { + "properties": { + "etime": { + "type": "integer" + }, + "id_client": { + "type": "string" + }, + "name": { + "type": "string" + } + }, + "type": "object" + }, + "model.EventSearchRequest": { + "properties": { + "from": { + "type": "integer" + }, + "id_client": { + "type": "string" + }, + "to": { + "type": "integer" + } + }, + "type": "object" + }, + "model.MetricModel": { + "properties": { + "id_client": { + "type": "string" + }, + "mtime": { + "type": "integer" + }, + "name": { + "type": "string" + }, + "value": { + "type": "string" + } + }, + "type": "object" + }, + "model.MetricSearchRequest": { + "properties": { + "from": { + "type": "integer" + }, + "id_client": { + "type": "string" + }, + "name": { + "type": "string" + }, + "to": { + "type": "integer" + } + }, + "type": "object" + } + } + }, + "externalDocs": { + "description": "OpenAPI", + "url": "https://swagger.io/resources/open-api/" + }, + "info": { + "contact": { + "name": "API Support", + "url": "https://" + }, + "description": "Yet Another Time Serie", + "license": { + "name": "Affero GPL", + "url": "https://www.gnu.org/licenses/agpl-3.0.en.html#license-text" + }, + "termsOfService": "https://kevwe.com/", + "title": "Yats Server", + "version": "1.0" + }, + "openapi": "3.0.3", + "paths": { + "/event": { + "post": { + "description": "store events", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/model.EventModel" + } + } + }, + "description": "Event body data", + "required": true, + "x-originalParamName": "event1" + }, + "responses": { + "202": { + "content": { + "application/json": { + "schema": { + "type": "string" + } + } + }, + "description": "Accepted" + } + }, + "summary": "write event to db", + "tags": [ + "Events" + ] + } + }, + "/event/get": { + "post": { + "description": "retrieve events in the specified time window", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/model.EventSearchRequest" + } + } + }, + "description": "Event query filters", + "required": true, + "x-originalParamName": "event2" + }, + "responses": { + "200": { + "content": { + "application/json": { + "schema": { + "type": "string" + } + } + }, + "description": "OK" + } + }, + "summary": "Get the events From ... and To when present", + "tags": [ + "Events" + ] + } + }, + "/event/get/{from}": { + "get": { + "description": "retrieve events from timestamp {from}", + "parameters": [ + { + "description": "Starting from timestamp :from", + "in": "path", + "name": "from", + "required": true, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/model.EventSearchRequest" + } + } + }, + "description": "Event query filters", + "required": true, + "x-originalParamName": "event2" + }, + "responses": { + "200": { + "content": { + "application/json": { + "schema": { + "type": "string" + } + } + }, + "description": "OK" + } + }, + "summary": "Get the events from timestamp {from}", + "tags": [ + "Events" + ] + } + }, + "/metric": { + "post": { + "description": "store metric", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/model.MetricModel" + } + } + }, + "description": "Metric request body", + "required": true, + "x-originalParamName": "metric2" + }, + "responses": { + "202": { + "content": { + "application/json": { + "schema": { + "type": "string" + } + } + }, + "description": "Accepted" + } + }, + "summary": "write metric to db", + "tags": [ + "Metrics" + ] + } + }, + "/metric/get": { + "post": { + "description": "retrieve metric in the specified time window", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/model.MetricSearchRequest" + } + } + }, + "description": "Metric query filters", + "required": true, + "x-originalParamName": "metric1" + }, + "responses": { + "200": { + "content": { + "application/json": { + "schema": { + "type": "string" + } + } + }, + "description": "OK" + } + }, + "summary": "Get the metrics From ... and To when present", + "tags": [ + "Metrics" + ] + } + }, + "/metric/get/{name}/{from}": { + "get": { + "description": "retrieve metric in the specified time window", + "parameters": [ + { + "description": "Metric Name :name", + "in": "path", + "name": "name", + "required": true, + "schema": { + "type": "string" + } + }, + { + "description": "Starting from epoch :from", + "in": "path", + "name": "from", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "content": { + "application/json": { + "schema": { + "type": "string" + } + } + }, + "description": "OK" + } + }, + "summary": "Get the spcific metric {name} starting from the epoch {from}", + "tags": [ + "Metrics" + ] + } + } + } +} \ No newline at end of file diff --git a/openapi/swagger.yaml b/openapi/swagger.yaml new file mode 100755 index 0000000000000000000000000000000000000000..ae38a8296bd5c32c3683e20529ee6e1ded515f32 --- /dev/null +++ b/openapi/swagger.yaml @@ -0,0 +1,196 @@ +components: + schemas: + model.EventModel: + properties: + etime: + type: integer + id_client: + type: string + name: + type: string + type: object + model.EventSearchRequest: + properties: + from: + type: integer + id_client: + type: string + to: + type: integer + type: object + model.MetricModel: + properties: + id_client: + type: string + mtime: + type: integer + name: + type: string + value: + type: string + type: object + model.MetricSearchRequest: + properties: + from: + type: integer + id_client: + type: string + name: + type: string + to: + type: integer + type: object +externalDocs: + description: OpenAPI + url: https://swagger.io/resources/open-api/ +info: + contact: + name: API Support + url: https:// + description: Yet Another Time Serie + license: + name: Affero GPL + url: https://www.gnu.org/licenses/agpl-3.0.en.html#license-text + termsOfService: https://kevwe.com/ + title: Yats Server + version: "1.0" +openapi: 3.0.3 +paths: + /event: + post: + description: store events + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/model.EventModel' + description: Event body data + required: true + x-originalParamName: event1 + responses: + "202": + content: + application/json: + schema: + type: string + description: Accepted + summary: write event to db + tags: + - Events + /event/get: + post: + description: retrieve events in the specified time window + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/model.EventSearchRequest' + description: Event query filters + required: true + x-originalParamName: event2 + responses: + "200": + content: + application/json: + schema: + type: string + description: OK + summary: Get the events From ... and To when present + tags: + - Events + /event/get/{from}: + get: + description: retrieve events from timestamp {from} + parameters: + - description: Starting from timestamp :from + in: path + name: from + required: true + schema: + type: string + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/model.EventSearchRequest' + description: Event query filters + required: true + x-originalParamName: event2 + responses: + "200": + content: + application/json: + schema: + type: string + description: OK + summary: Get the events from timestamp {from} + tags: + - Events + /metric: + post: + description: store metric + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/model.MetricModel' + description: Metric request body + required: true + x-originalParamName: metric2 + responses: + "202": + content: + application/json: + schema: + type: string + description: Accepted + summary: write metric to db + tags: + - Metrics + /metric/get: + post: + description: retrieve metric in the specified time window + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/model.MetricSearchRequest' + description: Metric query filters + required: true + x-originalParamName: metric1 + responses: + "200": + content: + application/json: + schema: + type: string + description: OK + summary: Get the metrics From ... and To when present + tags: + - Metrics + /metric/get/{name}/{from}: + get: + description: retrieve metric in the specified time window + parameters: + - description: Metric Name :name + in: path + name: name + required: true + schema: + type: string + - description: Starting from epoch :from + in: path + name: from + required: true + schema: + type: string + responses: + "200": + content: + application/json: + schema: + type: string + description: OK + summary: Get the spcific metric {name} starting from the epoch {from} + tags: + - Metrics diff --git a/server/docs/docs.go b/server/docs/docs.go index bc14cdb6b6b99398f498d3d269caca6cd50abc14..3819a6e5133c71765ee81b02829dffb837fc3d5a 100644 --- a/server/docs/docs.go +++ b/server/docs/docs.go @@ -9,7 +9,16 @@ "swagger": "2.0", "info": { "description": "{{escape .Description}}", "title": "{{.Title}}", - "contact": {}, + "termsOfService": "https://kevwe.com/ TODO", + "contact": { + "name": "API Support", + "url": "https:// TODO", + "email": "TODO" + }, + "license": { + "name": "Affero GPL", + "url": "https://www.gnu.org/licenses/agpl-3.0.en.html#license-text" + }, "version": "{{.Version}}" }, "host": "{{.Host}}", @@ -30,8 +39,15 @@ ], "summary": "write event to db", "parameters": [ { + "type": "string", + "description": "clientCN", + "name": "X-SSL-Client-CN", + "in": "header", + "required": true + }, + { "description": "Event body data", - "name": "data", + "name": "event1", "in": "body", "required": true, "schema": { @@ -49,7 +65,7 @@ } } } }, - "/event/get": { + "/event/search": { "post": { "description": "retrieve events in the specified time window", "consumes": [ @@ -64,25 +80,68 @@ ], "summary": "Get the events From ... and To when present", "parameters": [ { + "type": "string", + "description": "clientCN", + "name": "X-SSL-Client-CN", + "in": "header", + "required": true + }, + { "description": "Event query filters", - "name": "data", + "name": "event2", "in": "body", "required": true, "schema": { "$ref": "#/definitions/model.EventSearchRequest" } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "string" + } + } + } + } + }, + "/event/{from}": { + "get": { + "description": "retrieve events from timestamp {from}", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Events" + ], + "summary": "Get the events from timestamp {from}", + "parameters": [ + { + "type": "string", + "description": "clientCN", + "name": "X-SSL-Client-CN", + "in": "header", + "required": true }, { - "description": "Event response", - "name": "data", + "description": "Event query filters", + "name": "event2", "in": "body", "required": true, "schema": { - "type": "array", - "items": { - "$ref": "#/definitions/model.EventModel" - } + "$ref": "#/definitions/model.EventSearchRequest" } + }, + { + "type": "string", + "description": "Starting from timestamp :from", + "name": "from", + "in": "path", + "required": true } ], "responses": { @@ -110,8 +169,15 @@ ], "summary": "write metric to db", "parameters": [ { + "type": "string", + "description": "clientCN", + "name": "X-SSL-Client-CN", + "in": "header", + "required": true + }, + { "description": "Metric request body", - "name": "data", + "name": "metric2", "in": "body", "required": true, "schema": { @@ -129,9 +195,9 @@ } } } }, - "/metric/get": { + "/metric/search": { "post": { - "description": "retrieve metric in the specified time window", + "description": "search metrics in the specified timeframe", "consumes": [ "application/json" ], @@ -144,26 +210,64 @@ ], "summary": "Get the metrics From ... and To when present", "parameters": [ { + "type": "string", + "description": "clientCN", + "name": "X-SSL-Client-CN", + "in": "header", + "required": true + }, + { "description": "Metric query filters", - "name": "data", + "name": "metric1", "in": "body", "required": true, "schema": { "$ref": "#/definitions/model.MetricSearchRequest" } - }, - { - "description": "Metric response", - "name": "data", - "in": "body", - "required": true, + } + ], + "responses": { + "200": { + "description": "OK", "schema": { - "type": "array", - "items": { - "$ref": "#/definitions/model.MetricModel" - } + "type": "string" } } + } + } + }, + "/metric/{name}/{from}": { + "get": { + "description": "search metrics after the specified timestamp", + "produces": [ + "application/json" + ], + "tags": [ + "Metrics" + ], + "summary": "Get the spcific metric {name} starting from the epoch {from}", + "parameters": [ + { + "type": "string", + "description": "clientCN", + "name": "X-SSL-Client-CN", + "in": "header", + "required": true + }, + { + "type": "string", + "description": "Metric Name :name", + "name": "name", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "Starting from epoch :from", + "name": "from", + "in": "path", + "required": true + } ], "responses": { "200": { @@ -239,17 +343,21 @@ "type": "integer" } } } + }, + "externalDocs": { + "description": "OpenAPI", + "url": "https://swagger.io/resources/open-api/" } }` // SwaggerInfo holds exported Swagger Info so clients can modify it var SwaggerInfo = &swag.Spec{ - Version: "", + Version: "1.0", Host: "", - BasePath: "", + BasePath: "/", Schemes: []string{}, - Title: "", - Description: "", + Title: "Yats Server", + Description: "Yet Another Time Serie", InfoInstanceName: "swagger", SwaggerTemplate: docTemplate, LeftDelim: "{{", diff --git a/server/docs/swagger.json b/server/docs/swagger.json old mode 100644 new mode 100755 index cea35f892308f03d1013c0618523d999d9044bb7..447aed70f93e5a6a7e056fd33113bca3648b1349 --- a/server/docs/swagger.json +++ b/server/docs/swagger.json @@ -1,8 +1,21 @@ { "swagger": "2.0", "info": { - "contact": {} + "description": "Yet Another Time Serie", + "title": "Yats Server", + "termsOfService": "https://kevwe.com/ TODO", + "contact": { + "name": "API Support", + "url": "https:// TODO", + "email": "TODO" + }, + "license": { + "name": "Affero GPL", + "url": "https://www.gnu.org/licenses/agpl-3.0.en.html#license-text" + }, + "version": "1.0" }, + "basePath": "/", "paths": { "/event": { "post": { @@ -19,8 +32,15 @@ ], "summary": "write event to db", "parameters": [ { + "type": "string", + "description": "clientCN", + "name": "X-SSL-Client-CN", + "in": "header", + "required": true + }, + { "description": "Event body data", - "name": "data", + "name": "event1", "in": "body", "required": true, "schema": { @@ -38,7 +58,7 @@ } } } }, - "/event/get": { + "/event/search": { "post": { "description": "retrieve events in the specified time window", "consumes": [ @@ -53,25 +73,68 @@ ], "summary": "Get the events From ... and To when present", "parameters": [ { + "type": "string", + "description": "clientCN", + "name": "X-SSL-Client-CN", + "in": "header", + "required": true + }, + { "description": "Event query filters", - "name": "data", + "name": "event2", "in": "body", "required": true, "schema": { "$ref": "#/definitions/model.EventSearchRequest" } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "string" + } + } + } + } + }, + "/event/{from}": { + "get": { + "description": "retrieve events from timestamp {from}", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Events" + ], + "summary": "Get the events from timestamp {from}", + "parameters": [ + { + "type": "string", + "description": "clientCN", + "name": "X-SSL-Client-CN", + "in": "header", + "required": true }, { - "description": "Event response", - "name": "data", + "description": "Event query filters", + "name": "event2", "in": "body", "required": true, "schema": { - "type": "array", - "items": { - "$ref": "#/definitions/model.EventModel" - } + "$ref": "#/definitions/model.EventSearchRequest" } + }, + { + "type": "string", + "description": "Starting from timestamp :from", + "name": "from", + "in": "path", + "required": true } ], "responses": { @@ -98,9 +161,16 @@ "Metrics" ], "summary": "write metric to db", "parameters": [ + { + "type": "string", + "description": "clientCN", + "name": "X-SSL-Client-CN", + "in": "header", + "required": true + }, { "description": "Metric request body", - "name": "data", + "name": "metric2", "in": "body", "required": true, "schema": { @@ -118,9 +188,9 @@ } } } }, - "/metric/get": { + "/metric/search": { "post": { - "description": "retrieve metric in the specified time window", + "description": "search metrics in the specified timeframe", "consumes": [ "application/json" ], @@ -133,25 +203,63 @@ ], "summary": "Get the metrics From ... and To when present", "parameters": [ { + "type": "string", + "description": "clientCN", + "name": "X-SSL-Client-CN", + "in": "header", + "required": true + }, + { "description": "Metric query filters", - "name": "data", + "name": "metric1", "in": "body", "required": true, "schema": { "$ref": "#/definitions/model.MetricSearchRequest" } - }, - { - "description": "Metric response", - "name": "data", - "in": "body", - "required": true, + } + ], + "responses": { + "200": { + "description": "OK", "schema": { - "type": "array", - "items": { - "$ref": "#/definitions/model.MetricModel" - } + "type": "string" } + } + } + } + }, + "/metric/{name}/{from}": { + "get": { + "description": "search metrics after the specified timestamp", + "produces": [ + "application/json" + ], + "tags": [ + "Metrics" + ], + "summary": "Get the spcific metric {name} starting from the epoch {from}", + "parameters": [ + { + "type": "string", + "description": "clientCN", + "name": "X-SSL-Client-CN", + "in": "header", + "required": true + }, + { + "type": "string", + "description": "Metric Name :name", + "name": "name", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "Starting from epoch :from", + "name": "from", + "in": "path", + "required": true } ], "responses": { @@ -228,5 +336,9 @@ "type": "integer" } } } + }, + "externalDocs": { + "description": "OpenAPI", + "url": "https://swagger.io/resources/open-api/" } } \ No newline at end of file diff --git a/server/docs/swagger.yaml b/server/docs/swagger.yaml old mode 100644 new mode 100755 index cba537c3c35e5ce97c7f8583916f9ba5cb4eef5d..d9a07e80f8f60d6aa06be2abce32540dfb0e7122 --- a/server/docs/swagger.yaml +++ b/server/docs/swagger.yaml @@ -1,3 +1,4 @@ +basePath: / definitions: model.EventModel: properties: @@ -39,8 +40,21 @@ type: string to: type: integer type: object +externalDocs: + description: OpenAPI + url: https://swagger.io/resources/open-api/ info: - contact: {} + contact: + email: TODO + name: API Support + url: https:// TODO + description: Yet Another Time Serie + license: + name: Affero GPL + url: https://www.gnu.org/licenses/agpl-3.0.en.html#license-text + termsOfService: https://kevwe.com/ TODO + title: Yats Server + version: "1.0" paths: /event: post: @@ -48,9 +62,14 @@ consumes: - application/json description: store events parameters: + - description: clientCN + in: header + name: X-SSL-Client-CN + required: true + type: string - description: Event body data in: body - name: data + name: event1 required: true schema: $ref: '#/definitions/model.EventModel' @@ -64,26 +83,55 @@ type: string summary: write event to db tags: - Events - /event/get: - post: + /event/{from}: + get: consumes: - application/json - description: retrieve events in the specified time window + description: retrieve events from timestamp {from} parameters: + - description: clientCN + in: header + name: X-SSL-Client-CN + required: true + type: string - description: Event query filters in: body - name: data + name: event2 required: true schema: $ref: '#/definitions/model.EventSearchRequest' - - description: Event response + - description: Starting from timestamp :from + in: path + name: from + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + type: string + summary: Get the events from timestamp {from} + tags: + - Events + /event/search: + post: + consumes: + - application/json + description: retrieve events in the specified time window + parameters: + - description: clientCN + in: header + name: X-SSL-Client-CN + required: true + type: string + - description: Event query filters in: body - name: data + name: event2 required: true schema: - items: - $ref: '#/definitions/model.EventModel' - type: array + $ref: '#/definitions/model.EventSearchRequest' produces: - application/json responses: @@ -100,9 +148,14 @@ consumes: - application/json description: store metric parameters: + - description: clientCN + in: header + name: X-SSL-Client-CN + required: true + type: string - description: Metric request body in: body - name: data + name: metric2 required: true schema: $ref: '#/definitions/model.MetricModel' @@ -116,26 +169,52 @@ type: string summary: write metric to db tags: - Metrics - /metric/get: + /metric/{name}/{from}: + get: + description: search metrics after the specified timestamp + parameters: + - description: clientCN + in: header + name: X-SSL-Client-CN + required: true + type: string + - description: Metric Name :name + in: path + name: name + required: true + type: string + - description: Starting from epoch :from + in: path + name: from + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + type: string + summary: Get the spcific metric {name} starting from the epoch {from} + tags: + - Metrics + /metric/search: post: consumes: - application/json - description: retrieve metric in the specified time window + description: search metrics in the specified timeframe parameters: + - description: clientCN + in: header + name: X-SSL-Client-CN + required: true + type: string - description: Metric query filters in: body - name: data + name: metric1 required: true schema: $ref: '#/definitions/model.MetricSearchRequest' - - description: Metric response - in: body - name: data - required: true - schema: - items: - $ref: '#/definitions/model.MetricModel' - type: array produces: - application/json responses: diff --git a/server/main.go b/server/main.go index 10045388d5fe1140a6efe3504b116174cd76fe90..096fa6223c929d6024bffe188e2ddf0c11ce08ee 100644 --- a/server/main.go +++ b/server/main.go @@ -22,6 +22,22 @@ var configuration config.Configuration var instanceName = "default" +// @title Yats Server +// @version 1.0 +// @description Yet Another Time Serie +// @termsOfService https://kevwe.com/ TODO + +// @contact.name API Support +// @contact.url https:// TODO +// @contact.email TODO + +// @license.name Affero GPL +// @license.url https://www.gnu.org/licenses/agpl-3.0.en.html#license-text + +// @BasePath / + +// @externalDocs.description OpenAPI +// @externalDocs.url https://swagger.io/resources/open-api/ func main() { docs.SwaggerInfo.BasePath = "/" diff --git a/server/rest/rest-event.go b/server/rest/rest-event.go index ae475193ba18787f6606e0674acca4558c0e988a..46d947ff442c27a21507f4b813c8fbb5c3739d5a 100644 --- a/server/rest/rest-event.go +++ b/server/rest/rest-event.go @@ -14,18 +14,20 @@ import ( "fmt" "github.com/gin-gonic/gin" "net/http" + "strconv" "time" "yats-server/db" "yats-server/model" ) // WriteEvent godoc +// @Param X-SSL-Client-CN header string true "clientCN" // @Summary write event to db // @Schemes // @Description store events // @Tags Events // @Accept json -// @Param data body model.EventModel true "Event body data" +// @Param event1 body model.EventModel true "Event body data" // @Produce json // @Success 202 {string} WriteEvent // @Router /event [post] @@ -48,18 +50,18 @@ } c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK"}) } -// GetEvents godoc +// SearchEvents godoc +// @Param X-SSL-Client-CN header string true "clientCN" // @Summary Get the events From ... and To when present // @Schemes // @Description retrieve events in the specified time window // @Tags Events // @Accept json -// @Param data body model.EventSearchRequest true "Event query filters" +// @Param event2 body model.EventSearchRequest true "Event query filters" // @Produce json -// @Param data body []model.EventModel true "Event response" -// @Success 200 {string} GetEvents -// @Router /event/get [post] -func GetEvents(c *gin.Context) { +// @Success 200 {string} SearchEvents +// @Router /event/search [post] +func SearchEvents(c *gin.Context) { var event model.EventSearchRequest if err := c.BindJSON(&event); err != nil { @@ -84,3 +86,35 @@ eventsPack = db.EventsBetween(db.Session, clientCN, string(fromInBytes), string(toInBytes), 100) } c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK", "content": eventsPack}) } + +// GetEventsFrom godoc +// @Param X-SSL-Client-CN header string true "clientCN" +// @Summary Get the events from timestamp {from} +// @Schemes +// @Description retrieve events from timestamp {from} +// @Tags Events +// @Accept json +// @Param event2 body model.EventSearchRequest true "Event query filters" +// @Produce json +// @Success 200 {string} SearchEvents +// @Router /event/{from} [get] +// @Param from path string true "Starting from timestamp :from" +func GetEventsFrom(c *gin.Context) { + + fromParam := c.Param("from") + fromParamInt64, err := strconv.ParseInt(fromParam, 10, 64) + if err != nil { + c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "-1"}) + } + + clientCN := GetClientCN(c) + fmt.Printf("Client ID: %s\n ", clientCN) + + var eventsPack []model.EventModel + + unixTimeUTC := time.Unix(fromParamInt64, 0) + timeAsBytes, _ := unixTimeUTC.UTC().MarshalText() + eventsPack = db.EventsFrom(db.Session, clientCN, string(timeAsBytes), 100) + + c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK", "content": eventsPack}) +} diff --git a/server/rest/rest-metric.go b/server/rest/rest-metric.go index 2c7b96f5280c8972934ab08ee1436d961e879153..260775b52f8fcb5985460f3b64281dc414711ee9 100644 --- a/server/rest/rest-metric.go +++ b/server/rest/rest-metric.go @@ -14,6 +14,7 @@ import ( "fmt" "github.com/gin-gonic/gin" "net/http" + "strconv" "time" "yats-server/db" "yats-server/model" @@ -22,12 +23,13 @@ // @BasePath / // WriteMetric godoc +// @Param X-SSL-Client-CN header string true "clientCN" // @Summary write metric to db // @Schemes // @Description store metric // @Tags Metrics // @Accept json -// @Param data body model.MetricModel true "Metric request body" +// @Param metric2 body model.MetricModel true "Metric request body" // @Produce json // @Success 202 {string} WriteMetric // @Router /metric [post] @@ -52,18 +54,18 @@ c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK"}) } -// GetMetrics godoc +// SearchMetrics godoc +// @Param X-SSL-Client-CN header string true "clientCN" // @Summary Get the metrics From ... and To when present // @Schemes -// @Description retrieve metric in the specified time window +// @Description search metrics in the specified timeframe // @Tags Metrics // @Accept json -// @Param data body model.MetricSearchRequest true "Metric query filters" +// @Param metric1 body model.MetricSearchRequest true "Metric query filters" // @Produce json -// @Param data body []model.MetricModel true "Metric response" // @Success 200 {string} WriteMetric -// @Router /metric/get [post] -func GetMetrics(c *gin.Context) { +// @Router /metric/search [post] +func SearchMetrics(c *gin.Context) { var metric model.MetricSearchRequest if err := c.BindJSON(&metric); err != nil { @@ -89,3 +91,36 @@ metricsPack = db.MetricsBetween(db.Session, clientCN, metric.Name, string(fromInBytes), string(toInBytes), 100) } c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK", "content": metricsPack}) } + +// SearchMetricsFrom SearchMetrics godoc +// @Param X-SSL-Client-CN header string true "clientCN" +// @Summary Get the spcific metric {name} starting from the epoch {from} +// @Schemes +// @Description search metrics after the specified timestamp +// @Tags Metrics +// @Produce json +// @Success 200 {string} []model.MetricModel +// @Router /metric/{name}/{from} [get] +// @Param name path string true "Metric Name :name" +// @Param from path string true "Starting from epoch :from" +func SearchMetricsFrom(c *gin.Context) { + var metric model.MetricSearchRequest + + nameParam := c.Param("name") + fromParam := c.Param("from") + fromParamInt64, err := strconv.ParseInt(fromParam, 10, 64) + if err != nil { + c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "-1"}) + } + + clientCN := GetClientCN(c) + fmt.Printf("%s / %s ", clientCN, metric.Name) + + var metricsPack []model.MetricModel + + unixTimeUTC := time.Unix(fromParamInt64, 0) + timeAsBytes, _ := unixTimeUTC.UTC().MarshalText() + metricsPack = db.MetricsFrom(db.Session, clientCN, nameParam, string(timeAsBytes), 100) + + c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK", "content": metricsPack}) +} diff --git a/server/service-rest.go b/server/service-rest.go index 23764963b84f37817c981e170425c05f4eabba87..5baef4d6c5c455e5b50dfcc4713cefa87fc91aa9 100644 --- a/server/service-rest.go +++ b/server/service-rest.go @@ -30,10 +30,12 @@ router.SetTrustedProxies([]string{"127.0.0.1"}) gin.SetMode(gin.ReleaseMode) router.POST("/metric", rest.WriteMetric) - router.POST("/metric/get", rest.GetMetrics) + router.POST("/metric/search", rest.SearchMetrics) + router.GET("/metric/:name/:from", rest.SearchMetricsFrom) router.POST("/event", rest.WriteEvent) - router.POST("/event/get", rest.GetEvents) + router.POST("/event/search", rest.SearchEvents) + router.GET("/event/:from", rest.GetEventsFrom) enableSwaggerEndpoint(router)