Skip to content

Commit c37a5df

Browse files
committed
Added DB stats to the admin, minor code cleanup
1 parent f0f91e7 commit c37a5df

File tree

9 files changed

+182
-25
lines changed

9 files changed

+182
-25
lines changed

admin/db_stats.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
//-----------------------------------------------------------------------------------------------
3+
//My Program-O Version: 2.3.1
4+
//Program-O chatbot admin area
5+
//Written by Elizabeth Perreau and Dave Morton
6+
//Aug 2011
7+
//for more information and support please visit www.program-o.com
8+
//-----------------------------------------------------------------------------------------------
9+
// demochat.php
10+
$upperScripts = '';
11+
$topNav = $template->getSection('TopNav');
12+
$leftNav = $template->getSection('LeftNav');
13+
$main = $template->getSection('Main');
14+
$topNavLinks = makeLinks('top', $topLinks, 12);
15+
$navHeader = $template->getSection('NavHeader');
16+
$leftNavLinks = makeLinks('left', $leftLinks, 12);
17+
$FooterInfo = getFooter();
18+
$errMsgClass = (!empty($msg)) ? "ShowError" : "HideError";
19+
$errMsgStyle = $template->getSection($errMsgClass);
20+
$noLeftNav = '';
21+
$noTopNav = '';
22+
$noRightNav = $template->getSection('NoRightNav');
23+
$headerTitle = 'Actions:';
24+
$pageTitle = 'My-Program O - Database Statistics';
25+
$mainContent = "Database stats for $dbn";
26+
$mainContent = showStats();
27+
$mainTitle = 'DB Stats';
28+
29+
function showStats()
30+
{
31+
global $template, $dbn;
32+
$dbConn = db_open();
33+
$stats = mysql_stat($dbConn);
34+
list($ut, $th, $q, $sq, $o, $ft, $ot, $qps) = explode(' ', $stats);
35+
$out = $template->getSection('DbStats');
36+
list($nul, $utd) = explode(': ', $ut);
37+
list($nul, $thd) = explode(': ', $th);
38+
list($nul, $qd) = explode(': ', $q);
39+
list($nul, $sqd) = explode(': ', $sq);
40+
list($nul, $ftd) = explode(': ', $ft);
41+
list($nul, $otd) = explode(': ', $ot);
42+
list($nul, $qpsd) = explode(': ', $qps);
43+
44+
$out = str_replace('[stats_uptime]', seconds2YDHMS($utd), $out);
45+
$out = str_replace('[stats_threads]', $thd, $out);
46+
$out = str_replace('[stats_query_count]', number_format($qd), $out);
47+
$out = str_replace('[stats_slow_queries]', $sqd, $out);
48+
$out = str_replace('[stats_flush_count]', $ftd, $out);
49+
$out = str_replace('[stats_tables_open]', $otd, $out);
50+
$out = str_replace('[stats_qps]', 0+$qpsd, $out);
51+
$out = str_replace('[dbn]', $dbn, $out);
52+
$out .= <<<endComment
53+
54+
<!-- Raw Stats: $stats -->
55+
endComment;
56+
return $out;
57+
}
58+
59+
function seconds2YDHMS($seconds)
60+
{
61+
$secPerMin = 60;
62+
$secPerHour = 3600;
63+
$secPerDay = 86400;
64+
$secPerYear = 31557600; //Aproxomate - we're not splitting atoms, here. :)
65+
$years = (int)floor($seconds / $secPerYear);
66+
$seconds -= ($years * $secPerYear);
67+
$days = floor($seconds / $secPerDay);
68+
$seconds -= ($days * $secPerDay);
69+
$hours = floor($seconds / $secPerHour);
70+
$seconds -= ($hours * $secPerHour);
71+
$minutes = floor($seconds / $secPerMin);
72+
$seconds -= ($minutes * $secPerMin);
73+
$out = "$years years, $days days, $hours hours, $minutes minutes, $seconds seconds";
74+
return $out;
75+
}
76+
77+
?>

