Skip to content

Commit

Permalink
Renames grafanasink -> gsync; adds new article about creating a trigger
Browse files Browse the repository at this point in the history
table.
  • Loading branch information
gwynforthewyn committed Nov 11, 2024
1 parent 0038d9f commit 6cba7c7
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 26 deletions.
40 changes: 14 additions & 26 deletions content/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
.whoarewe-inner {
margin-right: 25%;
margin-left: 25%;
margin-bottom: 5%;
margin-bottom: 2%;
}

.playtechnique {
Expand All @@ -32,9 +32,6 @@
margin-left: 25%;
}

footer {
font-size: 0.9em;
}
</style>
</head>

Expand All @@ -58,39 +55,30 @@
<div class="whoarewe-inner">
<header class="playtechnique">
<span class="big-bold">play</span>technique
</header>
<div id="thesis">
<p>I believe learning how to play as an adult is a skill.</p>
<p>Developing technique makes skills more rewarding, more enjoyable, more fun.</p>
<p>I want to develop my skills, so that they're more fun, more playful.</p>
<p>The more I develop play technique, the more fun it is to play.</p>
</div>
</div>
</div>
<div id="links">
<h4><u>site organisation</u></h4>

<p>
<a href="/blog/index.html">blog</a> is one-off things I've written, and a little life reflection.
</p>

<p>
<a href="/projects/index.html">projects</a> document specific projects I'm working on.
</p>
<p>
Here's an <b>autogenerated Table of Contents</b> from <a href="https://github.com/playtechnique/andrew">Andrew</a>, a web server
I write that has a few features to take the tedium out of maintaining a website as a collection of html, css and javascript documents.
</p>
</div>


{{ .AndrewTableOfContents }}
<div style="font-size: 0.9em;"
<p>
This website is served with <a href="https://github.com/playtechnique/andrew">Andrew</a>, a web
server I put together to serve web pages from flat files on the file system.
</p>
<p>
Andrew has an RSS feed generator, a sitemap generator, automatic table of contents generation. Try it out, you
may like it.
</p>
</div>
</div>

<div id="playtechnique-header"></div>
</div>
</body>
<footer>
<p>This website is served with <a href="https://github.com/playtechnique/andrew">Andrew</a>, a web
server I put together that has a few nice features, like automatic page listings and a sitemap generator.</p>
<p>If you want to write a website in html, css and javascript, Andrew takes a little of the chore out of a few
pieces of maintenance. You might like it!</p>
</footer>
</body>
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<!DOCTYPE html>

<head>
<script type="text/javascript" src="/main.js"></script>
<title>Create A Trigger Table And Watch Grafana Save Changes</title>
<meta name="andrew-publish-time" content="2024-10-24 07:18:14" />
</head>

<body>
<nav class="navigation">
<a href="/"><img src="/images/logo.png" /></a>
<div id="links-group">
<a href="/" class="link">front page</a>
<a href="/blog/index.html" class="link">blog</a>
<a href="/projects/index.html" class="link">projects</a>
</div>
</nav>

<link rel="stylesheet" href="/styles.css" />
<div id="playtechnique-header"></div>
<main>
<article>
<h1>Create A Trigger Table And Watch Grafana Save Changes</h1>
<section id="introduction">
<p>
Time to do some serious business now. Let's actually watch some
changes in Grafana and respond to them.
</p>
<p>
The simplest response I can think of is logging to stdout some nice
text.
</p>
<p>
First, I'll make a trigger table watching the dashboards table, then
figure out what happens afterwards.
</p>
</section>

<section id="the-trigger-table">
<h2>The Trigger Table</h2>

<p>First let's grab a database real quick.</p>
<pre><code>
; docker run -v $(pwd):/mnt/ -it --entrypoint bash gwyn-baseline-adds-data-source:1
$ cp /var/lib/grafana/grafana.db /mnt/
ctrl+d
</code></pre>
<p>Ta da!</p>

<aside>
I wondered for a minute at /var/lib being the path for the db. I knew
that the FHS specified /var as being for mutable data (though I'll go
to my grave claiming this ought to be /usr/local), but why lib, which
suggests library, not.../var/db? The furthest I got was a claim that
this belongs to the standard that predates the FHS, but the
conversation at the time is lost to internet rot.
</aside>

<p>Now a quick trigger table:</p>
<pre><code>
-- Ensure the _gsync_dashboard_tracker table has the necessary columns
CREATE TABLE IF NOT EXISTS _gsync_dashboard_tracker (
_rowid INTEGER,
id INTEGER,
title TEXT, -- updated from 'input' to 'title'
_version INTEGER,
_updated INTEGER
);

-- Create the trigger to track inserts in the dashboard table

CREATE TRIGGER IF NOT EXISTS gsync_dashboard_insert_tracker
AFTER INSERT OR UPDATE ON dashboard
BEGIN
INSERT INTO _gsync_dashboard_tracker (_rowid, id, title, _version, _updated)
VALUES (new.rowid, new.id, new.title, 1, CAST(strftime('%s', 'now') AS INTEGER));
END;
</code></pre>
<p>
This triggers correctly; I get entries in _gsync_dashboard_tracker
when inserts or updates happen. Great. What should actually happen,
though?
</p>
</section>
<section id="lessons-learned">
<h2>The Wrong Approach</h2>
<p>
This exploration has been pretty interesting and neat and all, but I'm
starting to reach the conclusion it's the wrong approach.
</p>
<p>
What it accomplishes is updating a table whenever a different table is
updated. The reason is that the design I thought I wanted is to have a
callback in a go process that responds to changes in this table.
</p>
<p>
I hadn't considered a simple problem: what if the daemon dies? Does it
try to register another callback it can respond to? The limitation of
go callbacks for sqlite databases is that you can't have the callback
registered outside of the current database connection. If the current
db connection didn't create the callback trigger, I don't think it can
register a callback to automatically respond to the trigger changes;
it'd need to be monitoring the table actively. If that's the case, why
do I need the trigger table in the first place?
</p>
<p>
I have an idea towards a simpler design: a simple daemon that dumps
the contents of the dashboard table to json files. I can monitor the
json files for changes using fsnotify.
</p>
<p>
I learned a good load about using sqlite so far in this project,
though. I never had confidence with it, but now I've got a handle on
it, so that's got to be worth something, right?
</p>
</section>
</article>
</main>
</body>

0 comments on commit 6cba7c7

Please sign in to comment.