Skip to content

Instantly share code, notes, and snippets.

@Goblin80
Created October 17, 2015 19:09
Show Gist options
  • Save Goblin80/179be2cf68d70069b3a9 to your computer and use it in GitHub Desktop.
Save Goblin80/179be2cf68d70069b3a9 to your computer and use it in GitHub Desktop.
Rewritting "View Statistics.bat" from "TP-LINK-Remote-Management" in Ruby
require "Date"
require "colorize"
DEFAULTDAY = 25
DIRECTORY= "Sample/Log"
(puts "ERROR: Log Directory Not Found"; gets; exit) if !Dir.exist? "Log"
class LogFile
attr_accessor :name, :date
def initialize(direc, date)
@direc = direc
@date = date
@name = "#{@direc}/#{@date.strftime("%m - %d")}.txt"
end
def nxt
@date += 1
@name = "#{@direc}/#{@date.strftime("%m - %d")}.txt"
end
def formatted
@date.strftime("%a %d %b")
end
def data
File.read(@name).to_i
end
end
def display_art
puts "\t\t _____ ____ _____".green
puts "\t\t / / \\ \\".green
puts "\t\t/____ /_________\\____\\".green
puts "\t\t\\ \\ / /".green
puts "\t\t \\ \\ / /".green
puts "\t\t \\ \\ / /".green
puts "\t\t \\ \\/ /".green
puts "\t\t \\/\n".green
end
display_art
today = Date.today
from = Date.new(today.year,today.month,DEFAULTDAY)
from <<= 1 if today.day <= from.day
puts "============"
puts "View Quota:"
puts "============"
print "From (Leave empty for #{from.strftime('%a %d %b')}): "
s = gets.chomp
from = Date.parse("#{s}-#{today.year}") if !s.empty?
print "Till (Leave empty for #{today.strftime('%a %d %b')}): "
s = gets.chomp
today = Date.parse("#{s}-#{today.year}") if !s.empty?
puts "\n\nx Daily Traffic Consumption:"
puts "====================================================\n"
total = 0
mbs = 0
f = LogFile.new(DIRECTORY,from)
while f.date <= today
if File.exists? f.name
mbs = f.data
mbs = mbs / 1024
puts "* Traffic Consumed on #{f.formatted}: [#{mbs / 1024} Gigs and #{mbs % 1024} Megs]" if mbs != 0
total += mbs
end
f.nxt
end
puts "\nx Total Traffic Consumption = #{total / 1024} Gigs and #{total % 1024} Megs"
puts "====================================================\n"
gets
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment