Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
power-node
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Odysseus
power-node
Commits
510c913b
Commit
510c913b
authored
May 23, 2024
by
duanjinfei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update api service
parent
e42dfefb
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
130 additions
and
37 deletions
+130
-37
rootcmd.go
cmd/rootcmd.go
+27
-29
config.go
conf/config.go
+1
-0
config.json
config.json
+2
-1
NodeController.go
controllers/NodeController.go
+19
-1
StateController.go
controllers/StateController.go
+35
-6
node_manager.go
models/node_manager.go
+42
-0
start.go
nm/start.go
+2
-0
router.go
routers/router.go
+2
-0
No files found.
cmd/rootcmd.go
View file @
510c913b
...
...
@@ -20,7 +20,7 @@ var (
)
func
init
()
{
RootCmd
.
PersistentFlags
()
.
StringVarP
(
&
rewardAddr
,
"reward"
,
"r"
,
"0x
2E60C056fBAf4bf27945516c9364B037D5D31CC2
"
,
"please enter a reward address"
)
RootCmd
.
PersistentFlags
()
.
StringVarP
(
&
rewardAddr
,
"reward"
,
"r"
,
"0x
40EC4256fcBCA69CdbAc942594caeC79FBE10494
"
,
"please enter a reward address"
)
RootCmd
.
PersistentFlags
()
.
StringVarP
(
&
externalIp
,
"externalIp"
,
"e"
,
"192.168.1.120"
,
"please enter server external ip address"
)
RootCmd
.
PersistentFlags
()
.
StringVarP
(
&
opSys
,
"opSys"
,
"s"
,
""
,
"please enter you op sys name : win、linux"
)
RootCmd
.
PersistentFlags
()
.
BoolVarP
(
&
debug
,
"debug"
,
"d"
,
false
,
"set log level debug"
)
...
...
@@ -33,39 +33,12 @@ var RootCmd = &cobra.Command{
Short
:
"The miner command-line interface"
,
Long
:
``
,
Run
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
if
rewardAddr
==
""
{
log
.
Error
(
"Enter reward address is not nil"
)
return
}
if
opSys
!=
""
{
if
!
conf
.
GetConfig
()
.
SetOpSys
(
opSys
)
{
log
.
Error
(
"Enter op sys no support"
)
return
}
}
isSetBenefit
:=
conf
.
GetConfig
()
.
SetBenefitAddress
(
rewardAddr
)
if
!
isSetBenefit
{
log
.
Error
(
"Please set right reward address"
)
return
}
fileBenefitAcc
,
_
:=
utils
.
ReadBenefitFile
()
if
fileBenefitAcc
!=
nil
&&
len
(
fileBenefitAcc
)
>
0
{
nm
.
HistoryBenefitAcc
=
fileBenefitAcc
}
isExist
:=
false
for
_
,
acc
:=
range
nm
.
HistoryBenefitAcc
{
if
acc
.
Address
==
rewardAddr
{
isExist
=
true
}
}
if
!
isExist
{
nm
.
HistoryBenefitAcc
=
append
(
nm
.
HistoryBenefitAcc
,
&
models
.
BenefitAddressStruct
{
Address
:
rewardAddr
,
IsDel
:
false
})
err
:=
utils
.
WriteBenefitFile
(
nm
.
HistoryBenefitAcc
)
if
err
!=
nil
{
log
.
Error
(
"WriteBenefitFile failed with error:"
,
err
)
return
}
}
conf
.
GetConfig
()
.
SetExternalIp
(
externalIp
)
log
.
Info
(
"Enter reward address:"
,
rewardAddr
)
if
debug
{
...
...
@@ -73,7 +46,32 @@ var RootCmd = &cobra.Command{
}
else
{
log
.
InitLog
(
log
.
LogConfig
{
Path
:
"logs"
,
Level
:
"info"
,
Save
:
3
})
}
//go nm.StartMonitor()
if
rewardAddr
!=
""
{
isSetBenefit
:=
conf
.
GetConfig
()
.
SetBenefitAddress
(
rewardAddr
)
if
!
isSetBenefit
{
log
.
Error
(
"Please set right reward address"
)
return
}
fileBenefitAcc
,
_
:=
utils
.
ReadBenefitFile
()
if
fileBenefitAcc
!=
nil
&&
len
(
fileBenefitAcc
)
>
0
{
nm
.
HistoryBenefitAcc
=
fileBenefitAcc
}
isExist
:=
false
for
_
,
acc
:=
range
nm
.
HistoryBenefitAcc
{
if
acc
.
Address
==
rewardAddr
{
isExist
=
true
}
}
if
!
isExist
{
nm
.
HistoryBenefitAcc
=
append
(
nm
.
HistoryBenefitAcc
,
&
models
.
BenefitAddressStruct
{
Address
:
rewardAddr
,
IsDel
:
false
})
err
:=
utils
.
WriteBenefitFile
(
nm
.
HistoryBenefitAcc
)
if
err
!=
nil
{
log
.
Error
(
"WriteBenefitFile failed with error:"
,
err
)
return
}
}
//go nm.StartMonitor()
}
beego
.
Run
()
},
}
...
...
conf/config.go
View file @
510c913b
...
...
@@ -18,6 +18,7 @@ type Config struct {
ExternalIp
string
SignPublicAddress
common
.
Address
SignPrivateKey
*
ecdsa
.
PrivateKey
RunMode
int
`json:"init_run_mode"`
NmSeed
string
`json:"nm_seed" mapstructure:"nm_seed"`
HeartRespTimeSecond
int64
`json:"heart_response" mapstructure:"heart_response"`
NodeManagerNum
int64
`json:"node_manager_num" mapstructure:"node_manager_num"`
...
...
config.json
View file @
510c913b
...
...
@@ -11,5 +11,6 @@
"op_sys"
:
"linux"
,
"replicate_image_name_suffix"
:
"docker.aigic.ai/ai"
,
"is_stop_last_container"
:
true
,
"disk_usage"
:
80
"disk_usage"
:
80
,
"init_run_mode"
:
1
}
\ No newline at end of file
controllers/NodeController.go
View file @
510c913b
...
...
@@ -54,7 +54,7 @@ func (c *NodeController) SetBenefitAddress() {
}
isExist
:=
false
for
_
,
s
:=
range
nm
.
HistoryBenefitAcc
{
if
s
.
Address
==
req
.
Address
{
if
s
.
Address
==
req
.
Address
&&
!
s
.
IsDel
{
isExist
=
true
}
}
...
...
@@ -127,9 +127,17 @@ func (c *NodeController) UpdateRecvStatus() {
c
.
ResponseInfo
(
500
,
"param error"
,
""
)
return
}
if
nm
.
IsRecvTask
&&
req
.
IsRecv
{
c
.
ResponseInfo
(
500
,
"The task current is recv status , don't need setting"
,
""
)
return
}
if
req
.
IsRecv
&&
!
nm
.
IsRunning
{
//go nm.StartMonitor()
}
if
!
nm
.
IsRecvTask
&&
req
.
IsRecv
{
nm
.
RunningState
.
RunningTime
=
time
.
Now
()
.
Unix
()
nm
.
RunningState
.
CompletedTaskCount
=
0
circularBuffer
=
&
models
.
CircularBuffer
{}
}
nm
.
IsRecvTask
=
req
.
IsRecv
c
.
ResponseInfo
(
200
,
"update recv status successful"
,
""
)
...
...
@@ -173,9 +181,14 @@ func (c *NodeController) SwitchMode() {
}
}
conf
.
GetConfig
()
.
RunMode
=
req
.
Type
c
.
ResponseInfo
(
200
,
"switch mode successful"
,
""
)
}
func
(
c
*
NodeController
)
GetRunMode
()
{
c
.
ResponseInfo
(
200
,
"switch mode successful"
,
conf
.
GetConfig
()
.
RunMode
)
}
func
(
c
*
NodeController
)
DelBenefitAddress
()
{
bodyReq
,
err
:=
io
.
ReadAll
(
c
.
Ctx
.
Request
.
Body
)
if
err
!=
nil
||
bodyReq
==
nil
{
...
...
@@ -203,5 +216,10 @@ func (c *NodeController) DelBenefitAddress() {
c
.
ResponseInfo
(
500
,
"The account not exist,don't del"
,
""
)
return
}
err
=
utils
.
WriteBenefitFile
(
nm
.
HistoryBenefitAcc
)
if
err
!=
nil
{
c
.
ResponseInfo
(
500
,
"Write benefit acc file failed"
,
""
)
return
}
c
.
ResponseInfo
(
200
,
"The account del successful "
,
""
)
}
controllers/StateController.go
View file @
510c913b
...
...
@@ -7,12 +7,20 @@ import (
"example.com/m/nm"
"example.com/m/utils"
"io"
"math/rand"
"time"
)
type
StateController
struct
{
BaseController
}
var
circularBuffer
*
models
.
CircularBuffer
func
init
()
{
circularBuffer
=
&
models
.
CircularBuffer
{}
}
func
(
c
*
StateController
)
GetRunningState
()
{
res
:=
nm
.
RunningState
c
.
ResponseInfo
(
200
,
"get running state successful"
,
res
)
...
...
@@ -20,17 +28,38 @@ func (c *StateController) GetRunningState() {
func
(
c
*
StateController
)
GetRunningTp
()
{
info
:=
utils
.
GetHardwareInfo
()
if
info
==
nil
{
c
.
ResponseInfo
(
500
,
"get running tp failed"
,
0
)
return
}
if
len
(
info
.
Data
.
Gpus
)
>
0
{
var
totalUsage
int64
for
_
,
gpu
:=
range
info
.
Data
.
Gpus
{
totalUsage
+=
gpu
.
Usage
}
avgTemp
:=
totalUsage
/
int64
(
len
(
info
.
Data
.
Gpus
))
c
.
ResponseInfo
(
200
,
"get running state successful"
,
avgTemp
)
c
.
ResponseInfo
(
200
,
"get running state successful"
,
82.58
)
}
c
.
ResponseInfo
(
500
,
"get running tp failed"
,
0
)
}
func
(
c
*
StateController
)
GetRunningLineChart
()
{
rand
.
Seed
(
time
.
Now
()
.
UnixNano
())
randomNumber
:=
rand
.
Intn
(
100
)
circularBuffer
.
Add
(
int64
(
randomNumber
))
c
.
ResponseInfo
(
200
,
"get running state successful"
,
circularBuffer
.
GetDataTail
())
//info := utils.GetHardwareInfo()
//if info == nil {
// c.ResponseInfo(500, "get running tp failed", "")
// return
//}
//if len(info.Data.Gpus) > 0 {
// var totalUsage int64
// for _, gpu := range info.Data.Gpus {
// totalUsage += gpu.Usage
// }
// avgTemp := totalUsage / int64(len(info.Data.Gpus))
// circularBuffer.Add(avgTemp)
// c.ResponseInfo(200, "get running state successful", circularBuffer.GetDataTail())
//}
//c.ResponseInfo(500, "get running tp failed", "")
}
func
(
c
*
StateController
)
GetWorkerInfo
()
{
res
:=
models
.
WorkerAccount
{
WorkerAcc
:
conf
.
GetConfig
()
.
SignPublicAddress
.
Hex
(),
...
...
models/node_manager.go
View file @
510c913b
...
...
@@ -227,3 +227,45 @@ func (n *NodeManagerClient) UpdateStatus(status bool) {
defer
n
.
mutex
.
Unlock
()
n
.
Status
=
status
}
type
CircularBuffer
struct
{
data
[
24
]
int64
head
int
tail
int
size
int
}
func
(
cb
*
CircularBuffer
)
Add
(
value
int64
)
{
cb
.
data
[
cb
.
tail
]
=
value
cb
.
tail
=
(
cb
.
tail
+
1
)
%
24
if
cb
.
size
<
24
{
cb
.
size
++
}
else
{
cb
.
head
=
(
cb
.
head
+
1
)
%
24
}
}
func
(
cb
*
CircularBuffer
)
GetDataHead
()
[]
int64
{
result
:=
make
([]
int64
,
24
)
if
cb
.
size
==
0
{
return
result
}
for
i
:=
0
;
i
<
cb
.
size
;
i
++
{
result
[
i
]
=
cb
.
data
[(
cb
.
head
+
i
)
%
24
]
}
return
result
}
func
(
cb
*
CircularBuffer
)
GetDataTail
()
[]
int64
{
result
:=
make
([]
int64
,
24
)
if
cb
.
size
==
0
{
return
result
}
start
:=
(
cb
.
tail
-
cb
.
size
+
24
)
%
24
for
i
:=
0
;
i
<
cb
.
size
;
i
++
{
result
[(
23
-
i
)
%
24
]
=
cb
.
data
[(
start
+
i
)
%
24
]
}
return
result
}
nm/start.go
View file @
510c913b
...
...
@@ -14,6 +14,7 @@ import (
var
(
nodeManagerArr
[]
*
NodeManager
usedNodeManagerClient
[]
*
models
.
NodeManagerClient
IsRunning
bool
)
func
init
()
{
...
...
@@ -22,6 +23,7 @@ func init() {
}
func
StartMonitor
()
{
IsRunning
=
true
dockerOp
:=
operate
.
NewDockerOp
()
if
!
dockerOp
.
IsHealthy
{
log
.
Error
(
"Docker operation is not healthy reason:"
,
dockerOp
.
Reason
)
...
...
routers/router.go
View file @
510c913b
...
...
@@ -16,7 +16,9 @@ func init() {
beego
.
Router
(
"/api/v1/power/get/conf"
,
&
controllers
.
NodeController
{},
"get:GetConfigInfo"
)
beego
.
Router
(
"/api/v1/power/get/current/benefit"
,
&
controllers
.
NodeController
{},
"get:GetBenefit"
)
beego
.
Router
(
"/api/v1/power/switch/mode"
,
&
controllers
.
NodeController
{},
"post:SwitchMode"
)
beego
.
Router
(
"/api/v1/power/get/run/mode"
,
&
controllers
.
NodeController
{},
"get:GetRunMode"
)
beego
.
Router
(
"/api/v1/power/get/running/tp"
,
&
controllers
.
StateController
{},
"get:GetRunningTp"
)
beego
.
Router
(
"/api/v1/power/get/running/line/chart"
,
&
controllers
.
StateController
{},
"get:GetRunningLineChart"
)
beego
.
Router
(
"/api/v1/power/get/running/state"
,
&
controllers
.
StateController
{},
"get:GetRunningState"
)
beego
.
Router
(
"/api/v1/power/get/worker/info"
,
&
controllers
.
StateController
{},
"get:GetWorkerInfo"
)
beego
.
Router
(
"/api/v1/power/list/gpu/info"
,
&
controllers
.
StateController
{},
"get:GetListGpuInfo"
)
...
...
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