forked from xeals/signal-back
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyse.go
112 lines (101 loc) · 2.74 KB
/
analyse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package cmd
import (
"fmt"
"strings"
"github.com/pkg/errors"
"github.com/urfave/cli"
"github.com/xeals/signal-back/signal"
"github.com/xeals/signal-back/types"
)
// Analyse fulfils the `analyse` subcommand.
var Analyse = cli.Command{
Name: "analyse",
Usage: "Information about the backup file",
UsageText: "Display statistical information about the backup file.",
Aliases: []string{"analyze"},
CustomHelpTemplate: SubcommandHelp,
Flags: coreFlags,
Action: func(c *cli.Context) error {
bf, err := setup(c)
if err != nil {
return err
}
a, err := AnalyseTables(bf)
fmt.Println("This is still largely in flux and reflects whatever task I was having issues with at the time.\n")
fmt.Println(a)
fmt.Println("part:", len(examples["insert_into_part"].GetParameters()), examples["insert_into_part"])
return errors.WithMessage(err, "failed to analyse tables")
},
}
var examples = map[string]*signal.SqlStatement{}
// AnalyseTables calculates the frequency of all records in the backup file.
func AnalyseTables(bf *types.BackupFile) (map[string]int, error) {
counts := make(map[string]int)
frames, err := bf.Slurp()
if err != nil {
return nil, errors.Wrap(err, "failed to slurp frames")
}
for _, f := range frames {
if f.GetHeader() != nil {
counts["header"]++
continue
}
if f.GetVersion() != nil {
counts["version"]++
continue
}
if f.GetAttachment() != nil {
counts["attachment"]++
continue
}
if f.GetAvatar() != nil {
counts["avatar"]++
continue
}
if f.GetPreference() != nil {
counts["pref"]++
continue
}
if stmt := f.GetStatement(); stmt != nil {
if strings.HasPrefix(*stmt.Statement, "DROP TABLE") {
if counts["drop_table"] == 0 {
examples["drop_table"] = stmt
}
counts["drop_table"]++
continue
}
if strings.HasPrefix(*stmt.Statement, "CREATE TABLE") {
if counts["create_table"] == 0 {
examples["create_table"] = stmt
}
counts["create_table"]++
continue
}
if strings.HasPrefix(*stmt.Statement, "DROP INDEX") {
if counts["drop_index"] == 0 {
examples["drop_index"] = stmt
}
counts["drop_index"]++
continue
}
if strings.HasPrefix(*stmt.Statement, "CREATE INDEX") ||
strings.HasPrefix(*stmt.Statement, "CREATE UNIQUE INDEX") {
if counts["create_index"] == 0 {
examples["create_index"] = stmt
}
counts["create_index"]++
continue
}
if strings.HasPrefix(*stmt.Statement, "INSERT INTO") {
table := strings.Split(*stmt.Statement, " ")[2]
if counts["insert_into_"+table] == 0 {
examples["insert_into_"+table] = stmt
}
counts["insert_into_"+table]++
continue
}
counts["other_stmt"]++
}
}
return counts, nil
}