Revision 41953
Added by Eri Katsari over 8 years ago
modules/dnet-openaire-usage-stats-api/src/main/java/eu/dnetlib/usagestats/controllers/Application.java | ||
---|---|---|
1 |
package eu.dnetlib.usagestats.controllers; |
|
2 |
|
|
3 |
/** |
|
4 |
* Created by eri_k on 2/21/2016. |
|
5 |
*/ |
|
6 |
|
|
7 |
import org.springframework.boot.SpringApplication; |
|
8 |
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
|
9 |
import org.springframework.boot.autoconfigure.SpringBootApplication; |
|
10 |
import org.springframework.boot.orm.jpa.EntityScan; |
|
11 |
import org.springframework.context.annotation.ComponentScan; |
|
12 |
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; |
|
13 |
|
|
14 |
@SpringBootApplication |
|
15 |
@EnableAutoConfiguration |
|
16 |
@ComponentScan(basePackages = {"eu.dnetlib.usagestats.domain","eu.dnetlib.usagestats.repos","eu.dnetlib.usagestats.controllers"}) |
|
17 |
public class Application { |
|
18 |
public static void main(String[] args) { |
|
19 |
SpringApplication.run(Application.class, args); |
|
20 |
|
|
21 |
} |
|
22 |
|
|
23 |
/* Read more |
|
24 |
:http://mrbool.com/rest-server-with-spring-data-spring-boot-and-postgresql/34023#ixzz40oAA4qI3*/ |
|
25 |
|
|
26 |
} |
|
27 |
|
|
28 |
|
|
29 |
|
modules/dnet-openaire-usage-stats-api/src/main/java/eu/dnetlib/usagestats/controllers/UsageStatsController.java | ||
---|---|---|
1 |
package eu.dnetlib.usagestats.controllers; |
|
2 |
|
|
3 |
import eu.dnetlib.usagestats.domain.UsageStats; |
|
4 |
import eu.dnetlib.usagestats.repos.DatasourceRepo; |
|
5 |
import eu.dnetlib.usagestats.repos.OrganizationRepo; |
|
6 |
import eu.dnetlib.usagestats.repos.ProjectRepo; |
|
7 |
import eu.dnetlib.usagestats.repos.ResultRepo; |
|
8 |
import org.springframework.beans.factory.annotation.Autowired; |
|
9 |
import org.springframework.transaction.annotation.Transactional; |
|
10 |
import org.springframework.web.bind.annotation.PathVariable; |
|
11 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
12 |
import org.springframework.web.bind.annotation.RequestParam; |
|
13 |
import org.springframework.web.bind.annotation.RestController; |
|
14 |
|
|
15 |
import java.util.List; |
|
16 |
|
|
17 |
/** |
|
18 |
* Created by eri_k on 3/27/2016. |
|
19 |
*/ |
|
20 |
@RestController |
|
21 |
@Transactional |
|
22 |
public class UsageStatsController { |
|
23 |
@Autowired |
|
24 |
DatasourceRepo datasourceRepo; |
|
25 |
|
|
26 |
@Autowired |
|
27 |
ResultRepo resultRepo; |
|
28 |
|
|
29 |
@Autowired |
|
30 |
OrganizationRepo organizationRepo; |
|
31 |
|
|
32 |
@Autowired |
|
33 |
ProjectRepo projectRepo; |
|
34 |
|
|
35 |
|
|
36 |
//-----------------FOR DATASOURCES-------------------------- |
|
37 |
/*@RequestMapping("/home/{error}")*/ |
|
38 |
@RequestMapping(value = "/stats/datasources/{datasourceId}/clicks") |
|
39 |
public UsageStats getDatasourceClicks(@PathVariable(value = "datasourceId") String datasourceId) throws Exception { |
|
40 |
|
|
41 |
return datasourceRepo.getClicks(datasourceId); |
|
42 |
} |
|
43 |
|
|
44 |
|
|
45 |
@RequestMapping(value = "/stats/datasources/popular") |
|
46 |
public List<UsageStats> getDatasourceByPopularity() throws Exception { |
|
47 |
return datasourceRepo.getMostPopular(); |
|
48 |
} |
|
49 |
|
|
50 |
|
|
51 |
@RequestMapping(value = "/stats/datasources/{datasourceId}/pubs") |
|
52 |
public List<UsageStats> getDatasourcePubsByPopularity(@PathVariable(value = "datasourceId") String datasourceId) throws Exception { |
|
53 |
return datasourceRepo.getMostPopularPubs(datasourceId); |
|
54 |
} |
|
55 |
|
|
56 |
@RequestMapping(value = "/stats/datasources/{datasourceId}/projects") |
|
57 |
public List<UsageStats> getDatasourceProjectsByPopularity(@PathVariable(value = "datasourceId") String datasourceId) throws Exception { |
|
58 |
return datasourceRepo.getMostPopularProjects(datasourceId); |
|
59 |
} |
|
60 |
|
|
61 |
@RequestMapping(value = "/stats/datasources/{datasourceId}/popularity") |
|
62 |
public List<UsageStats> getDatasourcePopularity(@PathVariable(value = "datasourceId") String datasourceId) throws Exception { |
|
63 |
return datasourceRepo.getPopularityOverTime(datasourceId); |
|
64 |
} |
|
65 |
|
|
66 |
|
|
67 |
//-----------------FOR PROJECTS-------------------------- |
|
68 |
|
|
69 |
@RequestMapping(value = "/stats/projects/{projectId}/clicks") |
|
70 |
public UsageStats getProjectClicks(@PathVariable(value = "projectId") String projectId) throws Exception { |
|
71 |
|
|
72 |
return projectRepo.getClicks(projectId); |
|
73 |
} |
|
74 |
|
|
75 |
|
|
76 |
@RequestMapping(value = "/stats/projects/popular") |
|
77 |
public List<UsageStats> getProjectByPopularity() throws Exception { |
|
78 |
return projectRepo.getMostPopular(); |
|
79 |
} |
|
80 |
|
|
81 |
|
|
82 |
@RequestMapping(value = "/stats/projects/{projectId}/pubs") |
|
83 |
public List<UsageStats> getProjectPubsByPopularity(@PathVariable(value = "projectId") String projectId) throws Exception { |
|
84 |
return projectRepo.getMostPopularPubs(projectId); |
|
85 |
} |
|
86 |
|
|
87 |
|
|
88 |
@RequestMapping(value = "/stats/projects/{projectId}/popularity") |
|
89 |
public List<UsageStats> getProjectPopularity(@PathVariable(value = "projectId") String projectId) throws Exception { |
|
90 |
return projectRepo.getPopularityOverTime(projectId); |
|
91 |
} |
|
92 |
|
|
93 |
|
|
94 |
//FOR ORGANIZATIONS |
|
95 |
|
|
96 |
|
|
97 |
@RequestMapping(value = "/stats/organizations/{organizationId}/clicks") |
|
98 |
public UsageStats getOrganizationClicks(@PathVariable(value = "organizationId") String organizationId) throws Exception { |
|
99 |
|
|
100 |
return organizationRepo.getClicks(organizationId); |
|
101 |
} |
|
102 |
|
|
103 |
|
|
104 |
@RequestMapping(value = "/stats/organizations/popular") |
|
105 |
public List<UsageStats> getOrganizationByPopularity() throws Exception { |
|
106 |
return organizationRepo.getMostPopular(); |
|
107 |
} |
|
108 |
|
|
109 |
|
|
110 |
@RequestMapping(value = "/stats/organizations/{organizationId}/projects") |
|
111 |
public List<UsageStats> getOrganizationProjectsByPopularity(@PathVariable(value = "organizationId") String organizationId) throws Exception { |
|
112 |
return organizationRepo.getMostPopularProjects(organizationId); |
|
113 |
} |
|
114 |
|
|
115 |
//TOOD url not working??? |
|
116 |
|
|
117 |
@RequestMapping(value = "/stats/organizations/{organizationId}/popularity") |
|
118 |
public List<UsageStats> getOrganizationPopularity(@PathVariable(value = "organizationId") String organizationId) throws Exception { |
|
119 |
return projectRepo.getPopularityOverTime(organizationId); |
|
120 |
} |
|
121 |
|
|
122 |
|
|
123 |
// FOR RESULTS |
|
124 |
|
|
125 |
@RequestMapping(value = "/stats/results/{resultId}/clicks") |
|
126 |
public UsageStats getResultClicks(@PathVariable(value = "resultId") String resultId) throws Exception { |
|
127 |
|
|
128 |
return resultRepo.getClicks(resultId); |
|
129 |
} |
|
130 |
|
|
131 |
|
|
132 |
@RequestMapping(value = "/stats/results/popular") |
|
133 |
public List<UsageStats> getResultByPopularity() throws Exception { |
|
134 |
return resultRepo.getMostPopular(); |
|
135 |
} |
|
136 |
|
|
137 |
|
|
138 |
@RequestMapping(value = "/stats/results/{resultId}/popularity") |
|
139 |
public List<UsageStats> getResultPopularity(@PathVariable(value = "resultId") String resultId) throws Exception { |
|
140 |
return resultRepo.getPopularityOverTime(resultId); |
|
141 |
} |
|
142 |
|
|
143 |
|
|
144 |
} |
modules/dnet-openaire-usage-stats-api/src/main/java/eu/dnetlib/usagestats/domain/UsageStats.java | ||
---|---|---|
1 |
/** |
|
2 |
* Created by eri_k on 2/20/2016. |
|
3 |
*/ |
|
4 |
|
|
5 |
package eu.dnetlib.usagestats.domain; |
|
6 |
|
|
7 |
import javax.persistence.Entity; |
|
8 |
import javax.persistence.GeneratedValue; |
|
9 |
import javax.persistence.GenerationType; |
|
10 |
import javax.persistence.Id; |
|
11 |
import java.io.Serializable; |
|
12 |
import java.util.UUID; |
|
13 |
|
|
14 |
|
|
15 |
//@Entity(name = "usageStats") |
|
16 |
public class UsageStats implements Serializable { |
|
17 |
|
|
18 |
public UsageStats() { |
|
19 |
} |
|
20 |
|
|
21 |
/* @Id |
|
22 |
@GeneratedValue(strategy = GenerationType.IDENTITY)*/ |
|
23 |
private String id; |
|
24 |
private String name; |
|
25 |
private int value; |
|
26 |
|
|
27 |
public String getId() { |
|
28 |
return id; |
|
29 |
} |
|
30 |
|
|
31 |
public void setId(String id) { |
|
32 |
this.id = id; |
|
33 |
} |
|
34 |
|
|
35 |
public String getName() { |
|
36 |
return name; |
|
37 |
} |
|
38 |
|
|
39 |
public void setName(String name) { |
|
40 |
this.name = name; |
|
41 |
} |
|
42 |
|
|
43 |
public int getValue() { |
|
44 |
return value; |
|
45 |
} |
|
46 |
|
|
47 |
public void setValue(int value) { |
|
48 |
this.value = value; |
|
49 |
} |
|
50 |
} |
modules/dnet-openaire-usage-stats-api/src/main/java/eu/dnetlib/usagestats/repos/DatasourceRepo.java | ||
---|---|---|
1 |
/** |
|
2 |
* Created by eri_k on 2/20/2016. |
|
3 |
*/ |
|
4 |
package eu.dnetlib.usagestats.repos; |
|
5 |
|
|
6 |
|
|
7 |
import eu.dnetlib.usagestats.domain.UsageStats; |
|
8 |
import org.apache.log4j.Logger; |
|
9 |
import org.springframework.data.rest.core.annotation.RepositoryRestResource; |
|
10 |
import org.springframework.stereotype.Repository; |
|
11 |
|
|
12 |
import java.util.ArrayList; |
|
13 |
import java.util.List; |
|
14 |
|
|
15 |
|
|
16 |
@RepositoryRestResource(collectionResourceRel = "datasourceStats", path = "datasourceStats") |
|
17 |
@Repository |
|
18 |
public class DatasourceRepo extends BaseRepository { |
|
19 |
|
|
20 |
private Logger log = Logger.getLogger(this.getClass()); |
|
21 |
|
|
22 |
|
|
23 |
public UsageStats getClicks(String id) { |
|
24 |
|
|
25 |
String query = "select d.id, d.name , sum (ds.numberofviews) from datasource d, usagestats.datasourcesstats ds " + |
|
26 |
"where ds.datasourceid=d.id and d.id=? group by d.id, d.name;"; |
|
27 |
|
|
28 |
List<String> values = new ArrayList<String>(); |
|
29 |
values.add(id); |
|
30 |
|
|
31 |
List<UsageStats> results = executePreparedQuery(query, values); |
|
32 |
if (!results.isEmpty()) return results.get(0); |
|
33 |
|
|
34 |
return new UsageStats(); |
|
35 |
|
|
36 |
} |
|
37 |
|
|
38 |
|
|
39 |
public List<UsageStats> getMostPopular() { |
|
40 |
String query = "select d.id, d.name , sum (ds.numberofviews) from datasource d, usagestats.datasourcesstats ds where " + |
|
41 |
"ds.datasourceid=d.id group by d.id, d.name order by sum (ds.numberofviews) desc limit 10;"; |
|
42 |
|
|
43 |
return executeQuery(query); |
|
44 |
} |
|
45 |
|
|
46 |
public List<UsageStats> getMostPopularPubs(String datasourceId) { |
|
47 |
|
|
48 |
String query = "select r.id, r.title , sum(rs.numberofviews) from result r, usagestats.resultsstats rs, result_datasources rd where r.id=rd.id and rd.datasource=? and rs.resultid=r.id group by r.id, r. title order by sum(rs.numberofviews) desc limit 10;"; |
|
49 |
|
|
50 |
|
|
51 |
List<String> values = new ArrayList<String>(); |
|
52 |
values.add(datasourceId); |
|
53 |
return executePreparedQuery(query, values); |
|
54 |
|
|
55 |
} |
|
56 |
|
|
57 |
|
|
58 |
public List<UsageStats> getMostPopularProjects(String datasourceId) { |
|
59 |
|
|
60 |
String query = "select p.id, p.title, sum(ps.numberofviews) from project p, result_datasources rd, project_results pr , usagestats.projectsstats ps " + |
|
61 |
" where rd.datasource=? and p.id=pr.id and pr.result=rd.id and ps.projectid=p.id " + |
|
62 |
" group by p.id, p.title order by sum (ps.numberofviews) desc limit 10;"; |
|
63 |
List<String> values = new ArrayList<String>(); |
|
64 |
values.add(datasourceId); |
|
65 |
return executePreparedQuery(query, values); |
|
66 |
|
|
67 |
} |
|
68 |
|
|
69 |
public List<UsageStats> getPopularityOverTime(String datasourceId) { |
|
70 |
|
|
71 |
String query = "select ds.datasourceid, ds.timestamp_month , ds.numberofviews from usagestats.datasourcesstats ds " + |
|
72 |
" where ds.datasourceid=? order by ds.timestamp_month desc ;"; |
|
73 |
List<String> values = new ArrayList<String>(); |
|
74 |
values.add(datasourceId); |
|
75 |
return executePreparedQuery(query, values); |
|
76 |
|
|
77 |
} |
|
78 |
|
|
79 |
|
|
80 |
} |
|
81 |
|
|
82 |
|
modules/dnet-openaire-usage-stats-api/src/main/java/eu/dnetlib/usagestats/repos/ResultRepo.java | ||
---|---|---|
1 |
/** |
|
2 |
* Created by eri_k on 2/20/2016. |
|
3 |
*/ |
|
4 |
package eu.dnetlib.usagestats.repos; |
|
5 |
|
|
6 |
|
|
7 |
import eu.dnetlib.usagestats.domain.UsageStats; |
|
8 |
import org.apache.log4j.Logger; |
|
9 |
import org.springframework.beans.factory.annotation.Autowired; |
|
10 |
import org.springframework.data.rest.core.annotation.RepositoryRestResource; |
|
11 |
import org.springframework.stereotype.Repository; |
|
12 |
|
|
13 |
import javax.sql.DataSource; |
|
14 |
import java.sql.*; |
|
15 |
import java.util.ArrayList; |
|
16 |
import java.util.List; |
|
17 |
|
|
18 |
@RepositoryRestResource(collectionResourceRel = "resultStats", path = "resultStats") |
|
19 |
@Repository |
|
20 |
public class ResultRepo extends BaseRepository { |
|
21 |
private Logger log = Logger.getLogger(this.getClass()); |
|
22 |
|
|
23 |
public UsageStats getClicks(String id) { |
|
24 |
|
|
25 |
String query = "select r.id, r.title , sum (rs.numberofviews) from result r, usagestats.resultsstats rs " + |
|
26 |
"where rs.resultid=r.id and r.id=? group by r.id, r. title;"; |
|
27 |
|
|
28 |
List<String> values = new ArrayList<String>(); |
|
29 |
values.add(id); |
|
30 |
|
|
31 |
List<UsageStats> results = executePreparedQuery(query, values); |
|
32 |
|
|
33 |
if (!results.isEmpty()) return results.get(0); |
|
34 |
return new UsageStats(); |
|
35 |
|
|
36 |
} |
|
37 |
|
|
38 |
public List<UsageStats> getMostPopular() { |
|
39 |
|
|
40 |
String query = "select r.id, r.title , sum (rs.numberofviews) from result r, usagestats.resultsstats rs where " + |
|
41 |
"rs.resultid=r.id group by r.id, r. title order by sum (rs.numberofviews) desc limit 10;"; |
|
42 |
|
|
43 |
return executeQuery(query); |
|
44 |
|
|
45 |
} |
|
46 |
|
|
47 |
/* public List<UsageStats> getMostPopularPubs(String projectId) { |
|
48 |
|
|
49 |
String query = "select r.id, r.title , sum (rs.numberofviews) from result r, usagestats.resultsstats rs, project_results pr " + |
|
50 |
"where pr.id=? and r.id=pr.result and rs.resultid=r.id group by r.id, r. title order by sum (rs.numberofviews) desc limit 10;"; |
|
51 |
|
|
52 |
List<String> values = new ArrayList<String>(); |
|
53 |
values.add(projectId); |
|
54 |
return executePreparedQuery(query, values); |
|
55 |
|
|
56 |
} |
|
57 |
*/ |
|
58 |
|
|
59 |
public List<UsageStats> getPopularityOverTime(String resultId) { |
|
60 |
|
|
61 |
String query = "select rs.resultid, rs.timestamp_month , rs.numberofviews from usagestats.resultsstats rs " + |
|
62 |
" where rs.resultid=? order by rs.timestamp_month desc ;"; |
|
63 |
List<String> values = new ArrayList<String>(); |
|
64 |
values.add(resultId); |
|
65 |
return executePreparedQuery(query, values); |
|
66 |
|
|
67 |
} |
|
68 |
|
|
69 |
} |
|
70 |
|
|
71 |
|
modules/dnet-openaire-usage-stats-api/src/main/java/eu/dnetlib/usagestats/repos/BaseRepository.java | ||
---|---|---|
1 |
package eu.dnetlib.usagestats.repos; |
|
2 |
|
|
3 |
import eu.dnetlib.usagestats.domain.UsageStats; |
|
4 |
import org.apache.log4j.Logger; |
|
5 |
import org.springframework.beans.factory.annotation.Autowired; |
|
6 |
|
|
7 |
import javax.sql.DataSource; |
|
8 |
import java.sql.Connection; |
|
9 |
import java.sql.PreparedStatement; |
|
10 |
import java.sql.ResultSet; |
|
11 |
import java.sql.Statement; |
|
12 |
import java.util.ArrayList; |
|
13 |
import java.util.List; |
|
14 |
|
|
15 |
/** |
|
16 |
* Created by eri_k on 3/28/2016. |
|
17 |
*/ |
|
18 |
public class BaseRepository { |
|
19 |
@Autowired |
|
20 |
DataSource dataSource; |
|
21 |
|
|
22 |
private Connection connection; |
|
23 |
private Logger log = Logger.getLogger(this.getClass()); |
|
24 |
|
|
25 |
|
|
26 |
protected List<UsageStats> executeQuery(String query) { |
|
27 |
List<UsageStats> statsList = new ArrayList<UsageStats>(); |
|
28 |
|
|
29 |
try { |
|
30 |
Connection connection = dataSource.getConnection(); |
|
31 |
Statement st = connection.createStatement(); |
|
32 |
st.execute(query); |
|
33 |
ResultSet rs = st.getResultSet(); |
|
34 |
|
|
35 |
while (rs.next()) { |
|
36 |
UsageStats stats = new UsageStats(); |
|
37 |
stats.setId(rs.getString(1)); |
|
38 |
stats.setName(rs.getString(2)); |
|
39 |
stats.setValue(rs.getInt(3)); |
|
40 |
statsList.add(stats); |
|
41 |
} |
|
42 |
|
|
43 |
rs.close(); |
|
44 |
st.close(); |
|
45 |
connection.close(); |
|
46 |
} catch ( |
|
47 |
Exception e |
|
48 |
) |
|
49 |
|
|
50 |
{ |
|
51 |
log.error("Cannot execute query : " + e); |
|
52 |
} |
|
53 |
|
|
54 |
return statsList; |
|
55 |
} |
|
56 |
|
|
57 |
|
|
58 |
protected List<UsageStats> executePreparedQuery(String query, List<String> values) { |
|
59 |
List<UsageStats> statsList = new ArrayList<UsageStats>(); |
|
60 |
|
|
61 |
try { |
|
62 |
Connection connection = dataSource.getConnection(); |
|
63 |
PreparedStatement st = connection.prepareStatement(query); |
|
64 |
int i = 1; |
|
65 |
|
|
66 |
for (String s : values) { |
|
67 |
st.setString(i, s); |
|
68 |
i++; |
|
69 |
} |
|
70 |
|
|
71 |
|
|
72 |
ResultSet rs = st.executeQuery(); |
|
73 |
|
|
74 |
while (rs.next()) { |
|
75 |
UsageStats stats = new UsageStats(); |
|
76 |
stats.setId(rs.getString(1)); |
|
77 |
stats.setName(rs.getString(2)); |
|
78 |
stats.setValue(rs.getInt(3)); |
|
79 |
statsList.add(stats); |
|
80 |
} |
|
81 |
|
|
82 |
rs.close(); |
|
83 |
st.close(); |
|
84 |
connection.close(); |
|
85 |
} catch ( |
|
86 |
Exception e |
|
87 |
) |
|
88 |
|
|
89 |
{ |
|
90 |
log.error("Cannot execute query : " + e); |
|
91 |
} |
|
92 |
|
|
93 |
return statsList; |
|
94 |
} |
|
95 |
} |
modules/dnet-openaire-usage-stats-api/src/main/java/eu/dnetlib/usagestats/repos/OrganizationRepo.java | ||
---|---|---|
1 |
/** |
|
2 |
* Created by eri_k on 2/20/2016. |
|
3 |
*/ |
|
4 |
package eu.dnetlib.usagestats.repos; |
|
5 |
|
|
6 |
|
|
7 |
import eu.dnetlib.usagestats.domain.UsageStats; |
|
8 |
import org.apache.log4j.Logger; |
|
9 |
import org.springframework.data.rest.core.annotation.RepositoryRestResource; |
|
10 |
import org.springframework.stereotype.Repository; |
|
11 |
|
|
12 |
import java.util.ArrayList; |
|
13 |
import java.util.List; |
|
14 |
|
|
15 |
|
|
16 |
@Repository |
|
17 |
public class OrganizationRepo extends BaseRepository { |
|
18 |
|
|
19 |
private Logger log = Logger.getLogger(this.getClass()); |
|
20 |
|
|
21 |
public UsageStats getClicks(String organizationId) { |
|
22 |
|
|
23 |
String query = "select o.id, o.name , sum (os.numberofviews) from organization o, usagestats.organizationsstats os " + |
|
24 |
"where os.organizationid=o.id and o.id=? group by o.id, o.name;"; |
|
25 |
|
|
26 |
List<String> values = new ArrayList<String>(); |
|
27 |
values.add(organizationId); |
|
28 |
|
|
29 |
List<UsageStats> results = executePreparedQuery(query, values); |
|
30 |
if (!results.isEmpty()) return results.get(0); |
|
31 |
|
|
32 |
return new UsageStats(); |
|
33 |
} |
|
34 |
|
|
35 |
|
|
36 |
public List<UsageStats> getMostPopular() { |
|
37 |
String query = "select o.id, o.name , sum (os.numberofviews) from organization o, usagestats.organizationsstats os where " + |
|
38 |
"os.organizationid=o.id group by o.id, o.name order by sum(os.numberofviews) desc limit 10;"; |
|
39 |
|
|
40 |
return executeQuery(query); |
|
41 |
} |
|
42 |
|
|
43 |
|
|
44 |
public List<UsageStats> getMostPopularProjects(String organizationId) { |
|
45 |
|
|
46 |
String query = "select p.id, p.title, sum(ps.numberofviews) from project p, project_organizations por, usagestats.projectsstats ps " + |
|
47 |
" where por.organization=? and p.id=por.id and ps.projectid=p.id " + |
|
48 |
" group by p.id, p.title order by sum (ps.numberofviews) desc limit 10;"; |
|
49 |
List<String> values = new ArrayList<String>(); |
|
50 |
|
|
51 |
values.add(organizationId); |
|
52 |
return executePreparedQuery(query, values); |
|
53 |
} |
|
54 |
|
|
55 |
public List<UsageStats> getPopularityOverTime(String organizationId) { |
|
56 |
|
|
57 |
String query = "select os.organizationid, os.timestamp_month , os.numberofviews from usagestats.organizationsstats os " + |
|
58 |
" where os.organizationid=? order by os.timestamp_month desc ;"; |
|
59 |
List<String> values = new ArrayList<String>(); |
|
60 |
values.add(organizationId); |
|
61 |
return executePreparedQuery(query, values); |
|
62 |
|
|
63 |
} |
|
64 |
|
|
65 |
|
|
66 |
} |
|
67 |
|
|
68 |
|
modules/dnet-openaire-usage-stats-api/src/main/java/eu/dnetlib/usagestats/repos/ProjectRepo.java | ||
---|---|---|
1 |
/** |
|
2 |
* Created by eri_k on 2/20/2016. |
|
3 |
*/ |
|
4 |
package eu.dnetlib.usagestats.repos; |
|
5 |
|
|
6 |
|
|
7 |
import eu.dnetlib.usagestats.domain.UsageStats; |
|
8 |
import org.apache.log4j.Logger; |
|
9 |
import org.springframework.data.rest.core.annotation.RepositoryRestResource; |
|
10 |
import org.springframework.stereotype.Repository; |
|
11 |
|
|
12 |
import java.util.ArrayList; |
|
13 |
import java.util.List; |
|
14 |
|
|
15 |
|
|
16 |
@RepositoryRestResource(collectionResourceRel = "projectStats", path = "projectStats") |
|
17 |
@Repository |
|
18 |
public class ProjectRepo extends BaseRepository { |
|
19 |
|
|
20 |
private Logger log = Logger.getLogger(this.getClass()); |
|
21 |
|
|
22 |
|
|
23 |
public UsageStats getClicks(String projectId) { |
|
24 |
|
|
25 |
String query = "select p.id, p.title, sum (ps.numberofviews) from project p, usagestats.projectsstats ps " + |
|
26 |
"where ps.projectid=p.id and p.id=? group by p.id, p.title;"; |
|
27 |
|
|
28 |
List<String> values = new ArrayList<String>(); |
|
29 |
values.add(projectId); |
|
30 |
|
|
31 |
List<UsageStats> results = executePreparedQuery(query, values); |
|
32 |
if (!results.isEmpty()) return results.get(0); |
|
33 |
|
|
34 |
return new UsageStats(); |
|
35 |
|
|
36 |
} |
|
37 |
|
|
38 |
|
|
39 |
public List<UsageStats> getMostPopular() { |
|
40 |
String query = "select p.id, p.title , sum (ps.numberofviews) from project p, usagestats.projectsstats ps where " + |
|
41 |
"ps.projectid=p.id group by p.id, p.title order by sum(ps.numberofviews) desc limit 10;"; |
|
42 |
|
|
43 |
return executeQuery(query); |
|
44 |
} |
|
45 |
|
|
46 |
|
|
47 |
public List<UsageStats> getMostPopularPubs(String projectId) { |
|
48 |
|
|
49 |
String query = "select r.id, r.title , sum(rs.numberofviews) from result r, usagestats.resultsstats rs, " + |
|
50 |
"project_results pr where r.id=pr.result and pr.id=? and rs.resultid=r.id group by r.id, r. title order by sum(rs.numberofviews) desc limit 10;"; |
|
51 |
|
|
52 |
|
|
53 |
List<String> values = new ArrayList<String>(); |
|
54 |
values.add(projectId); |
|
55 |
return executePreparedQuery(query, values); |
|
56 |
|
|
57 |
} |
|
58 |
|
|
59 |
|
|
60 |
public List<UsageStats> getPopularityOverTime(String projectId) { |
|
61 |
|
|
62 |
String query = "select ps.projectid, ps.timestamp_month , ps.numberofviews from usagestats.projectsstats ps " + |
|
63 |
" where ps.projectid=? order by ps.timestamp_month desc ;"; |
|
64 |
List<String> values = new ArrayList<String>(); |
|
65 |
values.add(projectId); |
|
66 |
return executePreparedQuery(query, values); |
|
67 |
|
|
68 |
} |
|
69 |
|
|
70 |
|
|
71 |
} |
|
72 |
|
|
73 |
|
modules/dnet-openaire-usage-stats-api/src/main/resources/application.properties | ||
---|---|---|
1 |
#spring.jpa.database=POSTGRESQL |
|
2 |
#spring.datasource.platform=postgres |
|
3 |
#spring.jpa.show-sql=true |
|
4 |
#spring.jpa.hibernate.ddl-auto=validate |
|
5 |
spring.database.driverClassName=org.postgresql.Driver |
|
6 |
spring.datasource.url=jdbc:postgresql://194.177.192.118:5432/stats |
|
7 |
spring.datasource.username=postgres |
|
8 |
spring.datasource.password=vereniki |
|
9 |
server.port=8080 |
modules/dnet-openaire-usage-stats-api/pom.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
<project xmlns="http://maven.apache.org/POM/4.0.0" |
|
3 |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
|
4 |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|
5 |
<modelVersion>4.0.0</modelVersion> |
|
6 |
<groupId>eu.dnetlib</groupId> |
|
7 |
<artifactId>dnet-openaire-usage-stats-api</artifactId> |
|
8 |
<version>0.0.1-SNAPSHOT</version> |
|
9 |
|
|
10 |
<parent> |
|
11 |
<groupId>org.springframework.boot</groupId> |
|
12 |
<artifactId>spring-boot-starter-parent</artifactId> |
|
13 |
<version>1.3.2.RELEASE</version> |
|
14 |
</parent> |
|
15 |
|
|
16 |
<dependencies> |
|
17 |
<dependency> |
|
18 |
<groupId>org.springframework.boot</groupId> |
|
19 |
<artifactId>spring-boot-starter-test</artifactId> |
|
20 |
<scope>test</scope> |
|
21 |
</dependency> |
|
22 |
|
|
23 |
<dependency> |
|
24 |
<groupId>org.springframework.boot</groupId> |
|
25 |
<artifactId>spring-boot-starter-data-rest</artifactId> |
|
26 |
</dependency> |
|
27 |
<dependency> |
|
28 |
<groupId>org.springframework.boot</groupId> |
|
29 |
<artifactId>spring-boot-starter-data-jpa</artifactId> |
|
30 |
</dependency> |
|
31 |
|
|
32 |
<dependency> |
|
33 |
<groupId>postgresql</groupId> |
|
34 |
<artifactId>postgresql</artifactId> |
|
35 |
<version>9.1-901-1.jdbc4</version> |
|
36 |
<scope>runtime</scope> |
|
37 |
</dependency> |
|
38 |
|
|
39 |
<dependency> |
|
40 |
<groupId>junit</groupId> |
|
41 |
<artifactId>junit</artifactId> |
|
42 |
<version>3.8.1</version> |
|
43 |
<scope>test</scope> |
|
44 |
</dependency> |
|
45 |
|
|
46 |
<dependency> |
|
47 |
<groupId>com.googlecode.json-simple</groupId> |
|
48 |
<artifactId>json-simple</artifactId> |
|
49 |
<version>1.1.1</version> |
|
50 |
</dependency> |
|
51 |
|
|
52 |
</dependencies> |
|
53 |
|
|
54 |
<properties> |
|
55 |
<java.version>1.7</java.version> |
|
56 |
</properties> |
|
57 |
|
|
58 |
|
|
59 |
<build> |
|
60 |
<plugins> |
|
61 |
<plugin> |
|
62 |
<groupId>org.springframework.boot</groupId> |
|
63 |
<artifactId>spring-boot-maven-plugin</artifactId> |
|
64 |
</plugin> |
|
65 |
</plugins> |
|
66 |
</build> |
|
67 |
|
|
68 |
<repositories> |
|
69 |
<repository> |
|
70 |
<id>spring-releases</id> |
|
71 |
<url>https://repo.spring.io/libs-release</url> |
|
72 |
</repository> |
|
73 |
</repositories> |
|
74 |
<pluginRepositories> |
|
75 |
<pluginRepository> |
|
76 |
<id>spring-releases</id> |
|
77 |
<url>https://repo.spring.io/libs-release</url> |
|
78 |
</pluginRepository> |
|
79 |
</pluginRepositories> |
|
80 |
</project> |
Also available in: Unified diff
first commit