Class: Aio::Module::SpecialStyle::CompareOld

Inherits:
Aio::Module::SpecialStyle show all
Includes:
Aio::Module, Ui::Verbose
Defined in:
lib/modules/special/style/compare_old.rb

Instance Attribute Summary collapse

Attributes inherited from Aio::Module::SpecialStyle

#special_info

Instance Method Summary collapse

Methods included from Ui::Verbose

#clear_line, #print_error, #print_good, #progress_bar

Methods inherited from Aio::Module::SpecialStyle

#set_defaults, #type

Constructor Details

#initializeCompareOld

Returns a new instance of CompareOld.



16
17
18
19
20
21
# File 'lib/modules/special/style/compare_old.rb', line 16

def initialize
  super({
    :author		=> "Elin",
    :description => "此模块用于解析两个Hash值的不同点,并发出警告,此模块废弃",
  })
end

Instance Attribute Details

#device_managerObject

用于传递已经分析好的现有的device



11
12
13
# File 'lib/modules/special/style/compare_old.rb', line 11

def device_manager
  @device_manager
end

#input_benchmarkObject

用于传递由XML,Json生成并已经解析的Hash值



14
15
16
# File 'lib/modules/special/style/compare_old.rb', line 14

def input_benchmark
  @input_benchmark
end

Instance Method Details

#clear_other_info(device_warning, input_benchmark) ⇒ Object

只留下要比较的两个数据有共同命令的交集,其他的命令就丢弃因为即使是保留了,产生的也是大量的垃圾信息



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/modules/special/style/compare_old.rb', line 45

def clear_other_info(device_warning, input_benchmark)
  device_arr = {}
  input_arr  = {}

  # 获得命令信息
  device_warning.each_pair 	{ |k, v| device_arr[k] = v.keys }
  input_benchmark.each_pair { |k, v| input_arr[k]  = v.keys }

  # 删除 device_warning 中多余的命令信息
  device_warning.each_pair do |name, hash|
    hash.delete_if do |k, v|
      next true unless input_arr.has_key?(name)
      input_arr[name].include?(k) ? false : true
    end
  end

  # 删除 input_benchmark 中多余的命令信息
  input_benchmark.each_pair do |name, hash|
    hash.delete_if do |k, v|
      next true unless device_arr.has_key?(name)
      device_arr[name].include?(k) ? false : true
    end
  end
end

#delete(cm1, cm2) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/modules/special/style/compare_old.rb', line 70

def delete(cm1, cm2)
  compare = Aio::Config::Warning::Compare
  
  cm1.delete_if do |key, val|

    # 当末端是Hash时,迭代循环
    if val.class == Hash
      # 保证无论是Symbol 还是 String 都能互换
      # 有可能整个val都不一样
      unless cm2.has_key?(key)
        cm2_val = cm2[key.to_sym] if key.kind_of? String
        cm2_val = cm2[key.to_s]   if key.kind_of? Symbol
        next if cm2_val.nil?
      else
        cm2_val = cm2[key]
      end
      res = delete(cm1[key], cm2_val)
      # 当有内容时,说明存在差异,则保留
      res.empty? ? true : false
    else

      # 当末端不是Hash时,判断是否相等

      unless cm2.has_key?(key)
        cm2_val = cm2[key.to_sym] if key.kind_of? String
        cm2_val = cm2[key.to_s]   if key.kind_of? Symbol
        next if cm2_val.nil?
      else
        cm2_val = cm2[key]
      end

      #cm1[key].eql?(cm2_val)
      # 当配置中有信息时,那么只有当大于配置中的区间波动才会被记录
      # 当有Pass值的时候,直接Pass
      # 只有是数值才可以
      if compare.has_key?(key)
        compare[key] == "pass" ? true : (cm1[key].to_i < cm2_val.to_i + compare[key])
      else

        if cm1[key].eql?(cm2_val)
          true
        else
          cm1[key].compare_val = cm2_val
          false
        end
      end
    end
  end
end

#parseObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/modules/special/style/compare_old.rb', line 23

def parse
  # 将device_manager 中的按照{device_name => useful} 形式重新编排
  # cmds_useful为深度克隆信息,对本体无影响
  device_warning = {}
  total = device_manager.devices_number
  print_good "总共 #{total} 台设备进行比较"

  device_manager.each_devices_with_index do |device_name, device_klass, i|

    device_warning[device_name] = Aio::Base::Toolkit::DeepClone.clone(device_klass.cmds_useful)
    # 进度条
    progress_bar(total, i+1, device_name)

  end

  clear_line
  clear_other_info(device_warning, input_benchmark)
  delete(device_warning, input_benchmark)
end