RedemptionC commented on a change in pull request #2065:
URL: https://github.com/apache/apisix-dashboard/pull/2065#discussion_r696297658



##########
File path: api/internal/handler/cache_verify/cache_verify.go
##########
@@ -0,0 +1,268 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 cache_verify
+
+import (
+       "context"
+       "encoding/json"
+       "fmt"
+       "github.com/apisix/manager-api/internal/core/storage"
+       "github.com/apisix/manager-api/internal/core/store"
+       "github.com/apisix/manager-api/internal/handler"
+       "github.com/gin-gonic/gin"
+       "github.com/shiningrush/droplet"
+       wgin "github.com/shiningrush/droplet/wrapper/gin"
+       "path"
+       "reflect"
+)
+
+type Handler struct {
+       consumerStore     store.Interface
+       routeStore        store.Interface
+       serviceStore      store.Interface
+       sslStore          store.Interface
+       upstreamStore     store.Interface
+       scriptStore       store.Interface
+       globalPluginStore store.Interface
+       pluginConfigStore store.Interface
+       serverInfoStore   store.Interface
+       etcdStorage       storage.Interface
+}
+
+type inconsistentPair struct {
+       Key        string `json:"key"`
+       CacheValue string `json:"cache_value"`
+       EtcdValue  string `json:"etcd_value"`
+}
+
+type StatisticalData struct {
+       Total             int                `json:"total"`
+       ConsistentCount   int                `json:"consistent_count"`
+       InconsistentCount int                `json:"inconsistent_count"`
+       InconsistentPairs []inconsistentPair `json:"inconsistent_pairs"`
+}
+
+type items struct {
+       Consumers     StatisticalData `json:"consumers"`
+       Routes        StatisticalData `json:"routes"`
+       Services      StatisticalData `json:"services"`
+       SSLs          StatisticalData `json:"ssls"`
+       Upstreams     StatisticalData `json:"upstreams"`
+       Scripts       StatisticalData `json:"scripts"`
+       GlobalPlugins StatisticalData `json:"global_plugins"`
+       PluginConfigs StatisticalData `json:"plugin_configs"`
+       ServerInfos   StatisticalData `json:"server_infos"`
+}
+
+type OutputResult struct {
+       Total             int   `json:"total"`
+       ConsistentCount   int   `json:"consistent_count"`
+       InconsistentCount int   `json:"inconsistent_count"`
+       Items             items `json:"items"`
+}
+
+var infixMap = map[store.HubKey]string{
+       store.HubKeyConsumer:     "consumers",
+       store.HubKeyRoute:        "routes",
+       store.HubKeyService:      "services",
+       store.HubKeySsl:          "ssl",
+       store.HubKeyUpstream:     "upstreams",
+       store.HubKeyScript:       "scripts",
+       store.HubKeyGlobalRule:   "global_rules",
+       store.HubKeyServerInfo:   "data_plane/server_info",
+       store.HubKeyPluginConfig: "plugin_configs",
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+       return &Handler{
+               consumerStore:     store.GetStore(store.HubKeyConsumer),
+               routeStore:        store.GetStore(store.HubKeyRoute),
+               serviceStore:      store.GetStore(store.HubKeyService),
+               sslStore:          store.GetStore(store.HubKeySsl),
+               upstreamStore:     store.GetStore(store.HubKeyUpstream),
+               scriptStore:       store.GetStore(store.HubKeyScript),
+               globalPluginStore: store.GetStore(store.HubKeyGlobalRule),
+               pluginConfigStore: store.GetStore(store.HubKeyPluginConfig),
+               serverInfoStore:   store.GetStore(store.HubKeyServerInfo),
+               etcdStorage:       storage.GenEtcdStorage(),
+       }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+       r.GET("/apisix/admin/cache_verify", wgin.Wraps(h.CacheVerify))
+}
+
+func (h *Handler) CacheVerify(_ droplet.Context) (interface{}, error) {
+       checkConsistent := func(hubKey store.HubKey, s *store.Interface, rs 
*OutputResult, etcd *storage.Interface) {
+
+               keyPairs, err := (*etcd).List(context.TODO(), 
fmt.Sprintf("/apisix/%s/", infixMap[hubKey]))
+               if err != nil {
+                       fmt.Println(err)
+               }
+
+               rs.Total += len(keyPairs)
+
+               for i := range keyPairs {
+                       key := path.Base(keyPairs[i].Key)
+
+                       cacheObj, err := (*s).Get(context.TODO(), key)
+                       if err != nil {
+                               fmt.Println(err)
+                       }
+
+                       etcdValue := keyPairs[i].Value
+                       cmp, cacheValue := compare(keyPairs[i].Value, cacheObj)
+
+                       if !cmp {
+                               rs.InconsistentCount++
+                               cmpResult := inconsistentPair{EtcdValue: 
etcdValue, CacheValue: cacheValue, Key: key}
+                               if hubKey == store.HubKeyConsumer {

Review comment:
       > why not use switch ?
   
   @nic-chen 
   is it OK to use map or slice to avoid if or switch?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to