Revision 40135
Added by Antonis Lempesis over 8 years ago
StatsManagerImpl.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import eu.dnetlib.goldoa.domain.Request; |
4 | 4 |
import eu.dnetlib.goldoa.domain.stats.Browse; |
5 |
import eu.dnetlib.goldoa.domain.stats.DateSeries; |
|
5 | 6 |
import eu.dnetlib.goldoa.domain.stats.MapData; |
6 | 7 |
import eu.dnetlib.goldoa.domain.stats.Numbers; |
7 | 8 |
import eu.dnetlib.goldoa.domain.stats.Series; |
9 |
import eu.dnetlib.goldoa.domain.stats.Triple; |
|
8 | 10 |
import eu.dnetlib.goldoa.domain.stats.Tuple; |
9 | 11 |
import org.springframework.beans.factory.annotation.Autowired; |
10 | 12 |
import org.springframework.jdbc.core.JdbcTemplate; |
... | ... | |
14 | 16 |
import javax.sql.DataSource; |
15 | 17 |
import java.sql.ResultSet; |
16 | 18 |
import java.sql.SQLException; |
19 |
import java.text.SimpleDateFormat; |
|
20 |
import java.util.ArrayList; |
|
21 |
import java.util.Date; |
|
17 | 22 |
import java.util.HashMap; |
18 | 23 |
import java.util.List; |
19 | 24 |
import java.util.Map; |
... | ... | |
29 | 34 |
public static final String NUMBERS_TOTAL_AMOUNT = "select sum(apc_paid) from request r where r.status&" + Request.RequestStatus.APPROVED.getCode() + "= " + Request.RequestStatus.APPROVED.getCode() + " and (r.status&" + Request.RequestStatus.ACCOUNTING_DENIED.getCode() + "=0)"; |
30 | 35 |
|
31 | 36 |
public static final String SERIES_COUNTRY = "select c.name as category, count(r.id) from request r join organisation o on r.organization=o.id join country c on c.id=o.country %WHERE% group by c.name"; |
32 |
public static final String SERIES_ORGANIZATION = "select o.shortname as category, count(r.id) from request r join organisation o on r.organization=o.id %WHERE% group by o.shortname";
|
|
37 |
public static final String SERIES_ORGANIZATION = "select case when o.shortname is not null then o.shortname else o.name end as category, count(r.id) from request r join organisation o on r.organization=o.id %WHERE% group by o.shortname, o.name";
|
|
33 | 38 |
public static final String SERIES_PUBLISHER = "select p.name as category, count(r.id) from (select r.id, r.status, j.publisher, 'j' from request r join journal j on r.journal=j.id where j.publisher is not null union select r.id, r.status, p.id, 'p' from request r join publisher p on r.publisher=p.id) as r join publisher p on r.publisher=p.id %WHERE% group by p.name"; |
34 | 39 |
|
40 |
public static final String BROWSE_SCIENTIFIC_AREA = "select p.id as id, p.scientificarea as category, count(r.id) as count from request r join project p on r.project=p.id group by p. scientificarea"; |
|
41 |
public static final String BROWSE_COUNTRY = "select c.id as id, c.name as category, count(r.id) as count from request r join organisation o on r.organization=o.id join country c on c.id=o.country group by c.name"; |
|
42 |
public static final String BROWSE_ORGANIZATION = "select o.id as id, o.shortname as category, count(r.id) as count from request r join organisation o on o.id=r.organization group by o.shortname"; |
|
43 |
public static final String BROWSE_PUBLISHER = "select p.id as id, p.name as category, count(r.id) as count from (select r.id, r.status, j.publisher, 'j' from request r join journal j on r.journal=j.id where j.publisher is not null union select r.id, r.status, p.id, 'p' from request r join publisher p on r.publisher=p.id) as r join publisher p on r.publisher=p.id group by p.name"; |
|
44 |
|
|
35 | 45 |
@Autowired |
36 | 46 |
private DataSource dataSource; |
37 | 47 |
|
... | ... | |
111 | 121 |
} |
112 | 122 |
|
113 | 123 |
@Override |
114 |
public Browse browse(Browse.Category[] category) { |
|
115 |
return null; |
|
124 |
public DateSeries getDateSeries(DateSeries.Over over, boolean cummulative, Date from, Date to, String[] publishers, String[] organizations, String[] countries, String[] scientificAreas) { |
|
125 |
|
|
126 |
StringBuilder sb = new StringBuilder(); |
|
127 |
String dateColumn = null; |
|
128 |
String windowKeyword ; |
|
129 |
List<String> clauses = new ArrayList<String>(); |
|
130 |
|
|
131 |
switch (over) { |
|
132 |
case DAY: |
|
133 |
dateColumn = "date_trunc('day', submissiondate)"; |
|
134 |
break; |
|
135 |
case MONTH: |
|
136 |
dateColumn = "date_trunc('month', submissiondate)"; |
|
137 |
break; |
|
138 |
case YEAR: |
|
139 |
dateColumn = "date_trunc('year', submissiondate)"; |
|
140 |
break; |
|
141 |
} |
|
142 |
|
|
143 |
if (cummulative) |
|
144 |
windowKeyword = "order"; |
|
145 |
else |
|
146 |
windowKeyword = "partition"; |
|
147 |
|
|
148 |
if (from != null) { |
|
149 |
clauses.add(" r.submissiondate >= '" + new SimpleDateFormat("yyyy-MM-dd").format(from) + "' "); |
|
150 |
} |
|
151 |
if (to != null) { |
|
152 |
clauses.add(" r.submissiondate <= '" + new SimpleDateFormat("yyyy-MM-dd").format(to) + "' "); |
|
153 |
} |
|
154 |
|
|
155 |
if (publishers != null && publishers.length > 0) { |
|
156 |
String clause = "pub.id in ("; |
|
157 |
|
|
158 |
for (int i = 0; i < publishers.length; i++) { |
|
159 |
if (i > 0) |
|
160 |
clause += ", "; |
|
161 |
|
|
162 |
clause += "'" + publishers[i] + "'"; |
|
163 |
} |
|
164 |
|
|
165 |
clause += ")"; |
|
166 |
|
|
167 |
clauses.add(clause); |
|
168 |
} |
|
169 |
|
|
170 |
if (organizations != null && organizations.length > 0) { |
|
171 |
String clause = "o.id in ("; |
|
172 |
|
|
173 |
for (int i = 0; i < organizations.length; i++) { |
|
174 |
if (i > 0) |
|
175 |
clause += ", "; |
|
176 |
|
|
177 |
clause += "'" + organizations[i] + "'"; |
|
178 |
} |
|
179 |
|
|
180 |
clause += ")"; |
|
181 |
|
|
182 |
clauses.add(clause); |
|
183 |
} |
|
184 |
|
|
185 |
if (countries != null && countries.length > 0) { |
|
186 |
String clause = "c.id in ("; |
|
187 |
|
|
188 |
for (int i = 0; i < countries.length; i++) { |
|
189 |
if (i > 0) |
|
190 |
clause += ", "; |
|
191 |
|
|
192 |
clause += "'" + countries[i] + "'"; |
|
193 |
} |
|
194 |
|
|
195 |
clause += ")"; |
|
196 |
|
|
197 |
clauses.add(clause); |
|
198 |
} |
|
199 |
|
|
200 |
if (scientificAreas != null && scientificAreas.length > 0) { |
|
201 |
String clause = "p.scientificarea in ("; |
|
202 |
|
|
203 |
for (int i = 0; i < scientificAreas.length; i++) { |
|
204 |
if (i > 0) |
|
205 |
clause += ", "; |
|
206 |
|
|
207 |
clause += "'" + scientificAreas[i] + "'"; |
|
208 |
} |
|
209 |
|
|
210 |
clause += ")"; |
|
211 |
|
|
212 |
clauses.add(clause); |
|
213 |
} |
|
214 |
|
|
215 |
sb.append("select distinct ").append(dateColumn).append(" as date, count(r.id) over (").append(windowKeyword).append(" by ").append(dateColumn).append(") as count "); |
|
216 |
sb.append("from request r left join project p on r.project=p.id left join organisation o on r.organization=o.id left join country c on c.id=o.country left join (select r.id as rid, p.id, p.name from request r join journal j on r.journal=j.id join publisher p on p.id=j.publisher union select r.id, p.id, p.name from request r join publisher p on r.publisher=p.id) as pub on pub.rid=r.id"); |
|
217 |
|
|
218 |
if (clauses.size() > 0) |
|
219 |
sb.append(" where "); |
|
220 |
|
|
221 |
for (int i = 0; i < clauses.size(); i++) { |
|
222 |
if (i > 0) |
|
223 |
sb.append(" and "); |
|
224 |
|
|
225 |
sb.append(" (").append(clauses.get(i)).append(") "); |
|
226 |
} |
|
227 |
|
|
228 |
sb.append(" order by ").append(dateColumn); |
|
229 |
|
|
230 |
return new DateSeries(new JdbcTemplate(dataSource).query(sb.toString(), new RowMapper<Tuple<Date, Integer>>() { |
|
231 |
@Override |
|
232 |
public Tuple<Date, Integer> mapRow(ResultSet rs, int rowNum) throws SQLException { |
|
233 |
return new Tuple<Date, Integer>(rs.getDate("date"), rs.getInt("count")); |
|
234 |
} |
|
235 |
})); |
|
116 | 236 |
} |
117 | 237 |
|
238 |
public static void main(String[] args) { |
|
239 |
new StatsManagerImpl().getDateSeries(DateSeries.Over.DAY, true, new Date(), new Date(), new String[] {"123", "123"} , new String [] {"456"}, new String[] {"789"}, new String[] {"000"}); |
|
240 |
} |
|
241 |
|
|
118 | 242 |
@Override |
243 |
public Browse browse(Browse.Category[] categories) { |
|
244 |
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); |
|
245 |
Map<Browse.Category, List<Triple<String, String, Integer>>> map = new HashMap<Browse.Category, List<Triple<String, String, Integer>>>(); |
|
246 |
|
|
247 |
for (Browse.Category category:categories) { |
|
248 |
String query = null; |
|
249 |
|
|
250 |
switch (category) { |
|
251 |
case COUNTRY: |
|
252 |
query = BROWSE_COUNTRY; |
|
253 |
break; |
|
254 |
case ORGANIZATION: |
|
255 |
query = BROWSE_ORGANIZATION; |
|
256 |
break; |
|
257 |
case PUBLISHER: |
|
258 |
query = BROWSE_PUBLISHER; |
|
259 |
break; |
|
260 |
case SCIENTIFIC_AREA: |
|
261 |
query = BROWSE_SCIENTIFIC_AREA; |
|
262 |
break; |
|
263 |
} |
|
264 |
|
|
265 |
map.put(category, jdbcTemplate.query(query, new RowMapper<Triple<String, String, Integer>>() { |
|
266 |
@Override |
|
267 |
public Triple<String, String, Integer> mapRow(ResultSet rs, int rowNum) throws SQLException { |
|
268 |
return new Triple<String, String, Integer>(rs.getString("id"), rs.getString("category"), rs.getInt("count")); |
|
269 |
} |
|
270 |
})); |
|
271 |
} |
|
272 |
|
|
273 |
return new Browse(map); |
|
274 |
} |
|
275 |
|
|
276 |
@Override |
|
119 | 277 |
public MapData getMapData() { |
120 | 278 |
return null; |
121 | 279 |
} |
Also available in: Unified diff
finished stats data