Răsfoiți Sursa

feat: get log with code (#157)

zijiren 9 luni în urmă
părinte
comite
528a954ffe
5 a modificat fișierele cu 182 adăugiri și 20 ștergeri
  1. 14 4
      core/controller/log.go
  2. 28 4
      core/docs/docs.go
  3. 28 4
      core/docs/swagger.json
  4. 20 4
      core/docs/swagger.yaml
  5. 92 4
      core/model/log.go

+ 14 - 4
core/controller/log.go

@@ -36,6 +36,7 @@ func parseCommonParams(c *gin.Context) (params struct {
 	order      string
 	requestID  string
 	codeType   string
+	code       int
 	withBody   bool
 	ip         string
 	resultOnly bool
@@ -48,6 +49,7 @@ func parseCommonParams(c *gin.Context) (params struct {
 	params.order = c.Query("order")
 	params.requestID = c.Query("request_id")
 	params.codeType = c.Query("code_type")
+	params.code, _ = strconv.Atoi(c.Query("code"))
 	params.withBody, _ = strconv.ParseBool(c.Query("with_body"))
 	params.ip = c.Query("ip")
 	params.resultOnly, _ = strconv.ParseBool(c.Query("result_only"))
@@ -72,7 +74,8 @@ func parseCommonParams(c *gin.Context) (params struct {
 //	@Param			token_id		query		int		false	"Token ID"
 //	@Param			order			query		string	false	"Order"
 //	@Param			request_id		query		string	false	"Request ID"
-//	@Param			code_type		query		string	false	"Code type"
+//	@Param			code_type		query		string	false	"Status code type"
+//	@Param			code			query		int		false	"Status code"
 //	@Param			with_body		query		bool	false	"With body"
 //	@Param			ip				query		string	false	"IP"
 //	@Param			result_only		query		bool	false	"Result only"
@@ -95,6 +98,7 @@ func GetLogs(c *gin.Context) {
 		params.channelID,
 		params.order,
 		model.CodeType(params.codeType),
+		params.code,
 		params.withBody,
 		params.ip,
 		page,
@@ -126,7 +130,8 @@ func GetLogs(c *gin.Context) {
 //	@Param			token_id		query		int		false	"Token ID"
 //	@Param			order			query		string	false	"Order"
 //	@Param			request_id		query		string	false	"Request ID"
-//	@Param			code_type		query		string	false	"Code type"
+//	@Param			code_type		query		string	false	"Status code type"
+//	@Param			code			query		int		false	"Status code"
 //	@Param			with_body		query		bool	false	"With body"
 //	@Param			ip				query		string	false	"IP"
 //	@Param			result_only		query		bool	false	"Result only"
@@ -154,6 +159,7 @@ func GetGroupLogs(c *gin.Context) {
 		params.channelID,
 		params.order,
 		model.CodeType(params.codeType),
+		params.code,
 		params.withBody,
 		params.ip,
 		page,
@@ -186,7 +192,8 @@ func GetGroupLogs(c *gin.Context) {
 //	@Param			token_id		query		int		false	"Filter by token id"
 //	@Param			order			query		string	false	"Order"
 //	@Param			request_id		query		string	false	"Request ID"
-//	@Param			code_type		query		string	false	"Code type"
+//	@Param			code_type		query		string	false	"Status code type"
+//	@Param			code			query		int		false	"Status code"
 //	@Param			with_body		query		bool	false	"With body"
 //	@Param			ip				query		string	false	"IP"
 //	@Param			result_only		query		bool	false	"Result only"
@@ -212,6 +219,7 @@ func SearchLogs(c *gin.Context) {
 		params.channelID,
 		params.order,
 		model.CodeType(params.codeType),
+		params.code,
 		params.withBody,
 		params.ip,
 		page,
@@ -244,7 +252,8 @@ func SearchLogs(c *gin.Context) {
 //	@Param			token_id		query		int		false	"Filter by token id"
 //	@Param			order			query		string	false	"Order"
 //	@Param			request_id		query		string	false	"Request ID"
-//	@Param			code_type		query		string	false	"Code type"
+//	@Param			code_type		query		string	false	"Status code type"
+//	@Param			code			query		int		false	"Status code"
 //	@Param			with_body		query		bool	false	"With body"
 //	@Param			ip				query		string	false	"IP"
 //	@Param			result_only		query		bool	false	"Result only"
@@ -274,6 +283,7 @@ func SearchGroupLogs(c *gin.Context) {
 		params.channelID,
 		params.order,
 		model.CodeType(params.codeType),
+		params.code,
 		params.withBody,
 		params.ip,
 		page,

+ 28 - 4
core/docs/docs.go

@@ -2223,10 +2223,16 @@ const docTemplate = `{
                     },
                     {
                         "type": "string",
-                        "description": "Code type",
+                        "description": "Status code type",
                         "name": "code_type",
                         "in": "query"
                     },
+                    {
+                        "type": "integer",
+                        "description": "Status code",
+                        "name": "code",
+                        "in": "query"
+                    },
                     {
                         "type": "boolean",
                         "description": "With body",
@@ -2413,10 +2419,16 @@ const docTemplate = `{
                     },
                     {
                         "type": "string",
-                        "description": "Code type",
+                        "description": "Status code type",
                         "name": "code_type",
                         "in": "query"
                     },
+                    {
+                        "type": "integer",
+                        "description": "Status code",
+                        "name": "code",
+                        "in": "query"
+                    },
                     {
                         "type": "boolean",
                         "description": "With body",
@@ -2640,10 +2652,16 @@ const docTemplate = `{
                     },
                     {
                         "type": "string",
-                        "description": "Code type",
+                        "description": "Status code type",
                         "name": "code_type",
                         "in": "query"
                     },
+                    {
+                        "type": "integer",
+                        "description": "Status code",
+                        "name": "code",
+                        "in": "query"
+                    },
                     {
                         "type": "boolean",
                         "description": "With body",
@@ -2996,10 +3014,16 @@ const docTemplate = `{
                     },
                     {
                         "type": "string",
-                        "description": "Code type",
+                        "description": "Status code type",
                         "name": "code_type",
                         "in": "query"
                     },
+                    {
+                        "type": "integer",
+                        "description": "Status code",
+                        "name": "code",
+                        "in": "query"
+                    },
                     {
                         "type": "boolean",
                         "description": "With body",

+ 28 - 4
core/docs/swagger.json

@@ -2214,10 +2214,16 @@
                     },
                     {
                         "type": "string",
-                        "description": "Code type",
+                        "description": "Status code type",
                         "name": "code_type",
                         "in": "query"
                     },
+                    {
+                        "type": "integer",
+                        "description": "Status code",
+                        "name": "code",
+                        "in": "query"
+                    },
                     {
                         "type": "boolean",
                         "description": "With body",
@@ -2404,10 +2410,16 @@
                     },
                     {
                         "type": "string",
-                        "description": "Code type",
+                        "description": "Status code type",
                         "name": "code_type",
                         "in": "query"
                     },
+                    {
+                        "type": "integer",
+                        "description": "Status code",
+                        "name": "code",
+                        "in": "query"
+                    },
                     {
                         "type": "boolean",
                         "description": "With body",
@@ -2631,10 +2643,16 @@
                     },
                     {
                         "type": "string",
-                        "description": "Code type",
+                        "description": "Status code type",
                         "name": "code_type",
                         "in": "query"
                     },
+                    {
+                        "type": "integer",
+                        "description": "Status code",
+                        "name": "code",
+                        "in": "query"
+                    },
                     {
                         "type": "boolean",
                         "description": "With body",
@@ -2987,10 +3005,16 @@
                     },
                     {
                         "type": "string",
-                        "description": "Code type",
+                        "description": "Status code type",
                         "name": "code_type",
                         "in": "query"
                     },
+                    {
+                        "type": "integer",
+                        "description": "Status code",
+                        "name": "code",
+                        "in": "query"
+                    },
                     {
                         "type": "boolean",
                         "description": "With body",

+ 20 - 4
core/docs/swagger.yaml

@@ -2916,10 +2916,14 @@ paths:
         in: query
         name: request_id
         type: string
-      - description: Code type
+      - description: Status code type
         in: query
         name: code_type
         type: string
+      - description: Status code
+        in: query
+        name: code
+        type: integer
       - description: With body
         in: query
         name: with_body
@@ -3034,10 +3038,14 @@ paths:
         in: query
         name: request_id
         type: string
-      - description: Code type
+      - description: Status code type
         in: query
         name: code_type
         type: string
+      - description: Status code
+        in: query
+        name: code
+        type: integer
       - description: With body
         in: query
         name: with_body
@@ -3198,10 +3206,14 @@ paths:
         in: query
         name: request_id
         type: string
-      - description: Code type
+      - description: Status code type
         in: query
         name: code_type
         type: string
+      - description: Status code
+        in: query
+        name: code
+        type: integer
       - description: With body
         in: query
         name: with_body
@@ -3390,10 +3402,14 @@ paths:
         in: query
         name: request_id
         type: string
-      - description: Code type
+      - description: Status code type
         in: query
         name: code_type
         type: string
+      - description: Status code
+        in: query
+        name: code
+        type: integer
       - description: With body
         in: query
         name: with_body

+ 92 - 4
core/model/log.go

@@ -519,6 +519,7 @@ func buildGetLogsQuery(
 	tokenName string,
 	channelID int,
 	codeType CodeType,
+	code int,
 	ip string,
 	resultOnly bool,
 ) *gorm.DB {
@@ -564,6 +565,10 @@ func buildGetLogsQuery(
 		tx = tx.Where("code = 200")
 	case CodeTypeError:
 		tx = tx.Where("code != 200")
+	default:
+		if code != 0 {
+			tx = tx.Where("code = ?", code)
+		}
 	}
 
 	if tokenID != 0 {
@@ -583,6 +588,7 @@ func getLogs(
 	channelID int,
 	order string,
 	codeType CodeType,
+	code int,
 	withBody bool,
 	ip string,
 	page int,
@@ -605,6 +611,7 @@ func getLogs(
 			tokenName,
 			channelID,
 			codeType,
+			code,
 			ip,
 			resultOnly,
 		).Count(&total).Error
@@ -621,6 +628,7 @@ func getLogs(
 			tokenName,
 			channelID,
 			codeType,
+			code,
 			ip,
 			resultOnly,
 		)
@@ -658,6 +666,7 @@ func GetLogs(
 	channelID int,
 	order string,
 	codeType CodeType,
+	code int,
 	withBody bool,
 	ip string,
 	page int,
@@ -678,7 +687,24 @@ func GetLogs(
 
 	g.Go(func() error {
 		var err error
-		total, logs, err = getLogs(group, startTimestamp, endTimestamp, modelName, requestID, tokenID, tokenName, channelID, order, codeType, withBody, ip, page, perPage, resultOnly)
+		total, logs, err = getLogs(
+			group,
+			startTimestamp,
+			endTimestamp,
+			modelName,
+			requestID,
+			tokenID,
+			tokenName,
+			channelID,
+			order,
+			codeType,
+			code,
+			withBody,
+			ip,
+			page,
+			perPage,
+			resultOnly,
+		)
 		return err
 	})
 
@@ -706,6 +732,7 @@ func GetGroupLogs(
 	channelID int,
 	order string,
 	codeType CodeType,
+	code int,
 	withBody bool,
 	ip string,
 	page int,
@@ -727,7 +754,23 @@ func GetGroupLogs(
 
 	g.Go(func() error {
 		var err error
-		total, logs, err = getLogs(group, startTimestamp, endTimestamp, modelName, requestID, tokenID, tokenName, channelID, order, codeType, withBody, ip, page, perPage, resultOnly)
+		total, logs, err = getLogs(group,
+			startTimestamp,
+			endTimestamp,
+			modelName,
+			requestID,
+			tokenID,
+			tokenName,
+			channelID,
+			order,
+			codeType,
+			code,
+			withBody,
+			ip,
+			page,
+			perPage,
+			resultOnly,
+		)
 		return err
 	})
 
@@ -768,6 +811,7 @@ func buildSearchLogsQuery(
 	endTimestamp time.Time,
 	channelID int,
 	codeType CodeType,
+	code int,
 	ip string,
 	resultOnly bool,
 ) *gorm.DB {
@@ -813,6 +857,10 @@ func buildSearchLogsQuery(
 		tx = tx.Where("code = 200")
 	case CodeTypeError:
 		tx = tx.Where("code != 200")
+	default:
+		if code != 0 {
+			tx = tx.Where("code = ?", code)
+		}
 	}
 
 	if tokenID != 0 {
@@ -881,6 +929,7 @@ func searchLogs(
 	channelID int,
 	order string,
 	codeType CodeType,
+	code int,
 	withBody bool,
 	ip string,
 	page int,
@@ -904,6 +953,7 @@ func searchLogs(
 			endTimestamp,
 			channelID,
 			codeType,
+			code,
 			ip,
 			resultOnly,
 		).Count(&total).Error
@@ -921,6 +971,7 @@ func searchLogs(
 			endTimestamp,
 			channelID,
 			codeType,
+			code,
 			ip,
 			resultOnly,
 		)
@@ -960,6 +1011,7 @@ func SearchLogs(
 	channelID int,
 	order string,
 	codeType CodeType,
+	code int,
 	withBody bool,
 	ip string,
 	page int,
@@ -974,7 +1026,25 @@ func SearchLogs(
 
 	g.Go(func() error {
 		var err error
-		total, logs, err = searchLogs(group, keyword, requestID, tokenID, tokenName, modelName, startTimestamp, endTimestamp, channelID, order, codeType, withBody, ip, page, perPage, resultOnly)
+		total, logs, err = searchLogs(
+			group,
+			keyword,
+			requestID,
+			tokenID,
+			tokenName,
+			modelName,
+			startTimestamp,
+			endTimestamp,
+			channelID,
+			order,
+			codeType,
+			code,
+			withBody,
+			ip,
+			page,
+			perPage,
+			resultOnly,
+		)
 		return err
 	})
 
@@ -1009,6 +1079,7 @@ func SearchGroupLogs(
 	channelID int,
 	order string,
 	codeType CodeType,
+	code int,
 	withBody bool,
 	ip string,
 	page int,
@@ -1030,7 +1101,24 @@ func SearchGroupLogs(
 
 	g.Go(func() error {
 		var err error
-		total, logs, err = searchLogs(group, keyword, requestID, tokenID, tokenName, modelName, startTimestamp, endTimestamp, channelID, order, codeType, withBody, ip, page, perPage, resultOnly)
+		total, logs, err = searchLogs(group,
+			keyword,
+			requestID,
+			tokenID,
+			tokenName,
+			modelName,
+			startTimestamp,
+			endTimestamp,
+			channelID,
+			order,
+			codeType,
+			code,
+			withBody,
+			ip,
+			page,
+			perPage,
+			resultOnly,
+		)
 		return err
 	})