MySQL Stats System + Cache 0.90 BETA (MBC Base)

By Noutrious on Sep 06, 2011

This is a module for MySQL Bot Core system.
You'll need http://www.hawkee.com/snippet/8882/ in case for this to work.

  • Please note that this is in BETA version.

[13:47] !stats
[13:47] -#DEV-SQL- [Neisen:] lines: 29 words: 31 symbols: 153 joins: 2 parts: 2 actions: 0 notices: 0 // statistics calculated since 2011-09-06 13:38:48
[13:48] !chanstats
[13:48] -#DEV-SQL- [###:] lines: 30 words: 32 symbols: 159 joins: 2 parts: 2 actions: 0 notices: 0 // statistics calculated since 2011-09-06 13:38:48
[13:48] !stats hawkee
[13:48] -#DEV-SQL- [hawkee:] no stats retrieved yet.
[13:48] !chanstats #hawkee
[13:48] -#DEV-SQL- [#hawkee:] no stats retrieved yet.

MySQL Database Structure:

CREATE TABLE `stats` (
  `target` varchar(50) NOT NULL,
  `lines` int(10) unsigned NOT NULL DEFAULT '0',
  `words` int(10) unsigned NOT NULL DEFAULT '0',
  `symbols` int(10) unsigned NOT NULL DEFAULT '0',
  `joins` int(10) unsigned NOT NULL DEFAULT '0',
  `parts` int(10) unsigned NOT NULL DEFAULT '0',
  `actions` int(10) unsigned NOT NULL DEFAULT '0',
  `notices` int(10) unsigned NOT NULL DEFAULT '0',
  `start` datetime NOT NULL,
  PRIMARY KEY (`target`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

NOTE: The frequency of syncing is defined by the MySQL Bot Core timer, which executes m.sync. If channel(-s) has big traffic and bot starts to freeze for seconds, make the m.sync with bigger delay. :)

; MySQL Bot Core
; Module: Stats System
; Version: 0.90 BETA
; Author: Nils Putnins (Neisen@Neisen.lv)

; - aliases
alias -l m.stat.db return $qt($mircdirstats.mbc.ini)
alias -l m.stat.res return $calc($m.dbres($1,stats,target,$2) + $zero($readini($m.stat.db,$2,$1)))
alias -l m.stat {
  if (!$isid && $3) { writeini -n $m.stat.db $1 $2 $calc($readini($m.stat.db,$1,$2) + $3) }
  else {
    ; syncing with mysql
    if ($prop == sync) {
      var %targets = $ini($m.stat.db,0)
      while (%targets) {
        var %target = $ini($m.stat.db,%targets), %types = $ini($m.stat.db,$ini($m.stat.db,%targets),0)
        while (%types) {
          var %type = $ini($m.stat.db,$ini($m.stat.db,%targets),%types), %data = $m.dbres(%type,stats,target,%target)
          if (%data != $null) var %sql = UPDATE `stats` SET $m.sqlb(%type) = $calc(%data + $readini($m.stat.db,%target,%type)) WHERE `target` = $m.sqlq(%target)
          else var %sql = INSERT INTO `stats` $+($chr(40),$m.sqlb(target),$chr(44),$m.sqlb(%type),$chr(44),$m.sqlb(start),$chr(41)) VALUES $+($chr(40),$m.sqlq(%target),$chr(44),$m.sqlq($calc(%data + $readini($m.stat.db,%target,%type))),$chr(44),NOW(),$chr(41))
          noop $mysql_query(%m.sql, %sql)
          dec %types
        }
        dec %targets
      }
      .remove $m.stat.db
    }
    ; statistic returning
    else if ($1) {
      var %start = $m.dbres(start,stats,target,$1)
      $iif(!%start,return $brackets2($+($lower($1),$chr(58))) no stats retrieved yet.)
      return $brackets2($+($lower($1),$chr(58))) lines: $m.stat.res(lines,$1) words: $m.stat.res(words,$1) symbols: $m.stat.res(symbols,$1) joins: $m.stat.res(joins,$1) parts: $m.stat.res(parts,$1) actions: $m.stat.res(actions,$1) notices: $m.stat.res(notices,$1) // statistics calculated since $m.dbres(start,stats,target,$1)-
    }
  }
}

; - actions
on *:JOIN:#:{ m.stat $chan joins 1 | m.stat $nick joins 1 }
on *:PART:#:{ m.stat $chan parts 1 | m.stat $nick parts 1 }
on *:TEXT:*:#:{
  if ($1 == $command(stats)) $iif($chan,notice,msg) $nick $m.stat($iif($2,$2,$nick))
  else if ($1 == $command(chanstats)) $iif($chan,notice,msg) $nick $m.stat($iif($2,$2,$chan))
  m.stat $chan lines 1 | m.stat $chan symbols $len($1-) | m.stat $chan words $numtok($1-,32)
  m.stat $nick lines 1 | m.stat $nick symbols $len($1-) | m.stat $nick words $numtok($1-,32)
}
on *:ACTION:*:#:{
  m.stat $chan lines 1 | m.stat $chan symbols $len($1-) | m.stat $chan words $numtok($1-,32)
  m.stat $nick lines 1 | m.stat $nick symbols $len($1-) | m.stat $nick words $numtok($1-,32)
  m.stat $chan actions 1 | m.stat $nick actions 1
}
on *:NOTICE:*:#:{
  m.stat $chan lines 1 | m.stat $chan symbols $len($1-) | m.stat $chan words $numtok($1-,32)
  m.stat $nick lines 1 | m.stat $nick symbols $len($1-) | m.stat $nick words $numtok($1-,32)
  m.stat $chan notices 1 | m.stat $nick notices 1
}

; - communicating with core
on *:SIGNAL:m.modules:{
  m.modules $+(Stats System,$chr(124),$command(stats),$chr(124),0)
  noop $m.stat().sync
}

Comments

Sign in to comment.
Frenetic   -  Sep 07, 2011

Good work, it's nice to see more "advanced" work, done in mSL.

 Respond  
Noutrious   -  Sep 06, 2011

Have many implementations on my mind, including RANK, TOP, more detailed stats (including ratio's) and more configuration possibilities (for example stats only for logged in users) - by requests I could fulfil that. :)

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.