Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[STYLE] scheduler 주석 추가 #89

Merged
merged 1 commit into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions functions/db/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const getEntriesByRoomId = async (client, roomId) => {
return convertSnakeToCamel.keysToCamel(rows);
};

const getEntryIdsByRoomIds = async (client, roomIds) => {
const getEntriesByRoomIds = async (client, roomIds) => {
const { rows } = await client.query(
`
SELECT * FROM spark.entry e
Expand Down Expand Up @@ -426,5 +426,5 @@ module.exports = {
getCardsByUserId,
updateLife,
getFailRecords,
getEntryIdsByRoomIds,
getEntriesByRoomIds,
};
30 changes: 15 additions & 15 deletions functions/scheduler/funcs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,48 @@ const db = require('../db/db');
const { roomDB, recordDB } = require('../db');
const _ = require('lodash');
const dayjs = require('dayjs');
const { result } = require('lodash');
const { fail } = require('../lib/util');

const checkLife = async() => {
let client;
try {
client = await db.connect();
const failRecords = await roomDB.getFailRecords(client);
const roomGroupByFailCount = _.groupBy(failRecords, "failCount");
const failCountList = [... new Set(failRecords.map((o) => Number(o.failCount)))];
const failRecords = await roomDB.getFailRecords(client); // 습관방별 [실패한 record 개수(failCount)] 불러오기
const roomGroupByFailCount = _.groupBy(failRecords, "failCount"); // failCount별 roomId 묶어주기 (ex. [{"failCount": 1, "roomId": [1,2,3]}, {"failCount":2, "roomId": [4,5,6]}])
const failCountList = [... new Set(failRecords.map((o) => Number(o.failCount)))]; // failCount 뭐뭐있는지~ (ex. [1,2,3])
const roomIdsByFailCount = {'1':[], '2':[], '3':[]};
failCountList.map((failCount) => {
const roomIds = roomGroupByFailCount[failCount].map((o) => o.roomId);
const roomIds = roomGroupByFailCount[failCount].map((o) => o.roomId); // 해당 failCount의 roomId배열
if (failCount < 3) {
roomIdsByFailCount[failCount] = roomIds;
}
else {
else { // failCount 3보다 크면 3으로 일괄처리
roomIdsByFailCount[3] = roomIdsByFailCount[3].concat(roomIds);
}
});

let afterLife = [];
for(let i=1; i<=3; i++) {
let afterLife = []; // 수명 깎아 준 후 습관방별 수명들
for(let i=1; i<=3; i++) { // 수명 깎아주기! - 3번 진행 (수명 1개깎이는 방 / 2개 깎이는 방 / 3개 깎이는 방)
if(roomIdsByFailCount[i].length) {
afterLife = afterLife.concat(await roomDB.updateLife(client, i, roomIdsByFailCount[i]));
}
}
const failRoomIds = _.filter(afterLife, {life: 0}).map((o) => o.roomId);
const successRoomIds = _.difference(failRecords.map((o) => o.roomId), failRoomIds);
const failRoomIds = _.filter(afterLife, {life: 0}).map((o) => o.roomId); // 수명 깎아주고 나서 {life: 0} 이면 폭파된 방
const successRoomIds = _.difference(failRecords.map((o) => o.roomId), failRoomIds); // 살아남은 방들

if(!successRoomIds.length) {
if(!successRoomIds.length) { // 살아남은 방 없으면 return
return;
}
const successEntries = await roomDB.getEntryIdsByRoomIds(client, successRoomIds);
const successEntries = await roomDB.getEntriesByRoomIds(client, successRoomIds); // 성공한 방들의 entry 불러오기
const now = dayjs().add(9, 'hour');
const today = now.format('YYYY-MM-DD');
const insertEntries = successEntries.map((o) => {

const insertEntries = successEntries.map((o) => { // 추가해줄 record들의 속성들 빚어주기
const startDate = dayjs(o.startAt);
const day = dayjs(today).diff(startDate, 'day') + 1;
const queryParameter = "(" + o.entryId + ",'" + now.format('YYYY-MM-DD') + "'," + day + ")";
return queryParameter;
});
const resultRecords = await recordDB.insertRecords(client, insertEntries);
const resultRecords = await recordDB.insertRecords(client, insertEntries); // record 추가!
console.log(resultRecords);
} catch (error) {
} finally {
Expand Down