Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <body>
    <div class="heading">
      <div>
      What is this thing? The info/details 
        <a href="http://www.panurgy.net/2021/02/accumulating-and-graphing-fm-station.html" target="_blank">
        are in this blog post</a>.
        <hr/>
      </div>

        So like, what's been playing on 
        <select id="stationSelector">
            <option value="kqrs">KQRS - KQ92</option>
            <!-- option value="KXXR" >KXXR - 93X</option -->
            <option value="ktis">KTIS - 98.5</option>
            <option value="kdwb">KDWB - 101.3</option>
            <option value="kzjk">KZJK - Jack 104.1</option>
            <option value="kqql">KQQL - KOOL 108</option>
        </select><br>
        for the last 
        <button id="makeItSo2">Two days</button>
        <!-- or
        <button id="makeItSo5">Five days</button -->
      

      <div id="statusMessage"></div>
      </div>
  </div>
  <div id="graph"></div>
    
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>

<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>

<script src="http://d3js.org/d3.v3.min.js"></script>

<script>

var fetchData = async function(station, days) {
    $('#statusMessage').html('Loading ' + days + ' days of data for ' + station.toUpperCase() +'...');
    try {
      const flareData = await doStuff(station, days);
      // CodePen doesn't let us change the location's search
      // location.search = 's=' + station;
      showFlareData(flareData);
      d3.select(self.frameElement).style("height", height + "px");
    } catch (e) {
      $('#statusMessage').html(`<div class="error">${e.message}</div>`);
    }
};

$(function() {
  
    $('#makeItSo2').click(function() {
        var callsign = $('#stationSelector').val();
        fetchData(callsign, 2);
    });

    $('#makeItSo5').click(function() {
        var callsign = $('#stationSelector').val();
        fetchData(callsign, 5);
    });

    const urlParams = new URLSearchParams(window.location.search);
    const station = urlParams.get('s') || 'kzjk';
  
    $(`#stationSelector option[value="${station}"]`).prop('selected', true);
    fetchData(station, 2);
});

var width = 700;
    height = 8000;

var cluster = d3.layout.cluster()
    .size([height, width - 100]);

var diagonal = d3.svg.diagonal()
    .projection(function(d) { return [d.y, d.x]; });

var showFlareData = function(flareData) {
    
    // Use jQuery to perform a crude and heavy handed clearing of the DOM
    $('#graph').html('');

  var svg = d3.select("#graph").append("svg")
      
      .attr("width", width)
      .attr("height", height)
      .append("g")
      .attr("transform", "translate(40,0)");

  var nodes = cluster.nodes(flareData),
      links = cluster.links(nodes);

  var link = svg.selectAll(".link")
      .data(links)
      .enter().append("path")
      .attr("class", "link")
      .attr("d", diagonal)
      .attr("width", 100)

  var node = svg.selectAll(".node")
      .data(nodes)
      .enter().append("g")
      .attr("class", "node")
      .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; })

  node.append("circle")
      .attr("r", 4.5);

  node.append("text")
      .attr("dx", -8)
      //.attr("dx", function(d) { return d.children ? -8 : 8; })
      .attr("dy", 3)
//      .style("text-anchor", function(d) { return d.children ? "end" : "start"; })
      .style("text-anchor", "end")
      .text(function(d) { return d.name; });
    $('#statusMessage').html('');
};


</script>

              
            
!

CSS

              
                
body {
    font-family: arial, sans-serif;
}

.node circle {
  fill: #fff;
  stroke: steelblue;
  stroke-width: 1.5px;
}

.node {
  font: 10px sans-serif;
}

.link {
  fill: none;
  stroke: #ccc;
  stroke-width: 1.5px;
}

.heading {
    font-size: 1.2em;
    margin-bottom: 40px;
    text-align: center;
}

#statusMessage {
    text-align: center;
}

#graph {
    margin-left: 20px;
}

.disclaimer {
    text-align: center;
    font-size: .8em;
    color: #aaa;
    visibility: hidden;
}

.error {
  color: red;
}
              
            
!

JS

              
                // https://stackoverflow.com/questions/62246410/how-to-convert-a-firestore-document-to-plain-json-and-vice-versa

const documentToJson = (fields) => {
  let result = {};
  for (let f in fields) {
    let key = f, value = fields[f],
        isDocumentType = ['stringValue', 'booleanValue', 'doubleValue',
                          'integerValue', 'timestampValue', 'mapValue', 'arrayValue'].find(t => t === key);
    if (isDocumentType) {
      let item = ['stringValue', 'booleanValue', 'doubleValue', 'integerValue', 'timestampValue']
      .find(t => t === key)
      if (item)
        return value;
      else if ('mapValue' == key)
        return documentToJson(value.fields || {});
      else if ('arrayValue' == key) {
        let list = value.values;
        return !!list ? list.map(l => documentToJson(l)) : [];
      }
    } else {
      result[key] = documentToJson(value)
    }
  }
  return result;
}

// public-read firestore cloud db
const URL = 'https://firestore.googleapis.com/v1/projects/firebase-kqql/databases/(default)/documents/';
const TWO_DAYS_AGO_IN_MS = 1000 * 60 * 60 * 24 * 2;

