librelist archives

« back to archive

Outputting an array in Shoes and doing basic math with it?

Outputting an array in Shoes and doing basic math with it?

From:
Scott Harman
Date:
2013-07-25 @ 02:35
Hi all,
I'm building a shoes app to fetch a table of data over SNMP, and display it
nicely in a Shoes app


http://stackoverflow.com/questions/17825541/cast-a-returned-array-to-appropriate-values

I've got a question up on stack overflow, but the contents of my array are
basically:

Two arrays (one for each connected device) as ints
The names of each storage pool
Total Size (as an int)
Free Space (also an int)
Status (in this case zero, as they're both down)

I have tried returning the basic text as a return value, which kind of
works, but I want greater control, and don't want to rely on predefining
values and structures

I was hoping this was going to be a trivial way to port a commandline ruby
app to a GUI, but I seem to be getting stuck
The application takes one or more hostnames or IP addresses, then every 5
minutes updates the console with the current capacity of each storage pool
(and does some clever maths to work out capacity in hours, minutes and
seconds)

Kind regards
Scott

Re: [shoes] Outputting an array in Shoes and doing basic math with it?

From:
J. Kaiden
Date:
2013-07-25 @ 11:59
hi Scott,

  hmmm, could you post a simplified example of what's happening here? not
sure of what you're trying to get vs. what you're getting  ;)

shoes on,

  - j


On Thu, Jul 25, 2013 at 4:35 AM, Scott Harman <scott@harman.tv> wrote:

