Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
so-operation-api
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
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
smart-operation
so-operation-api
Commits
da1f6fa2
Commit
da1f6fa2
authored
Jul 31, 2023
by
李科
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 检查并创建OpenSearch索引
parent
392917c5
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
2 deletions
+45
-2
src/main.go
src/main.go
+6
-0
src/service/alert.go
src/service/alert.go
+39
-2
No files found.
src/main.go
View file @
da1f6fa2
...
@@ -45,6 +45,12 @@ func main() {
...
@@ -45,6 +45,12 @@ func main() {
// redis client
// redis client
go
client
.
GetRedisClient
()
go
client
.
GetRedisClient
()
// 初始化OpenSearch的索引
err
:=
service
.
CheckAndCreateIndex
()
if
err
!=
nil
{
conf
.
Logger
.
Fatal
(
"failed to init OpenSearch index."
,
zap
.
Error
(
err
))
}
//启动定时任务
//启动定时任务
cron
.
StartCron
()
cron
.
StartCron
()
service
.
PushWorkOrderMessage
()
service
.
PushWorkOrderMessage
()
...
...
src/service/alert.go
View file @
da1f6fa2
...
@@ -283,13 +283,50 @@ var (
...
@@ -283,13 +283,50 @@ var (
}`
)
}`
)
)
)
func
(
a
*
AlertSvc
)
CreateIndex
()
error
{
func
CheckAndCreateIndex
()
(
err
error
)
{
exist
,
err
:=
checkIndexExists
(
OpenSearchIndex
)
if
err
!=
nil
{
return
}
if
exist
{
return
nil
}
err
=
CreateIndex
(
OpenSearchIndex
)
if
err
!=
nil
{
return
err
}
return
nil
}
func
checkIndexExists
(
indexName
string
)
(
exist
bool
,
err
error
)
{
cli
,
err
:=
client
.
GetOpenSearch
()
if
err
!=
nil
{
return
}
req
:=
opensearchapi
.
IndicesExistsRequest
{
Index
:
[]
string
{
indexName
}}
res
,
err
:=
req
.
Do
(
context
.
Background
(),
cli
)
if
err
!=
nil
{
return
false
,
err
}
defer
res
.
Body
.
Close
()
if
res
.
StatusCode
==
http
.
StatusOK
{
return
true
,
nil
}
else
if
res
.
StatusCode
==
http
.
StatusNotFound
{
return
false
,
nil
}
else
{
return
false
,
fmt
.
Errorf
(
"请求失败,状态码:%d"
,
res
.
StatusCode
)
}
}
func
CreateIndex
(
indexName
string
)
error
{
cli
,
err
:=
client
.
GetOpenSearch
()
cli
,
err
:=
client
.
GetOpenSearch
()
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
res
:=
opensearchapi
.
IndicesCreateRequest
{
res
:=
opensearchapi
.
IndicesCreateRequest
{
Index
:
OpenSearchIndex
,
Index
:
indexName
,
Body
:
Mapping
,
Body
:
Mapping
,
}
}
do
,
err
:=
res
.
Do
(
context
.
Background
(),
cli
)
do
,
err
:=
res
.
Do
(
context
.
Background
(),
cli
)
...
...
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