Ada Young avatar Ada Young committed a8f7c52

We have basic searching!

Comments (0)

Files changed (1)

 Ref: https://bitbucket.org/adayoung/adacode/src/master/irelogs.py
 """
 
+import re
 import sys
 import json
 import urllib2
 
 		ProcessLogs(lognotes, orgid)
 
+def ValidateOrgname(orgname):
+	cursor.execute("SELECT orgname FROM orgs WHERE orgname LIKE ?", ("%" + orgname + "%", ))
+	orgnames = cursor.fetchall()
+	if len(orgnames) > 1:
+		print("Oops, multiple organizations match the supplied input:")
+		for i in orgnames:
+			print(i[0])
+	elif len(orgnames) < 1:
+		print("No matching organisations found :(")
+	else:
+		orgname = orgnames[0][0]
+		return orgname
+	return None
+
+def SearchLog(orgname, searchtext, fromdate=None, todate=None):
+	datenow = datetime.now().date()
+	if fromdate is None:
+		fromdate = datenow.strftime("%Y-%m-%d")
+	if todate is None:
+		todate = datenow.strftime("%Y-%m-%d")
+
+	orgname = ValidateOrgname(orgname)
+	if orgname is None: # bail out
+		exit(-1)
+
+	# validate date format
+	if not re.match("^\d{4}\-\d{2}\-\d{2}$", fromdate):
+		print("The supplied from date is invalid. The format is YYYY-MM-DD.")
+		exit(-1)
+	if not re.match("^\d{4}\-\d{2}\-\d{2}$", todate):
+		print("The supplied to date is invalid. The format is YYYY-MM-DD.")
+		exit(-1)
+
+	todate = todate + " 23:59:59" # just to include this day as well
+
+	print("Searching [%s] logs for [%s] in the range [%s] to [%s]" % (orgname, searchtext, fromdate, todate[:-9]))
+	cursor.execute("SELECT timestamp, message FROM org_logs l join orgs o on l.orgid=o.id WHERE o.orgname=? AND l.timestamp>=datetime(?) AND l.timestamp<=datetime(?) AND l.message LIKE ? ORDER BY l.timestamp ASC", (orgname, fromdate, todate, "%" + searchtext + "%"))
+	results = cursor.fetchall()
+	if len(results) > 0:
+		for i in results:
+			print("%s - %s" % (i[0], i[1]))
+		print("Total matches: %d" % len(results))
+	else:
+		print("Your query did not return any results.")
+
+	exit(0)
+
 def main():
 	# options parser be here
 	parser = optparse.OptionParser()
 	parser.add_option('-p', '--password', help='Your Achaea password')
 	parser.add_option('-d', '--days', help='Number of days to go back (max: 6)')
 	parser.add_option('-D', '--database', help="The database file")
+
+	parser.add_option('-S', '--search', help="Search through the database",
+		dest="searching", default=False, action="store_true")
+
+	parser.add_option('-o', '--orgname', help="Specify orgname to search through (required with -S)")
+	parser.add_option('-f', '--fromdate', help="Specify a 'from' date in the form YYYY-MM-DD")
+	parser.add_option('-t', '--todate', help="Specify a 'to' date in the form YYYY-MM-DD")
+
 	(opts, args) = parser.parse_args()
 
+	orgname = opts.orgname
+	fromdate = opts.fromdate
+	todate = opts.todate
+
 	if opts.database is None:
 		print("You need to specify a database file\n")
 		parser.print_help()
 		exit(-1)
-	elif opts.charname is None:
+
+	# init db if it isn't there yet
+	global cursor
+	global db
+
+	DBFile = opts.database
+	db = sqlite3.connect(DBFile)
+	cursor = db.cursor()
+	createdb()
+
+	if opts.searching:
+		if len(args) == 0:
+			print("You did not specify any search text\n")
+			parser.print_help()
+			exit(-1)
+		elif orgname is None:
+			print("You did not specify the orgname to search through\n")
+			parser.print_help()
+			exit(-1)
+		else:
+			searchtext = " ".join(args)
+			SearchLog(orgname, searchtext, fromdate, todate)
+
+	if opts.charname is None:
 		print("You need to specify a charname\n")
 		parser.print_help()
 		exit(-1)
 	password = opts.password
 	days = opts.days or 0
 
-	# init db if it isn't there yet
-	global cursor
-	global db
-
-	DBFile = opts.database
-	db = sqlite3.connect(DBFile)
-	cursor = db.cursor()
-	createdb()
-
 	dataset = orglogs_summary(charname, password, days)
 	dataset = process_orglogs(dataset)
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.