// returns the songs from the last x days, for the specified station call sign (lower case)
const fetchRecentSongs = async (whichStation, days) => {
  const now = new Date().getTime();
  const daysInMs = days * 24 * 60 * 60 * 1000;
  const startingTimeRange = now - daysInMs;
  
  const select = {
    "fields": [
      { fieldPath: 'artist' },
      { fieldPath: 'songTitle' },
      { fieldPath: 'when' }
    ]
  };
  
  const where = {
    fieldFilter: {
      field: {
        fieldPath: 'when'
      },
      op: 'GREATER_THAN',
      value: {
        integerValue: startingTimeRange
      }
    }
  };
  
  const from = [{
    collectionId: whichStation
  }];
  
  const orderBy = [{
    field: {
      fieldPath: 'when'
    },
    direction: 'ASCENDING'
  }];
  
  const q = {
    structuredQuery: {
      select,
      where,
      from,
      orderBy,
      // limit: 1
    }
  };
  // console.log(JSON.stringify(q));
  const qResponse = await fetch(`${URL}:runQuery`, {method:'POST', body:JSON.stringify(q)});
  if (qResponse.status !== 200) {
    const text = await qResponse.text();
    throw Error(`Unable to query db (error ${qResponse.status}): ${text}`);
  }
  const qResultsArray = await qResponse.json();
  const objArray = qResultsArray.map(entry => documentToJson(entry.document.fields));
  
  return objArray;
};


/** 
 *  Takes an array of songHistory records (usually from the DB)
 *  and recudes them into a single object:
 * {
 *      "AN ARTIST NAME": {
 *          artist: 'An Artist Name',
 *          SOME_SONG_NAME : {
 *              title: "Some Song Name",
 *              whenPlayed : [1234567890100, 1234567890400]
 *          },
 *          YET_ANOTHER_SONG : {
 *              title: "Yet Another Song"
 *              whenPlayed : [1234567890500]
 *          }
 *      },
 *      "REALLY KEWL ARTIST" {
 *          ...
 *      }
 * }
 */
const reduceData = (array) => {
    
    return array.reduce(function(everything, currentThing) {
        
        var when = currentThing.when;
        var songTitle = currentThing.songTitle;
        var artist = currentThing.artist;
        if ( ! artist ) {
            // skip it
            return everything;
        }

        var artistKey = artist.toUpperCase();
        if (!everything[artistKey]) {
            everything[artistKey] = {
                artist: artist
            }
        };

        var currentArtistStuff = everything[artistKey];
        var songKey = songTitle.toUpperCase();
        if (!currentArtistStuff[songKey]) {
            currentArtistStuff[songKey] = {
                title: songTitle,
                whenPlayed: []
            }
        }

        var currentSongStuff = currentArtistStuff[songKey];
        currentSongStuff.whenPlayed.push(when);
        return everything;

    }, {});
};

/*
 * given an array of when a song was played (long numbers), 
 * return an array of nicely formatted timestamps.
 */
const convertTimesToChildren = (array) => {
    var children = [];
    var len = array.length;
    var when;
    var DAY_OF_WEEK = ['Sun', 'Mon', 'Tue','Wed', 'Thu', 'Fri', 'Sat'];

    for (var i = 0; i < len; i ++) {
        when = new Date(Number.parseInt(array[i], 10));
        children.push( {
            name: DAY_OF_WEEK[when.getDay()] +' '+when.toLocaleTimeString()
        });
    }
    return children;
}

/*
 *  Takes a reduced form of the data and converts it into
 *  the name/children array structure for the flare graph.
 */
const convertToFlare = (callsign, obj) => {
    var flare = {
        name: callsign,
        children: []
    }

    var currentArtist;

    for (var artistKey in obj) {
        if (!obj.hasOwnProperty(artistKey)) {
            continue;
        }

        var artistObj = obj[artistKey];
        currentArtist = {
            name: artistObj.artist,
            children: []
        }

        for (var songKey in artistObj) {
            if (!artistObj.hasOwnProperty(songKey) ||
                 songKey === "artist") {
                continue;
            }
 
            var songObj = artistObj[songKey];
            currentArtist.children.push( {
                name: songObj.title + ' (' +
                          songObj.whenPlayed.length +')',
                children: convertTimesToChildren(songObj.whenPlayed)
            } );
        }

        flare.children.push(currentArtist);
    }
    return flare;
}

// given an array of objects (which represent a song, and when it was played), discard
//    any duplicate entries - such as:
//    { artist: 'foobarz', songTitle: 'foobar all nite', when: 12345},
//    { artist: 'foobarz', songTitle: 'foobar all nite', when: 12399},
const dedupeSongs = (arrayOfSongs) => {
  const cleanedArray = [];
  let currentSong = {};
  arrayOfSongs.forEach(entry => {
    // make sure any numeric artist/title is turned into a String
    entry.songTitle = String(entry.songTitle);
    entry.artist = String(entry.artist);

    if (entry.songTitle !== currentSong.songTitle &&
       entry.artist !== currentSong.artist) {
      // looks like a new/different song - keep it!
      cleanedArray.push(entry);
      currentSong = entry;
    } else {
      // this entry has the same artist and title, ignore it
    }
  });
  return cleanedArray;
}

const doStuff = async(stationName, days) => {
  // query the DB for the array of songs for the past 48 hours
  const rawArrayOfSongs = await fetchRecentSongs(stationName.toLowerCase(), days);
  const cleanedArray = dedupeSongs(rawArrayOfSongs);
  
  // TODO - dedupe any duplicate entries
  
  // turn the array of songs into a huge object of coalesced information
  const artistSongEverything = reduceData(cleanedArray);
  
  const flareData = convertToFlare(stationName.toUpperCase(), artistSongEverything);
  return flareData;
}




// doStuff('kqrs').then(console.log);



              
            
!
999px

Console