Thursday, July 28, 2022
HomeITVisualizing the Hacker Information API with HCL and SQL

Visualizing the Hacker Information API with HCL and SQL


Within the final episode we checked out a Steampipe dashboard that joins search outcomes throughout numerous APIs. That metasearch dashboard is outlined completely in code that blends two languages: SQL to question the APIs, and HCL (HashiCorp Configuration Language) to move outcomes right into a dashboard desk.

This dashboards-as-code strategy leans into the infrastructure-as-code philosophy. We imagine that dashboards are one other sort of infrastructure that ought to be outlined in software program, managed in version-controlled repositories, and deployed from repos.

On this episode we’ll have a look at a dashboard that explores objects posted to Hacker Information. It illustrates a number of methods during which Steampipe can take part in software-defined workflows.

Right here’s the dashboard in motion.

And right here’s the fast begin to run it for your self.

The visualizations cowl greater than 15,000 tales since June 21. That’s far more historical past than you’ll be able to conveniently slurp from the Hacker Information API in a single gulp. So the place does the historic knowledge come from? It’s within the repo!

Historical past

On an hourly foundation a GitHub Motion checks out the repo, runs Steampipe, queries for brand new objects, exports them to a CSV file, commits that file, and pushes to the origin. So once you clone or replace the repo you get contemporary knowledge. With Steampipe and the CSV plugin put in, right here’s the way you view the dashboard.

cd ~/steampipe-samples/all/hackernews
./replace.sh
steampipe dashboard

After which go to https://localhost:9194. Repeat ./replace.sh at any level to drag and incorporate up-to-the-hour knowledge. The GitHub Motion, scheduled to run hourly, does the next.

  • Try the repo
  • Set up Steampipe within the GitHub runner
  • Set up the Hacker Information plugin
  • Question the most recent tales and ship the output to a CSV file
  • Commit the brand new CSV file and push the adjustments

I used to be late to the GitHub Actions occasion however I’m there now. That is such a pleasant method to durably accumulate small snapshots!

For these maintaining rating, there are three cases of Steampipe on this workflow:

#1 takes the snapshots in a GitHub Motion utilizing the Hacker Information plugin.
#2 builds the desk in your native Steampipe database, utilizing the CSV plugin.
#3 launches the native dashboard server your browser connects to.

HCL + SQL

The Sources panel provides a picklist of domains, and finds tales whose URLs match a specific area. Right here is the SQL question that reads all of the URLs and extracts their domains. The trickiest half is the common expression, however that’s true for regexes in all languages. The SQL itself is straightforward and straightforward to grasp.

with domains as (
  choose distinct
    substring(url from 'http[s]*://([^/$]+)') as area
  from
    hn_items_all
)
choose
  area as label,
  area as worth
from
  domains
order by
  area

The HCL that creates the picklist is likewise easy and straightforward to grasp.

enter "area" {
  width = 3
  sql = <<EOQ
    -- as above
  EOQ    
}

That’s the sample in all places: SQL to get the info, a skinny HCL wrapper to move outcomes right into a widget. These two flavors of code, which collectively outline the dashboard, are born in a repo, evolve there, and deploy from there. In low-or-no-code techniques all the identical info lives in an information retailer managed by an app. It’s possible you’ll (I as soon as did) must export that data with the intention to report the system’s evolution in a repo. It’s higher to have it at all times there.

The purpose of low-or-no-code techniques is, in fact, to wrap code in tooling that unlocks the creativity of people that don’t code. There isn’t but such tooling for this HCL + SQL combo, however there could be. Programs outlined in code are inherently pleasant to instruments that learn and write that code. The Hacker Information repo, for instance, features a whimsical instance that animates a chart by programmatically modifying a dashboard file and altering sure HCL values.

Though I didn’t use a parser for that instance, there are in fact HCL and SQL parsers that render code as knowledge. In an earlier episode we confirmed how one other Steampipe plugin turns Terraform recordsdata into Steampipe tables, thus enabling a compliance checker that validates the Terraform definitions. As you construct and evolve dashboards, wouldn’t you need to validate them in the identical manner? An as-code structure ensures that it’s attainable.

That mentioned, coding Steampipe dashboards in pure HCL + SQL is simple, enjoyable, and wildly productive. Listed here are two of my favourite issues that make it so.

Stay modifying

Once you edit dashboard code in an editor that auto-saves, the dashboard reacts instantly. As proven right here, that’s true for adjustments in each the HCL code that orchestrates the info and the SQL code that fetches it.

It is a sort of direct manipulation interface: “an strategy to interfaces which includes steady illustration of objects of curiosity along with fast, reversible, and incremental actions and suggestions.” (Wikipedia)

Bret Victor is a superb champion of this strategy. On this phase of Inventing on Precept he nails the issue that direct manipulation addresses.

I’ve received this code, and I’ve received this image over right here, and I’ve to take care of the mapping between the 2 in my head.

The longer it takes to see the outcomes of code adjustments, the more durable it’s to take care of that mapping. Instant suggestions is priceless, and too usually unavailable. We took it with no consideration when coding the early net, however now there’s usually a construct step that thwarts direct manipulation.

When coding a Steampipe dashboard you get speedy suggestions from each HCL and SQL. I’ve written quite a lot of Postgres queries lately. Writing them in an atmosphere that refreshes question outcomes as I sort, or shows Postgres error messages once I make errors, has been transformative.

Internet sensibility

Coding Steampipe dashboards jogs my memory of the early net in different methods too. Hyperlinks, for instance, are first-class residents of Steampipe dashboards, and that’s not at all times true these days. The Submissions panel hyperlinks usernames to a different dashboard that explores all submissions from a specific consumer. All of it works simply as an online developer would anticipate.

There’s additionally a View Supply button. Keep in mind View Supply? It used to matter on the internet. These days it normally simply exhibits you minified line noise. However the HCL + SQL combo jogs my memory of the early HTML + JS combo — in a great way! Right here’s a peek on the sources behind one of many dashboard panels.

Together with direct suggestions, this sort of observability helped the early net take off. You’d see one thing, surprise the way it was achieved, view the supply, study, and imitate. Beginner net builders can now not take part in that sort of virtuous cycle. Beginner Steampipe dashboard builders can, and that bodes effectively for a thriving ecosystem.

Visualize knowledge on the pace of thought

As a result of Steampipe extends Postgres you should use any software to create dashboards backed by Steampipe knowledge. If you happen to’re proficient with Metabase, Tableau, or one other software that connects to Postgres, Steampipe’s API-to-database-table superpower will speed up your capacity to visualise knowledge from numerous sources in these environments. However for those who do strive dashboarding the Steampipe manner, let me know the way it goes. In my very own expertise, it’s the quickest and most fulfilling method to flip an concept right into a helpful dashboard.

Copyright © 2022 IDG Communications, Inc.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments