Skip to content
This repository has been archived by the owner on Mar 8, 2024. It is now read-only.

Commit

Permalink
API実行回数をカウントするcounterを作成
Browse files Browse the repository at this point in the history
  • Loading branch information
proshunsuke committed Mar 12, 2021
1 parent ac68124 commit abfee5d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
5 changes: 5 additions & 0 deletions src/calendar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ScheduleInterface, SiteCalendarInterface } from './calendarInterface';
import Retry from './lib/retry';
import Counter from './lib/counter';

export default class Calendar {
/**
Expand All @@ -10,6 +11,8 @@ export default class Calendar {
Retry.retryable(3, () => {
event.deleteEvent();
});
const counter = Counter.getInstance;
counter.incrementDeleteEventCallCount();
if (process.env.ENV === 'production') {
Utilities.sleep(500); // API制限に引っかかってそうなのでsleepする
}
Expand Down Expand Up @@ -54,6 +57,8 @@ export default class Calendar {
);
}
});
const counter = Counter.getInstance;
counter.incrementCreateEventCallCount();
console.info(
`予定を作成しました。日付: ${schedule.date}, タイトル: ${schedule.title}`
);
Expand Down
36 changes: 36 additions & 0 deletions src/lib/counter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export default class Counter {
private static instance: Counter;

private createEventCallCount;

private deleteEventCallCount;

private constructor() {
this.createEventCallCount = 0;
this.deleteEventCallCount = 0;
}

public static get getInstance(): Counter {
if (!this.instance) {
this.instance = new Counter();
}

return this.instance;
}

public incrementCreateEventCallCount(): void {
this.createEventCallCount += 1;
}

public getCreateEventCallCount(): number {
return this.createEventCallCount;
}

public incrementDeleteEventCallCount(): void {
this.deleteEventCallCount += 1;
}

public getDeleteEventCallCount(): number {
return this.deleteEventCallCount;
}
}
19 changes: 7 additions & 12 deletions src/oneMonthSchedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Calendar from './calendar';
import { ScheduleInterface, SiteCalendarInterface } from './calendarInterface';
import Retry from './lib/retry';
import 'regenerator-runtime';
import Counter from './lib/counter';

export default class OneMonthSchedule {
/**
Expand Down Expand Up @@ -57,7 +58,7 @@ export default class OneMonthSchedule {
date: dayjs.Dayjs,
siteCalendarIds: SiteCalendarInterface[]
) {
let deleteEventCallCount = 0;
const counter = Counter.getInstance;
siteCalendarIds.forEach((siteCalendarId) => {
if (process.env.ENV !== 'production') return;
const calendarApp = Retry.retryable(3, () =>
Expand All @@ -70,12 +71,11 @@ export default class OneMonthSchedule {
const events = calendarApp.getEventsForDay(targetDate.toDate());
// eslint-disable-next-line @typescript-eslint/no-loop-func
events.forEach((event) => {
deleteEventCallCount += 1;
try {
Calendar.deleteEvent(event);
} catch (e) {
console.error(
`カレンダー削除に失敗しました。失敗するまでに実行された回数: ${deleteEventCallCount.toString()}`
`カレンダー削除に失敗しました。失敗するまでに実行された回数: ${counter.getDeleteEventCallCount()}`
);
throw e;
}
Expand All @@ -86,7 +86,7 @@ export default class OneMonthSchedule {
console.info(
`${date.format(
'YYYY年MM月'
)}分のカレンダー削除実行回数${deleteEventCallCount.toString()}`
)}分時点のカレンダー削除実行回数${counter.getDeleteEventCallCount()}`
);
}

Expand All @@ -101,22 +101,17 @@ export default class OneMonthSchedule {
date: dayjs.Dayjs,
calendarIds: SiteCalendarInterface[]
) {
let createEventCallCount = 0;
const counter = Counter.getInstance;
scheduleList.forEach((schedule: ScheduleInterface) => {
createEventCallCount += 1;
try {
Calendar.createEvent(schedule, calendarIds);
} catch (e) {
console.error(
`カレンダー作成に失敗しました。失敗するまでに実行された回数: ${createEventCallCount.toString()}`
`カレンダー作成に失敗しました。失敗するまでに実行された回数: ${counter.getCreateEventCallCount()}`
);
throw e;
}
});
console.info(
`${date.format(
'YYYY年MM月'
)}分のカレンダー作成実行回数: ${createEventCallCount.toString()}`
);
console.info(`${date.format('YYYY年MM月')}分時点のカレンダー作成回数: ${counter.getCreateEventCallCount()}`);
}
}

0 comments on commit abfee5d

Please sign in to comment.