Cronicle
Cronicle copied to clipboard
Autodetect cycled event chain
I was experimenting with event chaining and by accident had some jobs to chain each other causing endless loop of events. I noticed that only few hours later :) Luckily it was just test environment. I think that would be nice to inform user if his events are forming such loops.
It's not hard to implement using existing graph algorithm libraries. Below is the front end implementation using graphlib
reference on index.html:
<script src="https://dagrejs.github.io/project/graphlib/latest/graphlib.min.js"></script>
// init graph
var g = new graphlib.Graph();
event_map = {}; // id to name map
// somewhere in the loop iterating through events
g.setNode(item.id);
if(item.chain) g.setEdge(item.id, item.chain)
if(item.chain_error) g.setEdge(item.id, item.chain_error)
event_map[item.id] = item.title;
// after the loop
var cycleWarning = ''
var cycles = graphlib.alg.findCycles(g) // return array of arrays (or empty array)
if(cycles.length) {
cycleWarningTitle = '<b> ! Schedule contains cycled event chains:</b><br>'
cycles.forEach( (item, index) => cycleWarningTitle += (item.map((e)=>event_map[e]).join(" ← ")+'<br>'));
cycleWarning = `<span title="${cycleWarningTitle}"> ⚠️ </span>`
}