Skip to content

Commit

Permalink
DebugTraceSqlQueriesRestController (#10698)
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Feb 23, 2021
1 parent 6d45da6 commit 912b516
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.NonNull;
import org.adempiere.ad.dao.IQueryStatisticsLogger;
import org.adempiere.ad.trx.api.ITrx;
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.exceptions.DBException;
Expand Down Expand Up @@ -112,8 +111,6 @@ public class DebugRestController
{
public static final String ENDPOINT = WebConfig.ENDPOINT_ROOT + "/debug";

private static final Logger logger = LogManager.getLogger(Logger.class);

@Autowired
private UserSession userSession;

Expand All @@ -135,10 +132,6 @@ public class DebugRestController
@Lazy
private ProcessRestController processesController;

@Autowired
@Lazy
private IQueryStatisticsLogger statisticsLogger;

@Autowired
@Lazy
private ObjectMapper sharedJsonObjectMapper;
Expand Down Expand Up @@ -236,25 +229,6 @@ public boolean isAllowDeprecatedRestAPI()
return userSession.isAllowDeprecatedRestAPI();
}

@RequestMapping(value = "/traceSqlQueries", method = RequestMethod.GET)
public void setTraceSqlQueries(

@ApiParam(value = "If Enabled, all SQL queries are logged with loglevel=WARN, or if the system property <code>" + IQueryStatisticsLogger.SYSTEM_PROPERTY_LOG_TO_SYSTEM_ERROR + "</code> is set to <code>true</code>, they will be written to std-err.", //
allowEmptyValue = false) //
@RequestParam("enabled") final boolean enabled)
{
userSession.assertLoggedIn();

if (enabled)
{
statisticsLogger.enableWithSqlTracing();
}
else
{
statisticsLogger.disable();
}
}

@RequestMapping(value = "/debugProtocol", method = RequestMethod.GET)
public void setDebugProtocol(@RequestParam("enabled") final boolean enabled)
{
Expand Down Expand Up @@ -390,7 +364,7 @@ public enum LoggingModule

private final Set<String> loggerNames;

private LoggingModule(final String... loggerNames)
LoggingModule(final String... loggerNames)
{
this.loggerNames = ImmutableSet.copyOf(loggerNames);
}
Expand Down Expand Up @@ -491,7 +465,7 @@ public String viewDeleteRowIds(

//
// Delete from DB selection
String sql = "DELETE FROM " + I_T_WEBUI_ViewSelection.Table_Name
final String sql = "DELETE FROM " + I_T_WEBUI_ViewSelection.Table_Name
+ " WHERE " + I_T_WEBUI_ViewSelection.COLUMNNAME_UUID + "=" + DB.TO_STRING(viewId.getViewId())
+ " AND " + I_T_WEBUI_ViewSelection.COLUMNNAME_IntKey1 + "=" + DB.buildSqlList(rowIds.toIntSet());
final int countDeleted = DB.executeUpdateEx(sql, ITrx.TRXNAME_None);
Expand Down Expand Up @@ -592,7 +566,7 @@ public void stressTest_massCacheInvalidation(
countEventsGenerated++;
}
}
catch (SQLException ex)
catch (final SQLException ex)
{
throw new DBException(ex);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* #%L
* metasfresh-webui-api
* %%
* Copyright (C) 2021 metas GmbH
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/

package de.metas.ui.web.debug;

import com.google.common.collect.ImmutableList;
import de.metas.ui.web.session.UserSession;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import org.adempiere.ad.dao.IQueryStatisticsLogger;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(DebugTraceSqlQueriesRestController.ENDPOINT)
public class DebugTraceSqlQueriesRestController
{
public static final String ENDPOINT = DebugRestController.ENDPOINT + "/traceSqlQueries";

private final UserSession userSession;
private final IQueryStatisticsLogger statisticsLogger;

public DebugTraceSqlQueriesRestController(
@NonNull final UserSession userSession,
@NonNull final IQueryStatisticsLogger statisticsLogger)
{
this.userSession = userSession;
this.statisticsLogger = statisticsLogger;
}

@GetMapping
public void setTraceSqlQueries(
@ApiParam("If Enabled, all SQL queries are logged with loglevel=WARN, or if the system property <code>" + IQueryStatisticsLogger.SYSTEM_PROPERTY_LOG_TO_SYSTEM_ERROR + "</code> is set to <code>true</code>, they will be written to std-err.")
@RequestParam("enabled") final boolean enabled)
{
userSession.assertLoggedIn();

if (enabled)
{
statisticsLogger.enableWithSqlTracing();
}
else
{
statisticsLogger.disable();
}
}

@GetMapping("/top/byAverageDuration")
@ApiOperation("Gets top SQL queries ordered by their average execution time (descending)")
public Map<String, Object> getTopAverageDurationQueriesAsString()
{
userSession.assertLoggedIn();
return queriesListToMap(statisticsLogger.getTopAverageDurationQueriesAsString());
}

@ApiOperation("Gets top SQL queries ordered by their total summed executon time (descending)")
@GetMapping("/top/byTotalDuration")
public Map<String, Object> getTopTotalDurationQueriesAsString()
{
userSession.assertLoggedIn();
return queriesListToMap(statisticsLogger.getTopTotalDurationQueriesAsString());
}

@GetMapping("/top/byExecutionCount")
@ApiOperation("Gets top SQL queries ordered by their execution count (descending)")
public Map<String, Object> getTopCountQueriesAsString()
{
userSession.assertLoggedIn();
return queriesListToMap(statisticsLogger.getTopCountQueriesAsString());
}

private static Map<String, Object> queriesListToMap(final String[] list)
{
final HashMap<String, Object> map = new HashMap<>();
map.put("queries", ImmutableList.copyOf(list));
return map;
}
}

0 comments on commit 912b516

Please sign in to comment.