github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/allegrosql/server/statistics_handler.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package server 15 16 import ( 17 "net/http" 18 "time" 19 20 "github.com/gorilla/mux" 21 "github.com/whtcorpsinc/BerolinaSQL/perceptron" 22 "github.com/whtcorpsinc/BerolinaSQL/allegrosql" 23 "github.com/whtcorpsinc/milevadb/petri" 24 "github.com/whtcorpsinc/milevadb/stochastik" 25 "github.com/whtcorpsinc/milevadb/stochastikctx/variable" 26 "github.com/whtcorpsinc/milevadb/types" 27 "github.com/whtcorpsinc/milevadb/soliton/gcutil" 28 "github.com/whtcorpsinc/milevadb/soliton/sqlexec" 29 ) 30 31 // StatsHandler is the handler for dumping statistics. 32 type StatsHandler struct { 33 do *petri.Petri 34 } 35 36 func (s *Server) newStatsHandler() *StatsHandler { 37 causetstore, ok := s.driver.(*MilevaDBDriver) 38 if !ok { 39 panic("Illegal driver") 40 } 41 42 do, err := stochastik.GetPetri(causetstore.causetstore) 43 if err != nil { 44 panic("Failed to get petri") 45 } 46 return &StatsHandler{do} 47 } 48 49 func (sh StatsHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { 50 w.Header().Set("Content-Type", "application/json") 51 52 params := mux.Vars(req) 53 54 is := sh.do.SchemaReplicant() 55 h := sh.do.StatsHandle() 56 tbl, err := is.BlockByName(perceptron.NewCIStr(params[FIDelBName]), perceptron.NewCIStr(params[pBlockName])) 57 if err != nil { 58 writeError(w, err) 59 } else { 60 js, err := h.DumpStatsToJSON(params[FIDelBName], tbl.Meta(), nil) 61 if err != nil { 62 writeError(w, err) 63 } else { 64 writeData(w, js) 65 } 66 } 67 } 68 69 // StatsHistoryHandler is the handler for dumping statistics. 70 type StatsHistoryHandler struct { 71 do *petri.Petri 72 } 73 74 func (s *Server) newStatsHistoryHandler() *StatsHistoryHandler { 75 causetstore, ok := s.driver.(*MilevaDBDriver) 76 if !ok { 77 panic("Illegal driver") 78 } 79 80 do, err := stochastik.GetPetri(causetstore.causetstore) 81 if err != nil { 82 panic("Failed to get petri") 83 } 84 return &StatsHistoryHandler{do} 85 } 86 87 func (sh StatsHistoryHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { 88 w.Header().Set("Content-Type", "application/json") 89 90 params := mux.Vars(req) 91 se, err := stochastik.CreateStochastik(sh.do.CausetStore()) 92 if err != nil { 93 writeError(w, err) 94 return 95 } 96 se.GetStochastikVars().StmtCtx.TimeZone = time.Local 97 t, err := types.ParseTime(se.GetStochastikVars().StmtCtx, params[pSnapshot], allegrosql.TypeTimestamp, 6) 98 if err != nil { 99 writeError(w, err) 100 return 101 } 102 t1, err := t.GoTime(time.Local) 103 if err != nil { 104 writeError(w, err) 105 return 106 } 107 snapshot := variable.GoTimeToTS(t1) 108 err = gcutil.ValidateSnapshot(se, snapshot) 109 if err != nil { 110 writeError(w, err) 111 return 112 } 113 114 is, err := sh.do.GetSnapshotSchemaReplicant(snapshot) 115 if err != nil { 116 writeError(w, err) 117 return 118 } 119 h := sh.do.StatsHandle() 120 tbl, err := is.BlockByName(perceptron.NewCIStr(params[FIDelBName]), perceptron.NewCIStr(params[pBlockName])) 121 if err != nil { 122 writeError(w, err) 123 return 124 } 125 se.GetStochastikVars().SnapshotschemaReplicant, se.GetStochastikVars().SnapshotTS = is, snapshot 126 historyStatsInterDirc := se.(sqlexec.RestrictedALLEGROSQLInterlockingDirectorate) 127 js, err := h.DumpStatsToJSON(params[FIDelBName], tbl.Meta(), historyStatsInterDirc) 128 if err != nil { 129 writeError(w, err) 130 } else { 131 writeData(w, js) 132 } 133 }