Automatic merge from release/2.1.0 -> release/2.2.0

* commit '5ddb796c9d51eb3dde002f808010e3f48703f2b7':
  不序列化export_file和export_texts
  修正:qutoa的images使用量不准确
  支持将list的结果导出为excel文件
This commit is contained in:
邱剑
2018-09-18 10:26:15 +08:00
45 changed files with 12511 additions and 3 deletions
+4 -1
View File
@@ -8,6 +8,8 @@ import (
"yunion.io/x/onecloud/pkg/cloudcommon/db/quotas"
"yunion.io/x/onecloud/pkg/compute/options"
"yunion.io/x/pkg/tristate"
"yunion.io/x/onecloud/pkg/mcclient/auth"
"yunion.io/x/onecloud/pkg/mcclient/modules"
)
var QuotaManager *quotas.SQuotaManager
@@ -89,7 +91,8 @@ func (self *SQuota) FetchUsage(projectId string) error {
self.Bw = net.InternalBandwidth
self.Ebw = net.ExternalBandwidth
self.Keypair = 0 // keypair
self.Image = 0
s := auth.GetAdminSession("", "")
self.Image, _ = modules.Images.GetPrivateImageCount(s, projectId, true)
self.Group = 0
self.Secgroup = totalSecurityGroupCount(projectId)
self.IsolatedDevice = guest.TotalIsolatedCount
+15
View File
@@ -128,6 +128,21 @@ func (this *ImageManager) List(session *mcclient.ClientSession, params jsonutils
return this._list(session, path, this.KeywordPlural)
}
func (this *ImageManager) GetPrivateImageCount(s *mcclient.ClientSession, ownerId string, isAdmin bool) (int, error) {
params := jsonutils.NewDict()
params.Add(jsonutils.NewString("none"), "is_public")
params.Add(jsonutils.NewString(ownerId), "owner")
if isAdmin {
params.Add(jsonutils.JSONTrue, "admin")
}
result, err := this.List(s, params)
if err != nil {
return 0, err
}
return len(result.Data), nil
}
type ImageUsageCount struct {
Count int64
Size int64
+3
View File
@@ -166,6 +166,9 @@ type BaseListOptions struct {
PendingDeleteAll *bool `help:"Show all resources including pending deleted" json:"-"`
Field []string `help:"Show only specified fields"`
ShowEmulated *bool `help:"Show all resources including the emulated resources"`
ExportFile string `help:"Export to file" metavar:"<EXPORT_FILE_PATH>" json:"-"`
ExportKeys string `help:"Export field keys"`
ExportTexts string `help:"Export field displayname texts" json:"-"`
}
func (opts *BaseListOptions) Params() (*jsonutils.JSONDict, error) {
+101
View File
@@ -0,0 +1,101 @@
package excelutils
import (
"io"
"bytes"
"fmt"
"os"
"github.com/360EntSecGroup-Skylar/excelize"
"yunion.io/x/jsonutils"
)
const (
DEFAULT_SHEET = "Sheet1"
)
func decimalBaseMaxWidth(decNum int, base int) int {
if decNum == 0 {
return 1
}
width := 0
for decNum > 0 {
decNum = decNum/base
width += 1
}
return width
}
func decimalBaseN(decNum int, base int, width int) (int, int) {
b := 1
for i := 0; i < width - 1; i += 1 {
decNum = decNum/base
b = b*base
}
return decNum, b
}
func decimal2Base(decNum int, base int) []int {
width := decimalBaseMaxWidth(decNum, base)
ret := make([]int, width)
for i := width; i > 0; i -= 1 {
ith, divider := decimalBaseN(decNum, base, i)
decNum -= ith*divider
ret[width - i] = ith
}
return ret
}
func decimal2Alphabet(decNum int) string {
var buf bytes.Buffer
b26 := decimal2Base(decNum, 26)
for i := 0; i < len(b26); i += 1 {
if i == 0 && len(b26) > 1 {
buf.WriteByte(byte('A' + b26[i] - 1))
} else {
buf.WriteByte(byte('A' + b26[i]))
}
}
return buf.String()
}
func exportHeader(xlsx *excelize.File, texts []string, rowIndex int) {
for i := 0; i < len(texts); i += 1 {
cell := fmt.Sprintf("%s%d", decimal2Alphabet(i), rowIndex)
xlsx.SetCellValue(DEFAULT_SHEET, cell, texts[i])
}
}
func exportRow(xlsx *excelize.File, data jsonutils.JSONObject, keys []string, rowIndex int) {
for i := 0; i < len(keys); i += 1 {
var valStr string
val, _ := data.GetIgnoreCases(keys[i])
if val != nil {
valStr, _ = val.GetString()
}
cell := fmt.Sprintf("%s%d", decimal2Alphabet(i), rowIndex)
xlsx.SetCellValue(DEFAULT_SHEET, cell, valStr)
}
}
func Export(data []jsonutils.JSONObject, keys []string, texts []string, writer io.Writer) error {
xlsx := excelize.NewFile()
exportHeader(xlsx, texts, 1)
for i := 0; i < len(data); i += 1 {
exportRow(xlsx, data[i], keys, i + 2)
}
return xlsx.Write(writer)
}
func ExportFile(data []jsonutils.JSONObject, keys []string, texts []string, filename string) error {
writer, err:= os.Create(filename)
if err != nil {
return err
}
defer writer.Close()
return Export(data, keys, texts, writer)
}
+94
View File
@@ -0,0 +1,94 @@
package excelutils
import "testing"
func arrayEqual(a1, a2 []int) bool {
if len(a1) != len(a2) {
return false
}
for i := 0; i < len(a1); i += 1 {
if a1[i] != a2[i] {
return false
}
}
return true
}
func TestDecimalBaseMaxWidth(t *testing.T) {
cases := []struct {
decIn int
baseIn int
want int
} {
{100, 10, 3},
{16, 16, 2},
{15, 16, 1},
}
for _, c := range cases {
got := decimalBaseMaxWidth(c.decIn, c.baseIn)
if got != c.want {
t.Errorf("decimalBaseMaxWidth(%d, %d) = %d != %d", c.decIn, c.baseIn, got, c.want)
}
}
cases2 := []struct {
decIn int
baseIn int
width int
want int
want2 int
} {
{100, 10, 3, 1, 100},
{16, 16, 2, 1, 16},
{15, 16, 1, 15, 1},
}
for _, c := range cases2 {
got1, got2 := decimalBaseN(c.decIn, c.baseIn, c.width)
if got1 != c.want || got2 != c.want2 {
t.Errorf("decimalBaseN(%d %d %d) = %d %d != %d %d", c.decIn, c.baseIn, c.width, got1, got2, c.want, c.want2)
}
}
cases3 := []struct {
decIn int
baseIn int
want []int
} {
{100, 10, []int{1, 0, 0}},
{16, 16, []int{1, 0}},
{0, 16, []int{0}},
{15, 16, []int{15}},
{0, 26, []int{0}},
{1, 26, []int{1}},
{25, 26, []int{25}},
{26, 26, []int{1, 0}},
{27, 26, []int{1, 1}},
{676, 26, []int{1, 0, 0}},
}
for _, c := range cases3 {
got := decimal2Base(c.decIn, c.baseIn)
if !arrayEqual(got, c.want) {
t.Errorf("decimal2Base(%d %d) = %#v != %#v", c.decIn, c.baseIn, got, c.want)
}
}
cases4 := []struct {
decIn int
want string
} {
{0, "A"},
{1, "B"},
{25, "Z"},
{26, "AA"},
{27, "AB"},
{676, "AAA"},
}
for _, c := range cases4 {
got := decimal2Alphabet(c.decIn)
if got != c.want {
t.Errorf("decimal2Alphabet(%d) = %s != %s", c.decIn, got, c.want)
}
}
}