Skip to content

Commit e984e73

Browse files
authored
Merge pull request #37 from bluewave-labs/ci/lint
Remove unimplemented codes & Lint Fixes
2 parents aace293 + ba2ab5f commit e984e73

File tree

13 files changed

+61
-134
lines changed

13 files changed

+61
-134
lines changed

.github/workflows/lint.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: lint
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
- develop
8+
9+
jobs:
10+
lint:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v4
15+
with:
16+
fetch-depth: 0
17+
18+
- name: Golangci-lint
19+
uses: golangci/[email protected]

cmd/capture/main.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var appConfig = config.NewConfig(
2323
func main() {
2424
r := gin.Default()
2525
apiV1 := r.Group("/api/v1")
26-
apiV1.Use(middleware.AuthRequired(appConfig.ApiSecret))
26+
apiV1.Use(middleware.AuthRequired(appConfig.APISecret))
2727

2828
// Health Check
2929
apiV1.GET("/health", handler.Health)
@@ -35,12 +35,10 @@ func main() {
3535
apiV1.GET("/metrics/disk", handler.MetricsDisk)
3636
apiV1.GET("/metrics/host", handler.MetricsHost)
3737

38-
// WebSocket Connection
39-
apiV1.GET("/ws/metrics", handler.WebSocket)
40-
4138
server := &http.Server{
42-
Addr: ":" + appConfig.Port,
43-
Handler: r.Handler(),
39+
Addr: ":" + appConfig.Port,
40+
Handler: r.Handler(),
41+
ReadHeaderTimeout: 5 * time.Second,
4442
}
4543

4644
// Graceful shutdown
@@ -57,10 +55,9 @@ func main() {
5755
if err := server.Shutdown(ctx); err != nil {
5856
log.Fatal("server shutdown:", err)
5957
}
60-
select {
61-
case <-ctx.Done():
62-
log.Println("timeout of 5 seconds.")
63-
}
58+
<-ctx.Done()
59+
log.Println("timeout of 5 seconds.")
60+
6461
log.Println("server exiting")
6562
}
6663

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ go 1.23.1
44

55
require (
66
github.com/gin-gonic/gin v1.10.0
7-
github.com/gorilla/websocket v1.5.3
87
github.com/shirou/gopsutil/v4 v4.24.9
98
github.com/stretchr/testify v1.9.0
109
)

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
3434
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
3535
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
3636
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
37-
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
38-
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
3937
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
4038
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
4139
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=

internal/config/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import "log"
44

55
type Config struct {
66
Port string
7-
ApiSecret string
7+
APISecret string
88
}
99

1010
var defaultPort = "59232"
@@ -22,13 +22,13 @@ func NewConfig(port string, apiSecret string) *Config {
2222

2323
return &Config{
2424
Port: port,
25-
ApiSecret: apiSecret,
25+
APISecret: apiSecret,
2626
}
2727
}
2828

2929
func Default() *Config {
3030
return &Config{
3131
Port: defaultPort,
32-
ApiSecret: "",
32+
APISecret: "",
3333
}
3434
}

internal/handler/metrics.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ func handleMetricResponse(c *gin.Context, metrics metric.Metric, errs []metric.C
1010
if len(errs) > 0 {
1111
statusCode = 207
1212
}
13-
c.JSON(statusCode, metric.ApiResponse{
13+
c.JSON(statusCode, metric.APIResponse{
1414
Data: metrics,
1515
Errors: errs,
1616
})
17-
return
1817
}
1918

2019
func Metrics(c *gin.Context) {
@@ -23,7 +22,7 @@ func Metrics(c *gin.Context) {
2322
}
2423

2524
func MetricsCPU(c *gin.Context) {
26-
cpuMetrics, metricsErrs := metric.CollectCpuMetrics()
25+
cpuMetrics, metricsErrs := metric.CollectCPUMetrics()
2726
handleMetricResponse(c, cpuMetrics, metricsErrs)
2827
}
2928

internal/handler/websocket.go

Lines changed: 0 additions & 79 deletions
This file was deleted.

internal/metric/cpu.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/shirou/gopsutil/v4/cpu"
88
)
99

10-
func CollectCpuMetrics() (*CpuData, []CustomErr) {
10+
func CollectCPUMetrics() (*CPUData, []CustomErr) {
1111
// Collect CPU Core Counts
1212
cpuPhysicalCoreCount, cpuPhysicalErr := cpu.Counts(false)
1313
cpuLogicalCoreCount, cpuLogicalErr := cpu.Counts(true)
@@ -60,7 +60,7 @@ func CollectCpuMetrics() (*CpuData, []CustomErr) {
6060
}
6161

6262
// Collect CPU Temperature from sysfs
63-
cpuTemp, cpuTempErr := sysfs.CpuTemperature()
63+
cpuTemp, cpuTempErr := sysfs.CPUTemperature()
6464

6565
if cpuTempErr != nil {
6666
cpuErrors = append(cpuErrors, CustomErr{
@@ -70,7 +70,7 @@ func CollectCpuMetrics() (*CpuData, []CustomErr) {
7070
cpuTemp = nil
7171
}
7272

73-
cpuCurrentFrequency, cpuCurFreqErr := sysfs.CpuCurrentFrequency()
73+
cpuCurrentFrequency, cpuCurFreqErr := sysfs.CPUCurrentFrequency()
7474
if cpuCurFreqErr != nil {
7575
cpuErrors = append(cpuErrors, CustomErr{
7676
Metric: []string{"cpu.current_frequency"},
@@ -79,7 +79,7 @@ func CollectCpuMetrics() (*CpuData, []CustomErr) {
7979
cpuCurrentFrequency = 0
8080
}
8181

82-
return &CpuData{
82+
return &CPUData{
8383
PhysicalCore: cpuPhysicalCoreCount,
8484
LogicalCore: cpuLogicalCoreCount,
8585
Frequency: cpuFrequency,

internal/metric/disk.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,15 @@ import (
1010
func CollectDiskMetrics() (MetricsSlice, []CustomErr) {
1111
defaultDiskData := []*DiskData{
1212
{
13-
Device: "unknown",
14-
ReadSpeedBytes: nil,
15-
WriteSpeedBytes: nil,
16-
TotalBytes: nil,
17-
FreeBytes: nil,
18-
UsagePercent: nil,
13+
Device: "unknown",
14+
TotalBytes: nil,
15+
FreeBytes: nil,
16+
UsagePercent: nil,
1917
},
2018
}
2119
var diskErrors []CustomErr
2220
var metricsSlice MetricsSlice
23-
var checkedSlice []string // To keep track of checked partitions
21+
var checkedSlice = make([]string, 0, 10) // To keep track of checked partitions
2422

2523
// Set all flag to "false" to get only necessary partitions
2624
// Avoiding unnecessary partitions like /run/user/1000, /run/credentials
@@ -52,12 +50,10 @@ func CollectDiskMetrics() (MetricsSlice, []CustomErr) {
5250

5351
checkedSlice = append(checkedSlice, p.Device)
5452
metricsSlice = append(metricsSlice, &DiskData{
55-
Device: p.Device,
56-
ReadSpeedBytes: nil, // TODO: Implement
57-
WriteSpeedBytes: nil, // TODO: Implement
58-
TotalBytes: &diskUsage.Total,
59-
FreeBytes: &diskUsage.Free,
60-
UsagePercent: RoundFloatPtr(diskUsage.UsedPercent/100, 4),
53+
Device: p.Device,
54+
TotalBytes: &diskUsage.Total,
55+
FreeBytes: &diskUsage.Free,
56+
UsagePercent: RoundFloatPtr(diskUsage.UsedPercent/100, 4),
6157
})
6258
}
6359

internal/metric/metric.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ type Metric interface {
88
isMetric()
99
}
1010

11-
type ApiResponse struct {
11+
type APIResponse struct {
1212
Data Metric `json:"data"`
1313
Errors []CustomErr `json:"errors"`
1414
}
1515

1616
type AllMetrics struct {
17-
Cpu CpuData `json:"cpu"`
17+
CPU CPUData `json:"cpu"`
1818
Memory MemoryData `json:"memory"`
1919
Disk MetricsSlice `json:"disk"`
2020
Host HostData `json:"host"`
@@ -27,7 +27,7 @@ type CustomErr struct {
2727
Error string `json:"err"`
2828
}
2929

30-
type CpuData struct {
30+
type CPUData struct {
3131
PhysicalCore int `json:"physical_core"` // Physical cores
3232
LogicalCore int `json:"logical_core"` // Logical cores aka Threads
3333
Frequency float64 `json:"frequency"` // Frequency in mHz
@@ -37,7 +37,7 @@ type CpuData struct {
3737
UsagePercent float64 `json:"usage_percent"` // Usage percentage //* Total - Idle / Total
3838
}
3939

40-
func (c CpuData) isMetric() {}
40+
func (c CPUData) isMetric() {}
4141

4242
type MemoryData struct {
4343
TotalBytes uint64 `json:"total_bytes"` // Total space in bytes
@@ -49,12 +49,10 @@ type MemoryData struct {
4949
func (m MemoryData) isMetric() {}
5050

5151
type DiskData struct {
52-
Device string `json:"device"` // Device
53-
ReadSpeedBytes *uint64 `json:"read_speed_bytes"` // TODO: Implement
54-
WriteSpeedBytes *uint64 `json:"write_speed_bytes"` // TODO: Implement
55-
TotalBytes *uint64 `json:"total_bytes"` // Total space of device in bytes
56-
FreeBytes *uint64 `json:"free_bytes"` // Free space of device in bytes
57-
UsagePercent *float64 `json:"usage_percent"` // Usage Percent of device
52+
Device string `json:"device"` // Device
53+
TotalBytes *uint64 `json:"total_bytes"` // Total space of device in bytes
54+
FreeBytes *uint64 `json:"free_bytes"` // Free space of device in bytes
55+
UsagePercent *float64 `json:"usage_percent"` // Usage Percent of device
5856
}
5957

6058
func (d DiskData) isMetric() {}
@@ -68,7 +66,7 @@ type HostData struct {
6866
func (h HostData) isMetric() {}
6967

7068
func GetAllSystemMetrics() (AllMetrics, []CustomErr) {
71-
cpu, cpuErr := CollectCpuMetrics()
69+
cpu, cpuErr := CollectCPUMetrics()
7270
memory, memErr := CollectMemoryMetrics()
7371
disk, diskErr := CollectDiskMetrics()
7472
host, hostErr := GetHostInformation()
@@ -92,7 +90,7 @@ func GetAllSystemMetrics() (AllMetrics, []CustomErr) {
9290
}
9391

9492
return AllMetrics{
95-
Cpu: *cpu,
93+
CPU: *cpu,
9694
Memory: *memory,
9795
Disk: disk,
9896
Host: *host,

internal/metric/metric_math.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ func RoundFloat(val float64, precision uint) float64 {
2020
return prc
2121
}
2222

23-
func RandomIntPtr(max int64) *int {
24-
n, err := rand.Int(rand.Reader, big.NewInt(max))
23+
func RandomIntPtr(maximum int64) *int {
24+
n, err := rand.Int(rand.Reader, big.NewInt(maximum))
2525
if err != nil {
2626
panic(err) // handle error appropriately in production
2727
}

internal/sysfs/cpu.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func readTempFile(path string) (float32, error) {
2222
return float32(temp) / 1000, nil
2323
}
2424

25-
func readCpuFreqFile(path string) (int, error) {
25+
func readCPUFreqFile(path string) (int, error) {
2626
data, err := os.ReadFile(path)
2727
if err != nil {
2828
return 0, err
@@ -36,7 +36,7 @@ func readCpuFreqFile(path string) (int, error) {
3636
return freq, nil
3737
}
3838

39-
func CpuTemperature() ([]float32, error) {
39+
func CPUTemperature() ([]float32, error) {
4040
// Look in all these folders for core temp
4141
corePaths := []string{
4242
"/sys/devices/platform/coretemp.0/hwmon/hwmon*/temp*_input",
@@ -74,8 +74,8 @@ func CpuTemperature() ([]float32, error) {
7474
return temps, nil
7575
}
7676

77-
func CpuCurrentFrequency() (int, error) {
78-
frequency, cpuFrequencyError := readCpuFreqFile("/sys/devices/system/cpu/cpufreq/policy0/scaling_cur_freq")
77+
func CPUCurrentFrequency() (int, error) {
78+
frequency, cpuFrequencyError := readCPUFreqFile("/sys/devices/system/cpu/cpufreq/policy0/scaling_cur_freq")
7979

8080
if cpuFrequencyError != nil {
8181
return 0, cpuFrequencyError

internal/sysfs/exec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
func ShellExec(c string) (string, error) {
1010
if strings.Contains(c, "&&") || strings.Contains(c, "||") || strings.Contains(c, ";") {
11-
return "", errors.New("It's forbidden to execute consecutive commands")
11+
return "", errors.New("it's forbidden to execute consecutive commands")
1212
}
1313
cmd := exec.Command("bash", "-c", c)
1414

0 commit comments

Comments
 (0)