19 C
New York
Thursday, October 24, 2024

Bored with looking out Slack, GitHub, and Google Drive individually? Do it suddenly in SQL

Share To Your Friends

[ad_1]

You realize the drill: The phrases you’re on the lookout for may be in Slack, or GitHub, or Google Drive, or Google Sheets, or Zendesk, or … the listing goes on. Looking throughout these silos is a standard frustration. It must be frictionless, and this Steampipe dashboard makes it so.

This wasn’t my first rodeo. I began this journey in 1996 and have revisited the concept periodically. In 2018 I wrote a couple of model that was the traditional instance of The Easiest Factor That May Probably Work: a net web page that corrals the search URLs for numerous providers and visits every in its personal tab. As foolish as that sounds it was useful sufficient to get used a bit, and never simply by me.

After all I needed to make use of the underlying APIs, normalize the outcomes, and merge them into a standard view. However the effort required to wrangle all of the APIs made that challenge extra bother than it was price. When you’ve achieved this type of factor earlier than you recognize that almost all providers present search APIs together with adapters in your most popular programming language. However every service may have its personal approach of calling the API, paginating outcomes, and formatting them. These variations create friction it’s worthwhile to overcome in an effort to work with the ends in a constant approach.

When API wrangling turns into frictionless, although, many issues turn into potential. Efficient metasearch is one in all them. Steampipe will get you out of the enterprise of calling APIs, paginating outcomes, and unpacking JSON objects. It calls the APIs for you and streams the outcomes into database tables so you may focus solely on working with the info. That solves the most important downside you face when constructing a metasearch dashboard.

Converging on a schema

The subsequent problem is to bind search outcomes to a standard schema. SQL is a superb setting during which to do this. The question that drives the dashboard proven within the screencast consists of three stanzas that you simply don’t must be a SQL wizard to write down. All of them observe the identical sample as this one for looking out GitHub points.

choose
   'github_issue' as sort,
   repository_full_name || ' ' || title as supply,
   to_char(created_at, 'YYYY-MM-DD') as date,
   html_url as hyperlink,
   substring(physique from 1 for 200) || '...' as content material
from
   github_search_issue
the place
   $1 ~ 'github_issue'
   and question = 'in:physique in:feedback org:github ' || $2
   restrict $3

Gadgets in blue are the names of columns in a database desk—on this case github_search_issue, one of many tables made by Steampipe’s GitHub plugin. The Steampipe hub makes it straightforward to examine the names and descriptions of the columns within the desk, and reveals you examples of tips on how to use the knowledge within the desk.

As a result of fetching the info doesn’t require calling APIs and unpacking JSON, you may deal with higher-order search syntax, which is a lot to consider, together with the fascinating (and enjoyable!) problem of mapping supply columns to a standard schema.

Gadgets in purple are the names of the columns that present up within the dashboard. For this dashboard we’ve determined every search end result will map to those 5 columns: sort, supply, date, hyperlink, and content material. SQL’s AS clause makes it straightforward for every stanza to rename its columns to match the schema.

The complete question

Right here’s the complete question that drives the dashboard. There are three stanzas just like the one above, every written as a CTE (widespread desk expression) with parameters equivalent to enter variables. And there’s nearly nothing else! Every stanza queries an API-based desk (slack_search, github_search_issue, googleworkspace_drive_my_file), selects (and possibly transforms) columns, then aliases the outcomes to match the schema. All that’s left is to UNION the three CTEs, which act like short-term tables, and order the outcomes.