admin/default.page.htm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,18 @@ <h3>Help...</h3>
623623
<div id="demoChatTitle">Talk to [bot_name] using the [format] interface:</div>
624624
<div id="demoChatContainer"><iframe id="demoChat" src="[pageSource]"></iframe></div>
625625
<!-- Section ChatDemo End -->
626+
<!-- Section DbStats Start -->
627+
[blank] <div id="dbStatsTitle">Database Stats for [dbn]</div>
628+
<div id="dbStatsContainer">
629+
<p>Uptime: [stats_uptime]</p>
630+
<p>Threads: [stats_threads]</p>
631+
<p>Queries: [stats_query_count]</p>
632+
<p>Slow queries: [stats_slow_queries]</p>
633+
<p>Flush table count: [stats_flush_count]</p>
634+
<p>Tables Opened: [stats_tables_open]</p>
635+
<p>Queries per second avg: [stats_qps]</p>
636+
</div>
637+
<!-- Section DbStats End -->
626638
<!-- Section MembersMain Start -->
627639
[blank] <div id="membersForm" class="fullWidth noBorder">
628640
<p>

admin/index.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,14 @@ function makeLeftLinks() {
437437
'[linkTitle]' => ' title="Toggle the Logo"',
438438
'[linkLabel]' => 'Toggle the Logo'
439439
),
440+
array(
441+
'[linkClass]' => '',
442+
'[linkHref]' => ' href="index.php?page=db_stats"',
443+
'[linkOnclick]' => '',
444+
'[linkAlt]' => ' alt="DB Stats"',
445+
'[linkTitle]' => ' title="DB Stats"',
446+
'[linkLabel]' => 'DB Stats'
447+
),
440448
array(
441449
'[linkClass]' => '',
442450
'[linkHref]' => ' href="' . _BASE_URL_ . '"',

chatbot/conversation_start.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@
174174
#if ($display == '') $display = $convoArr['send_to_user'];
175175
runDebug(__FILE__, __FUNCTION__, __LINE__, "Closing Database", 2);
176176
db_close($con);
177-
#echo $display;
178-
//save_file(_DEBUG_PATH_ . 'function_list.txt', print_r(get_defined_functions(), true));
177+
//save_file(_DEBUG_PATH_ . 'function_list.txt', print_r(get_defined_functions()['user'], true));
179178
$convoArr = handleDebug($convoArr); // Make sure this is the last line in the file, so that all debug entries are captured.
180-
if (isset($convoArr['conversation']['format'])) display_conversation($display, $convoArr['conversation']['format']);
179+
display_conversation($display, $convoArr['conversation']['format']);

chatbot/core/aiml/find_aiml.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,8 @@ function check_and_add_unknown_inputs($allrows,$convoArr){
742742
VALUES
743743
(NULL, '".mysql_real_escape_string($pattern)."','$bot_id','$u_id',NOW())";
744744
runDebug(__FILE__, __FUNCTION__, __LINE__, "Unknown Input SQL: $sql", 3);
745-
$res = mysql_query($sql, $con);
745+
$result = db_query($sql, $con);
746+
$numRows = mysql_affected_rows($result);
746747
}
747748
}
748749

chatbot/core/aiml/parse_aiml.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,8 @@ function make_learn($convoArr, $pattern, $template)
466466
VALUES
467467
(NULL, '$aiml','$pattern','$template','$u_id','$bot_id',NOW())";
468468
runDebug(__FILE__, __FUNCTION__, __LINE__, "Make learn SQL: $sql", 3);
469-
$res = mysql_query($sql, $con);
469+
$result = db_query($sql, $con);
470+
$numRows = mysql_affected_rows($result);
470471
}
471472

