Skip to content

Commit

Permalink
Add script to export playback events to CSV
Browse files Browse the repository at this point in the history
After we've recorded a number of playback events,
we may have interest in getting them all into a CSV with
the song title, playback timestamp, and who marked the playback
  • Loading branch information
dvnrsn committed Jan 17, 2024
1 parent c8b2d25 commit a7402b5
Show file tree
Hide file tree
Showing 4 changed files with 532 additions and 41 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,37 @@ inspired by [Max's Iron Cookie](https://www.ironcookie.com)

Built on the [indie stack](https://github.com/remix-run/indie-stack)

## How to get playback data in CSV

There is a manual recording of playback events which marks when a song is played at bar. Here is how we get that data into CSV

```bash
node playbackEvent-to-csv.js
```

| PLAYBACK TIME | TITLE | FIRST NAME | LAST NAME |
| ------------- | -------------------- | ---------- | --------- |
| 1700362160500 | Everything She Ain't | Devin | Rasmussen |
| 1700365249728 | Humble | Devin | Rasmussen |

Maybe we want to get the highest count

```sql
SELECT COUNT(p.id), s.title
FROM "PlaybackEvent" p
JOIN "Song" s ON p.songId = s.id
GROUP BY p.songId
ORDER BY COUNT(p.id) DESC;
```

| RANK | TITLE |
| ---- | --------------------- |
| 5 | Wobble |
| 4 | Burn it to the Ground |
| 4 | Fire Burning |
| 4 | Shivers |
| 4 | I See Country |

## What's in the stack?

- [Fly app deployment](https://fly.io) with [Docker](https://www.docker.com/)
Expand Down
48 changes: 48 additions & 0 deletions get-playback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import sqlite3Lib from "sqlite3";
import { createObjectCsvWriter } from "csv-writer";

const sqlite3 = sqlite3Lib.verbose();

// Open the database
let db = new sqlite3.Database("sqlite.db", sqlite3.OPEN_READONLY, (err) => {
if (err) {
console.error(err.message);
}
console.log("Connected to the SQLite database.");
});

const sql = `SELECT p.createdAt, s.title, u.firstName, u.lastName
FROM PlaybackEvent p
JOIN Song s ON p.songId = s.id
JOIN User u ON p.userId = u.id`;

// Specify path and header for CSV file
const csvWriter = createObjectCsvWriter({
path: "out.csv",
header: [
{ id: "createdAt", title: "PLAYBACK TIME" },
{ id: "title", title: "TITLE" },
{ id: "firstName", title: "FIRST NAME" },
{ id: "lastName", title: "LAST NAME" },
],
});

// Fetch data and write to CSV
db.all(sql, [], (err, rows) => {
if (err) {
throw err;
}
csvWriter
.writeRecords(rows) // returns a promise
.then(() => {
console.log("CSV file written successfully");
});
});

// Close the database connection
db.close((err) => {
if (err) {
console.error(err.message);
}
console.log("Closed the database connection.");
});
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@types/cookie": "^0.5.4",
"bcryptjs": "^2.4.3",
"crypto-js": "^4.2.0",
"csv-writer": "^1.6.0",
"fuse.js": "^7.0.0",
"is-ip": "^5.0.1",
"isbot": "^3.6.13",
Expand All @@ -51,6 +52,7 @@
"remix-auth-socials": "^2.0.5",
"remix-utils": "^7.3.0",
"resend": "^2.0.0",
"sqlite3": "^5.1.6",
"tiny-invariant": "^1.3.1",
"zod": "^3.22.4"
},
Expand Down
Loading

0 comments on commit a7402b5

Please sign in to comment.