2022-08-20 21:22:46 +08:00
|
|
|
// Copyright 2022 The Casdoor Authors. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2023-03-09 13:28:23 +08:00
|
|
|
"path"
|
2022-08-20 21:22:46 +08:00
|
|
|
"runtime"
|
|
|
|
"time"
|
|
|
|
|
2023-03-09 13:28:23 +08:00
|
|
|
"github.com/go-git/go-git/v5"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing/object"
|
2022-08-20 21:22:46 +08:00
|
|
|
"github.com/shirou/gopsutil/cpu"
|
|
|
|
"github.com/shirou/gopsutil/mem"
|
|
|
|
)
|
|
|
|
|
|
|
|
// get cpu usage
|
|
|
|
func GetCpuUsage() ([]float64, error) {
|
|
|
|
usage, err := cpu.Percent(time.Second, true)
|
|
|
|
return usage, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var fileDate, version string
|
|
|
|
|
|
|
|
// get memory usage
|
|
|
|
func GetMemoryUsage() (uint64, uint64, error) {
|
|
|
|
virtualMem, err := mem.VirtualMemory()
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var m runtime.MemStats
|
|
|
|
runtime.ReadMemStats(&m)
|
|
|
|
|
|
|
|
return m.TotalAlloc, virtualMem.Total, nil
|
|
|
|
}
|
|
|
|
|
2023-03-09 13:28:23 +08:00
|
|
|
// get github current commit and repo release version
|
2023-03-09 16:07:13 +08:00
|
|
|
func GetGitRepoVersion() (int, string, string, error) {
|
2023-03-09 13:28:23 +08:00
|
|
|
_, filename, _, _ := runtime.Caller(0)
|
|
|
|
rootPath := path.Dir(path.Dir(filename))
|
|
|
|
r, err := git.PlainOpen(rootPath)
|
2022-08-20 21:22:46 +08:00
|
|
|
if err != nil {
|
2023-03-09 16:07:13 +08:00
|
|
|
return -1, "", "", err
|
2022-08-20 21:22:46 +08:00
|
|
|
}
|
2023-03-09 13:28:23 +08:00
|
|
|
ref, err := r.Head()
|
|
|
|
if err != nil {
|
2023-03-09 16:07:13 +08:00
|
|
|
return -1, "", "", err
|
2023-03-09 13:28:23 +08:00
|
|
|
}
|
|
|
|
tags, err := r.Tags()
|
|
|
|
if err != nil {
|
2023-03-09 16:07:13 +08:00
|
|
|
return -1, "", "", err
|
2023-03-09 13:28:23 +08:00
|
|
|
}
|
|
|
|
tagMap := make(map[plumbing.Hash]string)
|
|
|
|
err = tags.ForEach(func(t *plumbing.Reference) error {
|
|
|
|
// This technique should work for both lightweight and annotated tags.
|
|
|
|
revHash, err := r.ResolveRevision(plumbing.Revision(t.Name()))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-08-20 21:22:46 +08:00
|
|
|
}
|
2023-03-09 13:28:23 +08:00
|
|
|
tagMap[*revHash] = t.Name().Short()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-03-09 16:07:13 +08:00
|
|
|
return -1, "", "", err
|
2022-08-20 21:22:46 +08:00
|
|
|
}
|
|
|
|
|
2023-03-09 13:28:23 +08:00
|
|
|
cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
|
|
|
|
|
2023-03-09 16:07:13 +08:00
|
|
|
aheadCnt := 0
|
2023-03-09 13:28:23 +08:00
|
|
|
releaseVersion := ""
|
|
|
|
// iterates over the commits
|
|
|
|
err = cIter.ForEach(func(c *object.Commit) error {
|
|
|
|
tag, ok := tagMap[c.Hash]
|
|
|
|
if ok {
|
|
|
|
if releaseVersion == "" {
|
|
|
|
releaseVersion = tag
|
|
|
|
}
|
|
|
|
}
|
2023-03-09 16:07:13 +08:00
|
|
|
if releaseVersion == "" {
|
|
|
|
aheadCnt++
|
|
|
|
}
|
2023-03-09 13:28:23 +08:00
|
|
|
return nil
|
|
|
|
})
|
2022-08-20 21:22:46 +08:00
|
|
|
if err != nil {
|
2023-03-09 16:07:13 +08:00
|
|
|
return -1, "", "", err
|
2022-08-20 21:22:46 +08:00
|
|
|
}
|
|
|
|
|
2023-03-09 16:07:13 +08:00
|
|
|
return aheadCnt, ref.Hash().String(), releaseVersion, nil
|
2022-08-20 21:22:46 +08:00
|
|
|
}
|