PtokaX forum

Archive => Archived 4.0 boards => Finished Lua 4 scripts => Topic started by: Herodes on 18 August, 2004, 00:22:37

Title: ProtocolData
Post by: Herodes on 18 August, 2004, 00:22:37
here's a mod I did today, ....on the DataLog by DirtyFinger
--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
--- KnowUrBandwidth v1
--- by Herodes --- a mod based on ---
--- --- Datalog - Lua Edition, Version 1.1,  by DirtyFinger
--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
--- this shows some nice info about where your bandwidth is going
--- it can only catch DC Protocol Data going in and out of the server
--- You will also need a file called "stats.tbl" with the following line in it:  (take off the "--" in the begging of the line ... ;)
-- Stats = {}
--- Note : Just playing with tables ;)
--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
--- Utility Functions Used : Serialize - DoTimeUnits
--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
--- Commands Available :
--- --- +stats   This displays the statistics page
--- --- +clearstats This clears the All-time Statistics
--- --- +allstats This switches the simple display mode on or off ...
--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
seconds = 1000
minutes = 1000*60
hours = 1000*(60^2)
simple = 1
---  Change this to anything than ' nil ' if you have a PtokaX 0.3.3 Beta ....
BetaPtokaX = nil
--- the command to display the stats
stscmd = "+stats"
rstcmd = "+clearstats"
allcmd = "+allstats"
--- This sets the intervalls in which the files get saved ? change accordingly always keep the format : timerIntervall = #* ?
---   ? where # is a number, where is one of : hours, minutes, seconds ?
timerIntervall = 10*minutes

Statistics = {
[1] = { "<", 0 },
[2] = { "$ConnectToMe", 0 },
[3] = { "$GetNickList", 0 },
[4] = { "$GetINFO", 0 },
[5] = { "$Kick", 0 },
[6] = { "$MultiConnectToMe", 0 },
[7] = { "$MultiSearch", 0 },
[8] = { "$MyINFO", 0 },
[9] = { "$MyPass", 0 },
[10] = { "$OpForceMove", 0 },
[11] = { "$Quit", 0 },
[12] = { "$RevConnectToMe", 0 },
[13] = { "$Search", 0 },
[14] = { "$SR", 0 },
[15] = { "$To:", 0 },
[16] = { "$ValidateNick", 0 },
[17] = { "$Version", 0 },
[18] = { "sOther", 0 },
[19] = { "ZTotal", 0 },
[20] = { "uptime", 0 },
[21] = { "laststart", 0 },
}

Statistics["uptime"] = floor(clock())
Statistics.laststart = date("%H:%M:%S ? %d/%m - %Y")


function Main()
frmHub:EnableFullData(1)
frmHub:EnableSearchData(1)
SendToAll("Stats", date(" launched at %B %d %Y %X "))
SetTimer(timerIntervall)
StartTimer()
end

function DataArrival( user, data )
local c = 0
for i , v in Statistics do
c = c + 1
if c <= 17 then
if strsub(data, 1, strlen(Statistics[i][1])) == Statistics[i][1] then
Statistics[i][2] = Statistics[i][2] + 1
else
Statistics[18][2] = Statistics[18][2] +1
end
Statistics[19][2] = Statistics[19][2] + 1
else break end
end

if strsub(data, 1, 1) == "<" then
data = strsub(data, 1 ,strlen(data)-1)
local s,e,cmd = strfind(data, "%b<>%s+(%S+)")
if strfind(data, "<"..user.sName..">%s+"..stscmd) then
LoadFile("stats.txt")
if Stats[1] == nil then
OnExit()
LoadFile("stats.txt")
end
local msg = "\r\n\t\tData Stats for "..frmHub:GetHubName().." --- current uptime : "..DoTimeUnits( floor(clock()) - Statistics.uptime).." ("..Statistics.laststart..")\r\n"
msg = msg.."\r\n\t\t\t\t Since Last Startup\t    All Time Totals\t           Percentage\r\n"

local tStr = { [1] = "Main Chat Messages", [2] = "ConnectToMe\t",
[3] = "NickList\t\t", [4] = "GetINFOs\t",
[5] = "Kick Commands\t", [6] = "MutliConnectToMe",
[7] =  "MyINFOs\t", [8] = "Password Validations",
[9] = "Redirects\t", [10] = "Disconnects\t",
[11] = "Pasv Request Cons", [12] = "Searches\t",
[13] = "Passive Search Results", [14] = "Private Messages\t",
[15] = "Nick Validations\t", [16] = "Version\t\t",
[17] = "Unidentified\t", [18] = "Total Protocol Commands",
}
for i = 1, 18 do
msg = msg..Line(Statistics[i][2], Stats[i][2], tStr[i])
end
user:SendData("Stats", msg)
msg = nil
return 1
elseif strfind(data, "<"..user.sName..">%s+"..rstcmd) then
LoadFile("stats.txt")
for i, v in Stats do
Stats[i][2] = 0
end
SaveFile(Stats , "Stats", "stats.txt")
user:SendData("Stats", "The All-time Protocol Data Stats have been deleted ...")
return 1
elseif strfind(data, "<"..user.sName..">%s+"..allcmd.."%son") then
simple = 1
user:SendData("Stats", "Typing ' "..stscmd.." ' will provide you with Stats only about the Protocol Data that have been used.")
return 1
elseif strfind(data, "<"..user.sName..">%s+"..allcmd.."%soff") then
simple = 0
user:SendData("Stats", "Typing ' "..stscmd.." ' will provide you with Stats about all Protocol Data.")
return 1
end
end

