-
Notifications
You must be signed in to change notification settings - Fork 627
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12547 from ashanhr/improvement-3017
Add transaction counting capability
- Loading branch information
Showing
27 changed files
with
1,128 additions
and
6 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
...carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/TransactionCountDAO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. | ||
* | ||
* WSO2 LLC. 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. | ||
*/ | ||
|
||
package org.wso2.carbon.apimgt.impl.dao; | ||
|
||
import org.wso2.carbon.apimgt.api.APIManagementException; | ||
import org.wso2.carbon.apimgt.api.ExceptionCodes; | ||
import org.wso2.carbon.apimgt.impl.dao.constants.SQLConstants; | ||
import org.wso2.carbon.apimgt.impl.dto.TransactionCountDTO; | ||
import org.wso2.carbon.apimgt.impl.utils.APIMgtDBUtil; | ||
|
||
import java.sql.*; | ||
|
||
/** | ||
* This class represent the TransactionCountDAO. | ||
*/ | ||
public class TransactionCountDAO { | ||
|
||
private static final TransactionCountDAO transactionCountDAO = new TransactionCountDAO(); | ||
|
||
private TransactionCountDAO() { | ||
} | ||
|
||
public static TransactionCountDAO getInstance() { | ||
return transactionCountDAO; | ||
} | ||
|
||
public boolean insertTransactionRecords(TransactionCountDTO[] dto) throws APIManagementException { | ||
try (Connection connection = APIMgtDBUtil.getConnection()) { | ||
connection.setAutoCommit(false); | ||
|
||
try (PreparedStatement insertTransactionRecordsStatement = connection.prepareStatement( | ||
SQLConstants.TransactionCountConstants.INSERT_TRANSACTION_COUNT)) { | ||
for (TransactionCountDTO record : dto) { | ||
insertTransactionRecordsStatement.setString(1, record.getId()); | ||
insertTransactionRecordsStatement.setString(2, record.getHost()); | ||
insertTransactionRecordsStatement.setString(3, record.getServerID()); | ||
insertTransactionRecordsStatement.setString(4, record.getServerType()); | ||
insertTransactionRecordsStatement.setInt(5, record.getCount()); | ||
|
||
Timestamp recordedTimestamp = Timestamp.valueOf(record.getRecordedTime()); | ||
insertTransactionRecordsStatement.setTimestamp(6, recordedTimestamp); | ||
|
||
insertTransactionRecordsStatement.addBatch(); | ||
} | ||
insertTransactionRecordsStatement.executeBatch(); | ||
connection.commit(); | ||
return true; | ||
} catch (SQLException e) { | ||
connection.rollback(); | ||
throw new APIManagementException("Error while inserting transaction records", e, | ||
ExceptionCodes.INTERNAL_ERROR); | ||
} | ||
} catch (SQLException e) { | ||
throw new APIManagementException("Error while retrieving database connection", e, | ||
ExceptionCodes.INTERNAL_ERROR); | ||
} | ||
} | ||
|
||
public TransactionCountDTO getTransactionCount(String startTime, String endTime) throws APIManagementException { | ||
StringBuilder query = new StringBuilder(SQLConstants.TransactionCountConstants.GET_TRANSACTION_COUNT); | ||
|
||
try (Connection connection = APIMgtDBUtil.getConnection()) { | ||
PreparedStatement getTransactionCountStatement = connection.prepareStatement(query.toString()); | ||
|
||
Timestamp startTimestamp = Timestamp.valueOf(startTime); | ||
getTransactionCountStatement.setTimestamp(1, startTimestamp); | ||
|
||
Timestamp endTimestamp = Timestamp.valueOf(endTime); | ||
getTransactionCountStatement.setTimestamp(2, endTimestamp); | ||
|
||
try (ResultSet resultSet = getTransactionCountStatement.executeQuery()) { | ||
if (resultSet.next()) { | ||
int count = resultSet.getInt(1); | ||
TransactionCountDTO dto = new TransactionCountDTO(); | ||
dto.setCount(count); | ||
return dto; | ||
} else { | ||
return new TransactionCountDTO(); | ||
} | ||
} catch (SQLException e) { | ||
throw new APIManagementException("Error while fetching transaction count", e, | ||
ExceptionCodes.INTERNAL_ERROR); | ||
} | ||
} catch (SQLException e) { | ||
throw new APIManagementException("Error while retrieving database connection", e, | ||
ExceptionCodes.INTERNAL_ERROR); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dto/TransactionCountDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. | ||
* | ||
* WSO2 LLC. 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. | ||
*/ | ||
|
||
package org.wso2.carbon.apimgt.impl.dto; | ||
|
||
public class TransactionCountDTO { | ||
private String id; | ||
private String host; | ||
private String serverID; | ||
private String serverType; | ||
private int count; | ||
private String recordedTime; | ||
|
||
public TransactionCountDTO(String id, String host, String serverId, String serverType, int count, | ||
String recordedTime) { | ||
this.id = id; | ||
this.host = host; | ||
this.serverID = serverId; | ||
this.serverType = serverType; | ||
this.count = count; | ||
this.recordedTime = recordedTime; | ||
} | ||
|
||
public TransactionCountDTO() { | ||
|
||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public String getHost() { | ||
return host; | ||
} | ||
|
||
public void setHost(String host) { | ||
this.host = host; | ||
} | ||
|
||
public String getServerID() { | ||
return serverID; | ||
} | ||
|
||
public void setServerID(String serverID) { | ||
this.serverID = serverID; | ||
} | ||
|
||
public String getServerType() { | ||
return serverType; | ||
} | ||
|
||
public void setServerType(String serverType) { | ||
this.serverType = serverType; | ||
} | ||
|
||
public int getCount() { | ||
return count; | ||
} | ||
|
||
public void setCount(int count) { | ||
this.count = count; | ||
} | ||
|
||
public String getRecordedTime() { | ||
return recordedTime; | ||
} | ||
|
||
public void setRecordedTime(String recordedTime) { | ||
this.recordedTime = recordedTime; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...l.service/src/gen/java/org/wso2/carbon/apimgt/internal/service/TransactionRecordsApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. | ||
* | ||
* WSO2 LLC. 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. | ||
*/ | ||
|
||
package org.wso2.carbon.apimgt.internal.service; | ||
|
||
import java.util.List; | ||
import org.wso2.carbon.apimgt.internal.service.dto.TransactionRecordDTO; | ||
import org.wso2.carbon.apimgt.internal.service.TransactionRecordsApiService; | ||
import org.wso2.carbon.apimgt.internal.service.impl.TransactionRecordsApiServiceImpl; | ||
import org.wso2.carbon.apimgt.api.APIManagementException; | ||
|
||
import javax.ws.rs.*; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.core.SecurityContext; | ||
import javax.inject.Inject; | ||
|
||
import io.swagger.annotations.*; | ||
import java.io.InputStream; | ||
|
||
import org.apache.cxf.jaxrs.ext.MessageContext; | ||
import org.apache.cxf.jaxrs.ext.multipart.Attachment; | ||
import org.apache.cxf.jaxrs.ext.multipart.Multipart; | ||
|
||
import java.util.Map; | ||
import java.util.List; | ||
import javax.validation.constraints.*; | ||
@Path("/transaction-records") | ||
|
||
@Api(description = "the transaction-records API") | ||
|
||
@Produces({ "application/json" }) | ||
|
||
|
||
public class TransactionRecordsApi { | ||
|
||
@Context MessageContext securityContext; | ||
|
||
TransactionRecordsApiService delegate = new TransactionRecordsApiServiceImpl(); | ||
|
||
|
||
@POST | ||
|
||
|
||
@Produces({ "application/json" }) | ||
@ApiOperation(value = "Insert Transaction Records", notes = "Inserts a list of transaction records into the database.", response = Boolean.class, tags={ }) | ||
@ApiResponses(value = { | ||
@ApiResponse(code = 200, message = "Successfully inserted transaction records", response = Boolean.class), | ||
@ApiResponse(code = 400, message = "Bad request. Invalid input data.", response = Void.class), | ||
@ApiResponse(code = 500, message = "Internal server error. Failed to insert transaction records.", response = Void.class) }) | ||
public Response insertTransactionRecords(@ApiParam(value = "A list of transaction records to be inserted" ,required=true) List<TransactionRecordDTO> body) throws APIManagementException{ | ||
return delegate.insertTransactionRecords(body, securityContext); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...ce/src/gen/java/org/wso2/carbon/apimgt/internal/service/TransactionRecordsApiService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. | ||
* | ||
* WSO2 LLC. 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. | ||
*/ | ||
|
||
package org.wso2.carbon.apimgt.internal.service; | ||
|
||
import org.wso2.carbon.apimgt.internal.service.*; | ||
import org.wso2.carbon.apimgt.internal.service.dto.*; | ||
|
||
import org.apache.cxf.jaxrs.ext.MessageContext; | ||
import org.apache.cxf.jaxrs.ext.multipart.Attachment; | ||
import org.apache.cxf.jaxrs.ext.multipart.Multipart; | ||
|
||
import org.wso2.carbon.apimgt.api.APIManagementException; | ||
|
||
import java.util.List; | ||
import org.wso2.carbon.apimgt.internal.service.dto.TransactionRecordDTO; | ||
|
||
import java.util.List; | ||
|
||
import java.io.InputStream; | ||
|
||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.core.SecurityContext; | ||
|
||
|
||
public interface TransactionRecordsApiService { | ||
public Response insertTransactionRecords(List<TransactionRecordDTO> body, MessageContext messageContext) throws APIManagementException; | ||
} |
Oops, something went wrong.