diff --git a/internal/web/actions/default/servers/components/cache/clean.go b/internal/web/actions/default/servers/components/cache/clean.go index 9e9fbb0e..d0077ac0 100644 --- a/internal/web/actions/default/servers/components/cache/clean.go +++ b/internal/web/actions/default/servers/components/cache/clean.go @@ -1,6 +1,16 @@ package cache -import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" +import ( + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/nodes/nodeutils" + "github.com/TeaOSLab/EdgeCommon/pkg/messageconfigs" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" + "github.com/iwind/TeaGo/actions" + "github.com/iwind/TeaGo/maps" + "github.com/iwind/TeaGo/types" + "net/http" + "strconv" +) type CleanAction struct { actionutils.ParentAction @@ -11,5 +21,72 @@ func (this *CleanAction) Init() { } func (this *CleanAction) RunGet(params struct{}) { + // 默认的集群ID + cookie, err := this.Request.Cookie("cache_cluster_id") + if cookie != nil && err == nil { + this.Data["clusterId"] = types.Int64(cookie.Value) + } + + // 集群列表 + clustersResp, err := this.RPC().NodeClusterRPC().FindAllEnabledNodeClusters(this.AdminContext(), &pb.FindAllEnabledNodeClustersRequest{}) + if err != nil { + this.ErrorPage(err) + return + } + clusterMaps := []maps.Map{} + for _, cluster := range clustersResp.Clusters { + clusterMaps = append(clusterMaps, maps.Map{ + "id": cluster.Id, + "name": cluster.Name, + }) + } + this.Data["clusters"] = clusterMaps + this.Show() } + +func (this *CleanAction) RunPost(params struct { + CachePolicyId int64 + ClusterId int64 + + Must *actions.Must +}) { + // 记录clusterId + this.AddCookie(&http.Cookie{ + Name: "cache_cluster_id", + Value: strconv.FormatInt(params.ClusterId, 10), + }) + + cachePolicyResp, err := this.RPC().HTTPCachePolicyRPC().FindEnabledHTTPCachePolicyConfig(this.AdminContext(), &pb.FindEnabledHTTPCachePolicyConfigRequest{CachePolicyId: params.CachePolicyId}) + if err != nil { + this.ErrorPage(err) + return + } + cachePolicyJSON := cachePolicyResp.CachePolicyJSON + if len(cachePolicyJSON) == 0 { + this.Fail("找不到要操作的缓存策略") + } + + // 发送命令 + msg := &messageconfigs.CleanCacheMessage{ + CachePolicyJSON: cachePolicyJSON, + } + results, err := nodeutils.SendMessageToCluster(this.AdminContext(), params.ClusterId, messageconfigs.MessageCodeCleanCache, msg, 10) + if err != nil { + this.ErrorPage(err) + return + } + + isAllOk := true + for _, result := range results { + if !result.IsOK { + isAllOk = false + break + } + } + + this.Data["isAllOk"] = isAllOk + this.Data["results"] = results + + this.Success() +} diff --git a/internal/web/actions/default/servers/components/cache/preheat.go b/internal/web/actions/default/servers/components/cache/preheat.go index dadf14a7..214a0ded 100644 --- a/internal/web/actions/default/servers/components/cache/preheat.go +++ b/internal/web/actions/default/servers/components/cache/preheat.go @@ -1,6 +1,18 @@ package cache -import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" +import ( + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/nodes/nodeutils" + "github.com/TeaOSLab/EdgeCommon/pkg/messageconfigs" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" + "github.com/iwind/TeaGo/actions" + "github.com/iwind/TeaGo/lists" + "github.com/iwind/TeaGo/maps" + "github.com/iwind/TeaGo/types" + "net/http" + "strconv" + "strings" +) type PreheatAction struct { actionutils.ParentAction @@ -11,5 +23,90 @@ func (this *PreheatAction) Init() { } func (this *PreheatAction) RunGet(params struct{}) { + // 默认的集群ID + cookie, err := this.Request.Cookie("cache_cluster_id") + if cookie != nil && err == nil { + this.Data["clusterId"] = types.Int64(cookie.Value) + } + + // 集群列表 + clustersResp, err := this.RPC().NodeClusterRPC().FindAllEnabledNodeClusters(this.AdminContext(), &pb.FindAllEnabledNodeClustersRequest{}) + if err != nil { + this.ErrorPage(err) + return + } + clusterMaps := []maps.Map{} + for _, cluster := range clustersResp.Clusters { + clusterMaps = append(clusterMaps, maps.Map{ + "id": cluster.Id, + "name": cluster.Name, + }) + } + this.Data["clusters"] = clusterMaps + this.Show() } + +func (this *PreheatAction) RunPost(params struct { + CachePolicyId int64 + ClusterId int64 + Keys string + + Must *actions.Must +}) { + // 记录clusterId + this.AddCookie(&http.Cookie{ + Name: "cache_cluster_id", + Value: strconv.FormatInt(params.ClusterId, 10), + }) + + cachePolicyResp, err := this.RPC().HTTPCachePolicyRPC().FindEnabledHTTPCachePolicyConfig(this.AdminContext(), &pb.FindEnabledHTTPCachePolicyConfigRequest{CachePolicyId: params.CachePolicyId}) + if err != nil { + this.ErrorPage(err) + return + } + cachePolicyJSON := cachePolicyResp.CachePolicyJSON + if len(cachePolicyJSON) == 0 { + this.Fail("找不到要操作的缓存策略") + } + + if len(params.Keys) == 0 { + this.Fail("请输入要预热的Key列表") + } + + realKeys := []string{} + for _, key := range strings.Split(params.Keys, "\n") { + key = strings.TrimSpace(key) + if len(key) == 0 { + continue + } + if lists.ContainsString(realKeys, key) { + continue + } + realKeys = append(realKeys, key) + } + + // 发送命令 + msg := &messageconfigs.PreheatCacheMessage{ + CachePolicyJSON: cachePolicyJSON, + Keys: realKeys, + } + results, err := nodeutils.SendMessageToCluster(this.AdminContext(), params.ClusterId, messageconfigs.MessageCodePreheatCache, msg, 300) + if err != nil { + this.ErrorPage(err) + return + } + + isAllOk := true + for _, result := range results { + if !result.IsOK { + isAllOk = false + break + } + } + + this.Data["isAllOk"] = isAllOk + this.Data["results"] = results + + this.Success() +} diff --git a/internal/web/actions/default/servers/components/cache/purge.go b/internal/web/actions/default/servers/components/cache/purge.go index 245f0336..b374f5ca 100644 --- a/internal/web/actions/default/servers/components/cache/purge.go +++ b/internal/web/actions/default/servers/components/cache/purge.go @@ -1,6 +1,18 @@ package cache -import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" +import ( + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/nodes/nodeutils" + "github.com/TeaOSLab/EdgeCommon/pkg/messageconfigs" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" + "github.com/iwind/TeaGo/actions" + "github.com/iwind/TeaGo/lists" + "github.com/iwind/TeaGo/maps" + "github.com/iwind/TeaGo/types" + "net/http" + "strconv" + strings "strings" +) type PurgeAction struct { actionutils.ParentAction @@ -11,5 +23,89 @@ func (this *PurgeAction) Init() { } func (this *PurgeAction) RunGet(params struct{}) { + // 默认的集群ID + cookie, err := this.Request.Cookie("cache_cluster_id") + if cookie != nil && err == nil { + this.Data["clusterId"] = types.Int64(cookie.Value) + } + + // 集群列表 + clustersResp, err := this.RPC().NodeClusterRPC().FindAllEnabledNodeClusters(this.AdminContext(), &pb.FindAllEnabledNodeClustersRequest{}) + if err != nil { + this.ErrorPage(err) + return + } + clusterMaps := []maps.Map{} + for _, cluster := range clustersResp.Clusters { + clusterMaps = append(clusterMaps, maps.Map{ + "id": cluster.Id, + "name": cluster.Name, + }) + } + this.Data["clusters"] = clusterMaps + this.Show() } + +func (this *PurgeAction) RunPost(params struct { + CachePolicyId int64 + ClusterId int64 + Keys string + Must *actions.Must +}) { + // 记录clusterId + this.AddCookie(&http.Cookie{ + Name: "cache_cluster_id", + Value: strconv.FormatInt(params.ClusterId, 10), + }) + + cachePolicyResp, err := this.RPC().HTTPCachePolicyRPC().FindEnabledHTTPCachePolicyConfig(this.AdminContext(), &pb.FindEnabledHTTPCachePolicyConfigRequest{CachePolicyId: params.CachePolicyId}) + if err != nil { + this.ErrorPage(err) + return + } + cachePolicyJSON := cachePolicyResp.CachePolicyJSON + if len(cachePolicyJSON) == 0 { + this.Fail("找不到要操作的缓存策略") + } + + if len(params.Keys) == 0 { + this.Fail("请输入要删除的Key列表") + } + + realKeys := []string{} + for _, key := range strings.Split(params.Keys, "\n") { + key = strings.TrimSpace(key) + if len(key) == 0 { + continue + } + if lists.ContainsString(realKeys, key) { + continue + } + realKeys = append(realKeys, key) + } + + // 发送命令 + msg := &messageconfigs.PurgeCacheMessage{ + CachePolicyJSON: cachePolicyJSON, + Keys: realKeys, + } + results, err := nodeutils.SendMessageToCluster(this.AdminContext(), params.ClusterId, messageconfigs.MessageCodePurgeCache, msg, 10) + if err != nil { + this.ErrorPage(err) + return + } + + isAllOk := true + for _, result := range results { + if !result.IsOK { + isAllOk = false + break + } + } + + this.Data["isAllOk"] = isAllOk + this.Data["results"] = results + + this.Success() +} diff --git a/internal/web/actions/default/servers/components/cache/stat.go b/internal/web/actions/default/servers/components/cache/stat.go index 3c32ecd3..6efd5746 100644 --- a/internal/web/actions/default/servers/components/cache/stat.go +++ b/internal/web/actions/default/servers/components/cache/stat.go @@ -1,6 +1,16 @@ package cache -import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" +import ( + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/nodes/nodeutils" + "github.com/TeaOSLab/EdgeCommon/pkg/messageconfigs" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" + "github.com/iwind/TeaGo/actions" + "github.com/iwind/TeaGo/maps" + "github.com/iwind/TeaGo/types" + "net/http" + "strconv" +) type StatAction struct { actionutils.ParentAction @@ -11,5 +21,72 @@ func (this *StatAction) Init() { } func (this *StatAction) RunGet(params struct{}) { + // 默认的集群ID + cookie, err := this.Request.Cookie("cache_cluster_id") + if cookie != nil && err == nil { + this.Data["clusterId"] = types.Int64(cookie.Value) + } + + // 集群列表 + clustersResp, err := this.RPC().NodeClusterRPC().FindAllEnabledNodeClusters(this.AdminContext(), &pb.FindAllEnabledNodeClustersRequest{}) + if err != nil { + this.ErrorPage(err) + return + } + clusterMaps := []maps.Map{} + for _, cluster := range clustersResp.Clusters { + clusterMaps = append(clusterMaps, maps.Map{ + "id": cluster.Id, + "name": cluster.Name, + }) + } + this.Data["clusters"] = clusterMaps + this.Show() } + +func (this *StatAction) RunPost(params struct { + CachePolicyId int64 + ClusterId int64 + + Must *actions.Must +}) { + // 记录clusterId + this.AddCookie(&http.Cookie{ + Name: "cache_cluster_id", + Value: strconv.FormatInt(params.ClusterId, 10), + }) + + cachePolicyResp, err := this.RPC().HTTPCachePolicyRPC().FindEnabledHTTPCachePolicyConfig(this.AdminContext(), &pb.FindEnabledHTTPCachePolicyConfigRequest{CachePolicyId: params.CachePolicyId}) + if err != nil { + this.ErrorPage(err) + return + } + cachePolicyJSON := cachePolicyResp.CachePolicyJSON + if len(cachePolicyJSON) == 0 { + this.Fail("找不到要操作的缓存策略") + } + + // 发送命令 + msg := &messageconfigs.StatCacheMessage{ + CachePolicyJSON: cachePolicyJSON, + } + results, err := nodeutils.SendMessageToCluster(this.AdminContext(), params.ClusterId, messageconfigs.MessageCodeStatCache, msg, 10) + if err != nil { + this.ErrorPage(err) + return + } + + isAllOk := true + for _, result := range results { + if !result.IsOK { + isAllOk = false + break + } + } + + this.Data["isAllOk"] = isAllOk + this.Data["results"] = results + + this.Success() +} diff --git a/internal/web/actions/default/servers/components/cache/test.go b/internal/web/actions/default/servers/components/cache/test.go index cc2d988d..a50c6b00 100644 --- a/internal/web/actions/default/servers/components/cache/test.go +++ b/internal/web/actions/default/servers/components/cache/test.go @@ -4,6 +4,7 @@ import ( "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/iwind/TeaGo/maps" + "github.com/iwind/TeaGo/types" ) type TestAction struct { @@ -15,6 +16,12 @@ func (this *TestAction) Init() { } func (this *TestAction) RunGet(params struct{}) { + // 默认的集群ID + cookie, err := this.Request.Cookie("cache_cluster_id") + if cookie != nil && err == nil { + this.Data["clusterId"] = types.Int64(cookie.Value) + } + // 集群列表 clustersResp, err := this.RPC().NodeClusterRPC().FindAllEnabledNodeClusters(this.AdminContext(), &pb.FindAllEnabledNodeClustersRequest{}) if err != nil { diff --git a/internal/web/actions/default/servers/components/cache/testRead.go b/internal/web/actions/default/servers/components/cache/testRead.go index 92de0947..faab5343 100644 --- a/internal/web/actions/default/servers/components/cache/testRead.go +++ b/internal/web/actions/default/servers/components/cache/testRead.go @@ -5,6 +5,8 @@ import ( "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/nodes/nodeutils" "github.com/TeaOSLab/EdgeCommon/pkg/messageconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" + "net/http" + "strconv" ) type TestReadAction struct { @@ -16,6 +18,12 @@ func (this *TestReadAction) RunPost(params struct { CachePolicyId int64 Key string }) { + // 记录clusterId + this.AddCookie(&http.Cookie{ + Name: "cache_cluster_id", + Value: strconv.FormatInt(params.ClusterId, 10), + }) + cachePolicyResp, err := this.RPC().HTTPCachePolicyRPC().FindEnabledHTTPCachePolicyConfig(this.AdminContext(), &pb.FindEnabledHTTPCachePolicyConfigRequest{CachePolicyId: params.CachePolicyId}) if err != nil { this.ErrorPage(err) diff --git a/internal/web/actions/default/servers/components/cache/testWrite.go b/internal/web/actions/default/servers/components/cache/testWrite.go index 5ac67bac..83852a57 100644 --- a/internal/web/actions/default/servers/components/cache/testWrite.go +++ b/internal/web/actions/default/servers/components/cache/testWrite.go @@ -5,6 +5,8 @@ import ( "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/nodes/nodeutils" "github.com/TeaOSLab/EdgeCommon/pkg/messageconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" + "net/http" + "strconv" ) type TestWriteAction struct { @@ -17,6 +19,12 @@ func (this *TestWriteAction) RunPost(params struct { Key string Value string }) { + // 记录clusterId + this.AddCookie(&http.Cookie{ + Name: "cache_cluster_id", + Value: strconv.FormatInt(params.ClusterId, 10), + }) + cachePolicyResp, err := this.RPC().HTTPCachePolicyRPC().FindEnabledHTTPCachePolicyConfig(this.AdminContext(), &pb.FindEnabledHTTPCachePolicyConfigRequest{CachePolicyId: params.CachePolicyId}) if err != nil { this.ErrorPage(err) diff --git a/web/views/@default/@layout.js b/web/views/@default/@layout.js index 22b05e25..c1e97b8f 100644 --- a/web/views/@default/@layout.js +++ b/web/views/@default/@layout.js @@ -49,9 +49,13 @@ Tea.context(function () { this.globalChangedClusters = []; }) .done(function () { + let delay = 3000 + if (this.globalChangedClusters.length > 0) { + delay = 30000 + } this.$delay(function () { this.checkClusterChanges() - }, 3000) + }, delay) }) }; diff --git a/web/views/@default/servers/components/cache/clean.html b/web/views/@default/servers/components/cache/clean.html new file mode 100644 index 00000000..06712dc9 --- /dev/null +++ b/web/views/@default/servers/components/cache/clean.html @@ -0,0 +1,34 @@ +{$layout} +{$template "/left_menu"} + +
严重注意:该操作将清理集群所有节点上的所有对应缓存。
+ +可以批量删除一组Key。
+ +