Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transaction counting capability #12547

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
ashanhr marked this conversation as resolved.
Show resolved Hide resolved

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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4388,4 +4388,12 @@ public static class SystemConfigsConstants {
+ "SET CONFIGURATION = ? WHERE ORGANIZATION = ? AND CONFIG_TYPE = ?";
}

/**
* Static class to hold database queries related to AM_TRANSACTION_RECORDS table
*/
public static class TransactionCountConstants {
public static final String INSERT_TRANSACTION_COUNT = "INSERT INTO AM_TRANSACTION_RECORDS " + "(ID, HOST, SERVER_ID, SERVER_TYPE, COUNT, RECORDED_TIME) " + "VALUES (?,?,?,?,?,?)";
public static final String GET_TRANSACTION_COUNT = "SELECT SUM(COUNT) FROM AM_TRANSACTION_RECORDS " + "WHERE RECORDED_TIME >= ? AND RECORDED_TIME <= ?";
}

}
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;
}
}
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);
}
}
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;
}
Loading
Loading