mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
Merge pull request #9706 from rainzm/st/desc_lang
feat(region): multilingual timer description
This commit is contained in:
@@ -23,6 +23,9 @@ import (
|
||||
type ScheduledTaskDetails struct {
|
||||
apis.VirtualResourceDetails
|
||||
SScheduledTask
|
||||
|
||||
// 描述
|
||||
TimerDesc string `json:"timer_desc"`
|
||||
// 定时方式触发
|
||||
Timer TimerDetails `json:"timer"`
|
||||
// 周期方式触发
|
||||
|
||||
@@ -2068,7 +2068,6 @@ type SScheduledTask struct {
|
||||
apis.SEnabledResourceBase
|
||||
ScheduledType string `json:"scheduled_type"`
|
||||
STimer
|
||||
TimerDesc string `json:"timer_desc"`
|
||||
ResourceType string `json:"resource_type"`
|
||||
Operation string `json:"operation"`
|
||||
LabelType string `json:"label_type"`
|
||||
|
||||
@@ -18,8 +18,6 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
@@ -205,17 +203,7 @@ func (st *SScalingTimer) TriggerId() string {
|
||||
var cstSh, _ = time.LoadLocation("Asia/Shanghai")
|
||||
|
||||
func (st *SScalingTimer) TriggerDescription() string {
|
||||
var detail string
|
||||
switch st.Type {
|
||||
case api.TIMER_TYPE_ONCE:
|
||||
detail = st.EndTime.In(cstSh).Format("2006-01-02 15:04:05")
|
||||
case api.TIMER_TYPE_DAY:
|
||||
detail = fmt.Sprintf("%d:%d every day", st.Hour, st.Minute)
|
||||
case api.TIMER_TYPE_WEEK:
|
||||
detail = st.WeekDaysDesc()
|
||||
case api.TIMER_TYPE_MONTH:
|
||||
detail = st.MonthDaysDesc()
|
||||
}
|
||||
detail := st.descEnglish()
|
||||
name := st.ScalingPolicyId
|
||||
sp, _ := st.ScalingPolicy()
|
||||
if sp != nil {
|
||||
@@ -442,61 +430,3 @@ var units = map[string]string{
|
||||
api.INDICATOR_FLOW_INTO: "KB/s",
|
||||
api.INDICATOR_FLOW_OUT: "KB/s",
|
||||
}
|
||||
|
||||
var weekDays = []string{"", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
|
||||
|
||||
func (st *SScalingTimer) WeekDaysDesc() string {
|
||||
if st.WeekDays == 0 {
|
||||
return ""
|
||||
}
|
||||
var desc strings.Builder
|
||||
wds := st.GetWeekDays()
|
||||
i := 0
|
||||
desc.WriteString(fmt.Sprintf("%d:%d every %s", st.Hour, st.Minute, weekDays[wds[i]]))
|
||||
for i++; i < len(wds)-1; i++ {
|
||||
desc.WriteString(", ")
|
||||
desc.WriteString(weekDays[wds[i]])
|
||||
}
|
||||
if i == len(wds)-1 {
|
||||
desc.WriteString(" and ")
|
||||
desc.WriteString(weekDays[wds[i]])
|
||||
}
|
||||
return desc.String()
|
||||
}
|
||||
|
||||
func (st *SScalingTimer) MonthDaysDesc() string {
|
||||
if st.MonthDays == 0 {
|
||||
return ""
|
||||
}
|
||||
var desc strings.Builder
|
||||
mds := st.GetMonthDays()
|
||||
i := 0
|
||||
desc.WriteString(fmt.Sprintf("%d:%d on the %d%s", st.Hour, st.Minute, mds[i], dateSuffix(mds[i])))
|
||||
for i++; i < len(mds)-1; i++ {
|
||||
desc.WriteString(", ")
|
||||
desc.WriteString(strconv.Itoa(mds[i]))
|
||||
desc.WriteString(dateSuffix(mds[i]))
|
||||
}
|
||||
if i == len(mds)-1 {
|
||||
desc.WriteString(" and ")
|
||||
desc.WriteString(strconv.Itoa(mds[i]))
|
||||
desc.WriteString(dateSuffix(mds[i]))
|
||||
}
|
||||
desc.WriteString(" of each month")
|
||||
return desc.String()
|
||||
}
|
||||
|
||||
func dateSuffix(date int) string {
|
||||
var ret string
|
||||
switch date {
|
||||
case 1:
|
||||
ret = "st"
|
||||
case 2:
|
||||
ret = "nd"
|
||||
case 3:
|
||||
ret = "rd"
|
||||
default:
|
||||
ret = "th"
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
@@ -72,7 +72,6 @@ type SScheduledTask struct {
|
||||
|
||||
STimer
|
||||
|
||||
TimerDesc string `width:"128" charset:"utf8" list:"user" get:"user"`
|
||||
ResourceType string `width:"32" charset:"ascii" create:"required" list:"user" get:"user"`
|
||||
Operation string `width:"32" charset:"ascii" create:"required" list:"user" get:"user"`
|
||||
LabelType string `width:"4" charset:"ascii" create:"required" list:"user" get:"user"`
|
||||
@@ -139,6 +138,7 @@ func (st *SScheduledTask) getMoreDetails(ctx context.Context, userCred mcclient.
|
||||
case api.ST_TYPE_CYCLE:
|
||||
out.CycleTimer = st.STimer.CycleTimerDetails()
|
||||
}
|
||||
out.TimerDesc = st.Description(ctx)
|
||||
// fill label
|
||||
stLabels, err := st.STLabels()
|
||||
if err != nil {
|
||||
@@ -184,36 +184,6 @@ func (stm *SScheduledTaskManager) ValidateCreateData(ctx context.Context, userCr
|
||||
return input, nil
|
||||
}
|
||||
|
||||
var wdsCN = []string{"", "一", "二", "三", "四", "五", "六", "日"}
|
||||
var zone = time.FixedZone("GMT", 8*3600)
|
||||
|
||||
func (st *SScheduledTask) TimerDescription() string {
|
||||
format := "2006-01-02 15:04:05"
|
||||
var prefix string
|
||||
timer := st.STimer
|
||||
switch timer.Type {
|
||||
case api.TIMER_TYPE_ONCE:
|
||||
return fmt.Sprintf("单次 %s触发", timer.StartTime.In(zone).Format(format))
|
||||
case api.TIMER_TYPE_DAY:
|
||||
prefix = "每天"
|
||||
case api.TIMER_TYPE_WEEK:
|
||||
wds := timer.GetWeekDays()
|
||||
weekDays := make([]string, len(wds))
|
||||
for i := range wds {
|
||||
weekDays[i] = fmt.Sprintf("星期%s", wdsCN[wds[i]])
|
||||
}
|
||||
prefix = fmt.Sprintf("每周 【%s】", strings.Join(weekDays, "|"))
|
||||
case api.TIMER_TYPE_MONTH:
|
||||
mns := timer.GetMonthDays()
|
||||
monthDays := make([]string, len(mns))
|
||||
for i := range mns {
|
||||
monthDays[i] = fmt.Sprintf("%d号", mns[i])
|
||||
}
|
||||
prefix = fmt.Sprintf("每月 【%s】", strings.Join(monthDays, "|"))
|
||||
}
|
||||
return fmt.Sprintf("%s %02d:%02d触发 有效时间为%s至%s", prefix, timer.Hour, timer.Minute, timer.StartTime.In(zone).Format(format), timer.EndTime.In(zone).Format(format))
|
||||
}
|
||||
|
||||
func (st *SScheduledTask) AllowPerformEnable(ctx context.Context, userCred mcclient.TokenCredential,
|
||||
query jsonutils.JSONObject, input apis.PerformEnableInput) bool {
|
||||
return true
|
||||
@@ -288,7 +258,7 @@ func (st *SScheduledTask) PostCreate(ctx context.Context, userCred mcclient.Toke
|
||||
st.Update(time.Time{})
|
||||
st.Status = api.ST_STATUS_READY
|
||||
st.Enabled = tristate.True
|
||||
st.TimerDesc = st.TimerDescription()
|
||||
// st.TimerDesc = st.Description(ctx)
|
||||
err = st.GetModelManager().TableSpec().InsertOrUpdate(ctx, st)
|
||||
if err != nil {
|
||||
createFailed("update itself")
|
||||
|
||||
@@ -15,13 +15,17 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"yunion.io/x/log"
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
"yunion.io/x/onecloud/pkg/i18n"
|
||||
"yunion.io/x/onecloud/pkg/util/bitmap"
|
||||
)
|
||||
|
||||
@@ -159,6 +163,134 @@ func checkTimerCreateInput(in api.TimerCreateInput) (api.TimerCreateInput, error
|
||||
return in, nil
|
||||
}
|
||||
|
||||
var (
|
||||
timerDescTable = i18n.Table{}
|
||||
TIMERLANG = "timerLang"
|
||||
)
|
||||
|
||||
func init() {
|
||||
timerDescTable.Set("timerLang", i18n.NewTableEntry().EN("en").CN("cn"))
|
||||
}
|
||||
|
||||
func (st *STimer) Description(ctx context.Context) string {
|
||||
lang := timerDescTable.Lookup(ctx, TIMERLANG)
|
||||
switch lang {
|
||||
case "en":
|
||||
return st.descEnglish()
|
||||
case "cn":
|
||||
return st.descChinese()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
var (
|
||||
wdsCN = []string{"", "一", "二", "三", "四", "五", "六", "日"}
|
||||
wdsEN = []string{"", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
|
||||
zone = time.Now().Local().Location()
|
||||
//zone = time.FixedZone("GMT", 8*3600)
|
||||
)
|
||||
|
||||
func (st *STimer) descChinese() string {
|
||||
format := "2006-01-02 15:04:05"
|
||||
var prefix string
|
||||
switch st.Type {
|
||||
case api.TIMER_TYPE_ONCE:
|
||||
return fmt.Sprintf("单次 %s触发", st.StartTime.In(zone).Format(format))
|
||||
case api.TIMER_TYPE_DAY:
|
||||
prefix = "每天"
|
||||
case api.TIMER_TYPE_WEEK:
|
||||
wds := st.GetWeekDays()
|
||||
weekDays := make([]string, len(wds))
|
||||
for i := range wds {
|
||||
weekDays[i] = fmt.Sprintf("星期%s", wdsCN[wds[i]])
|
||||
}
|
||||
prefix = fmt.Sprintf("每周 【%s】", strings.Join(weekDays, "|"))
|
||||
case api.TIMER_TYPE_MONTH:
|
||||
mns := st.GetMonthDays()
|
||||
monthDays := make([]string, len(mns))
|
||||
for i := range mns {
|
||||
monthDays[i] = fmt.Sprintf("%d号", mns[i])
|
||||
}
|
||||
prefix = fmt.Sprintf("每月 【%s】", strings.Join(monthDays, "|"))
|
||||
}
|
||||
return fmt.Sprintf("%s %02d:%02d触发 有效时间为%s至%s", prefix, st.Hour, st.Minute, st.StartTime.In(zone).Format(format), st.EndTime.In(zone).Format(format))
|
||||
}
|
||||
|
||||
func (st *STimer) descEnglish() string {
|
||||
var detail string
|
||||
format := "2006-01-02 15:04:05"
|
||||
switch st.Type {
|
||||
case api.TIMER_TYPE_ONCE:
|
||||
return st.EndTime.In(zone).Format(format)
|
||||
case api.TIMER_TYPE_DAY:
|
||||
detail = fmt.Sprintf("%d:%d every day", st.Hour, st.Minute)
|
||||
case api.TIMER_TYPE_WEEK:
|
||||
detail = st.weekDaysDesc()
|
||||
case api.TIMER_TYPE_MONTH:
|
||||
detail = st.monthDaysDesc()
|
||||
}
|
||||
if st.EndTime.IsZero() {
|
||||
return detail
|
||||
}
|
||||
return fmt.Sprintf("%s, from %s to %s", detail, st.StartTime.In(zone).Format(format), st.EndTime.In(zone).Format(format))
|
||||
}
|
||||
|
||||
func (st *STimer) weekDaysDesc() string {
|
||||
if st.WeekDays == 0 {
|
||||
return ""
|
||||
}
|
||||
var desc strings.Builder
|
||||
wds := st.GetWeekDays()
|
||||
i := 0
|
||||
desc.WriteString(fmt.Sprintf("%d:%d every %s", st.Hour, st.Minute, wdsEN[wds[i]]))
|
||||
for i++; i < len(wds)-1; i++ {
|
||||
desc.WriteString(", ")
|
||||
desc.WriteString(wdsEN[wds[i]])
|
||||
}
|
||||
if i == len(wds)-1 {
|
||||
desc.WriteString(" and ")
|
||||
desc.WriteString(wdsEN[wds[i]])
|
||||
}
|
||||
return desc.String()
|
||||
}
|
||||
|
||||
func (st *STimer) monthDaysDesc() string {
|
||||
if st.MonthDays == 0 {
|
||||
return ""
|
||||
}
|
||||
var desc strings.Builder
|
||||
mds := st.GetMonthDays()
|
||||
i := 0
|
||||
desc.WriteString(fmt.Sprintf("%d:%d on the %d%s", st.Hour, st.Minute, mds[i], st.dateSuffix(mds[i])))
|
||||
for i++; i < len(mds)-1; i++ {
|
||||
desc.WriteString(", ")
|
||||
desc.WriteString(strconv.Itoa(mds[i]))
|
||||
desc.WriteString(st.dateSuffix(mds[i]))
|
||||
}
|
||||
if i == len(mds)-1 {
|
||||
desc.WriteString(" and ")
|
||||
desc.WriteString(strconv.Itoa(mds[i]))
|
||||
desc.WriteString(st.dateSuffix(mds[i]))
|
||||
}
|
||||
desc.WriteString(" of each month")
|
||||
return desc.String()
|
||||
}
|
||||
|
||||
func (st *STimer) dateSuffix(date int) string {
|
||||
var ret string
|
||||
switch date {
|
||||
case 1:
|
||||
ret = "st"
|
||||
case 2:
|
||||
ret = "nd"
|
||||
case 3:
|
||||
ret = "rd"
|
||||
default:
|
||||
ret = "th"
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func checkCycleTimerCreateInput(in api.CycleTimerCreateInput) (api.CycleTimerCreateInput, error) {
|
||||
now := time.Now()
|
||||
if in.Minute < 0 || in.Minute > 59 {
|
||||
|
||||
Reference in New Issue
Block a user