All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fastchar.extjs.systemtool.entity.FinalLogThreadEntity Maven / Gradle / Ivy

The newest version!
package com.fastchar.extjs.systemtool.entity;
import com.fastchar.core.FastChar;
import com.fastchar.extjs.core.FastExtEntity;
import com.fastchar.database.FastPage;
import com.fastchar.database.info.FastSqlInfo;
import com.fastchar.utils.FastDateUtils;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.*;
import java.util.concurrent.locks.LockSupport;

import com.fastchar.utils.FastStringUtils;

/**
 * 系统线程运行记录数据库实体类
 * 
 * @author Janesen
 * @date 2024/03/25 10:59
 */
public class FinalLogThreadEntity extends FastExtEntity {
	private static final long serialVersionUID = 1L;

	public static FinalLogThreadEntity dao() {
		return FastChar.getOverrides().singleInstance(FinalLogThreadEntity.class);
	}

	public static FinalLogThreadEntity newInstance() {
		return FastChar.getOverrides().newInstance(FinalLogThreadEntity.class);
	}

	@Override
	public String getTableName() {
		return "final_log_thread";
	}

	@Override
	public String getTableDetails() {
		return "系统线程运行记录";
	}

	@Override
	public String getEntityCode() {
		return this.getClass().getSimpleName();
	}

	@Override
	public FastPage showList(int page, int pageSize) {

		String sqlStr = "select t.*" +
				" from final_log_thread as t" +
				" ";
		FastSqlInfo sqlInfo = toSelectSql(sqlStr);
		return selectBySql(page, pageSize, sqlInfo.getSql(), sqlInfo.toParams());
	}



	@Override
	public void setDefaultValue() {
		set("threadElapsed", 0);
		set("logDateTime", FastDateUtils.getDateString());
	}

	@Override
	public void convertValue() {
		super.convertValue();

	}

	@Override
	public boolean save(String...checks) {

		return super.save(checks);
	}

	@Override
	public boolean update(String...checks){

		return super.update(checks);
	}


	public enum ThreadStateEnum{
		NEW,
		RUNNABLE,
		BLOCKED,
		WAITING,
		TIMED_WAITING,
		TERMINATED;
	}


	public void refreshData() {
		String clearSqlStr = "delete from final_log_thread ";
		this.updateBySql(clearSqlStr);
		try {
			List list = new ArrayList<>();
			ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();

			int index = 1;
			ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(true, true);
			for (ThreadInfo threadInfo : threadInfos) {
				long elapsedTime = threadMXBean.getThreadCpuTime(threadInfo.getThreadId());

				FinalLogThreadEntity logThreadEntity = FinalLogThreadEntity.newInstance();
				logThreadEntity.set("logId", index++);
				logThreadEntity.set("logDateTime", FastDateUtils.getDateString());
				logThreadEntity.set("threadName", threadInfo.getThreadName());
				logThreadEntity.set("threadId", threadInfo.getThreadId());
				logThreadEntity.set("threadState", threadInfo.getThreadState().name());
				logThreadEntity.set("threadElapsed", elapsedTime / 1_000_000_000.0);

				List stackTraces = new ArrayList<>();
				StackTraceElement[] stackTrace = threadInfo.getStackTrace();
				for (StackTraceElement stackTraceElement : stackTrace) {
					stackTraces.add(stackTraceElement.toString());
				}
				logThreadEntity.set("threadStack", FastStringUtils.join(stackTraces, "\n\t"));
				list.add(logThreadEntity);
			}
			FastChar.getDB().batchSaveEntity(list, list.size());
		} catch (Exception e) {
			FastChar.getLogger().error(this.getClass(), e);
		}
	}



	/**
	 * 获得数据详情
	 */
	public FinalLogThreadEntity getDetails(String logId) {
		List linkColumns = new ArrayList<>();
		linkColumns.add("t.*");
		String sqlStr = "select " + FastStringUtils.join(linkColumns, ",") + " from final_log_thread as t" + " "
				+ " where t.logId = ?  ";
		FinalLogThreadEntity entity = selectFirstBySql(sqlStr, logId);
		if (entity != null) {
			// to-do something
		}
		return entity;
	}
	/**
	 * 获得本实体列表集合
	 * 
	 * @return 分页数据
	 */
	public FastPage getList(int page, int pageSize, Map where,
			Map sort) {

		List linkColumns = new ArrayList<>();
		linkColumns.add("t.*");

		StringBuilder sqlStr = new StringBuilder("select " + FastStringUtils.join(linkColumns, ",")
				+ " from final_log_thread as t" + " " + " where 1=1 ");

		List values = new ArrayList<>();

		if (where != null) {
			for (Entry stringObjectEntry : where.entrySet()) {
				String key = stringObjectEntry.getKey();
				if (key.startsWith("^")) {
					continue;
				}
				Object value = where.get(key);
				key = key.split("@")[0];

				if (key.equals("__searchKey")) {
					String[] searchFields = new String[]{"threadName", "threadId", "threadState", "threadStack"};
					List orWhere = new ArrayList<>();
					for (String searchField : searchFields) {
						orWhere.add(searchField + " like ? ");
						values.add("%" + value + "%");
					}
					sqlStr.append(" and ").append(" ( ").append(FastStringUtils.join(orWhere, " or ")).append(" ) ");
					continue;
				}

				String prefix = "t.";
				if (key.contains("__")) {
					String[] keyArray = key.split("__");
					prefix = keyArray[0] + ".";
					key = keyArray[1];
				}

				String[] compareChar = new String[]{">=", "<=", ">", "<", "=", "!="};
				if (value == null) {
					continue;
				}
				boolean hasCompare = false;
				for (String compare : compareChar) {
					if (value.toString().startsWith(compare)) {
						sqlStr.append(" and ").append(prefix).append(key).append(" ").append(compare).append(" ? ");
						values.add(value.toString().replace(compare, ""));
						hasCompare = true;
						break;
					}
				}
				if (hasCompare) {
					continue;
				}
				if (isNumberColumn(key)) {
					sqlStr.append(" and ").append(prefix).append(key).append(" = ? ");
					values.add(value);
				} else {
					sqlStr.append(" and ").append(prefix).append(key).append(" like ? ");
					values.add("%" + value + "%");
				}
			}
		}

		if (sort != null) {
			List sortKeys = new ArrayList<>();
			for (Entry stringObjectEntry : sort.entrySet()) {
				String key = stringObjectEntry.getKey();
				if (key.startsWith("^")) {
					continue;
				}
				String prefix = "t.";
				if (key.contains("__")) {
					prefix = key.split("__")[0] + ".";
				}
				sortKeys.add(prefix + key + " " + stringObjectEntry.getValue());
			}
			if (sortKeys.size() > 0) {
				sqlStr.append(" order by ").append(FastStringUtils.join(sortKeys, ","));
			}
		}

		FastPage pageList = selectBySql(page, pageSize, sqlStr.toString(), values);
		for (FinalLogThreadEntity entity : pageList.getList()) {

		}
		return pageList;
	}

	/**
	 * 根据层级编号获得本实体集合
	 * 
	 * @return 分页数据
	 */
	public FastPage getListByLayerCode(int page, String layerCode) {

		List linkColumns = new ArrayList<>();
		linkColumns.add("t.*");

		String sqlStr = "select " + FastStringUtils.join(linkColumns, ",") + " from final_log_thread as t" + " "
				+ " where 1=1 and t.logLayerCode like ? ";
		FastPage pageList = selectBySql(page, 10, sqlStr, layerCode + "@%");
		for (FinalLogThreadEntity entity : pageList.getList()) {

		}
		return pageList;
	}
}