-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
143 lines (135 loc) · 4.04 KB
/
app.js
File metadata and controls
143 lines (135 loc) · 4.04 KB
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
const express = require("express");
const fs = require("fs");
const schedule = require("node-schedule");
const path = require("path");
const Log = require('log')
, log = new Log('info', fs.createWriteStream('log.log'));
const app = express();
var mysql = require('mysql');
var pool = mysql.createPool({
host: '192.168.120.221',
user: 'root',
password: 'password',
database: 'registro_ftp'
});
var usuarios = [];
function getLinea(linea) {
var archivo = getFecha();
return {
dia: linea[0],
mes: linea[1],
diaFecha: linea[2],
hora: linea[3],
año: linea[4],
velocidad_desc: linea[5],
ip: linea[6],
tamaño_archivo: linea[7],
ruta_archivo: linea[8],
B: linea[9],
Guion: linea[10],
O: linea[11],
R: linea[12],
usuario: linea[13],
protocolo: linea[14],
Cero: linea[15],
Asterisco: linea[16],
estado: linea[17],
archivo_log: archivo
}
}
function getFecha() {
var d = new Date();
var dia = d.getDate();
var mes = d.getMonth() + 1;
var año = d.getFullYear();
var fecha = año + "" + mes + "" + dia;
var archivo = "xferlog-" + fecha;
return archivo;
}
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get("/select", function (req, res) {
pool.query('SELECT * from registroftp', function (err, rows, fields) {
rows.forEach(function (item, index) {
console.log(item.usuario);
});
});
res.send("ok");
})
log.info("1");
app.get("/actual", function (req, res) {
var reporte;
var rpta = {};
fs.readFile(__dirname + "/../../var/log/xferlog", "utf8", function (err, data) {
if (err) {
console.log("no se pudo");
rpta = {
"cod": 0,
"msg": "error"
};
log.info("casi");
res.send(rpta);
} else {
console.log("si se pudo");
var tr = data.split("\n");
tr.forEach(function (item, index) {
var palabra = item.split(" ");
if (palabra.length === 19) {
var i = palabra.indexOf("");
palabra.splice(i, 1);
}
reporte = getLinea(palabra);
pool.query('INSERT INTO reporteftp SET ?', reporte, function (err) {
if (err) {
console.log(err);
if (err.code == "ER_DUP_ENTRY") {
console.log("error, ya existen");
log.info("ya existe");
}
}else{
log.info("bien");
}
});
})
rpta = {
"cod": 1,
"msg": "ok"
};
res.send(rpta);
}
});
})
var reporte;
var archivo = getFecha();
var rule = new schedule.RecurrenceRule();
rule.hour = 06;
rule.minute = 00;
var j = schedule.scheduleJob(rule, function () {
fs.readFile(__dirname + "/../../var/log/" + archivo, "utf8", function (err, data) {
if (err) {
console.log("no se pudo");
} else {
console.log("si se pudo subir");
var tr = data.split("\n");
tr.forEach(function (item, index) {
var palabra = item.split(" ");
reporte = getLinea(palabra);
pool.query('INSERT INTO reporteftp SET ?', reporte, function (err) {
if (err) {
console.log(err);
if (err.code == "ER_DUP_ENTRY") {
console.log("error, ya existen");
}
}else{
}
});
})
}
});
});
app.listen(3000, function () {
console.log("Listo puerto");
});