forked from turbot/steampipe-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.sp
151 lines (133 loc) · 3.67 KB
/
mod.sp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
mod "hypothesis" {
title = "Hypothesis Insights"
}
variable "search_limit" {
type = number
default = 2500
}
locals {
host = "http://localhost:9194"
}
/*
Advanced feature: histograms
variable "note_word_count_buckets" {
type = number
default = 7
}
variable "user_anno_count_buckets" {
type = number
default = 5
}
To "install" these functions, you can copy/paste one at a time into the `steampipe query` console. Or if you have `psql` installed then you can run it like so:
psql -h localhost -p 9193 -d steampipe -U steampipe
And then copy/paste all the functions at once into that console.
-----
create or replace function user_anno_counts(search_limit int, groupid text, search_uri text) returns table (value int) as $$
with anno_counts as (
select
username,
count(*)
from
hypothesis_search
where
query = 'limit=' || search_limit
|| '&group=' || groupid
|| case when search_uri = 'all' then '' else '&uri=' || search_uri end
group by
username
)
select
count::int as value
from
anno_counts;
$$ language sql;
create or replace function user_anno_counts(search_limit int, groupid text, search_uri text) returns table (value int) as $$
with anno_counts as (
select
username,
count(*)
from
hypothesis_search
where
query = 'limit=' || search_limit
|| '&group=' || groupid
|| case when search_uri = 'all' then '' else '&uri=' || search_uri end
group by
username
)
select
count::int as value
from
anno_counts
$$ language sql;
create or replace function note_word_counts(search_limit int, groupid text, search_uri text) returns table (value int) as $$
with word_counts as (
select
(select count(*)::int from regexp_matches(text, '\s+', 'g'))
from
hypothesis_search
where
query = 'limit=' || search_limit
|| '&group=' || groupid
|| case when search_uri = 'all' then '' else '&uri=' || search_uri end
)
select
count as value
from
word_counts
$$ language sql;
-- This is a variant of the `histogram` function from https://github.com/turbot/steampipe-samples/tree/main/all/histogram
-- It switches the language from plpgsql to plpython to handle the added complexity of passing params to the values_fn
-- See https://github.com/turbot/steampipe-samples/tree/main/all/github-traffic for details on using plpython
create or replace function histogram(search_limit int, groupid text, search_uri text, bucket_count int, values_fn text)
returns table(range int4range, count bigint) as $$
sql = f"""
with min_max as (
select
value,
( select min(value) from {values_fn}('{search_limit}', '{groupid}', '{search_uri}') ),
( select max(value) from {values_fn}('{search_limit}', '{groupid}', '{search_uri}') )
from
{values_fn}('{search_limit}', '{groupid}', '{search_uri}')
),
buckets as (
select
value,
width_bucket(
value,
min,
max,
{bucket_count}
) as bucket
from
min_max
where
max > min
),
bucket_min_max as (
select
bucket,
min(value),
max(value)
from
buckets
group by
bucket
),
ranges as (
select
bucket,
int4range(min, max, '[]') as range
from bucket_min_max
)
select
r.range,
count(b.*)
from ranges r join buckets b using (bucket)
where r.range != 'empty'
group by b.bucket, r.range
order by b.bucket
"""
return plpy.execute(sql)
$$ language plpython3u;
*/