forked from a597873885/webfunny_monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable_create.js
166 lines (148 loc) · 5.74 KB
/
table_create.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
var fs = require('fs');
require('./util/extension')
const db = require('./config/db')
const log = require("./config/log");
const Sequelize = db.sequelize;
var stat = fs.stat;
var proName = process.argv[2]
var dateString = process.argv[3]
/**
* 创建表
*/
function createTable() {
const BehaviorInfo = Sequelize.import('./schema_temp/behaviorInfo');
BehaviorInfo.sync({force: false});
const CustomerPV = Sequelize.import('./schema_temp/customerPV');
CustomerPV.sync({force: false});
const CustomerPvLeave = Sequelize.import('./schema_temp/CustomerPvLeave');
CustomerPvLeave.sync({force: false});
const CustomerStayTime = Sequelize.import('./schema_temp/CustomerStayTime');
CustomerStayTime.sync({force: false});
const ExtendBehaviorInfo = Sequelize.import('./schema_temp/extendBehaviorInfo');
ExtendBehaviorInfo.sync({force: false});
const HttpErrorInfo = Sequelize.import('./schema_temp/HttpErrorInfo');
HttpErrorInfo.sync({force: false});
const HttpLogInfo = Sequelize.import('./schema_temp/HttpLogInfo');
HttpLogInfo.sync({force: false});
const JavascriptErrorInfo = Sequelize.import('./schema_temp/javascriptErrorInfo');
JavascriptErrorInfo.sync({force: false});
const LocationPoints = Sequelize.import('./schema_temp/locationPoints');
LocationPoints.sync({force: false});
const LoadPageInfo = Sequelize.import('./schema_temp/loadPageInfo');
LoadPageInfo.sync({force: false});
const ResourceLoadInfo = Sequelize.import('./schema_temp/resourceLoadInfo');
ResourceLoadInfo.sync({force: false});
const ScreenShotInfo = Sequelize.import('./schema_temp/ScreenShotInfo');
ScreenShotInfo.sync({force: false});
const VideosInfo = Sequelize.import('./schema_temp/videosInfo');
VideosInfo.sync({force: false});
setTimeout(function() {
console.log("此进程将会重复多次,请不要中断,等待程序自动停止...")
}, 2000)
}
/*
* 复制目录中的所有文件包括子目录
* @param{ String } 需要复制的目录
* @param{ String } 复制到指定的目录
*/
var copy = function( src, dst ){
// 读取目录中的所有文件/目录
fs.readdir( src, function( err, paths ){
if( err ){ throw err; }
paths.forEach(function( path ){
var _src = src + '/' + path,
_dst = dst + '/' + path,
readable, writable;
stat( _src, function( err, st ){
if( err ){ throw err; }
// 判断是否为文件
if( st.isFile() ){
// 创建读取流
readable = fs.createReadStream( _src );
// 创建写入流
writable = fs.createWriteStream( _dst );
// 通过管道来传输流
readable.pipe( writable );
}
// 如果是目录则递归调用自身
else if( st.isDirectory() ){
exists( _src, _dst, copy );
}
});
});
});
};
/**
* 替换日期方法
*/
function replaceDate(proName, dateString) {
let path = './schema_temp';
let files = fs.readdirSync(path);
let fileCount = files.length
let replaceCount = 0
for(let i = 0; i < files.length; i++){
fs.readFile(`${path}/${files[i]}`,function(err, data){
if (err) throw err;
let newString = data.toString()
if (newString.indexOf("id-head") != -1 || newString.indexOf("date-tail") != -1) {
replaceCount ++
}
if (newString.indexOf("id-head") != -1) {
newString = newString.replace(/id-head/g, proName)
}
if (newString.indexOf("date-tail") != -1) {
newString = newString.replace(/date-tail/g, dateString)
}
fs.writeFile(`${path}/${files[i]}`, newString, (err) => {
if (err) throw err;
log.printInfo(files[i] + " 关键信息替换!" + i);
});
})
}
let timeout = setInterval(function() {
// 因为有个baseInfo.js不用替换,所以需要减一
if (replaceCount == fileCount - 1) {
createTable()
clearInterval(timeout)
}
}, 500)
}
/**
* 启动程序
*/
function startProgram(proName) {
console.log("===============================================")
console.log("= 即将启动创建程序,时间比较长,请耐心等待... =")
console.log("===============================================")
fs.mkdir( "./schema_temp", function(err) {
if ( err ) {
console.log("文件夹 /schema_temp 已经存在, 删除原来的内容...")
let path = './schema_temp';
let files = fs.readdirSync(path);
files.forEach(function(fileName, index) {
fs.unlink(path + "/" + fileName,() => {
console.log("成功删除 " + fileName)
// 如果最后一个删除成功
if (index == files.length - 1) {
copy("./schema_base/", "./schema_temp/")
}
});
})
} else {
console.log("新建文件夹 /schema_temp")
copy("./schema_base/", "./schema_temp/")
}
});
// 将package.json恢复成原样
fs.readFile('./package.json', function(err, data){
let newString = data.toString().replace(/node .* \d{8}/g, "table_create_command")
fs.writeFile('./package.json', newString, (err) => {
if (err) throw err;
console.log("命令配置已经恢复");
});
})
setTimeout(function() {
replaceDate(proName, dateString)
}, 3000)
}
startProgram(proName)