> Hi all,
> I'm building a shoes app to fetch a table of data over SNMP, and display
> it nicely in a Shoes app
>
>
> 
http://stackoverflow.com/questions/17825541/cast-a-returned-array-to-appropriate-values
>
> I've got a question up on stack overflow, but the contents of my array are
> basically:
>
> Two arrays (one for each connected device) as ints
> The names of each storage pool
> Total Size (as an int)
> Free Space (also an int)
> Status (in this case zero, as they're both down)
>
> I have tried returning the basic text as a return value, which kind of
> works, but I want greater control, and don't want to rely on predefining
> values and structures
>
> I was hoping this was going to be a trivial way to port a commandline ruby
> app to a GUI, but I seem to be getting stuck
> The application takes one or more hostnames or IP addresses, then every 5
> minutes updates the console with the current capacity of each storage pool
> (and does some clever maths to work out capacity in hours, minutes and
> seconds)
>
> Kind regards
> Scott
>

Re: [shoes] Outputting an array in Shoes and doing basic math with it?

From:
ashbb
Date:
2013-07-25 @ 12:18
Hi Scott,

Thank you for the post.
But sorry, I'm not sure what your problem is... :(

Try out the following snippet. It works as I expected on my Windows 7.

require 'green_shoes'
require 'snmp'
Shoes.app do
  man = SNMP::Manager.new
  man.instance_variables.each do |iv|
    data = man.instance_variable_get(iv)
    case data
    when Hash
      data.each{|k, v| para "#{k} : #{v}"}
    else
      para data
    end
  end
end

Hope this helps,
ashbb

Re: [shoes] Outputting an array in Shoes and doing basic math with it?

From:
Bruno Bonamin
Date:
2013-07-25 @ 12:51
Hi Scott,

I don't understand your problem 100%, but in case it helps, here's a
method<https://github.com/bbonamin/timesheet/blob/master/timesheet.rb#L86>on
a simple GUI app I'm working on with Shoes, where I have an array of
hashes with 'users' which I update and 'redraw', displaying it
here<https://github.com/bbonamin/timesheet/blob/master/timesheet.rb#L58>,
showing a table-like structure with its attributes. I took some of this
code from one of the samples that comes bundled with Shoes 3.0

2013/7/25 ashbb <ashbbb@gmail.com>

> Hi Scott,
>
> Thank you for the post.
> But sorry, I'm not sure what your problem is... :(
>
> Try out the following snippet. It works as I expected on my Windows 7.
>
> require 'green_shoes'
> require 'snmp'
> Shoes.app do
>   man = SNMP::Manager.new
>   man.instance_variables.each do |iv|
>     data = man.instance_variable_get(iv)
>     case data
>     when Hash
>       data.each{|k, v| para "#{k} : #{v}"}
>     else
>       para data
>     end
>   end
> end
>
> Hope this helps,
> ashbb
>



-- 
Bruno Bonamin.

Re: [shoes] Outputting an array in Shoes and doing basic math with it?

From:
Scott Harman
Date:
2013-07-26 @ 00:37
Hi guys
Thanks for the feedback

Here's what I've been working with, and I will update the snippet with a
sample response just so it's clear

Starts
====
Shoes.setup do
  gem 'snmp'
end

class SNMPManager
  require 'snmp'
  include SNMP
  attr_accessor :hostname, :framerate

  def initialize(params = {})
    @hostname = params.fetch(:hostname, 'localhost')
    @framerate = params.fetch(:framerate, 25)

  end

  def getValues
    Manager.open(:host => @hostname, :mib_modules => ["SNMPv2-MIB"]) do
|manager|
      poolArray = []
      ifTable = ObjectId.new("1.3.6.1.4.1.15248.2.5.1.3.1")
      manager.walk(ifTable) do |row|
        poolArray.push(row.inspect)
      end
      groups = poolArray.group_by{ |s| s.split(',').first[/\d+$/] }
      @values = groups.map{ |key, ary| ary.map{ |s| s[/value=(\S+)/, 1] } }

    end
  end

  def to_s
    return @values
  end
end



Shoes.app do
  #@hostname = edit_line
  #man =SNMPManager.new(:host => @hostname)
  man = SNMPManager.new
  man.getValues
  puts 'SNMP Space Monitor'
  man.to_s.each {|v|
    @note = para "#{v[1]} \t(Pool: #{v[0]}) \tCapacity: #{v[3].to_i}
 \tFree Protons: #{v[2].to_i} \tPercent Free:
%#{(v[3].to_f/v[2].to_f*100).round(2)}"
      }
end

====
Ends


On Thu, Jul 25, 2013 at 10:51 PM, Bruno Bonamin <bruno@bonamin.org> wrote:

> Hi Scott,
>
> I don't understand your problem 100%, but in case it helps, here's a
> 
method<https://github.com/bbonamin/timesheet/blob/master/timesheet.rb#L86>on
a simple GUI app I'm working on with Shoes, where I have an array of
> hashes with 'users' which I update and 'redraw', displaying it 
here<https://github.com/bbonamin/timesheet/blob/master/timesheet.rb#L58>,
> showing a table-like structure with its attributes. I took some of this
> code from one of the samples that comes bundled with Shoes 3.0
>
>
> 2013/7/25 ashbb <ashbbb@gmail.com>
>
>> Hi Scott,
>>
>> Thank you for the post.
>> But sorry, I'm not sure what your problem is... :(
>>
>> Try out the following snippet. It works as I expected on my Windows 7.
>>
>> require 'green_shoes'
>> require 'snmp'
>> Shoes.app do
>>   man = SNMP::Manager.new
>>   man.instance_variables.each do |iv|
>>     data = man.instance_variable_get(iv)
>>     case data
>>     when Hash
>>       data.each{|k, v| para "#{k} : #{v}"}
>>     else
>>       para data
>>     end
>>   end
>> end
>>
>> Hope this helps,
>> ashbb
>>
>
>
>
> --
> Bruno Bonamin.

Re: [shoes] Outputting an array in Shoes and doing basic math with it?

From:
Scott Harman
Date:
2013-07-29 @ 04:20
Switching to green_shoes actually completely solved that issue, so it looks
like it was a red_shoes specific issue - not sure how that kind of thing
comes about, but glad to know I wasn't going completely nuts.

Cheers