with slack as (
  choose
    'slack' as sort,
    user_name || ' in #' || (channel ->> 'identify')::textual content as supply,
    to_char(timestamp, 'YYYY-MM-DD') as date,
    permalink as hyperlink,
    substring(textual content from 1 for 200) as content material
  from
    slack_search
  the place
    $1 ~ 'slack'
    and question = 'in:#steampipe after:${native.config.slack_date} ' || $2
  restrict $3
),
github_issue as (
  choose
    'github_issue' as sort,
    repository_full_name || ' ' || title as supply,
    to_char(created_at, 'YYYY-MM-DD') as date,
    html_url as hyperlink,
    substring(physique from 1 for 200) || '...' as content material
  from
    github_search_issue
  the place
    $1 ~ 'github_issue'
    and question = ' in:physique in:feedback org:${native.config.github_org} ' || $2
  restrict $3
),
gdrive as (
  choose
    'gdrive' as sort,
    substitute(mime_type,'software/vnd.google-apps.','') as supply,
    to_char(created_time, 'YYYY-MM-DD') as date,
    'https://docs.google.com/doc/d/' || id as hyperlink,
    identify as content material
  from
    googleworkspace_drive_my_file
  the place
    $1 ~ 'gdrive'
    and question = 'fullText comprises ' || '''' || $2 || ''''
  restrict $3
)

choose * from slack
union 
choose * from github_issue
union 
choose * from gdrive

order by
  date desc

Dashboards as code

Many dashboard techniques can work with this question. You may, for instance, join Metabase, or Tableau, or one other Postgres consumer to Steampipe and construct the identical sort of interactive dashboard as proven right here. You’d do this work in a low-code setting the place widgets and settings are dealt with in a person interface. Steampipe’s dashboard subsystem takes a special method knowledgeable by its infrastructure-as-code (IaC) roots. Queries towards APIs must be expressed in SQL code that’s managed, like all different code, in version-controlled repositories. The dashboard widgets that show the outcomes of these queries ought to likewise be expressed in code, and on this case the language is Terraform’s HCL.

Right here’s the HCL definition of the metasearch dashboard. It declares three sorts of enter block: sources (multi-select), search_term (textual content), and max_per_source (single-select, which is the default). You are able to do rather more with the enter block—notably, you may fill it with outcomes from a SQL question, as proven in the documentation. That’s not wanted right here, although.

The desk block makes use of the question outlined above, and defines the parameters handed to it. The wrap argument ensures that columns with plenty of textual content shall be readable.

dashboard "metasearch" {

  enter "sources" {
    title = "sources"
    sort = "multiselect"
    width = 2
    possibility "slack" {} 
    possibility "github_issue" {}
    possibility "gdrive" {}
  }  

  enter "search_term" {
    sort = "textual content"
    width = 2
    title = "search time period"
  }

  enter "max_per_source" {
    title = "max per supply"
    width = 2
    possibility "2" {}
    possibility "5" {}
    possibility "10" {}   
    possibility "20" {}
  }  

  desk {
    title = "search slack + github + gdrive"
    question = question.metasearch
    args = [
      self.input.sources,
      self.input.search_term,
      self.input.max_per_source
    ]
    column "supply" {
      wrap = "all"
    }
    column "hyperlink" {
      wrap = "all"
    }
    column "content material" {
      wrap = "all"
    }
  }

}

Once more there’s not a lot else to see right here, nor ought to there be. Constructing dashboards as code shouldn’t require plenty of advanced code, and it doesn’t.

No wizardry required

Simply as you don’t have to be a SQL wizard to create new subqueries, you additionally don’t have to be an HCL wizard so as to add them to the dashboard. Would you want so as to add sources? There are dozens of different plugins to select from, with extra added every month. They don’t all supply search however many do, and it’s straightforward to seek out them with (in fact!) a Steampipe question.

choose
  identify
  html_url
from
  github_search_code
the place
  question = 'search org:turbot org:francois2metz org:ellisvalentiner org:theapsgroup'
  and identify ~ 'desk'
  and identify ~ 'search'
order by
  identify

Within the steampipe-samples repo we’ve included the code for the dashboard proven right here, together with an additional search stanza for Zendesk that we eliminated when our trial account expired. Have enjoyable extending this dashboard! If a search API you want isn’t already accessible, drop by our Slack group and tell us. Anyone would possibly already be writing the plugin you want—or possibly you’d wish to deal with that your self. Each new plugin makes it potential for anybody who can work with fundamental HCL plus SQL to wield APIs like a professional and remedy actual issues.

Copyright © 2022 IDG Communications, Inc.

[ad_2]


Share To Your Friends

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles