Skip to content

Commit 849f354

Browse files
authored
Merge pull request #209 from whiskels/T-35_proxy
T-35: batch processing of debt data
2 parents 2f9cd4b + bd2c0a5 commit 849f354

13 files changed

+82
-44
lines changed

src/main/java/com/whiskels/notifier/slack/SlackPayload.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
@Getter
99
public class SlackPayload {
1010
private final String url;
11-
private final Payload payload;
11+
private final Payload data;
1212
}

src/main/java/com/whiskels/notifier/slack/SlackWebHookExecutor.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.whiskels.notifier.slack.reporter.SlackReporter;
55
import lombok.extern.slf4j.Slf4j;
66
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
7+
import org.springframework.http.HttpStatus;
78
import org.springframework.stereotype.Component;
89

910
import java.io.IOException;
@@ -14,13 +15,16 @@
1415
public class SlackWebHookExecutor {
1516
private final Slack slack = Slack.getInstance();
1617

17-
public void execute(SlackPayload payload) {
18-
if (payload == null) return;
18+
public int execute(SlackPayload payload) {
19+
if (payload == null) return HttpStatus.NO_CONTENT.value();
1920

2021
try {
21-
log.info(String.valueOf(slack.send(payload.getUrl(), payload.getPayload())));
22+
var response = slack.send(payload.getUrl(), payload.getData());
23+
log.info(String.valueOf(response));
24+
return response.getCode();
2225
} catch (IOException e) {
23-
log.error("Error while sending {} to webhook: {}", payload.getPayload(), payload.getUrl());
26+
log.error("Error while sending {} to webhook: {}", payload.getData(), payload.getUrl());
27+
return HttpStatus.INTERNAL_SERVER_ERROR.value();
2428
}
2529
}
2630
}

src/main/java/com/whiskels/notifier/slack/reporter/AbstractEmployeeEventReporter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ public AbstractEmployeeEventReporter(@Value("${slack.employee.webhook}") String
3939

4040
protected abstract List<Predicate<EmployeeDto>> birthdayPredicates();
4141

42-
protected final SlackPayload prepare(String header) {
43-
return prepare(header, false);
42+
protected final SlackPayload createPayload(String header) {
43+
return createPayload(header, false);
4444
}
4545

46-
protected final SlackPayload prepare(String header, boolean skipEmpty) {
46+
protected final SlackPayload createPayload(String header, boolean skipEmpty) {
4747
log.debug("Creating employee event payload");
4848
SlackPayloadBuilder builder = builder()
4949
.hook(webHook)

src/main/java/com/whiskels/notifier/slack/reporter/SlackReporter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.whiskels.notifier.slack.reporter;
22

33
import com.whiskels.notifier.external.ReportSupplier;
4-
import com.whiskels.notifier.slack.SlackPayload;
54
import com.whiskels.notifier.slack.SlackWebHookExecutor;
65
import lombok.AllArgsConstructor;
76
import lombok.extern.slf4j.Slf4j;
@@ -13,5 +12,5 @@ public abstract class SlackReporter<T> {
1312
protected final SlackWebHookExecutor executor;
1413
protected final ReportSupplier<T> provider;
1514

16-
public abstract SlackPayload prepare();
15+
public abstract void prepareAndSend();
1716
}

src/main/java/com/whiskels/notifier/slack/reporter/impl/CustomerEventReporterAtMonthMiddle.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.whiskels.notifier.common.util.Util;
44
import com.whiskels.notifier.external.ReportSupplier;
55
import com.whiskels.notifier.external.google.customer.CustomerBirthdayInfoDto;
6-
import com.whiskels.notifier.slack.SlackPayload;
76
import com.whiskels.notifier.slack.SlackWebHookExecutor;
87
import com.whiskels.notifier.slack.reporter.AbstractCustomerEventReporter;
98
import org.springframework.beans.factory.annotation.Value;
@@ -34,11 +33,12 @@ public CustomerEventReporterAtMonthMiddle(@Value("${slack.customer.birthday.webh
3433

3534
@Scheduled(cron = "${slack.customer.birthday.month-middle.cron:0 0 9 15 * *}", zone = "${common.timezone}")
3635
public void executeScheduled() {
37-
executor.execute(prepare());
36+
prepareAndSend();
3837
}
3938

40-
public SlackPayload prepare() {
41-
return createPayload(header);
39+
public void prepareAndSend() {
40+
var payload = createPayload(header);
41+
executor.execute(payload);
4242
}
4343

4444
protected List<Predicate<CustomerBirthdayInfoDto>> birthdayPredicates() {

src/main/java/com/whiskels/notifier/slack/reporter/impl/CustomerEventReporterAtMonthStart.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.whiskels.notifier.external.ReportSupplier;
44
import com.whiskels.notifier.external.google.customer.CustomerBirthdayInfoDto;
5-
import com.whiskels.notifier.slack.SlackPayload;
65
import com.whiskels.notifier.slack.SlackWebHookExecutor;
76
import com.whiskels.notifier.slack.reporter.AbstractCustomerEventReporter;
87
import org.springframework.beans.factory.annotation.Value;
@@ -33,11 +32,12 @@ public CustomerEventReporterAtMonthStart(@Value("${slack.customer.birthday.webho
3332

3433
@Scheduled(cron = "${slack.customer.birthday.month-start.cron:0 0 9 1 * *}", zone = "${common.timezone}")
3534
public void executeScheduled() {
36-
executor.execute(prepare());
35+
prepareAndSend();
3736
}
3837

39-
public SlackPayload prepare() {
40-
return createPayload(header);
38+
public void prepareAndSend() {
39+
var payload = createPayload(header);
40+
executor.execute(payload);
4141
}
4242

4343
protected List<Predicate<CustomerBirthdayInfoDto>> birthdayPredicates() {

src/main/java/com/whiskels/notifier/slack/reporter/impl/CustomerEventReporterOnEvent.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.whiskels.notifier.common.util.Util;
44
import com.whiskels.notifier.external.ReportSupplier;
55
import com.whiskels.notifier.external.google.customer.CustomerBirthdayInfoDto;
6-
import com.whiskels.notifier.slack.SlackPayload;
76
import com.whiskels.notifier.slack.SlackWebHookExecutor;
87
import com.whiskels.notifier.slack.reporter.AbstractCustomerEventReporter;
98
import org.springframework.beans.factory.annotation.Value;
@@ -34,12 +33,13 @@ public CustomerEventReporterOnEvent(@Value("${slack.customer.birthday.webhook}")
3433

3534
@Scheduled(cron = "${slack.customer.birthday.daily.cron:0 0 9 * * *}", zone = "${common.timezone}")
3635
public void executeScheduled() {
37-
executor.execute(prepare());
36+
prepareAndSend();
3837
}
3938

40-
public SlackPayload prepare() {
39+
public void prepareAndSend() {
4140
var data = provider.get();
42-
return createPayload(header + reportDate(data.getReportDate()), true);
41+
var payload = createPayload(header + reportDate(data.getReportDate()), true);
42+
executor.execute(payload);
4343
}
4444

4545
protected List<Predicate<CustomerBirthdayInfoDto>> birthdayPredicates() {

src/main/java/com/whiskels/notifier/slack/reporter/impl/DebtDailyReporter.java

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.whiskels.notifier.slack.reporter.impl;
22

3+
import com.whiskels.notifier.external.ReportData;
34
import com.whiskels.notifier.external.ReportSupplier;
45
import com.whiskels.notifier.external.json.debt.DebtDto;
56
import com.whiskels.notifier.slack.SlackPayload;
@@ -11,6 +12,9 @@
1112
import org.springframework.scheduling.annotation.Scheduled;
1213
import org.springframework.stereotype.Component;
1314

15+
import java.util.ArrayList;
16+
import java.util.List;
17+
1418
import static com.whiskels.notifier.common.util.DateTimeUtil.reportDate;
1519
import static com.whiskels.notifier.common.util.FormatUtil.COLLECTOR_TWO_NEW_LINES;
1620
import static com.whiskels.notifier.slack.reporter.builder.SlackPayloadBuilder.builder;
@@ -19,6 +23,7 @@
1923
@ConditionalOnProperty("slack.customer.debt.webhook")
2024
@ConditionalOnBean(value = DebtDto.class, parameterizedContainer = ReportSupplier.class)
2125
class DebtDailyReporter extends SlackReporter<DebtDto> {
26+
private static final int SINGLE_REPORT_CUTOFF = 20;
2227
private final String header;
2328

2429
public DebtDailyReporter(@Value("${slack.customer.debt.webhook}") String webHook,
@@ -31,17 +36,49 @@ public DebtDailyReporter(@Value("${slack.customer.debt.webhook}") String webHook
3136

3237
@Scheduled(cron = "${slack.customer.debt.cron:0 0 13 * * MON-FRI}", zone = "${common.timezone}")
3338
public void executeScheduled() {
34-
executor.execute(prepare());
39+
prepareAndSend();
3540
}
3641

37-
public SlackPayload prepare() {
42+
public void prepareAndSend() {
3843
var data = provider.get();
39-
return builder()
44+
if (data.getContent().size() < SINGLE_REPORT_CUTOFF) {
45+
singleReport(data);
46+
} else {
47+
multiReport(data);
48+
}
49+
}
50+
51+
private void singleReport(ReportData<DebtDto> data) {
52+
var report = builder()
4053
.hook(webHook)
4154
.collector(COLLECTOR_TWO_NEW_LINES)
4255
.header(header + reportDate(data.getReportDate()))
4356
.notifyChannel()
4457
.block(data.getContent())
4558
.build();
59+
executor.execute(report);
60+
}
61+
62+
private void multiReport(ReportData<DebtDto> data) {
63+
List<SlackPayload> reports = new ArrayList<>();
64+
boolean isFirstReport = true;
65+
var content = data.getContent();
66+
int reportNum = 1;
67+
for (int i = 0; i < content.size(); i += SINGLE_REPORT_CUTOFF) {
68+
var currentReportBuilder = builder();
69+
70+
if (isFirstReport) {
71+
currentReportBuilder.notifyChannel();
72+
isFirstReport = false;
73+
}
74+
currentReportBuilder.hook(webHook)
75+
.collector(COLLECTOR_TWO_NEW_LINES)
76+
.header(header + reportDate(data.getReportDate()) + " #" + reportNum++)
77+
.block(content.subList(i, Math.min(content.size(), i + SINGLE_REPORT_CUTOFF)));
78+
79+
reports.add(currentReportBuilder.build());
80+
}
81+
82+
reports.forEach(executor::execute);
4683
}
4784
}

src/main/java/com/whiskels/notifier/slack/reporter/impl/EmployeeEventReporterAtMonthMiddle.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.whiskels.notifier.common.util.Util;
44
import com.whiskels.notifier.external.ReportSupplier;
55
import com.whiskels.notifier.external.json.employee.EmployeeDto;
6-
import com.whiskels.notifier.slack.SlackPayload;
76
import com.whiskels.notifier.slack.SlackWebHookExecutor;
87
import com.whiskels.notifier.slack.reporter.AbstractEmployeeEventReporter;
98
import org.springframework.beans.factory.annotation.Value;
@@ -36,11 +35,12 @@ public EmployeeEventReporterAtMonthMiddle(@Value("${slack.employee.webhook}") St
3635

3736
@Scheduled(cron = "${slack.employee.month-middle.cron:0 0 9 15 * *}", zone = "${common.timezone}")
3837
public void executeScheduled() {
39-
executor.execute(prepare());
38+
prepareAndSend();
4039
}
4140

42-
public SlackPayload prepare() {
43-
return prepare(header);
41+
public void prepareAndSend() {
42+
var payload = createPayload(header);
43+
executor.execute(payload);
4444
}
4545

4646
protected List<Predicate<EmployeeDto>> birthdayPredicates() {

src/main/java/com/whiskels/notifier/slack/reporter/impl/EmployeeEventReporterAtMonthStart.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.whiskels.notifier.external.ReportSupplier;
44
import com.whiskels.notifier.external.json.employee.EmployeeDto;
5-
import com.whiskels.notifier.slack.SlackPayload;
65
import com.whiskels.notifier.slack.SlackWebHookExecutor;
76
import com.whiskels.notifier.slack.reporter.AbstractEmployeeEventReporter;
87
import org.springframework.beans.factory.annotation.Value;
@@ -35,11 +34,12 @@ public EmployeeEventReporterAtMonthStart(@Value("${slack.employee.webhook}") Str
3534

3635
@Scheduled(cron = "${slack.employee.cron.monthStart:0 0 9 1 * *}", zone = "${common.timezone}")
3736
public void executeScheduled() {
38-
executor.execute(prepare());
37+
prepareAndSend();
3938
}
4039

41-
public SlackPayload prepare() {
42-
return prepare(header);
40+
public void prepareAndSend() {
41+
var payload = createPayload(header);
42+
executor.execute(payload);
4343
}
4444

4545
protected List<Predicate<EmployeeDto>> birthdayPredicates() {

0 commit comments

Comments
 (0)