Tuesday, November 05, 2024

Dopamine rush

I'm currently reading the book "Coders: the making of a new tribe and the remaking of the world" by Clive Thompson. I initially thought that I had not read the book before, but evidently I have as I left digital notes throughout the book. I wonder whether the title of this book subconsciously inspired the title of my song, "Looking for his tribe". I'm sure that there's a line in the book about the dopamine rush that comes from writing successful code, but I can't find it now.

So, in search of some dopamine, I considered what I could add to my blog manager program. After some musing, I thought that it would be 'neat' to take a list of blogs (e.g. those from the current month) and graph the most popular tags contained in those blogs. I have a graphing unit and I also wrote a query that returns the tag count - I use this to prepare the table that I display at the end of every 100 blogs.

I had to modify that query slightly in order to insert values into the temp table so that they could be passed to the graph unit. My first attempt was as follows

insert into temp (instance, id, chardata, payload) select inttostr (inst), tags.id, tags.name, count (*) from tags inner join tag2entry on tag2entry.tag = tags.id inner join temp t1 on t1.id = tag2entry.entry where t1.instance = :p1 group by tags.id, tags.name

That sql query won't actually work as written because of the 'inttostr (inst)' statement. I am actually building the query as text then adding it to the TSqlQuery component because as far as I know, one can't use a parameter in the select clause. Although the query compiled without error, no results were graphed; after examining the code, I realised that I had used the wrong instance number in the query but had passed to the graph unit the correct instance. As written in 'Coders' and quoting Seymour Papert, No program works right the first time.

When I used the correct instance number, the tag counts were graphed. Immediately I could see that whilst the correct tags were displayed, there were too many of them for to display all their names. I remembered that in previous uses of the graph unit, I had limited the number of tags to be displayed to 10. In those uses, the values had been inserted via a cursor, so I could maintain a count of how many tags had been inserted. Here I was using the 'insert into/select from' syntax, so I was forced inspired to use an SQL command that I rarely use; the first line became 'select first 10' and so only ten values were inserted into the temp table and then displayed.

Whilst this did indeed display only ten values and these ten were displayed in ascending order (a trick of the graph unit), they weren't the correct ten values. I had to sort the query first before selecting the first ten, and so at the end of the query I added the line 'order by 4 desc'. The use of the index 4 means that the query result should be sorted by the fourth field in the select statement, namely count (*). This field doesn't have a name exactly so the index solved that problem.

Now I do have the top ten tags for a given range of blog entries graphed. As is my wont, I contemplated this code whilst walking the dog and realised that the previous queries that I had written to display a top ten of tags could be simplified/complicated by using this syntax. I alluded to this two paragraphs ago when I wrote the values had been inserted via a cursor; whilst the SQL code would be slightly more complicated, the program code itself would be greatly simplified. I'll save that dopamine rush for later.

Internal links
[1] 1844



This day in history:

Blog #
Date
TitleTags
127205/11/2019
Draft thesis reviewDBA
135505/11/2020
First rainPersonal, Weather
143505/11/2021
Counting beats with Van der Graaf Generator (3)Van der Graaf Generator, Time signatures
154905/11/2022
November 5Israel, Literature, Peter Robinson

No comments: