-
Notifications
You must be signed in to change notification settings - Fork 0
/
ballerina_service.bal
134 lines (118 loc) · 6.18 KB
/
ballerina_service.bal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (c) 2018 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
//
// WSO2 Inc. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
import ballerina.net.http;
import ballerina.data.sql;
import ballerina.log;
import ballerina.config;
import org.WSO2.Ballerina;
string username = config:getGlobalValue("USERNAME");
string password = config:getGlobalValue("PASSWORD");
string hostOrPath = config:getGlobalValue("HOST");
string databaseName = config:getGlobalValue("DATABASE_NAME");
int port = Ballerina:getPortNumber();
sql:ClientConnector clientConnector = create sql:ClientConnector(
sql:DB.MYSQL,
hostOrPath,
port,
databaseName+"?useSSL=false",
username,
password,
{maximumPoolSize:5});
service<http> ballerinaService {
@http:resourceConfig {methods:["GET"]}
resource pullRequests(http:Connection httpConnection, http:InRequest inRequest) {
http:OutResponse outResponse = {};
// Query to return pull requests sent by outsiders , repositories ,
// respective product , url of pull request and open duration by
// comparing list whole open pull requests with list of users from WSO2
// and product, repositories mapping.
json jsonPayload = databaseConnector("SELECT RepositoryName,Url,Days,Weeks,githubId,product,State FROM
pullRequests LEFT OUTER JOIN WSO2contributors ON pullRequests.GithubId=WSO2contributors.userId LEFT OUTER JOIN
product ON pullRequests.RepositoryName=product.RepoName WHERE WSO2contributors.userId is null");
int iterator;
while(iterator < lengthof jsonPayload){
if(jsonPayload[iterator].product == null){
jsonPayload[iterator].product = "unknown";
}
iterator = iterator + 1;
}
outResponse.addHeader("Access-Control-Allow-Origin","*");
outResponse.setJsonPayload(jsonPayload);
_ = httpConnection.respond(outResponse);
}
resource summaryOfPullRequests(http:Connection httpConnection,http:InRequest inRequest){
http:OutResponse outResponse = {};
// Query to return the total number of pull requests sent by outsiders
// for each product where repositories which do not have jenkins build
// also accumulated under 'unknown'.
json jsonPayload = databaseConnector("SELECT productName, SUM(totalNum) as Total FROM
(SELECT IFNULL(product.Product,\"unknown\") as productName, COUNT(*) as totalNum FROM
pullRequests LEFT OUTER JOIN WSO2contributors ON pullRequests.GithubId=WSO2contributors.userId
LEFT OUTER JOIN product ON pullRequests.RepositoryName=product.RepoName WHERE WSO2contributors.userId
is null GROUP BY product.Product) AS T GROUP BY productName ");
outResponse.addHeader("Access-Control-Allow-Origin","*");
outResponse.setJsonPayload(jsonPayload);
_ = httpConnection.respond(outResponse);
}
resource issues(http:Connection httpConnection, http:InRequest inRequest){
http:OutResponse outResponse = {};
// Query to return issues sent by outsiders , repositories ,
// respective product , url of issue and open duration by
// comparing list of whole open issues with list of users from WSO2
// and product, repositories mapping.
json jsonPayload = databaseConnector("SELECT RepositoryName,Url,Days,Weeks,githubId,product FROM issues
LEFT OUTER JOIN WSO2contributors ON issues.GithubId=WSO2contributors.userId LEFT OUTER JOIN product ON
issues.RepositoryName=product.RepoName WHERE WSO2contributors.userId is null");
int iterator;
while(iterator < lengthof jsonPayload){
if(jsonPayload[iterator].product == null){
jsonPayload[iterator].product = "unknown";
}
iterator = iterator + 1;
}
outResponse.addHeader("Access-Control-Allow-Origin","*");
outResponse.setJsonPayload(jsonPayload);
_ = httpConnection.respond(outResponse);
}
resource summaryOfIssues(http:Connection httpConnection, http:InRequest inRequest){
http:OutResponse outResponse = {};
// Query to return the total number of issues sent by outsiders
// for each product where repositories which do not have jenkins build
// also accumulated under 'unknown'.
json jsonPayload = databaseConnector("SELECT productName, SUM(totalNum) as Total FROM (SELECT IFNULL
(product.Product,\"unknown\") as productName, COUNT(*) as totalNum FROM issues LEFT OUTER JOIN
WSO2contributors ON issues.GithubId=WSO2contributors.userId LEFT OUTER JOIN product ON
issues.RepositoryName=product.RepoName WHERE WSO2contributors.userId is null GROUP BY
product.Product) AS T GROUP BY productName");
outResponse.addHeader("Access-Control-Allow-Origin","*");
outResponse.setJsonPayload(jsonPayload);
_ = httpConnection.respond(outResponse);
}
}
@Description { value:"establish connection with database and fetch data"}
@Param { value:"stringPayload: mySQL query as string"}
function databaseConnector(string stringPayload)(json jsonPayload){
endpoint<sql:ClientConnector> testDB {
clientConnector;
}
table data = testDB.select(stringPayload,null,null);
error typeCastError;
jsonPayload, typeCastError = <json>data;
if(typeCastError != null){
log:printInfo("Error in casting : " + typeCastError.message);
}
return;
}