472473
/**

chatbot/core/aiml/parse_aiml_as_XML.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ function parse_set_tag($convoArr, $element, $parentName, $level)
336336
runDebug(__FILE__, __FUNCTION__, __LINE__, "Checking the client_properties table for the value of $var_name. - SQL:\n$sql", 3);
337337
$result = db_query($sql, $con) or trigger_error('Error looking up DB info in ' . __FILE__ . ', function ' . __FUNCTION__ . ', line ' . __LINE__ . ' - Error message: ' . mysql_error());
338338
$rowCount = mysql_num_rows($result);
339+
$var_name = mysql_real_escape_string($var_name, $con);
340+
$var_value = mysql_real_escape_string($var_value, $con);
339341
if ($rowCount == 0)
340342
{
341343
$sql = "insert into `$dbn`.`client_properties` (`id`, `user_id`, `bot_id`, `name`, `value`)
@@ -673,10 +675,11 @@ function parse_learn_tag($convoArr, $element, $parentName, $level)
673675
$pattern = (IS_MB_ENABLED) ? mb_strtoupper($pattern) : strtoupper($pattern);
674676
$thatpattern = (string)$category->that;
675677
$template = $category->template->asXML();
678+
$template = substr($template, 10);
679+
$tplLen = strlen($template);
680+
$template = substr($template,0,$tplLen - 11);
676681
$template = str_replace('<text>', '', $template);
677682
$template = str_replace('</text>', '', $template);
678-
$template = str_replace('<template>', '', $template);
679-
$template = str_replace('</template>', '', $template);
680683
$template_eval = $category->template->eval;
681684
if (!empty($template_eval))
682685
{
@@ -688,10 +691,10 @@ function parse_learn_tag($convoArr, $element, $parentName, $level)
688691
$catXML->addChild('template', $template);
689692
$category = $catXML->asXML();
690693
$category = trim(str_replace('<?xml version="1.0"?>', '', $category));
691-
$sqlAdd = str_replace('[aiml]', $category, $sqlTemplate);
692-
$sqlAdd = str_replace('[pattern]', $pattern, $sqlAdd);
693-
$sqlAdd = str_replace('[that]', $thatpattern, $sqlAdd);
694-
$sqlAdd = str_replace('[template]', $template, $sqlAdd);
694+
$sqlAdd = str_replace('[aiml]', mysql_real_escape_string($category, $con), $sqlTemplate);
695+
$sqlAdd = str_replace('[pattern]', mysql_real_escape_string($pattern, $con), $sqlAdd);
696+
$sqlAdd = str_replace('[that]', mysql_real_escape_string($thatpattern, $con), $sqlAdd);
697+
$sqlAdd = str_replace('[template]', mysql_real_escape_string($template, $con), $sqlAdd);
695698
$sql .= $sqlAdd;
696699
$result = db_query($sql, $con) or trigger_error('Looks like we have a problem adding stuff to the aiml_userdefined table. Error: ' . mysql_error());
697700
}
@@ -755,7 +758,7 @@ function parse_eval_tag($convoArr, $element, $parentName, $level)
755758
}
756759

757760
/*
758-
* function parse_tostring_tag
761+
* function tag_to_string
759762
* Converts the contents of the AIML tag to a string.
760763
* @param (array) $convoArr
761764
* @param (SimpleXMLelement) $element

chatbot/core/conversation/display_conversation.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,17 +188,20 @@ function get_xml($convoArr, $conversation)
188188
* @param (array) $convoArr
189189
* @return (void) [return value]
190190
**/
191-
function display_conversation(& $display, $format)
191+
function display_conversation($convoArr)
192192
{
193-
$format = trim(strtolower($format));
193+
$format =(isset($convoArr['conversation']['format'])) ? strtolower(trim($convoArr['conversation']['format'])) : 'html';
194194
switch ($format) {
195195
case 'html' :
196196
$display = str_ireplace('<![CDATA[', '', $display);
197197
$display = str_replace(']]>', '', $display);
198198
break;
199199
case 'xml' :
200200
header("Content-type: text/xml; charset=utf-8");
201+
echo $display;
202+
break;
201203
case 'json' :
204+
header("Content-type: text/plain; charset=utf-8");
202205
echo $display;
203206
break;
204207
default :

library/reference/bot_test.aiml

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
<?xml version="1.0" encoding="UTF-8"?>
32
<!-- Copyright ©2013 Dave Morton -->
43
<!-- Permission is hereby granted, free of charge, to any person -->
@@ -26,31 +25,65 @@
2625
<aiml version="1.0">
2726
<category>
2827
<pattern>AIML TEST SENTENCE *</pattern>
29-
<template>Only the first letter of the following should be capitalized: "<sentence><star/></sentence>"</template>
28+
<template>Only the first letter of the following should be capitalized:
29+
"<sentence><star/></sentence>"
30+
</template>
3031
</category>
3132
<category>
3233
<pattern>AIML TEST FORMAL *</pattern>
33-
<template>Every word should be capitalized here: "<formal><star/></formal>"</template>
34+
<template>Every word should be capitalized here:
35+
"<formal><star/></formal>"
36+
</template>
3437
</category>
3538
<category>
3639
<pattern>AIML TEST SRAI *</pattern>
37-
<template>Running SRAI test on <star/>: <br/><srai><star/></srai></template>
40+
<template>Running SRAI test on <star/>: <br/>
41+
<srai><star/></srai>
42+
</template>
3843
</category>
3944
<category>
4045
<pattern>AIML TEST BOT PROPERTY *</pattern>
4146
<template>The bot property <star/> is <bot name="*"/>.</template>
4247
</category>
4348
<category>
4449
<pattern>AIML TEST UPPERCASE *</pattern>
45-
<template>The following should all be in capital letters: "<uppercase><star/></uppercase>"</template>
50+
<template>The following should all be in capital letters:
51+
"<uppercase><star/></uppercase>"
52+
</template>
4653
</category>
4754
<category>
4855
<pattern>AIML TEST HTML</pattern>
49-
<template>Testing HTML tag parsing:<br/>Link tag: <a href="http://www.geekcavecreations.com" target="_blank">Geek Cave Creations</a><br/><b>BOLD</b><br/><i>italic</i><br/><s>strikethrough</s><br/><u>underline</u><sub>subscript</sub><br/><sup>superscript</sup><br/><big>BIG</big><br/><strong>STRONG</strong><small>small</small><br/>Unordered list:<br/><ul><li>Item 1</li><li>Item 2</li><li>Item 3</li></ul><br/>Ordered list:<br/><ol><li>Item 1</li><li>Item 2</li><li>Item 3</li></ol><br/>Test complete.</template>
56+
<template>Testing HTML tag parsing:<br/>
57+
Link tag: <a href="http://www.geekcavecreations.com" target="_blank">Geek Cave Creations</a><br/>
58+
<b>BOLD</b><br/>
59+
<i>italic</i><br/>
60+
<s>strikethrough</s><br/>
61+
<u>underline</u><br/>
62+
<sub>subscript</sub><br/>
63+
<sup>superscript</sup><br/>
64+
<big>BIG</big><br/>
65+
<strong>STRONG</strong><br/>
66+
<small>small</small><br/>
67+
Unordered list:<br/>
68+
<ul>
69+
<li>Item 1</li>
70+
<li>Item 2</li>
71+
<li>Item 3</li>
72+
</ul><br/>
73+
Ordered list:<br/>
74+
<ol>
75+
<li>Item 1</li>
76+
<li>Item 2</li>
77+
<li>Item 3</li>
78+
</ol><br/>
79+
Test complete.
80+
</template>
5081
</category>
5182
<category>
5283
<pattern>AIML TEST SR *</pattern>
53-
<template>Running SR test on <star/>: <br/><sr/></template>
84+
<template>Running SR test on <star/>:<br/>
85+
<sr/>
86+
</template>
5487
</category>
5588
<category>
5689
<pattern>AIML TEST ID</pattern>
@@ -64,18 +97,38 @@
6497
</category>
6598
<category>
6699
<pattern>AIML TEST CONDITION *</pattern>
67-
<template><b>Testing Condition tag with NAME and VALUE attributes:</b><br/><condition name="gender" value="female"> attractive.</condition><condition name="gender" value="male"> handsome.</condition><br/><b>Testing Condition tag with only NAME attribute:</b><br/><condition name="gender"><li value="female"> attractive.</li><li value="male"> handsome.</li></condition><br/><b>Testing Condition tag with no attributes:</b><br/><condition><li name="gender" value="female"> attractive.</li><li name="gender" value="male"> handsome.</li></condition><br/>Testing complete.</template>
100+
<template>
101+
<b>Testing Condition tag with NAME and VALUE attributes:</b><br/>
102+
<condition name="gender" value="female"> attractive.</condition>
103+
<condition name="gender" value="male"> handsome.</condition><br/>
104+
<b>Testing Condition tag with only NAME attribute:</b><br/>
105+
<condition name="gender">
106+
<li value="female"> attractive.</li>
107+
<li value="male"> handsome.</li>
108+
</condition><br/>
109+
<b>Testing Condition tag with no attributes:</b><br/>
110+
<condition>
111+
<li name="gender" value="female"> attractive.</li>
112+
<li name="gender" value="male"> handsome.</li>
113+
</condition><br/>
114+
Testing complete.
115+
</template>
68116
</category>
69117
<category>
70118
<pattern> AIML TEST SET * TO *</pattern>
71-
<template><think><set name="*"><star index="2"/></set></think>The value of <star/> has been set to <star index="2"/>.</template>
119+
<template>
120+
<think><set name="*"><star index="2"/></set></think>
121+
The value of <star/> has been set to <star index="2"/>.
122+
</template>
72123
</category>
73124
<category>
74125
<pattern> AIML TEST GET *</pattern>
75126
<template>The value of <star/> is <get name="*"/>.</template>
76127
</category>
77128
<category>
78129
<pattern>AIML TEST LOWERCASE *</pattern>
79-
<template>The following should all be in lower case letters: "<lowercase><star/></lowercase>"</template>
130+
<template>The following should all be in lower case letters:
131+
"<lowercase><star/></lowercase>"
132+
</template>
80133
</category>
81-
</aiml>
134+
</aiml>

0 commit comments

Comments
 (0)