public int querySpeedId(String page, String step) { int value = -1; Speed speed = m_speeds.get(page + "-" + step); if (speed != null) { value = speed.getId(); } return value; }
private void updateData() { Map<Integer, Speed> speeds = m_config.getSpeeds(); Map<String, Speed> tmp = new ConcurrentHashMap<String, Speed>(); for (Entry<Integer, Speed> entry : speeds.entrySet()) { Speed s = entry.getValue(); tmp.put(s.getPage() + "-" + s.getStep(), s); } m_speeds = tmp; }
public int generateId() { List<Integer> ids = new ArrayList<Integer>(); for (Speed s : m_config.getSpeeds().values()) { ids.add(s.getId()); } int max = 0; if (!ids.isEmpty()) { Collections.sort(ids); max = ids.get(ids.size() - 1); } if (ids.size() < max) { for (int i = 1; i <= max; i++) { if (!ids.contains(i)) { return i; } } } return max + 1; }
public void process(Action action, Payload payload, Model model) { int id; switch (action) { case APP_NAME_CHECK: if (m_appConfigManager.isNameDuplicate(payload.getName())) { model.setNameUniqueResult("{\"isNameUnique\" : false}"); } else { model.setNameUniqueResult("{\"isNameUnique\" : true}"); } break; case APP_LIST: buildListInfo(model, payload); break; case APP_COMMMAND_UPDATE: id = payload.getId(); if (m_appConfigManager.containCommand(id)) { Command command = m_appConfigManager.getConfig().findCommand(id); if (command == null) { command = new Command(); } model.setUpdateCommand(command); } break; case APP_COMMAND_SUBMIT: id = payload.getId(); String domain = payload.getDomain(); String name = payload.getName(); String title = payload.getTitle(); boolean all = payload.isAll(); int timeThreshold = payload.getThreshold(); if (m_appConfigManager.containCommand(id)) { Command command = new Command(); command .setDomain(domain) .setName(name) .setTitle(title) .setAll(all) .setThreshold(timeThreshold); if (m_appConfigManager.updateCommand(id, command)) { model.setOpState(true); } else { model.setOpState(false); } } else { try { Command command = new Command() .setDomain(domain) .setTitle(title) .setName(name) .setAll(all) .setThreshold(timeThreshold); if (m_appConfigManager.addCommand(command).getKey()) { model.setOpState(true); } else { model.setOpState(false); } } catch (Exception e) { model.setOpState(false); } } buildListInfo(model, payload); break; case APP_COMMAND_DELETE: id = payload.getId(); if (m_appConfigManager.deleteCommand(id)) { model.setOpState(true); } else { model.setOpState(false); } buildListInfo(model, payload); break; case APP_CODE_UPDATE: id = payload.getId(); int codeId = payload.getCode(); if (payload.isConstant()) { Code code = m_appConfigManager.getConfig().getCodes().get(codeId); model.setCode(code); } else { Command cmd = m_appConfigManager.getRawCommands().get(id); if (cmd != null) { Code code = cmd.getCodes().get(codeId); model.setCode(code); model.setUpdateCommand(cmd); } } break; case APP_CODE_SUBMIT: try { id = payload.getId(); String codeStr = payload.getContent(); List<String> strs = Splitters.by(":").split(codeStr); codeId = Integer.parseInt(strs.get(0)); name = strs.get(1); int status = Integer.parseInt(strs.get(2)); Code code = new Code(codeId); code.setName(name).setStatus(status); if (payload.isConstant()) { m_appConfigManager.updateCode(code); } else { m_appConfigManager.updateCode(id, code); } buildListInfo(model, payload); } catch (Exception e) { Cat.logError(e); } break; case APP_CODE_ADD: id = payload.getId(); model.setId(String.valueOf(id)); break; case APP_CODE_DELETE: try { id = payload.getId(); codeId = payload.getCode(); if (payload.isConstant()) { m_appConfigManager.getCodes().remove(codeId); } else { m_appConfigManager.deleteCode(id, codeId); } buildListInfo(model, payload); } catch (Exception e) { Cat.logError(e); } break; case APP_SPEED_UPDATE: case APP_SPEED_ADD: id = payload.getId(); Speed speed = m_appSpeedConfigManager.getConfig().getSpeeds().get(id); if (speed != null) { model.setSpeed(speed); } break; case APP_SPEED_DELETE: try { id = payload.getId(); m_appSpeedConfigManager.deleteSpeed(id); buildListInfo(model, payload); } catch (Exception e) { Cat.logError(e); } break; case APP_SPEED_SUBMIT: try { id = payload.getId(); String speedStr = payload.getContent(); List<String> strs = Splitters.by(":").split(speedStr); String page = strs.get(0).trim(); int step = Integer.parseInt(strs.get(1).trim()); title = strs.get(2).trim(); int threshold = Integer.parseInt(strs.get(3).trim()); int speedId = id > 0 ? id : m_appSpeedConfigManager.generateId(); speed = new Speed(speedId); speed.setPage(page).setStep(step).setTitle(title).setThreshold(threshold); m_appSpeedConfigManager.updateConfig(speed); buildListInfo(model, payload); } catch (Exception e) { Cat.logError(e); } break; case APP_CONFIG_UPDATE: String appConfig = payload.getContent(); if (!StringUtils.isEmpty(appConfig)) { model.setOpState(m_appConfigManager.insert(appConfig)); } model.setContent(m_appConfigManager.getConfig().toString()); break; case APP_RULE: buildAppConfigInfo(m_appConfigManager, model); model.setRules(m_appRuleConfigManager.getMonitorRules().getRules().values()); break; case APP_RULE_ADD_OR_UPDATE: buildAppConfigInfo(m_appConfigManager, model); generateRuleConfigContent(payload.getRuleId(), m_appRuleConfigManager, model); break; case APP_RULE_ADD_OR_UPDATE_SUBMIT: buildAppConfigInfo(m_appConfigManager, model); model.setOpState( addSubmitRule(m_appRuleConfigManager, payload.getRuleId(), "", payload.getConfigs())); model.setRules(m_appRuleConfigManager.getMonitorRules().getRules().values()); break; case APP_RULE_DELETE: buildAppConfigInfo(m_appConfigManager, model); model.setOpState(deleteRule(m_appRuleConfigManager, payload.getRuleId())); model.setRules(m_appRuleConfigManager.getMonitorRules().getRules().values()); break; case APP_COMPARISON_CONFIG_UPDATE: String appComparisonConfig = payload.getContent(); if (!StringUtils.isEmpty(appComparisonConfig)) { model.setOpState(m_appComparisonConfigManager.insert(appComparisonConfig)); } model.setContent(m_appComparisonConfigManager.getConfig().toString()); break; case APP_RULE_BATCH_UPDATE: appRuleBatchUpdate(payload, model); buildListInfo(model, payload); break; case APP_CONSTANT_ADD: break; case APP_CONSTANT_UPDATE: Item item = m_appConfigManager.queryItem(payload.getType(), payload.getId()); model.setAppItem(item); break; case APP_CONSTATN_SUBMIT: try { id = payload.getId(); String content = payload.getContent(); String[] strs = content.split(":"); String type = strs[0]; int constantId = Integer.valueOf(strs[1]); String value = strs[2]; model.setOpState(m_appConfigManager.addConstant(type, constantId, value)); buildListInfo(model, payload); } catch (Exception e) { Cat.logError(e); } break; case APP_COMMAND_FORMAT_CONFIG: String content = payload.getContent(); if (StringUtils.isNotEmpty(content)) { m_urlConfigManager.insert(content); } model.setContent(m_urlConfigManager.getUrlFormat().toString()); break; default: throw new RuntimeException("Error action name " + action.getName()); } }