CCTV_ver_1. DB에 주기적으로 가상 데이터 업로드
createSampleData.js
Database 테이블 생성
create table test (
id int not null auto_increment primary key,
start_date datetime,
end_date datetime,
people int,
vehicle int);
MySQL 연결
var mysql = require('mysql');
// mysql 연결
var conn = mysql.createConnection({
host : '',
user : '',
password : '',
database : ''
});
conn.connect();
타이머 콜백함수
var updateTime = 5; // 데이터 생성 주기시간
setInterval(timerCallback, updateTime * 1000);
// 타이머 콜백 함수
function timerCallback() {
var data = createData();
console.log(data);
var sql = 'INSERT into test(start_date, end_date, people, vehicle) value (?, ?, ?, ?)';
conn.query(sql, [data.startDate, data.endDate, data.peopleNum, data.vehicleNum], (err) => {
if (err) {
console.log(err);
} else {
console.log('complete instert data');
}
});
}
가상 데이터 랜덤으로 생성
// 샘플 데이터 랜덤으로 생성
function createData() {
var peopleNum = Math.floor(Math.random() * 10);
var vehicleNum = Math.floor(Math.random() * 5);
console.log(peopleNum, vehicleNum);
var currentDate = new Date();
var endDate = new Date(currentDate);
var startDate = new Date(currentDate.setSeconds(currentDate.getSeconds() - updateTime));
console.log(startDate, endDate);
return {
peopleNum: peopleNum,
vehicleNum: vehicleNum,
startDate: startDate,
endDate: endDate
};
}
결과 확인
mysql> SELECT * FROM test;
+----+---------------------+---------------------+--------+---------+
| id | start_date | end_date | people | vehicle |
+----+---------------------+---------------------+--------+---------+
| 1 | 2021-06-30 23:02:41 | 2021-06-30 23:02:46 | 7 | 2 |
| 2 | 2021-06-30 23:02:46 | 2021-06-30 23:02:51 | 9 | 2 |
| 3 | 2021-06-30 23:02:51 | 2021-06-30 23:02:56 | 8 | 3 |
| 4 | 2021-06-30 23:02:56 | 2021-06-30 23:03:01 | 0 | 2 |
| 5 | 2021-06-30 23:03:01 | 2021-06-30 23:03:06 | 8 | 3 |
| 6 | 2021-06-30 23:03:06 | 2021-06-30 23:03:11 | 7 | 2 |
| 7 | 2021-06-30 23:03:11 | 2021-06-30 23:03:16 | 6 | 4 |
| 8 | 2021-06-30 23:03:16 | 2021-06-30 23:03:21 | 4 | 0 |
| 9 | 2021-06-30 23:03:21 | 2021-06-30 23:03:26 | 8 | 2 |
| 10 | 2021-06-30 23:03:26 | 2021-06-30 23:03:31 | 0 | 0 |
| 11 | 2021-06-30 23:03:31 | 2021-06-30 23:03:36 | 6 | 1 |
| 12 | 2021-06-30 23:03:36 | 2021-06-30 23:03:41 | 4 | 4 |
| 13 | 2021-06-30 23:03:41 | 2021-06-30 23:03:46 | 0 | 3 |
| 14 | 2021-06-30 23:03:46 | 2021-06-30 23:03:51 | 3 | 4 |
| 15 | 2021-06-30 23:03:51 | 2021-06-30 23:03:56 | 1 | 1 |
| 16 | 2021-06-30 23:03:56 | 2021-06-30 23:04:01 | 7 | 1 |
| 17 | 2021-06-30 23:04:01 | 2021-06-30 23:04:06 | 3 | 1 |
+----+---------------------+---------------------+--------+---------+
17 rows in set (0.01 sec)
Leave a comment