Skip to content

Commit

Permalink
fixed bug
Browse files Browse the repository at this point in the history
  • Loading branch information
goozp committed Aug 5, 2019
1 parent 948ce5d commit d33aeaf
Show file tree
Hide file tree
Showing 17 changed files with 173 additions and 89 deletions.
2 changes: 1 addition & 1 deletion console/console.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!DOCTYPE html><html><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><title>Puti</title><link rel="shortcut icon" href=/favicon.ico><link href=/static/css/app.b87fea970dd2f0f3cc6d1972231b6c33.css rel=stylesheet></head><body><div id=app></div><script type=text/javascript src=/static/js/manifest.9b1e33fc6eaad7508c0e.js></script><script type=text/javascript src=/static/js/vendor.da81a196354a14e1415f.js></script><script type=text/javascript src=/static/js/app.d78ddc8024cac44b52c2.js></script></body></html>
<!DOCTYPE html><html><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><title>Puti</title><link rel="shortcut icon" href=/favicon.ico><link href=/static/css/app.b87fea970dd2f0f3cc6d1972231b6c33.css rel=stylesheet></head><body><div id=app></div><script type=text/javascript src=/static/js/manifest.6b5ad7a62c56237a5629.js></script><script type=text/javascript src=/static/js/vendor.da81a196354a14e1415f.js></script><script type=text/javascript src=/static/js/app.d09294188a83aeaffc2a.js></script></body></html>
1 change: 1 addition & 0 deletions console/static/js/2.cad5fba51e64efff154a.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion console/static/js/2.d0c3feebd45cc24c524f.js

This file was deleted.

1 change: 1 addition & 0 deletions console/static/js/app.d09294188a83aeaffc2a.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion console/static/js/app.d78ddc8024cac44b52c2.js

This file was deleted.

1 change: 1 addition & 0 deletions console/static/js/manifest.6b5ad7a62c56237a5629.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion console/static/js/manifest.9b1e33fc6eaad7508c0e.js

This file was deleted.

7 changes: 5 additions & 2 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# Changelog

## v0.2.1 Bug fixed and small adjustment [2019-08]
## v0.2.1 Bug fixed and small adjustment [2019-08-06]

### 功能和优化
- 优化后台管理样式
- 调整后台管理样式
- 优化 Lin 主题的一些样式

### Bug 修复
- 修复创建或编辑文章时,选择专题的树形结构取值不正确导致文章数计算不对
- 修复 Lin 主题页面和归档页侧栏无法展开
- 修复仪表盘系统信息 widget 的显示错误

***

Expand Down
15 changes: 15 additions & 0 deletions docs/CHANGELOG_EN.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Changelog

## v0.2.1 Bug fixed and small adjustment [2019-08-06]

### Feature and Enhancement
- Adjusted background management styles
- Optimized some styles of theme Lin

### Bug Fixed
- Fixed the tree structure of subject which cause the number of articles is incorrectly calculate when created or edited article
- Fixed the theme Lin's page and archive page sidebar cannot be expanded
- Fixed display error of dashboard system information widget

***

## v0.2.0 New Theme [2019-07-21]

### Feature and Enhancement
Expand All @@ -13,6 +26,8 @@
- Fixed catergory select to strectly tree when creating or editing article.
- Fixed theme Emma import katex css in wrong way

***

## v0.1.0 Fisrt Version [2019-04-01]

