Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Power Distribution
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
JetBrains YouTrack
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
KSZK
Sysadmin
Power Distribution
Commits
3ac6586c
Commit
3ac6586c
authored
Sep 2, 2021
by
Tóth Miklós Tibor
Browse files
Options
Downloads
Patches
Plain Diff
move api route, add visual feedback to reload button
parent
b57c0006
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
web/api.go
+1
-95
1 addition, 95 deletions
web/api.go
web/router.go
+1
-1
1 addition, 1 deletion
web/router.go
web/templates/index.html
+7
-1
7 additions, 1 deletion
web/templates/index.html
web/visjs.go
+99
-0
99 additions, 0 deletions
web/visjs.go
with
108 additions
and
97 deletions
web/api.go
+
1
−
95
View file @
3ac6586c
package
web
package
web
import
(
import
(
"fmt"
"git.sch.bme.hu/mikewashere/power-distribution/models"
"git.sch.bme.hu/mikewashere/power-distribution/models"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
"net/http"
"net/http"
)
)
type
visJS
struct
{
func
visJSApi
(
c
*
gin
.
Context
)
{
Nodes
[]
Node
`json:"nodes"`
Edges
[]
Edge
`json:"edges"`
}
type
Node
struct
{
Id
string
`json:"id"`
Value
float64
`json:"value"`
Label
string
`json:"label"`
Shape
string
`json:"shape"`
Color
string
`json:"color"`
}
type
Edge
struct
{
Id
string
`json:"id"`
From
string
`json:"from"`
To
string
`json:"to"`
Value
float64
`json:"value"`
Label
string
`json:"label"`
Arrows
string
`json:"arrows"`
Color
string
`json:"color"`
}
func
visJSFromGraph
(
g
*
models
.
GraphNode
)
*
visJS
{
if
_
,
ok
:=
g
.
ExtraData
[
"power"
];
!
ok
{
models
.
CalcPowerUsageForGraphNodes
(
g
)
}
devicesMap
:=
make
(
map
[
*
models
.
Device
]
bool
)
edges
:=
make
([]
Edge
,
0
)
processNode
(
g
,
devicesMap
,
&
edges
)
nodes
:=
make
([]
Node
,
len
(
devicesMap
))
i
:=
0
for
d
:=
range
devicesMap
{
shape
:=
"diamond"
color
:=
"#97C2FC"
switch
d
.
Type
{
case
"ups"
:
shape
=
"ellipse"
case
"powerstrip"
:
shape
=
"circle"
case
"uplink"
:
shape
=
"circle"
case
"powersource"
:
shape
=
"circle"
}
nodes
[
i
]
.
Label
=
d
.
Name
nodes
[
i
]
.
Value
=
d
.
PowerConsumption
nodes
[
i
]
.
Id
=
d
.
Id
nodes
[
i
]
.
Shape
=
shape
nodes
[
i
]
.
Color
=
color
i
++
}
return
&
visJS
{
Edges
:
edges
,
Nodes
:
nodes
,
}
}
func
api
(
c
*
gin
.
Context
)
{
g
,
e
:=
models
.
GetAllDevicesAsTree
(
c
)
g
,
e
:=
models
.
GetAllDevicesAsTree
(
c
)
if
e
!=
nil
{
if
e
!=
nil
{
c
.
Error
(
e
)
c
.
Error
(
e
)
}
}
c
.
JSON
(
http
.
StatusOK
,
visJSFromGraph
(
g
))
c
.
JSON
(
http
.
StatusOK
,
visJSFromGraph
(
g
))
}
}
func
processNode
(
g
*
models
.
GraphNode
,
devices
map
[
*
models
.
Device
]
bool
,
edges
*
[]
Edge
)
{
devices
[
g
.
Device
]
=
true
for
_
,
e
:=
range
g
.
Edges
{
power
:=
e
.
Node
.
Device
.
PowerConsumption
if
p
,
ok
:=
e
.
Node
.
ExtraData
[
"power"
];
ok
{
if
p
,
ok
:=
p
.
(
float64
);
ok
{
power
+=
p
}
}
label
:=
""
if
power
!=
0
{
label
=
fmt
.
Sprintf
(
"%gW"
,
power
)
}
*
edges
=
append
(
*
edges
,
Edge
{
Id
:
e
.
Id
,
From
:
g
.
Device
.
Id
,
To
:
e
.
Node
.
Device
.
Id
,
Value
:
power
,
Label
:
label
,
Arrows
:
"from"
,
Color
:
"#4449c9"
,
})
processNode
(
e
.
Node
,
devices
,
edges
)
}
}
This diff is collapsed.
Click to expand it.
web/router.go
+
1
−
1
View file @
3ac6586c
...
@@ -18,5 +18,5 @@ func init() {
...
@@ -18,5 +18,5 @@ func init() {
Router
.
GET
(
"/"
,
renderTemplate
)
Router
.
GET
(
"/"
,
renderTemplate
)
Router
.
StaticFS
(
"/static"
,
http
.
FS
(
staticfs
))
Router
.
StaticFS
(
"/static"
,
http
.
FS
(
staticfs
))
Router
.
GET
(
"/api
"
,
a
pi
)
Router
.
GET
(
"/api
/visjs"
,
visJSA
pi
)
}
}
This diff is collapsed.
Click to expand it.
web/templates/index.html
+
7
−
1
View file @
3ac6586c
...
@@ -33,8 +33,12 @@
...
@@ -33,8 +33,12 @@
let
data
=
{{
.
visjs
}};
let
data
=
{{
.
visjs
}};
let
nodes
=
new
vis
.
DataSet
(
data
.
nodes
);
let
nodes
=
new
vis
.
DataSet
(
data
.
nodes
);
let
edges
=
new
vis
.
DataSet
(
data
.
edges
);
let
edges
=
new
vis
.
DataSet
(
data
.
edges
);
const
reloadBtn
=
document
.
getElementById
(
"
reload
"
);
const
reload
=
async
()
=>
{
const
reload
=
async
()
=>
{
const
response
=
await
fetch
(
'
/api
'
)
reloadBtn
.
innerText
=
"
reloading...
"
;
reloadBtn
.
disabled
=
true
;
const
response
=
await
fetch
(
'
/api/visjs
'
)
data
=
await
response
.
json
()
data
=
await
response
.
json
()
data
.
nodes
.
forEach
(
e
=>
{
data
.
nodes
.
forEach
(
e
=>
{
...
@@ -59,6 +63,8 @@
...
@@ -59,6 +63,8 @@
toRemoveEdges
.
forEach
(
edge
=>
{
toRemoveEdges
.
forEach
(
edge
=>
{
edges
.
remove
(
edge
)
edges
.
remove
(
edge
)
})
})
reloadBtn
.
innerText
=
"
reload
"
;
reloadBtn
.
disabled
=
false
;
}
}
const
container
=
document
.
getElementById
(
"
canvas
"
);
const
container
=
document
.
getElementById
(
"
canvas
"
);
const
options
=
{
const
options
=
{
...
...
This diff is collapsed.
Click to expand it.
web/visjs.go
0 → 100644
+
99
−
0
View file @
3ac6586c
package
web
import
(
"fmt"
"git.sch.bme.hu/mikewashere/power-distribution/models"
)
type
visJS
struct
{
Nodes
[]
Node
`json:"nodes"`
Edges
[]
Edge
`json:"edges"`
}
type
Node
struct
{
Id
string
`json:"id"`
Value
float64
`json:"value"`
Label
string
`json:"label"`
Shape
string
`json:"shape"`
Color
string
`json:"color"`
}
type
Edge
struct
{
Id
string
`json:"id"`
From
string
`json:"from"`
To
string
`json:"to"`
Value
float64
`json:"value"`
Label
string
`json:"label"`
Arrows
string
`json:"arrows"`
Color
string
`json:"color"`
}
func
visJSFromGraph
(
g
*
models
.
GraphNode
)
*
visJS
{
if
_
,
ok
:=
g
.
ExtraData
[
"power"
];
!
ok
{
models
.
CalcPowerUsageForGraphNodes
(
g
)
}
devicesMap
:=
make
(
map
[
*
models
.
Device
]
bool
)
edges
:=
make
([]
Edge
,
0
)
processNode
(
g
,
devicesMap
,
&
edges
)
nodes
:=
make
([]
Node
,
len
(
devicesMap
))
i
:=
0
for
d
:=
range
devicesMap
{
shape
:=
"diamond"
color
:=
"#97C2FC"
switch
d
.
Type
{
case
"ups"
:
shape
=
"ellipse"
case
"powerstrip"
:
shape
=
"circle"
case
"uplink"
:
shape
=
"circle"
case
"powersource"
:
shape
=
"circle"
}
nodes
[
i
]
.
Label
=
d
.
Name
nodes
[
i
]
.
Value
=
d
.
PowerConsumption
nodes
[
i
]
.
Id
=
d
.
Id
nodes
[
i
]
.
Shape
=
shape
nodes
[
i
]
.
Color
=
color
i
++
}
return
&
visJS
{
Edges
:
edges
,
Nodes
:
nodes
,
}
}
func
processNode
(
g
*
models
.
GraphNode
,
devices
map
[
*
models
.
Device
]
bool
,
edges
*
[]
Edge
)
{
devices
[
g
.
Device
]
=
true
for
_
,
e
:=
range
g
.
Edges
{
power
:=
e
.
Node
.
Device
.
PowerConsumption
if
p
,
ok
:=
e
.
Node
.
ExtraData
[
"power"
];
ok
{
if
p
,
ok
:=
p
.
(
float64
);
ok
{
power
+=
p
}
}
label
:=
""
if
power
!=
0
{
label
=
fmt
.
Sprintf
(
"%gW"
,
power
)
}
*
edges
=
append
(
*
edges
,
Edge
{
Id
:
e
.
Id
,
From
:
g
.
Device
.
Id
,
To
:
e
.
Node
.
Device
.
Id
,
Value
:
power
,
Label
:
label
,
Arrows
:
"from"
,
Color
:
"#4449c9"
,
})
processNode
(
e
.
Node
,
devices
,
edges
)
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment