|
| 1 | +package pika_keys_analysis |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/desertbit/grumble" |
| 10 | + "github.com/fatih/color" |
| 11 | +) |
| 12 | + |
| 13 | +var App = grumble.New(&grumble.Config{ |
| 14 | + Name: "pika_keys_analysis", |
| 15 | + Description: "A tool for analyzing keys in Pika", |
| 16 | + HistoryFile: "/tmp/.pika_keys_analysis_history", |
| 17 | + Prompt: "pika_keys_analysis > ", |
| 18 | + HistoryLimit: 100, |
| 19 | + ErrorColor: color.New(color.FgRed, color.Bold, color.Faint), |
| 20 | + HelpHeadlineColor: color.New(color.FgGreen), |
| 21 | + HelpHeadlineUnderline: false, |
| 22 | + HelpSubCommands: true, |
| 23 | + PromptColor: color.New(color.FgBlue, color.Bold), |
| 24 | + Flags: func(f *grumble.Flags) {}, |
| 25 | +}) |
| 26 | + |
| 27 | +func init() { |
| 28 | + App.OnInit(func(a *grumble.App, fm grumble.FlagMap) error { |
| 29 | + return nil |
| 30 | + }) |
| 31 | + App.SetPrintASCIILogo(func(a *grumble.App) { |
| 32 | + fmt.Println(strings.Join([]string{` |
| 33 | + ............. .... ..... ..... ..... |
| 34 | + ################# #### ##### ##### ####### |
| 35 | + #### ##### #### ##### ##### ######### |
| 36 | + #### ##### #### ##### ##### #### ##### |
| 37 | + #### ##### #### ##### ##### #### ##### |
| 38 | + ################ #### ##### ##### #### ##### |
| 39 | + #### #### ##### ##### ################# |
| 40 | + #### #### ##### ###### ##### ##### |
| 41 | + #### #### ##### ###### ##### ##### |
| 42 | +`}, "\r\n")) |
| 43 | + }) |
| 44 | + register(App) |
| 45 | +} |
| 46 | + |
| 47 | +func register(app *grumble.App) { |
| 48 | + app.AddCommand(&grumble.Command{ |
| 49 | + Name: "bigKey", |
| 50 | + Help: "list the big keys", |
| 51 | + LongHelp: "list the big keys", |
| 52 | + Run: func(c *grumble.Context) error { |
| 53 | + listBigKeys, err := PikaInstance.ListBigKeysByScan(context.Background()) |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + start := time.Now() |
| 58 | + for keyType, data := range listBigKeys { |
| 59 | + fmt.Printf("Type: %s, Head: %d\n", keyType, Head) |
| 60 | + if len(data.GetTopN(Head)) == 0 { |
| 61 | + fmt.Println("No big key found") |
| 62 | + } |
| 63 | + for _, v := range data.GetTopN(Head) { |
| 64 | + fmt.Printf("Key : %s, Size: %d, From: %s\n", v.Key, v.UsedSize, v.Client) |
| 65 | + } |
| 66 | + } |
| 67 | + end := time.Now() |
| 68 | + if PrintKeyNum { |
| 69 | + fmt.Println("Total Key Number:", PikaInstance.GetTotalKeyNumber()) |
| 70 | + } |
| 71 | + fmt.Println("Cost Time:", end.Sub(start)) |
| 72 | + return nil |
| 73 | + }, |
| 74 | + }) |
| 75 | + |
| 76 | + app.AddCommand(&grumble.Command{ |
| 77 | + Name: "apply", |
| 78 | + Help: "Apply the settings to Pika", |
| 79 | + LongHelp: "Apply the settings to Pika", |
| 80 | + Args: func(a *grumble.Args) { |
| 81 | + a.String("filename", "The configuration file") |
| 82 | + }, |
| 83 | + Run: func(c *grumble.Context) error { |
| 84 | + filename := c.Args.String("filename") |
| 85 | + return Init(filename) |
| 86 | + }, |
| 87 | + }) |
| 88 | + |
| 89 | + app.AddCommand(&grumble.Command{ |
| 90 | + Name: "compress", |
| 91 | + Help: "Compress the big keys", |
| 92 | + LongHelp: "Compress the big keys and store them to pika", |
| 93 | + Args: func(a *grumble.Args) { |
| 94 | + a.String("key", "The key to compress") |
| 95 | + }, |
| 96 | + Run: func(c *grumble.Context) error { |
| 97 | + key := c.Args.String("key") |
| 98 | + return PikaInstance.CompressKey(context.Background(), key) |
| 99 | + }, |
| 100 | + }) |
| 101 | + |
| 102 | + app.AddCommand(&grumble.Command{ |
| 103 | + Name: "decompress", |
| 104 | + Help: "Decompress the big keys", |
| 105 | + LongHelp: "Decompress the big keys and store them to pika", |
| 106 | + Args: func(a *grumble.Args) { |
| 107 | + a.String("key", "The key to decompress") |
| 108 | + }, |
| 109 | + Flags: func(f *grumble.Flags) { |
| 110 | + f.Bool("s", "save", false, "Save the decompressed value to pika") |
| 111 | + }, |
| 112 | + Run: func(c *grumble.Context) error { |
| 113 | + key := c.Args.String("key") |
| 114 | + save := c.Flags.Bool("save") |
| 115 | + decompressKey, err := PikaInstance.DecompressKey(context.Background(), key, save) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + fmt.Printf("Key: %s, Decompress: %s\n", key, decompressKey) |
| 120 | + return nil |
| 121 | + }, |
| 122 | + }) |
| 123 | + |
| 124 | + app.AddCommand(&grumble.Command{ |
| 125 | + Name: "recover", |
| 126 | + Help: "Recover the big keys", |
| 127 | + LongHelp: "Recover the big keys and store them to pika", |
| 128 | + Args: func(a *grumble.Args) { |
| 129 | + a.String("key", "The key to recover") |
| 130 | + a.String("newKey", "The new key to store the recovered value") |
| 131 | + }, |
| 132 | + Run: func(c *grumble.Context) error { |
| 133 | + key := c.Args.String("key") |
| 134 | + newKey := c.Args.String("newKey") |
| 135 | + return PikaInstance.RecoverKey(context.Background(), key, newKey) |
| 136 | + }, |
| 137 | + }) |
| 138 | +} |
0 commit comments