end

function Line(now, total, string)
if total ~= nil then
if now == 0 and (total ~= 0 and total ~= nil) then
return "\t - "..string.."\t:\t0\t\t"..total.."\t\t0%\r\n"
elseif now ~= 0 and total == 0 then
return "\t - "..string.."\t:\t"..now.."\t\t0\t\t100%\r\n"
elseif now == 0 and total == 0 then
if simple == 1 then return ""
else return "\t - "..string.."\t:\t0\t\t0\t\t0%\r\n"
end
else
return "\t - "..string.."\t:\t"..now.."\t\t"..total.."\t\t"..( format("%0.1f", ( 100*(now/total) )) or 0 ).."%\r\n"
end
else return "\t - "..string.."\t:\t"..now.."\t\t0\t\t100%\r\n"
end
end

function OnTimer()
if not BetaPtokaX then
AddUp(19)
end
end

function OnExit()
AddUp(19)
end

function AddUp(val)
local c = 0
LoadFile("stats.txt")
for i, v in Statistics do
c = c +1
if c <= val then
if Stats[i] == nil then
Stats[i] = {}
Stats[i][1] = Statistics[i][1]
Stats[i][2] = Statistics[i][2]
else
if type(Statistics[i][2]) == "number" then
Stats[i][2] = Stats[i][2] + Statistics[i][2]
else Stats[i][2] = Statistics[i][2]
end
end
else break end
end
SaveFile(Stats , "Stats", "stats.txt")
end

--- // --- Table Serialization ( part 1/3 ) ? creating input ? --- Thanks Ninghtlitch for providing this to me
function Serialize(savTable, sTableName, hFile, sTab)
assert(savTable, "savTable equals nil");
assert(sTableName, "sTableName equals nil");
assert(hFile, "hFile equals nil");
assert(type(savTable) == "table", "savTable must be a table!");
assert(type(sTableName) == "string", "sTableName must be a string!");
sTab = sTab or "";
write(hFile, sTab..sTableName.." = {\n" );
for key, value in savTable do
local sKey = (type(key) == "string") and format("[%q]",key) or format("[%d]",key);
if(type(value) == "table") then
Serialize(value, sKey, hFile, sTab.."\t");
else
local sValue = (type(value) == "string") and format("%q",value) or tostring(value);
write(hFile, sTab.."\t"..sKey.." = "..sValue);
end
write(hFile, ",\n");
end
write(hFile, sTab.."}");
end


--- // --- Table Serialization ( part 2/3 ) ? Loading from file ?
function LoadFile(file)
assert(readfrom(file),file.." is not found.")
dostring(read("*all"))
readfrom()
end


--- // --- Table Serialization ( part 3/3 ) ? Saving to file ?
function SaveFile(table , tablename, file)
local hFile = openfile(file, "w");
Serialize(table, tablename, hFile);
closefile(hFile);
end

function DoTimeUnits(time)
local tTimes = {}
local time = time * 1000
if ( time >= 86400000 ) then
repeat
if tTimes[4] then
tTimes[4] = tTimes[4] + 1
else tTimes[4] = 1
end
time = time - 86400000
until time < 86400000
end

if ( time >= 3600000 ) then
repeat
if tTimes[3] then
tTimes[3] = tTimes[3] + 1
else tTimes[3] = 1
end
time = time - 3600000
until time < 3600000
end

if ( time >= 60000 ) then
repeat

if tTimes[2] then
tTimes[2] = tTimes[2] + 1
else tTimes[2] = 1
end
time = time - 60000
until time < 60000
end

if ( time >= 1000 ) then
repeat
if tTimes[1] then
tTimes[1] = tTimes[1] + 1
else tTimes[1] = 1
end
time = time - 1000
until time < 1000
end
local msg = ""
local tTimeUns = { "seconds", "minutes", "hours", "days"}
for i,v in tTimes do
msg = v.." "..tTimeUns[i].." "..msg
end
return msg
end

You will also need a file called "stats.tbl" with the following in it
Stats = {}
tell me what you think ...