Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
E
elp
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
崔为之
elp
Commits
4360aaed
Commit
4360aaed
authored
Nov 21, 2023
by
崔为之
💪🏽
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update project
parent
beb4afc8
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
79 additions
and
14 deletions
+79
-14
application/dao/log.py
application/dao/log.py
+5
-4
application/libs/__init__.py
application/libs/__init__.py
+2
-1
application/libs/error/__init__.py
application/libs/error/__init__.py
+1
-1
application/libs/error/exceptions.py
application/libs/error/exceptions.py
+5
-0
application/libs/flask_consul/consul.py
application/libs/flask_consul/consul.py
+6
-3
application/libs/response/__init__.py
application/libs/response/__init__.py
+13
-0
application/libs/response/response.py
application/libs/response/response.py
+34
-0
application/services/log.py
application/services/log.py
+1
-1
application/views/log/log.py
application/views/log/log.py
+12
-4
No files found.
application/dao/log.py
View file @
4360aaed
...
@@ -16,6 +16,7 @@ from typing import Generator
...
@@ -16,6 +16,7 @@ from typing import Generator
from
loguru
import
logger
from
loguru
import
logger
from
sqlalchemy
import
text
from
sqlalchemy
import
text
from
application.libs
import
ServerError
from
application.libs.helper
import
MySQLHelper
from
application.libs.helper
import
MySQLHelper
from
application.models
import
Log
from
application.models
import
Log
from
application.utils
import
ElasticsearchUtil
,
ParseUtil
from
application.utils
import
ElasticsearchUtil
,
ParseUtil
...
@@ -44,14 +45,14 @@ class LogDao:
...
@@ -44,14 +45,14 @@ class LogDao:
dict: The data returned from Elasticsearch.
dict: The data returned from Elasticsearch.
Raises:
Raises:
S
ystem
Error: If none of the required parameters are provided.
S
erver
Error: If none of the required parameters are provided.
"""
"""
if
sid
is
not
None
:
if
sid
is
not
None
:
return
ElasticsearchUtil
.
scroll_search
(
sid
)
return
ElasticsearchUtil
.
scroll_search
(
sid
)
elif
index
is
not
None
and
dsl
is
not
None
:
elif
index
is
not
None
and
dsl
is
not
None
:
return
ElasticsearchUtil
.
search
(
index
,
dsl
)
return
ElasticsearchUtil
.
search
(
index
,
dsl
)
else
:
else
:
raise
S
ystem
Error
(
'Could not get data from Elasticsearch'
)
raise
S
erver
Error
(
'Could not get data from Elasticsearch'
)
@
classmethod
@
classmethod
def
get_mdata
(
cls
,
data
:
dict
)
->
list
:
def
get_mdata
(
cls
,
data
:
dict
)
->
list
:
...
@@ -70,7 +71,7 @@ class LogDao:
...
@@ -70,7 +71,7 @@ class LogDao:
mdata
=
data
.
get
(
'hits'
).
get
(
'hits'
)
mdata
=
data
.
get
(
'hits'
).
get
(
'hits'
)
if
not
mdata
:
if
not
mdata
:
logger
.
error
(
'the mdata is an empty list ...'
)
logger
.
error
(
'the mdata is an empty list ...'
)
raise
S
ystem
Error
(
'the mdata is an empty list ...'
)
raise
S
erver
Error
(
'the mdata is an empty list ...'
)
return
mdata
return
mdata
@
classmethod
@
classmethod
...
@@ -184,7 +185,7 @@ class LogDao:
...
@@ -184,7 +185,7 @@ class LogDao:
except
Exception
as
e
:
except
Exception
as
e
:
# Log the error and raise a SystemError.
# Log the error and raise a SystemError.
logger
.
error
(
f
'The error:
{
e
}
'
)
logger
.
error
(
f
'The error:
{
e
}
'
)
raise
S
ystemError
(
)
raise
S
erverError
(
'Operate data error'
)
else
:
else
:
# Log the success of the process.
# Log the success of the process.
logger
.
debug
(
'The process of inserting data succeed!'
)
logger
.
debug
(
'The process of inserting data succeed!'
)
...
...
application/libs/__init__.py
View file @
4360aaed
...
@@ -11,7 +11,8 @@
...
@@ -11,7 +11,8 @@
"""
"""
from
.config
import
ConsulConfig
,
LocalConfig
from
.config
import
ConsulConfig
,
LocalConfig
from
.error
import
ConfigKeyError
from
.error
import
ConfigKeyError
,
ServerError
from
.flask_consul
import
FlaskConsulService
from
.flask_consul
import
FlaskConsulService
from
.flask_elasticsearch
import
FlaskElasticsearch
from
.flask_elasticsearch
import
FlaskElasticsearch
from
.flask_loguru
import
FlaskLoguru
from
.flask_loguru
import
FlaskLoguru
from
.response
import
JsonResponse
application/libs/error/__init__.py
View file @
4360aaed
...
@@ -10,4 +10,4 @@
...
@@ -10,4 +10,4 @@
# @Description : error 自定义模块
# @Description : error 自定义模块
"""
"""
from
.exceptions
import
ConfigKeyError
from
.exceptions
import
ConfigKeyError
,
ServerError
application/libs/error/exceptions.py
View file @
4360aaed
...
@@ -18,3 +18,8 @@ class ConfigKeyError(Exception):
...
@@ -18,3 +18,8 @@ class ConfigKeyError(Exception):
def
__init__
(
self
,
key
):
def
__init__
(
self
,
key
):
self
.
key
=
key
self
.
key
=
key
super
().
__init__
(
f
'Key
{
self
.
key
}
not found in configuration'
)
super
().
__init__
(
f
'Key
{
self
.
key
}
not found in configuration'
)
class
ServerError
(
Exception
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
pass
application/libs/flask_consul/consul.py
View file @
4360aaed
...
@@ -14,7 +14,9 @@
...
@@ -14,7 +14,9 @@
import
atexit
import
atexit
import
consul
import
consul
from
flask
import
Flask
from
flask
import
Flask
from
loguru
import
logger
from
application.libs
import
ServerError
from
application.libs.helper
import
ConfigHelper
from
application.libs.helper
import
ConfigHelper
from
application.libs.helper
import
EnvVarHelper
from
application.libs.helper
import
EnvVarHelper
...
@@ -59,8 +61,8 @@ class FlaskConsulService:
...
@@ -59,8 +61,8 @@ class FlaskConsulService:
if
self
.
service_id
is
None
:
if
self
.
service_id
is
None
:
self
.
service_id
=
f
'
{
service_name
}
-
{
service_address
}
-
{
service_port
}
'
self
.
service_id
=
f
'
{
service_name
}
-
{
service_address
}
-
{
service_port
}
'
try
:
try
:
check
=
consul
.
Check
().
tcp
(
host
=
service_address
,
port
=
service_port
,
interval
=
check_interval
,
timeout
=
timeout
,
check
=
consul
.
Check
().
tcp
(
host
=
service_address
,
port
=
service_port
,
interval
=
check_interval
,
deregister
=
deregister_after
)
timeout
=
timeout
,
deregister
=
deregister_after
)
if
self
.
client
is
None
:
if
self
.
client
is
None
:
self
.
client
=
consul
.
Consul
(
**
env_vars
)
self
.
client
=
consul
.
Consul
(
**
env_vars
)
self
.
client
.
agent
.
service
.
register
(
self
.
client
.
agent
.
service
.
register
(
...
@@ -71,7 +73,8 @@ class FlaskConsulService:
...
@@ -71,7 +73,8 @@ class FlaskConsulService:
check
=
check
check
=
check
)
)
except
Exception
as
e
:
except
Exception
as
e
:
print
(
e
)
logger
.
error
(
f
'Failed to register service, error:
{
e
}
'
)
ServerError
(
'Failed to register service'
)
def
deregister_service
(
self
):
def
deregister_service
(
self
):
self
.
client
.
agent
.
service
.
deregister
(
self
.
service_id
)
self
.
client
.
agent
.
service
.
deregister
(
self
.
service_id
)
application/libs/response/__init__.py
0 → 100644
View file @
4360aaed
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
# @Version : Python 3.11.4
# @Software : Sublime Text 4
# @Author : StudentCWZ
# @Email : StudentCWZ@outlook.com
# @Date : 2023/11/21 14:09
# @File : __init__.py
# @Description :
"""
from
.response
import
JsonResponse
application/libs/response/response.py
0 → 100644
View file @
4360aaed
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
# @Version : Python 3.11.4
# @Software : Sublime Text 4
# @Author : StudentCWZ
# @Email : StudentCWZ@outlook.com
# @Date : 2023/11/21 14:10
# @File : response.py
# @Description :
"""
from
flask
import
Flask
,
jsonify
app
=
Flask
(
__name__
)
class
JsonResponse
:
@
staticmethod
def
success
(
data
,
message
=
'Success'
):
return
jsonify
({
'status'
:
'success'
,
'message'
:
message
,
'data'
:
data
,
}),
200
@
staticmethod
def
error
(
message
=
'Error'
,
status_code
=
400
):
return
jsonify
({
'status'
:
'error'
,
'message'
:
message
,
}),
status_code
application/services/log.py
View file @
4360aaed
...
@@ -32,7 +32,7 @@ class LogService:
...
@@ -32,7 +32,7 @@ class LogService:
The result of the log parsing as an integer.
The result of the log parsing as an integer.
Raises:
Raises:
S
ystem
Error: If there is an issue with the process.
S
erver
Error: If there is an issue with the process.
"""
"""
# Retrieve the start and end date from the given item.
# Retrieve the start and end date from the given item.
...
...
application/views/log/log.py
View file @
4360aaed
...
@@ -17,6 +17,8 @@ from pydantic import ValidationError
...
@@ -17,6 +17,8 @@ from pydantic import ValidationError
from
application.schemas
import
ParseLogRequestItem
from
application.schemas
import
ParseLogRequestItem
from
application.services
import
LogService
from
application.services
import
LogService
from
application.libs
import
JsonResponse
from
application.libs
import
ServerError
# Define the log API blueprint
# Define the log API blueprint
...
@@ -37,15 +39,21 @@ def parse():
...
@@ -37,15 +39,21 @@ def parse():
# If no data provided, return error message
# If no data provided, return error message
if
not
json_data
:
if
not
json_data
:
logger
.
error
(
'No input data provided'
)
logger
.
error
(
'No input data provided'
)
return
jsonify
({
"message"
:
"No input data provided"
}),
400
return
JsonResponse
.
error
(
message
=
'No input data provided'
,
status_code
=
400
)
try
:
try
:
# Validate input data
# Validate input data
item
=
ParseLogRequestItem
(
**
json_data
)
item
=
ParseLogRequestItem
(
**
json_data
)
except
ValidationError
as
e
:
except
ValidationError
as
e
:
# If validation fails, return error message
# If validation fails, return error message
return
jsonify
({
"message"
:
"Invalid input data"
,
"errors"
:
e
.
errors
()}),
400
logger
.
error
(
f
'Invalid input data, error:
{
e
}
'
)
return
JsonResponse
.
error
(
message
=
'Invalid input data'
,
status_code
=
400
)
# If validation passes, parse logs and return total number of logs parsed
# If validation passes, parse logs and return total number of logs parsed
total
=
LogService
.
parse
(
item
)
try
:
return
jsonify
(
search_total
=
total
),
200
total
=
LogService
.
parse
(
item
)
except
ServerError
as
e
:
logger
.
error
(
'Internal server error'
)
return
JsonResponse
.
error
(
message
=
'Internal server error'
,
status_code
=
500
)
else
:
return
JsonResponse
.
success
(
message
=
'Successfully parsed'
,
data
=
total
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment