Jason R. Coombs avatar Jason R. Coombs committed 9b2d657

Now store original sqlite id for logs in the mongodb objectID

Comments (0)

Files changed (1)

pmxbot/logging.py

 import re
 import datetime
 import itertools
+import struct
 
 import pytz
 
 		parse_date(result)
 		return result
 
-	def all_messages(self):
-		query = 'SELECT datetime, nick, message, channel from logs'
+	def export_all(self):
+		query = 'SELECT id, datetime, nick, message, channel from logs'
 		def robust_text(text):
 			for encoding in 'utf-8', 'latin-1':
 				try:
 			raise
 		self.db.text_factory = robust_text
 		cursor = self.db.execute(query)
-		fields = 'datetime', 'nick', 'message', 'channel'
+		fields = 'id', 'datetime', 'nick', 'message', 'channel'
 		results = (dict(zip(fields, record)) for record in cursor)
 		return itertools.imap(parse_date, results)
 
-	export_all = all_messages
-
 def parse_date(record):
 	dt = record.pop('datetime')
 	fmts = [
 		# construct a unique objectid with the correct datetime.
 		dt = message['datetime']
 		oid_time = storage.pymongo.objectid.ObjectId.from_datetime(dt)
-		oid_rest = storage.pymongo.objectid.ObjectId()
-		oid_new = str(oid_time)[:8] + str(oid_rest)[8:]
+		# store the original sqlite object ID in the 
+		orig_id = message.pop('id')
+		orig_id_packed = struct.pack('L', orig_id)
+		oid_new = str(oid_time)[:4] + '\x00'*4 + orig_id_packed
 		oid = storage.pymongo.objectid.ObjectId(oid_new)
 		message['_id'] = oid
 		message['datetime'] = self._fmt_date(dt)
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.