The first version, which contains basic features, has been initially formed. Contains the following:
Expand Down
2 changes: 1 addition & 1 deletion internal/backend/handler/statistics/dashboard..go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// Dashboard get some basic statistics
func Dashboard(c *gin.Context) {
data, err := service.GetDashdoardStatisticsData()
data, err := service.GetDashboardStatisticsData()
if err != nil {
Response.SendResponse(c, errno.ErrDatabase, nil)
return
Expand Down
17 changes: 9 additions & 8 deletions internal/backend/service/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@ package service

import "github.com/puti-projects/puti/internal/common/model"

// DashdoardData some statistics index
type DashdoardData struct {
// DashboardData some statistics index
type DashboardData struct {
TotalViews uint64
TotalComments uint64
TotalArticles uint64
TotalMedia uint64
}

// GetDashdoardStatisticsData get dashdoard statistics data
func GetDashdoardStatisticsData() (*DashdoardData, error) {
// GetDashboardStatisticsData get dashboard statistics data
// TODO store in cache first
func GetDashboardStatisticsData() (*DashboardData, error) {
var totalViews, totalComments, totalArticles, totalMedia uint64
postModel := &model.PostModel{}
totalViewsRow := model.DB.Local.Table(postModel.TableName()).Where("`status` != ? AND `deleted_time` is null", "deleted").
Select("sum(`view_count`) as total_views").
Row()
totalViewsRow.Scan(&totalViews)
_ = totalViewsRow.Scan(&totalViews)

if err := model.DB.Local.Table(postModel.TableName()).
Where("`post_type` = ? AND `status` != ? AND `deleted_time` is null", model.PostTypeArticle, "deleted").
Expand All @@ -31,13 +32,13 @@ func GetDashdoardStatisticsData() (*DashdoardData, error) {
return nil, err
}

var dashdoardData *DashdoardData
dashdoardData = &DashdoardData{
var dashboardData *DashboardData
dashboardData = &DashboardData{
TotalViews: totalViews,
TotalComments: totalComments,
TotalArticles: totalArticles,
TotalMedia: totalMedia,
}

return dashdoardData, nil
return dashboardData, nil
}
152 changes: 99 additions & 53 deletions internal/backend/service/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package service

import (
"fmt"

"github.com/puti-projects/puti/internal/pkg/constvar"

"github.com/puti-projects/puti/internal/pkg/logger"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/load"
"strconv"

"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/mem"
)

Expand All @@ -23,7 +24,14 @@ const (
GB = 1024 * MB
)

// SystemInfo system infomation
const (
statusERROR = "ERROR"
statusCRITICAL = "CRITICAL"
statusWARNING = "WARNING"
statusNORMAL = "NORMAL"
)

// SystemInfo system information
type SystemInfo struct {
Hostname string `json:"hostname"`
Uptime string `json:"uptime"`
Expand All @@ -36,31 +44,27 @@ type Health struct {
HealthStatus string `json:"healthStatus"`
}

// HealthPercent include health precentage
// HealthPercent include health percentage
type HealthPercent struct {
UsedPercent int `json:"usedPercent"`
}

// DiskHealth includes used disk, total disk, userd percent and health status
// DiskHealth includes used disk, total disk, used percent and health status
type DiskHealth struct {
Health
HealthPercent

UsedMB int `json:"usedMB"`
UsedGB int `json:"usedGB"`
TotalMB int `json:"totalMB"`
TotalGB int `json:"totalGB"`
Used string `json:"used"`
Total string `json:"total"`
}

// RAMHealth includes used RAM, total RAM, userd percent and health status
// RAMHealth includes used RAM, total RAM, used percent and health status
type RAMHealth struct {
Health
HealthPercent

UsedMB int `json:"usedMB"`
UsedGB int `json:"usedGB"`
TotalMB int `json:"totalMB"`
TotalGB int `json:"totalGB"`
Used string `json:"used"`
Total string `json:"total"`
}

// CPUHealth includes CPU cores, load average for one minutes, load average for five minutes, load average for fifteen minutes and health status
Expand All @@ -73,56 +77,106 @@ type CPUHealth struct {
LoadAverage15 float64 `json:"loadAverage15"`
}

// GetHealthStatusByPercent get health status by precentage
// GetHealthStatusByPercent get health status by percentage
func (h *HealthPercent) GetHealthStatusByPercent() string {
if h.UsedPercent == 0 {
return statusERROR
}

if h.UsedPercent >= 95 {
return "CRITICAL"
return statusCRITICAL
} else if h.UsedPercent >= 80 {
return "WARNING"
return statusWARNING
}

return "NORMAL"
return statusNORMAL
}

// GetHealthStatusByCores only fot CPU check; get health status by CPU cores
func (c *CPUHealth) GetHealthStatusByCores() string {
if c.LoadAverage5 >= float64(c.CPUCores-1) {
return "CRITICAL"
} else if c.LoadAverage5 >= float64(c.CPUCores-2) {
return "WARNING"
var criticalLevel, warningLevel float64
switch c.CPUCores {
case 1:
criticalLevel = float64(c.CPUCores) - 0.1
warningLevel = float64(c.CPUCores) - 0.25
break
case 2:
criticalLevel = float64(c.CPUCores) - 0.25
warningLevel = float64(c.CPUCores) - 0.5
break
default:
criticalLevel = float64(c.CPUCores-1)
warningLevel = float64(c.CPUCores-2)
break
}

return "NORMAL"
if c.LoadAverage5 >= criticalLevel {
return statusCRITICAL
} else if c.LoadAverage5 >= warningLevel {
return statusWARNING
}

return statusNORMAL
}

// DiskCheck checks the disk usage.
func DiskCheck() *DiskHealth {
var diskHealth *DiskHealth
u, err := disk.Usage("/")
if err != nil {
diskHealth.HealthStatus = fmt.Sprintf("%s", err)
logger.Errorf("error when getting disk info: %s", err)

diskHealth = &DiskHealth{
Health: Health{HealthStatus: statusERROR},
}
return diskHealth
}

diskHealth = &DiskHealth{
UsedMB: int(u.Used) / MB,
UsedGB: int(u.Used) / GB,
TotalMB: int(u.Total) / MB,
TotalGB: int(u.Total) / GB,

Used: transSize(u.Used),
Total: transSize(u.Total),
HealthPercent: HealthPercent{UsedPercent: int(u.UsedPercent)},
}
diskHealth.HealthStatus = diskHealth.GetHealthStatusByPercent()

return diskHealth
}

// RAMCheck checks the RAM usage.
func RAMCheck() *RAMHealth {
var ramHealth *RAMHealth

u, err := mem.VirtualMemory()
if err != nil {
logger.Errorf("error when getting RAM info: %s", err)

ramHealth = &RAMHealth{
HealthPercent: HealthPercent{UsedPercent: 0},
}
ramHealth.HealthStatus = ramHealth.GetHealthStatusByPercent()
return ramHealth
}

ramHealth = &RAMHealth{
Used: transSize(u.Used),
Total: transSize(u.Total),
HealthPercent: HealthPercent{UsedPercent: int(u.UsedPercent)},
}
ramHealth.HealthStatus = ramHealth.GetHealthStatusByPercent()

return ramHealth
}

// CPUCheck checks the cpu usage.
func CPUCheck() *CPUHealth {
var cpuHealth *CPUHealth
cores, err := cpu.Counts(false)
if err != nil {
cpuHealth.HealthStatus = fmt.Sprintf("%s", err)
logger.Errorf("error when getting cpu info: %s", err)

cpuHealth = &CPUHealth{
Health: Health{HealthStatus: statusERROR},
}
return cpuHealth
}

Expand All @@ -139,28 +193,6 @@ func CPUCheck() *CPUHealth {
return cpuHealth
}

// RAMCheck checks the RAM usage.
func RAMCheck() *RAMHealth {
var ramHealth *RAMHealth
u, err := mem.VirtualMemory()
if err != nil {
ramHealth.HealthStatus = fmt.Sprintf("%s", err)
return ramHealth
}

ramHealth = &RAMHealth{
UsedMB: int(u.Used) / MB,
UsedGB: int(u.Used) / GB,
TotalMB: int(u.Total) / MB,
TotalGB: int(u.Total) / GB,

HealthPercent: HealthPercent{UsedPercent: int(u.UsedPercent)},
}
ramHealth.HealthStatus = ramHealth.GetHealthStatusByPercent()

return ramHealth
}

// SystemInfoCheck get system information
func SystemInfoCheck() *SystemInfo {
info, _ := host.Info()
Expand All @@ -181,3 +213,17 @@ func SystemInfoCheck() *SystemInfo {

return systemInfo
}

// transSize tool function for change size
func transSize(size uint64) string {
var transfer string
if size >= 1 * GB {
sizeFloat, _ := strconv.ParseFloat(fmt.Sprintf("%d.%.2d", size / GB, size % GB), 64)
sizeGB := strconv.FormatFloat(sizeFloat,'f',2,64)
transfer = fmt.Sprintf("%s GB", sizeGB)
} else {
transfer = fmt.Sprintf("%d MB", size / MB)
}

return transfer
}
Loading

0 comments on commit d33aeaf

Please sign in to comment.