summaryrefslogtreecommitdiffstats
path: root/libxml2/doc/queries.py
blob: 0f019ec4bcb788fc7f1676974ca04c89be043267 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/python -u
#
# Processing of the queries results
#
import sys
import index
import time
import traceback
import string

if index.openMySQL(verbose = 0) < 0:
    print "Failed to connect to the MySQL database"
    sys.exit(1)

DB = index.DB

def getTopQueriesDB(base = "Queries", number = 50):
    global DB

    try:
        import os
	os.mkdir("searches")
    except:
        pass
    
    date = time.strftime("%Y%m%d")
    f = open("searches/%s-%s.xml" % (base, date), "w")
    c = DB.cursor()
    try:
        ret = c.execute("""select sum(Count) from %s""" % (base))
	row = c.fetchone()
	total = int(row[0])
        ret = c.execute("""select count(*) from %s""" % (base))
	row = c.fetchone()
	uniq = int(row[0])
        ret = c.execute(
           """select * from %s ORDER BY Count DESC LIMIT %d""" % (base, number))
	i = 0;
	f.write("<queries total='%d' uniq='%d' nr='%d' date='%s'>\n" % (
	      total, uniq, number, date))
	while i < ret:
	    row = c.fetchone()
	    f.write("  <query count='%d'>%s</query>\n" % (int(row[2]), row[1]))
	    i = i + 1
	f.write("</queries>\n")
    except:
        print "getTopQueries %s %d failed\n" % (base, number)
	print sys.exc_type, sys.exc_value
	return -1
    f.close()

def getTopQueries(number = 50):
    return getTopQueriesDB(base = "Queries", number = number)

def getAllTopQueries(number = 50):
    return getTopQueriesDB(base = "AllQueries", number = number)

def increaseTotalCount(Value, count):
    global DB

    c = DB.cursor()
    try:
        ret = c.execute("""select ID,Count from AllQueries where Value='%s'""" %
	                (Value))
	row = c.fetchone()
	id = row[0]
	cnt = int(row[1]) + count
	ret = c.execute("""UPDATE AllQueries SET Count = %d where ID = %d""" %
	                (cnt, id))
    except:
        ret = c.execute(
	"""INSERT INTO AllQueries (Value, Count) VALUES ('%s', %d)""" %
	                (Value, count))
    

def checkString(str):
    if string.find(str, "'") != -1 or \
       string.find(str, '"') != -1 or \
       string.find(str, "\\") != -1 or \
       string.find(str, " ") != -1 or \
       string.find(str, "\t") != -1 or \
       string.find(str, "\n") != -1 or \
       string.find(str, "\r") != -1:
        return 0
    return 1
def addCounts(frmtable):
    global DB

    i = 0
    c = DB.cursor()
    entries=[]
    try:
        ret = c.execute("""select Value,Count from %s""" % (frmtable))
	while i < ret:
	    i = i + 1
	    row = c.fetchone()
	    if checkString(row[0]):
		entries.append((row[0], int(row[1])))
	    else:
		entries.append((None, int(row[1])))
	
	for row in entries:
	    if row[0] != None:
		increaseTotalCount(row[0], row[1])
    except:
        print "addCounts %s failed" % (frmtable)
	print sys.exc_type, sys.exc_value
	traceback.print_exc(file=sys.stdout)
        
    try:
	c.execute("""DELETE from %s""" % (frmtable))
    except:
	pass


    
getTopQueries()
addCounts('Queries')
getAllTopQueries()