Skip to content

Commit ed9a15d

Browse files
committed
admin_deily_report
1 parent 22b3e5b commit ed9a15d

File tree

15 files changed

+109
-30
lines changed

15 files changed

+109
-30
lines changed

src/main/java/ru/mystamps/web/Url.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public final class Url {
3737
public static final String INDEX_PAGE = "/";
3838
public static final String ROBOTS_TXT = "/robots.txt";
3939
public static final String SITEMAP_XML = "/sitemap.xml";
40-
40+
41+
public static final String DAILY_STATISTICS = "/report/daily";
4142
public static final String SITE_EVENTS_PAGE = "/site/events";
4243

4344
public static final String REGISTRATION_PAGE = "/account/register";
@@ -141,6 +142,7 @@ public static Map<String, String> asMap(boolean serveContentFromSingleHost) {
141142
map.put("INFO_COLLECTION_PAGE", INFO_COLLECTION_PAGE);
142143
map.put("SITE_EVENTS_PAGE", SITE_EVENTS_PAGE);
143144
map.put("BOOTSTRAP_LANGUAGE", BOOTSTRAP_LANGUAGE);
145+
map.put("DAILY_STATISTICS", DAILY_STATISTICS);
144146

145147
if (serveContentFromSingleHost) {
146148
map.put("BOOTSTRAP_CSS", BOOTSTRAP_CSS);

src/main/java/ru/mystamps/web/config/ControllersConfig.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ public ParticipantController getParticipantController() {
8787
public RobotsTxtController getRobotsTxtController() {
8888
return new RobotsTxtController();
8989
}
90+
91+
@Bean
92+
public ReportController getReportController() {
93+
return new ReportController(
94+
servicesConfig.getMailService(),
95+
servicesConfig.getCronService()
96+
);
97+
}
9098

9199
@Bean
92100
public SeriesController getSeriesController() {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (C) 2009-2016 Slava Semushin <slava.semushin@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
19+
package ru.mystamps.web.controller;
20+
21+
import org.springframework.stereotype.Controller;
22+
import org.springframework.web.bind.annotation.GetMapping;
23+
import org.springframework.web.bind.annotation.ResponseBody;
24+
25+
import lombok.RequiredArgsConstructor;
26+
27+
import ru.mystamps.web.Url;
28+
import ru.mystamps.web.service.CronService;
29+
import ru.mystamps.web.service.MailService;
30+
31+
/**
32+
* @author Maxim Shestakov
33+
*/
34+
@Controller
35+
@RequiredArgsConstructor
36+
public class ReportController {
37+
38+
private final MailService mailService;
39+
private final CronService cronService;
40+
41+
@GetMapping(path = Url.DAILY_STATISTICS, produces = "text/plain; charset=UTF-8")
42+
@ResponseBody
43+
public String showDailyReport() {
44+
return mailService.getTextOfDailyStatisticsMail(
45+
cronService.getDailyStatistics()
46+
);
47+
}
48+
49+
}

src/main/java/ru/mystamps/web/service/CronService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
*/
1818
package ru.mystamps.web.service;
1919

20+
import ru.mystamps.web.service.dto.AdminDailyReport;
21+
2022
public interface CronService {
2123
int PURGE_AFTER_DAYS = 3;
2224

2325
void sendDailyStatistics();
26+
AdminDailyReport getDailyStatistics();
2427
void purgeUsersActivations();
2528
}

src/main/java/ru/mystamps/web/service/CronServiceImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ public class CronServiceImpl implements CronService {
5555
@Scheduled(cron = EVERY_DAY_AT_00_00)
5656
@Transactional(readOnly = true)
5757
public void sendDailyStatistics() {
58+
mailService.sendDailyStatisticsToAdmin(getDailyStatistics());
59+
}
60+
61+
@Override
62+
public AdminDailyReport getDailyStatistics() {
5863
Date today = DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH);
5964
Date yesterday = DateUtils.addDays(today, -1);
6065

@@ -100,7 +105,7 @@ public void sendDailyStatistics() {
100105
);
101106
report.setInvalidCsrfCounter(invalidCsrfCounter);
102107

103-
mailService.sendDailyStatisticsToAdmin(report);
108+
return report;
104109
}
105110

106111
@Override

src/main/java/ru/mystamps/web/service/MailService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@
2323
public interface MailService {
2424
void sendActivationKeyToUser(SendUsersActivationDto activation);
2525
void sendDailyStatisticsToAdmin(AdminDailyReport report);
26+
String getTextOfDailyStatisticsMail(AdminDailyReport report);
2627
}

src/main/java/ru/mystamps/web/service/MailServiceImpl.java

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,36 @@ public void sendDailyStatisticsToAdmin(AdminDailyReport report) {
114114
adminLang
115115
);
116116
}
117+
118+
@Override
119+
public String getTextOfDailyStatisticsMail(AdminDailyReport report) {
120+
String template = messageSource.getMessage("daily_stat.text", null, adminLang);
121+
String fromDate = shortDatePrinter.format(report.getStartDate());
122+
String tillDate = shortDatePrinter.format(report.getEndDate());
123+
124+
Map<String, String> ctx = new HashMap<>();
125+
ctx.put("from_date", fromDate);
126+
ctx.put("to_date", tillDate);
127+
128+
put(ctx, "added_countries_cnt", report.getAddedCountriesCounter());
129+
put(ctx, "untranslated_countries_cnt", report.getUntranslatedCountriesCounter());
130+
put(ctx, "added_categories_cnt", report.getAddedCategoriesCounter());
131+
put(ctx, "untranslated_categories_cnt", report.getUntranslatedCategoriesCounter());
132+
put(ctx, "added_series_cnt", report.getAddedSeriesCounter());
133+
put(ctx, "updated_series_cnt", report.getUpdatedSeriesCounter());
134+
put(ctx, "updated_collections_cnt", report.getUpdatedCollectionsCounter());
135+
put(ctx, "registration_requests_cnt", report.getRegistrationRequestsCounter());
136+
put(ctx, "registered_users_cnt", report.getRegisteredUsersCounter());
137+
put(ctx, "events_cnt", report.countEvents());
138+
put(ctx, "not_found_cnt", report.getNotFoundCounter());
139+
put(ctx, "failed_auth_cnt", report.getFailedAuthCounter());
140+
put(ctx, "missing_csrf_cnt", report.getMissingCsrfCounter());
141+
put(ctx, "invalid_csrf_cnt", report.getInvalidCsrfCounter());
142+
put(ctx, "bad_request_cnt", -1L); // TODO: #122
117143

144+
return new StrSubstitutor(ctx).replace(template);
145+
}
146+
118147
@SuppressWarnings("PMD.UseObjectForClearerAPI")
119148
private void sendMail(
120149
final String email,
@@ -183,34 +212,6 @@ private String getSubjectOfDailyStatisticsMail(AdminDailyReport report) {
183212
StrSubstitutor substitutor = new StrSubstitutor(ctx);
184213
return substitutor.replace(template);
185214
}
186-
187-
private String getTextOfDailyStatisticsMail(AdminDailyReport report) {
188-
String template = messageSource.getMessage("daily_stat.text", null, adminLang);
189-
String fromDate = shortDatePrinter.format(report.getStartDate());
190-
String tillDate = shortDatePrinter.format(report.getEndDate());
191-
192-
Map<String, String> ctx = new HashMap<>();
193-
ctx.put("from_date", fromDate);
194-
ctx.put("to_date", tillDate);
195-
196-
put(ctx, "added_countries_cnt", report.getAddedCountriesCounter());
197-
put(ctx, "untranslated_countries_cnt", report.getUntranslatedCountriesCounter());
198-
put(ctx, "added_categories_cnt", report.getAddedCategoriesCounter());
199-
put(ctx, "untranslated_categories_cnt", report.getUntranslatedCategoriesCounter());
200-
put(ctx, "added_series_cnt", report.getAddedSeriesCounter());
201-
put(ctx, "updated_series_cnt", report.getUpdatedSeriesCounter());
202-
put(ctx, "updated_collections_cnt", report.getUpdatedCollectionsCounter());
203-
put(ctx, "registration_requests_cnt", report.getRegistrationRequestsCounter());
204-
put(ctx, "registered_users_cnt", report.getRegisteredUsersCounter());
205-
put(ctx, "events_cnt", report.countEvents());
206-
put(ctx, "not_found_cnt", report.getNotFoundCounter());
207-
put(ctx, "failed_auth_cnt", report.getFailedAuthCounter());
208-
put(ctx, "missing_csrf_cnt", report.getMissingCsrfCounter());
209-
put(ctx, "invalid_csrf_cnt", report.getInvalidCsrfCounter());
210-
put(ctx, "bad_request_cnt", -1L); // TODO: #122
211-
212-
return new StrSubstitutor(ctx).replace(template);
213-
}
214215

215216
private static void put(Map<String, String> ctx, String key, long value) {
216217
ctx.put(key, String.valueOf(value));

src/main/java/ru/mystamps/web/support/spring/security/Authority.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public final class Authority {
3333
public static final GrantedAuthority UPDATE_COLLECTION = new SimpleGrantedAuthority(StringAuthority.UPDATE_COLLECTION);
3434
public static final GrantedAuthority VIEW_SITE_EVENTS = new SimpleGrantedAuthority(StringAuthority.VIEW_SITE_EVENTS);
3535
public static final GrantedAuthority VIEW_SERIES_SALES = new SimpleGrantedAuthority(StringAuthority.VIEW_SERIES_SALES);
36+
public static final GrantedAuthority VIEW_DAILY_STATS = new SimpleGrantedAuthority(StringAuthority.VIEW_DAILY_STATS);
3637

3738
private Authority() {
3839
}

src/main/java/ru/mystamps/web/support/spring/security/CustomUserDetailsService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ private static Collection<? extends GrantedAuthority> getAuthorities(UserDetails
8181
authorities.add(Authority.ADD_SERIES_SALES);
8282
authorities.add(Authority.VIEW_SERIES_SALES);
8383
authorities.add(Authority.MANAGE_TOGGLZ);
84+
authorities.add(Authority.VIEW_DAILY_STATS);
8485
}
8586

8687
return authorities;

src/main/java/ru/mystamps/web/support/spring/security/HasAuthority.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public final class HasAuthority {
2424
public static final String CREATE_SERIES = "hasAuthority('" + StringAuthority.CREATE_SERIES + "')";
2525
public static final String CREATE_CATEGORY = "hasAuthority('" + StringAuthority.CREATE_CATEGORY + "')";
2626
public static final String CREATE_COUNTRY = "hasAuthority('" + StringAuthority.CREATE_COUNTRY + "')";
27+
public static final String VIEW_DAILY_STATS = "hasAuthority('" + StringAuthority.VIEW_DAILY_STATS + "')";
2728
public static final String UPDATE_COLLECTION = "hasAuthority('" + StringAuthority.UPDATE_COLLECTION + "')";
2829
public static final String VIEW_SITE_EVENTS = "hasAuthority('" + StringAuthority.VIEW_SITE_EVENTS + "')";
2930
public static final String VIEW_SERIES_SALES = "hasAuthority('" + StringAuthority.VIEW_SERIES_SALES + "')";

0 commit comments

Comments
 (0)