webplot: add raw events in snapshot

Since the "remote" package has kept the event raw data in a snapshot,
webplot should be able to record the raw events in a data file for
further examination.

BUG=chromium:476458
TEST=After running and terminating the webplot, check the raw events
in /tmp/webplot.dat

Change-Id: Ibe8dfd6a9c88af2d2a0f019fbde9a9e04eebb28c
Reviewed-on: https://chromium-review.googlesource.com/265365
Reviewed-by: Charlie Mooney <charliemooney@chromium.org>
Commit-Queue: Shyh-In Hwang <josephsih@chromium.org>
Tested-by: Shyh-In Hwang <josephsih@chromium.org>
diff --git a/webplot/webplot.py b/webplot/webplot.py
index 4bba3b2..2812e6c 100755
--- a/webplot/webplot.py
+++ b/webplot/webplot.py
@@ -386,6 +386,10 @@
       converted['fingers'].append(leaving_finger)
     self._prev_tids = curr_tids
 
+    # Convert raw events from a list of classes to a list of its strings
+    # so that the raw_events is serializable.
+    converted['raw_events'] = [str(event) for event in converted['raw_events']]
+
     return converted
 
   def GetSnapshot(self):
@@ -396,6 +400,7 @@
     """Convert the snapshot to a proper format and publish it to clients."""
     snapshot = self._ConvertNamedtupleToDict(snapshot)
     cherrypy.engine.publish('websocket-broadcast', json.dumps(snapshot))
+    return snapshot
 
   def GetAndPlotSnapshots(self):
     """Get and plot snapshots."""
@@ -407,11 +412,9 @@
           if not snapshot:
             cherrypy.log('webplot is terminated.')
             break
-          # TODO: get the raw events from the sanpshot
-          events = []
-          f.write('\n'.join(events) + '\n')
+          converted_snapshot = self.AddSnapshot(snapshot)
+          f.write('\n'.join(converted_snapshot['raw_events']) + '\n')
           f.flush()
-          self.AddSnapshot(snapshot)
         except KeyboardInterrupt:
           cherrypy.log('Keyboard Interrupt accepted')
           cherrypy.log('webplot is being terminated...')