forked from daptin/daptin
/
action_generate_random_data.go
127 lines (96 loc) · 2.98 KB
/
action_generate_random_data.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package resource
import (
"context"
"github.com/artpar/api2go"
"github.com/artpar/go.uuid"
"github.com/daptin/daptin/server/auth"
log "github.com/sirupsen/logrus"
"net/http"
"strconv"
)
type RandomDataGeneratePerformer struct {
cmsConfig *CmsConfig
cruds map[string]*DbResource
tableMap map[string][]api2go.ColumnInfo
}
func (d *RandomDataGeneratePerformer) Name() string {
return "generate.random.data"
}
func (d *RandomDataGeneratePerformer) DoAction(request ActionRequest, inFields map[string]interface{}) (api2go.Responder, []ActionResponse, []error) {
responses := make([]ActionResponse, 0)
//subjectInstance := inFields["subject"].(map[string]interface{})
userReferenceId := ""
//userIdInt := uint64(1)
var err error
log.Info("%v", inFields)
if inFields["user_reference_id"] != nil {
userReferenceId = inFields["user_reference_id"].(string)
}
userIdInt, _ := strconv.ParseInt(inFields["user_account_id"].(string), 10, 32)
//userIdInt, err = d.cruds["user"].GetReferenceIdToId("user", userReferenceId)
if err != nil {
log.Errorf("Failed to get user id from user reference id: %v", err)
}
tableName := inFields["table_name"].(string)
count := int(inFields["count"].(float64))
rows := make([]map[string]interface{}, 0)
for i := 0; i < count; i++ {
row := GetFakeRow(d.tableMap[tableName])
u, _ := uuid.NewV4()
row["reference_id"] = u.String()
row["permission"] = auth.DEFAULT_PERMISSION
rows = append(rows, row)
}
httpRequest := &http.Request{
Method: "POST",
}
sessionUser := &auth.SessionUser{
UserId: userIdInt,
UserReferenceId: userReferenceId,
Groups: []auth.GroupPermission{},
}
httpRequest = httpRequest.WithContext(context.WithValue(context.Background(), "user", sessionUser))
req := api2go.Request{
PlainRequest: httpRequest,
}
for _, row := range rows {
_, err := d.cruds[tableName].Create(api2go.NewApi2GoModelWithData(tableName, nil, 0, nil, row), req)
if err != nil {
log.Errorf("Was about to insert this fake object: %v", row)
log.Errorf("Failed to fake insert into table [%v] : %v", tableName, err)
}
}
return nil, responses, nil
}
func GetFakeRow(columns []api2go.ColumnInfo) map[string]interface{} {
row := make(map[string]interface{})
for _, col := range columns {
if col.IsForeignKey {
continue
}
isStandardColumn := false
for _, c := range StandardColumns {
if col.ColumnName == c.ColumnName {
isStandardColumn = true
}
}
if isStandardColumn {
continue
}
fakeValue := ColumnManager.GetFakedata(col.ColumnType)
row[col.ColumnName] = fakeValue
}
return row
}
func NewRandomDataGeneratePerformer(initConfig *CmsConfig, cruds map[string]*DbResource) (ActionPerformerInterface, error) {
tableMap := make(map[string][]api2go.ColumnInfo)
for _, table := range initConfig.Tables {
tableMap[table.TableName] = table.Columns
}
handler := RandomDataGeneratePerformer{
cmsConfig: initConfig,
cruds: cruds,
tableMap: tableMap,
}
return &handler, nil
}