Changeset - 5633155c0300
[Not reviewed]
default
0 2 0
Drew Perttula - 6 years ago 2019-06-06 11:10:55
drewp@bigasterisk.com
sequencer doesn't hang so much, and it logs when you go from error->working
Ignore-this: 865392a34c76f0efaf18cb7f3d2c0c26
2 files changed with 73 insertions and 72 deletions:
0 comments (0 inline, 0 general)
light9/effect/sequencer.html
Show inline comments
 
@@ -19,25 +19,28 @@
 
    <link rel="stylesheet"  href="/style.css">
 
  </head>
 
  <body>
 

	
 
    <dom-module id="light9-sequencer-ui">
 
      <template>
 
        <style>
 
         :host {
 
             display: block;
 
         }
 
         td {
 
             white-space: nowrap;
 
             padding: 0 20px;
 
             padding: 0 10px;
 
             vertical-align: top;
 
             vertical-align: top;
 
             text-align: start;
 
         }
 
         tr.active { background: #151515; }
 
         .inactive > * { opacity: .5; }
 
         .effectSetting {
 
             display: inline-block;
 
             background: #1b1e21;
 
             margin: 1px 3px;
 
         }
 
         .chart {
 
             height: 40px;
 
             background: #222;
 
             display: inline-flex;
 
@@ -47,34 +50,24 @@
 
             background: #a4a54f;
 
             width: 8px;
 
             margin: 0 1px;
 
         }
 
         .number {
 
             display: inline-block;
 
             min-width: 4em;
 
         }
 
        </style>
 
        <rdfdb-synced-graph graph="{{graph}}"></rdfdb-synced-graph>
 

	
 
        <h1>Sequencer <a href="stats/">[stats]</a></h1>
 

	
 
        <p>fps={{report.recentFps}}</p>
 
        
 
        <p>Recent frame times (best to worst): 
 
        <div class="chart">
 
         <template is="dom-repeat" items="{{report.recentDeltasStyle}}">
 
           <div style$="{{item}}"></div>
 
         </template>
 
        </div>
 
        </p>
 
        
 
        <h2>Song</h2>
 

	
 
        <resource-display graph="{{graph}}" uri="{{report.song}}"></resource-display>
 
        t={{report.roundT}}
 
        
 
        <h3>Notes</h3>
 

	
 
        <table>
 
          <tr>
 
            <th>Note</th>
 
            <th>Effect class</th>
 
@@ -83,28 +76,30 @@
 
          </tr>
 
          <template is="dom-repeat" items="{{report.songNotes}}">
 

	
 
            <tr class$="{{item.rowClass}}">
 
              <td>
 
                <resource-display graph="{{graph}}" uri="{{item.note}}"></resource-display>
 
              </td>
 
              <td>
 
                <resource-display graph="{{graph}}" uri="{{item.effectClass}}"></resource-display>
 
              </td>  
 
              <td>
 
                <template is="dom-repeat" items="{{item.effectSettingsPairs}}">
 
                  <div>
 
                  <span class="effectSetting">
 
                    <resource-display graph="{{graph}}" uri="{{item.effectAttr}}"></resource-display>:
 
                    <span class="number">{{item.value}}</span>
 
                  </span>
 
                  </div>
 
                </template>
 
              </td>
 
              <td>
 
                {{item.devicesAffected}}
 
              </td>
 
            </tr>
 
          </template>
 
        </table>
 

	
 
      </template>
 
      <script>
 
       HTMLImports.whenReady(function () {
light9/effect/sequencer.py
Show inline comments
 
'''
 
copies from effectloop.py, which this should replace
 
'''
 

	
 
from louie import dispatcher
 
from rdflib import URIRef
 
from twisted.internet import reactor
 
from twisted.internet import defer
 
from twisted.internet.defer import Deferred, inlineCallbacks
 
from twisted.internet.inotify import INotify
 
from twisted.python.filepath import FilePath
 
import cyclone.sse
 
import logging, bisect, time
 
import traceback
 
from typing import Any, Callable, Dict, List, Tuple, cast, Union
 

	
 
from light9.ascoltami.musictime_client import MusicTime
 
from light9.effect import effecteval
 
from light9.effect.settings import DeviceSettings
 
from light9.effect.simple_outputs import SimpleOutputs
 
from light9.namespaces import L9, RDF
 
@@ -23,27 +24,26 @@ from rdfdb.syncedgraph import SyncedGrap
 
from standardservice.scalessetup import gatherProcessStats
 

	
 
from greplin import scales
 
import imp
 

	
 
log = logging.getLogger('sequencer')
 

	
 
gatherProcessStats()
 
updateStats = scales.collection(
 
    '/update/',
 
    scales.PmfStat('s0_getMusic', recalcPeriod=1),
 
    scales.PmfStat('s1_eval', recalcPeriod=1),
 
    scales.PmfStat('s2_sendToWeb', recalcPeriod=1),
 
    #scales.PmfStat('s3_send_client', recalcPeriod=1),
 
    scales.PmfStat('s3_send', recalcPeriod=1),
 
    scales.PmfStat('sendPhase', recalcPeriod=1),
 
    scales.PmfStat('updateLoopLatency', recalcPeriod=1),
 
    scales.DoubleStat('updateLoopLatencyGoal'),
 
    scales.RecentFpsStat('updateFps'),
 
    scales.DoubleStat('goalFps'),
 
)
 
compileStats = scales.collection(
 
    '/compile/',
 
    scales.PmfStat('graph', recalcPeriod=1),
 
    scales.PmfStat('song', recalcPeriod=1),
 
)
 

	
 

	
 
@@ -151,124 +151,130 @@ class CodeWatcher(object):
 
            log.info("reload effecteval")
 
            imp.reload(effecteval)
 
            self.onChange()
 

	
 
        # in case we got an event at the start of the write
 
        reactor.callLater(.1, go)
 

	
 

	
 
class Sequencer(object):
 

	
 
    def __init__(self,
 
                 graph: SyncedGraph,
 
                 sendToCollector: Callable[[DeviceSettings], defer.Deferred],
 
                 sendToCollector: Callable[[DeviceSettings], Deferred],
 
                 fps=40):
 
        self.graph = graph
 
        self.fps = fps
 
        updateStats.goalFps = self.fps
 
        updateStats.updateLoopLatencyGoal = 1 / self.fps
 
        self.sendToCollector = sendToCollector
 
        self.music = MusicTime(period=.2, pollCurvecalc=False)
 

	
 
        self.recentUpdateTimes: List[float] = []
 
        self.lastStatLog = 0.0
 
        self._compileGraphCall = None
 
        self.notes: Dict[Song, List[Note]] = {}  # song: [notes]
 
        self.simpleOutputs = SimpleOutputs(self.graph)
 
        self.graph.addHandler(self.compileGraph)
 
        self.lastLoopSucceeded = False
 

	
 
        self.codeWatcher = CodeWatcher(onChange=self.onCodeChange)
 
        self.updateLoop()
 

	
 
        self.codeWatcher = CodeWatcher(
 
            onChange=lambda: self.graph.addHandler(self.compileGraph))
 

	
 
    def onCodeChange(self):
 
        log.debug('seq.onCodeChange')
 
        self.graph.addHandler(self.compileGraph)
 
        #self.updateLoop()
 
        
 
    @compileStats.graph.time()
 
    def compileGraph(self) -> None:
 
        """rebuild our data from the graph"""
 
        for song in self.graph.subjects(RDF.type, L9['Song']):
 

	
 
            def compileSong(song: Song = cast(Song, song)) -> None:
 
                self.compileSong(song)
 

	
 
            self.graph.addHandler(compileSong)
 

	
 
    @compileStats.song.time()
 
    def compileSong(self, song: Song) -> None:
 
        anyErrors = False
 
        self.notes[song] = []
 
        for note in self.graph.objects(song, L9['note']):
 
            self.notes[song].append(
 
                Note(self.graph, NoteUri(note), effecteval, self.simpleOutputs))
 
            try:
 
                n = Note(self.graph, NoteUri(note), effecteval, self.simpleOutputs)
 
            except Exception:
 
                log.warn(f"failed to build Note {note} - skipping")
 
                anyErrors = True
 
                continue
 
            self.notes[song].append(n)
 
        if not anyErrors:
 
            log.info('built all notes')
 

	
 
    @inlineCallbacks
 
    def updateLoop(self) -> None:
 
        frameStart = time.time()
 

	
 
        d = self.update()
 
        sendStarted = time.time()
 

	
 
        def done(sec: float):
 
        try:
 
            sec = yield self.update()
 
        except Exception as e:
 
            self.lastLoopSucceeded = False
 
            traceback.print_exc()
 
            log.warn('updateLoop: %r', e)
 
            reactor.callLater(1, self.updateLoop)
 
        else:
 
            took = time.time() - frameStart
 
            delay = max(0, 1 / self.fps - took)
 
            updateStats.updateLoopLatency = took
 

	
 
            # time to send to collector, reported by collector_client
 
            if isinstance(
 
                    sec,
 
                    float):  # sometimes None, not sure why, and neither is mypy
 
                updateStats.s3_send = sec
 

	
 
            # time to send to collector, measured in this function,
 
            # from after sendToCollector returned its deferred until
 
            # when the deferred was called.
 
            updateStats.sendPhase = time.time() - sendStarted
 
            if not self.lastLoopSucceeded:
 
                log.info('Sequencer.update is working')
 
                self.lastLoopSucceeded = True
 
        
 
            delay = max(0, 1 / self.fps - took)
 
            reactor.callLater(delay, self.updateLoop)
 

	
 
        def err(e):
 
            log.warn('updateLoop: %r', e)
 
            reactor.callLater(2, self.updateLoop)
 

	
 
        d.addCallbacks(done, err)
 
    @updateStats.updateFps.rate()
 
    @inlineCallbacks
 
    def update(self) -> Deferred:
 
        with updateStats.s0_getMusic.time():
 
            musicState = self.music.getLatest()
 
            if not musicState.get('song') or not isinstance(
 
                    musicState.get('t'), float):
 
                return defer.succeed(0.0)
 
            song = Song(URIRef(musicState['song']))
 
            dispatcher.send('state',
 
                            update={
 
                                'song': str(song),
 
                                't': musicState['t']
 
                            })
 

	
 
    @updateStats.updateFps.rate()
 
    def update(self) -> defer.Deferred:
 
        try:
 
            with updateStats.s0_getMusic.time():
 
                musicState = self.music.getLatest()
 
                if not musicState.get('song') or not isinstance(
 
                        musicState.get('t'), float):
 
                    return defer.succeed(0.0)
 
                song = Song(URIRef(musicState['song']))
 
                dispatcher.send('state',
 
                                update={
 
                                    'song': str(song),
 
                                    't': musicState['t']
 
                                })
 
        with updateStats.s1_eval.time():
 
            settings = []
 
            songNotes = sorted(self.notes.get(song, []),
 
                               key=lambda n: n.uri)
 
            noteReports = []
 
            for note in songNotes:
 
                s, report = note.outputSettings(musicState['t'])
 
                noteReports.append(report)
 
                settings.append(s)
 
            devSettings = DeviceSettings.fromList(self.graph, settings)
 

	
 
            with updateStats.s1_eval.time():
 
                settings = []
 
                songNotes = sorted(self.notes.get(song, []),
 
                                   key=lambda n: n.uri)
 
                noteReports = []
 
                for note in songNotes:
 
                    s, report = note.outputSettings(musicState['t'])
 
                    noteReports.append(report)
 
                    settings.append(s)
 
                devSettings = DeviceSettings.fromList(self.graph, settings)
 
        dispatcher.send('state', update={'songNotes': noteReports})
 

	
 
        with updateStats.s3_send.time(): # our measurement
 
            sendSecs = yield self.sendToCollector(devSettings)
 

	
 
            with updateStats.s2_sendToWeb.time():
 
                dispatcher.send('state', update={'songNotes': noteReports})
 

	
 
            return self.sendToCollector(devSettings)
 
        except Exception:
 
            traceback.print_exc()
 
            raise
 

	
 
        # sendToCollector's own measurement.
 
        # (sometimes it's None, not sure why, and neither is mypy)
 
        #if isinstance(sendSecs, float):
 
        #    updateStats.s3_send_client = sendSecs
 

	
 
class Updates(cyclone.sse.SSEHandler):
 

	
 
    def __init__(self, application, request, **kwargs) -> None:
 
        cyclone.sse.SSEHandler.__init__(self, application, request, **kwargs)
 
        self.state: Dict = {}
 
        dispatcher.connect(self.updateState, 'state')
 
        self.numConnected = 0
 

	
 
    def updateState(self, update: Dict):
 
        self.state.update(update)
 

	
0 comments (0 inline, 0 general)