Artifact
6bfa596be6c874425ce368c9843fa286e3ab7946c2e7171a8e432db64c45b437:
0000: 63 72 65 61 74 65 20 74 61 62 6c 65 20 72 73 73 create table rss
0010: 74 67 5f 75 70 64 61 74 65 73 20 28 6f 77 6e 65 tg_updates (owne
0020: 72 20 69 6e 74 65 67 65 72 2c 20 75 70 64 61 74 r integer, updat
0030: 65 20 6a 73 6f 6e 62 29 3b 0a 0a 63 72 65 61 74 e jsonb);..creat
0040: 65 20 75 6e 69 71 75 65 20 69 6e 64 65 78 20 72 e unique index r
0050: 73 73 74 67 5f 75 70 64 61 74 65 73 5f 5f 69 64 sstg_updates__id
0060: 20 6f 6e 20 72 73 73 74 67 5f 75 70 64 61 74 65 on rsstg_update
0070: 73 28 75 70 64 61 74 65 2d 3e 3e 27 75 70 64 61 s(update->>'upda
0080: 74 65 5f 69 64 27 29 3b 0a 0a 63 72 65 61 74 65 te_id');..create
0090: 20 74 61 62 6c 65 20 72 73 73 74 67 5f 73 6f 75 table rsstg_sou
00a0: 72 63 65 20 28 0a 09 73 6f 75 72 63 65 5f 69 64 rce (..source_id
00b0: 20 73 65 72 69 61 6c 2c 0a 09 63 68 61 6e 6e 65 serial,..channe
00c0: 6c 20 74 65 78 74 20 6e 6f 74 20 6e 75 6c 6c 2c l text not null,
00d0: 0a 09 63 68 61 6e 6e 65 6c 5f 69 64 20 69 6e 74 ..channel_id int
00e0: 65 67 65 72 20 6e 6f 74 20 6e 75 6c 6c 2c 0a 09 eger not null,..
00f0: 75 72 6c 20 74 65 78 74 20 6e 6f 74 20 6e 75 6c url text not nul
0100: 6c 2c 0a 09 6c 61 73 74 5f 73 63 72 61 70 65 20 l,..last_scrape
0110: 6e 6f 74 20 6e 75 6c 6c 20 74 69 6d 65 73 74 61 not null timesta
0120: 6d 70 74 7a 20 64 65 66 61 75 6c 74 20 6e 6f 77 mptz default now
0130: 28 29 2c 0a 09 65 6e 61 62 6c 65 64 20 62 6f 6f (),..enabled boo
0140: 6c 65 61 6e 20 6e 6f 74 20 6e 75 6c 6c 20 64 65 lean not null de
0150: 66 61 75 6c 74 20 74 72 75 65 2c 0a 09 69 76 5f fault true,..iv_
0160: 68 61 73 68 20 74 65 78 74 2c 0a 09 6f 77 6e 65 hash text,..owne
0170: 72 20 62 69 67 69 6e 74 20 6e 6f 74 20 6e 75 6c r bigint not nul
0180: 6c 2c 0a 09 75 72 6c 5f 72 65 20 74 65 78 74 29 l,..url_re text)
0190: 3b 0a 63 72 65 61 74 65 20 75 6e 69 71 75 65 20 ;.create unique
01a0: 69 6e 64 65 78 20 72 73 73 74 67 5f 73 6f 75 72 index rsstg_sour
01b0: 63 65 5f 5f 73 6f 75 72 63 65 5f 69 64 20 6f 6e ce__source_id on
01c0: 20 72 73 73 74 67 5f 73 6f 75 72 63 65 28 73 6f rsstg_source(so
01d0: 75 72 63 65 5f 69 64 29 3b 0a 63 72 65 61 74 65 urce_id);.create
01e0: 20 75 6e 69 71 75 65 20 69 6e 64 65 78 20 72 73 unique index rs
01f0: 73 74 67 5f 73 6f 75 72 63 65 5f 5f 63 68 61 6e stg_source__chan
0200: 6e 65 6c 5f 69 64 5f 5f 6f 77 6e 65 72 20 6f 6e nel_id__owner on
0210: 20 72 73 73 74 67 5f 73 6f 75 72 63 65 28 63 68 rsstg_source(ch
0220: 61 6e 6e 65 6c 5f 69 64 2c 20 6f 77 6e 65 72 29 annel_id, owner)
0230: 3b 0a 63 72 65 61 74 65 20 69 6e 64 65 78 20 72 ;.create index r
0240: 73 73 74 67 5f 73 6f 75 72 63 65 5f 5f 6f 77 6e sstg_source__own
0250: 65 72 20 6f 6e 20 72 73 73 74 67 5f 73 6f 75 72 er on rsstg_sour
0260: 63 65 28 6f 77 6e 65 72 29 3b 0a 0a 63 72 65 61 ce(owner);..crea
0270: 74 65 20 74 61 62 6c 65 20 72 73 73 74 67 5f 70 te table rsstg_p
0280: 6f 73 74 20 28 0a 09 73 6f 75 72 63 65 5f 69 64 ost (..source_id
0290: 20 69 6e 74 65 67 65 72 20 6e 6f 74 20 6e 75 6c integer not nul
02a0: 6c 2c 0a 09 70 6f 73 74 65 64 20 74 69 6d 65 73 l,..posted times
02b0: 74 61 6d 70 74 7a 20 6e 6f 74 20 6e 75 6c 6c 2c tamptz not null,
02c0: 0a 09 75 72 6c 20 74 65 78 74 20 6e 6f 74 20 6e ..url text not n
02d0: 75 6c 6c 2c 0a 09 68 6f 75 72 20 73 6d 61 6c 6c ull,..hour small
02e0: 69 6e 74 20 6e 6f 74 20 6e 75 6c 6c 20 67 65 6e int not null gen
02f0: 65 72 61 74 65 64 20 61 6c 77 61 79 73 20 61 73 erated always as
0300: 20 28 65 78 74 72 61 63 74 28 27 68 6f 75 72 27 (extract('hour'
0310: 20 66 72 6f 6d 20 70 6f 73 74 65 64 20 61 74 20 from posted at
0320: 74 69 6d 65 20 7a 6f 6e 65 20 27 75 74 63 27 29 time zone 'utc')
0330: 29 20 73 74 6f 72 65 64 2c 0a 09 68 78 6d 20 73 ) stored,..hxm s
0340: 6d 61 6c 6c 69 6e 74 20 6e 6f 74 20 6e 75 6c 6c mallint not null
0350: 20 67 65 6e 65 72 61 74 65 64 20 61 6c 77 61 79 generated alway
0360: 73 20 61 73 20 28 68 78 6d 28 70 6f 73 74 65 64 s as (hxm(posted
0370: 29 29 20 73 74 6f 72 65 64 2c 0a 09 46 4f 52 45 )) stored,..FORE
0380: 49 47 4e 20 4b 45 59 20 28 73 6f 75 72 63 65 5f IGN KEY (source_
0390: 69 64 29 20 52 45 46 45 52 45 4e 43 45 53 20 72 id) REFERENCES r
03a0: 73 73 74 67 5f 73 6f 75 72 63 65 28 73 6f 75 72 sstg_source(sour
03b0: 63 65 5f 69 64 29 20 6f 6e 20 64 65 6c 65 74 65 ce_id) on delete
03c0: 20 63 61 73 63 61 64 65 2c 0a 29 3b 0a 63 72 65 cascade,.);.cre
03d0: 61 74 65 20 75 6e 69 71 75 65 20 69 6e 64 65 78 ate unique index
03e0: 20 72 73 73 74 67 5f 70 6f 73 74 5f 5f 75 72 6c rsstg_post__url
03f0: 20 6f 6e 20 72 73 73 74 67 5f 70 6f 73 74 28 75 on rsstg_post(u
0400: 72 6c 29 3b 0a 63 72 65 61 74 65 20 69 6e 64 65 rl);.create inde
0410: 78 20 72 73 73 74 67 5f 70 6f 73 74 5f 5f 68 6f x rsstg_post__ho
0420: 75 72 20 6f 6e 20 72 73 73 74 67 5f 70 6f 73 74 ur on rsstg_post
0430: 28 68 6f 75 72 29 3b 0a 63 72 65 61 74 65 20 69 (hour);.create i
0440: 6e 64 65 78 20 72 73 73 74 67 5f 70 6f 73 74 5f ndex rsstg_post_
0450: 5f 70 6f 73 74 65 64 5f 68 6f 75 72 20 6f 6e 20 _posted_hour on
0460: 72 73 73 74 67 5f 70 6f 73 74 28 70 6f 73 74 65 rsstg_post(poste
0470: 64 2c 68 6f 75 72 29 3b 0a 63 72 65 61 74 65 20 d,hour);.create
0480: 69 6e 64 65 78 20 72 73 73 74 67 5f 70 6f 73 74 index rsstg_post
0490: 5f 5f 68 78 6d 20 6f 6e 20 72 73 73 74 67 5f 70 __hxm on rsstg_p
04a0: 6f 73 74 28 68 78 6d 29 3b 0a 63 72 65 61 74 65 ost(hxm);.create
04b0: 20 69 6e 64 65 78 20 72 73 73 74 67 5f 70 6f 73 index rsstg_pos
04c0: 74 5f 5f 70 6f 73 74 65 64 5f 68 78 6d 20 6f 6e t__posted_hxm on
04d0: 20 72 73 73 74 67 5f 70 6f 73 74 28 70 6f 73 74 rsstg_post(post
04e0: 65 64 2c 68 78 6d 29 3b 0a 0a 63 72 65 61 74 65 ed,hxm);..create
04f0: 20 6f 72 20 72 65 70 6c 61 63 65 20 76 69 65 77 or replace view
0500: 20 72 73 73 74 67 5f 6f 72 64 65 72 5f 6f 6c 64 rsstg_order_old
0510: 20 61 73 0a 09 73 65 6c 65 63 74 0a 09 09 73 6f as..select...so
0520: 75 72 63 65 5f 69 64 2c 0a 09 09 63 6f 61 6c 65 urce_id,...coale
0530: 73 63 65 28 6c 61 73 74 5f 73 63 72 61 70 65 20 sce(last_scrape
0540: 2b 20 6d 61 6b 65 5f 69 6e 74 65 72 76 61 6c 28 + make_interval(
0550: 30 2c 30 2c 30 2c 30 2c 30 2c 28 36 30 20 2f 20 0,0,0,0,0,(60 /
0560: 28 63 6f 61 6c 65 73 63 65 28 61 63 74 69 76 69 (coalesce(activi
0570: 74 79 2c 20 31 29 2f 37 20 2b 20 31 29 20 29 3a ty, 1)/7 + 1) ):
0580: 3a 69 6e 74 65 67 65 72 29 2c 20 6e 6f 77 28 29 :integer), now()
0590: 20 2d 20 69 6e 74 65 72 76 61 6c 20 27 31 20 6d - interval '1 m
05a0: 69 6e 75 74 65 27 29 20 61 73 20 6e 65 78 74 5f inute') as next_
05b0: 66 65 74 63 68 2c 0a 09 09 6f 77 6e 65 72 2c 0a fetch,...owner,.
05c0: 09 09 6c 61 73 74 5f 73 63 72 61 70 65 0a 09 66 ..last_scrape..f
05d0: 72 6f 6d 20 72 73 73 74 67 5f 73 6f 75 72 63 65 rom rsstg_source
05e0: 20 6e 61 74 75 72 61 6c 20 6c 65 66 74 20 6a 6f natural left jo
05f0: 69 6e 0a 09 09 28 73 65 6c 65 63 74 20 73 6f 75 in...(select sou
0600: 72 63 65 5f 69 64 2c 20 63 6f 75 6e 74 28 2a 29 rce_id, count(*)
0610: 20 61 73 20 61 63 74 69 76 69 74 79 0a 09 09 09 as activity....
0620: 66 72 6f 6d 20 72 73 73 74 67 5f 70 6f 73 74 20 from rsstg_post
0630: 77 68 65 72 65 20 0a 09 09 09 09 68 6f 75 72 20 where .....hour
0640: 3d 20 65 78 74 72 61 63 74 28 27 68 6f 75 72 27 = extract('hour'
0650: 20 66 72 6f 6d 20 6e 6f 77 28 29 29 3a 3a 73 6d from now())::sm
0660: 61 6c 6c 69 6e 74 0a 09 09 09 09 61 6e 64 20 70 allint.....and p
0670: 6f 73 74 65 64 20 3e 20 6e 6f 77 28 29 20 2d 20 osted > now() -
0680: 69 6e 74 65 72 76 61 6c 20 27 37 20 64 61 79 73 interval '7 days
0690: 27 0a 09 09 09 09 61 6e 64 20 70 6f 73 74 65 64 '.....and posted
06a0: 20 3c 20 6e 6f 77 28 29 20 2d 20 69 6e 74 65 72 < now() - inter
06b0: 76 61 6c 20 27 31 20 68 6f 75 72 27 0a 09 09 09 val '1 hour'....
06c0: 67 72 6f 75 70 20 62 79 20 73 6f 75 72 63 65 5f group by source_
06d0: 69 64 29 20 61 73 20 61 63 74 0a 09 09 77 68 65 id) as act...whe
06e0: 72 65 20 65 6e 61 62 6c 65 64 0a 09 09 6f 72 64 re enabled...ord
06f0: 65 72 20 62 79 20 6e 65 78 74 5f 66 65 74 63 68 er by next_fetch
0700: 3b 0a 0a 63 72 65 61 74 65 20 6f 72 20 72 65 70 ;..create or rep
0710: 6c 61 63 65 20 66 75 6e 63 74 69 6f 6e 20 68 78 lace function hx
0720: 6d 28 74 69 6d 65 73 74 61 6d 70 74 7a 29 20 72 m(timestamptz) r
0730: 65 74 75 72 6e 73 20 73 6d 61 6c 6c 69 6e 74 0a eturns smallint.
0740: 09 61 73 20 24 24 20 73 65 6c 65 63 74 28 65 78 .as $$ select(ex
0750: 74 72 61 63 74 28 27 68 6f 75 72 27 20 66 72 6f tract('hour' fro
0760: 6d 20 24 31 29 20 2a 20 65 78 74 72 61 63 74 28 m $1) * extract(
0770: 27 6d 69 6e 75 74 65 27 20 66 72 6f 6d 20 24 31 'minute' from $1
0780: 29 29 3b 20 24 24 0a 09 6c 61 6e 67 75 61 67 65 )); $$..language
0790: 20 73 71 6c 20 69 6d 6d 75 74 61 62 6c 65 20 72 sql immutable r
07a0: 65 74 75 72 6e 73 20 6e 75 6c 6c 20 6f 6e 20 6e eturns null on n
07b0: 75 6c 6c 20 69 6e 70 75 74 3b 0a 0a 63 72 65 61 ull input;..crea
07c0: 74 65 20 6f 72 20 72 65 70 6c 61 63 65 20 76 69 te or replace vi
07d0: 65 77 20 72 73 73 74 67 5f 6f 72 64 65 72 20 61 ew rsstg_order a
07e0: 73 0a 09 73 65 6c 65 63 74 0a 09 09 73 6f 75 72 s..select...sour
07f0: 63 65 5f 69 64 2c 0a 09 09 63 6f 61 6c 65 73 63 ce_id,...coalesc
0800: 65 28 6c 61 73 74 5f 73 63 72 61 70 65 20 2b 20 e(last_scrape +
0810: 6d 61 6b 65 5f 69 6e 74 65 72 76 61 6c 28 30 2c make_interval(0,
0820: 30 2c 30 2c 30 2c 30 2c 28 36 30 20 2f 20 28 63 0,0,0,0,(60 / (c
0830: 6f 61 6c 65 73 63 65 28 61 63 74 69 76 69 74 79 oalesce(activity
0840: 2c 20 31 29 2f 37 20 2b 20 31 29 20 29 3a 3a 69 , 1)/7 + 1) )::i
0850: 6e 74 65 67 65 72 29 2c 20 6e 6f 77 28 29 20 2d nteger), now() -
0860: 20 69 6e 74 65 72 76 61 6c 20 27 31 20 6d 69 6e interval '1 min
0870: 75 74 65 27 29 20 61 73 20 6e 65 78 74 5f 66 65 ute') as next_fe
0880: 74 63 68 2c 0a 09 09 6f 77 6e 65 72 2c 0a 09 09 tch,...owner,...
0890: 6c 61 73 74 5f 73 63 72 61 70 65 0a 09 66 72 6f last_scrape..fro
08a0: 6d 20 72 73 73 74 67 5f 73 6f 75 72 63 65 20 6e m rsstg_source n
08b0: 61 74 75 72 61 6c 20 6c 65 66 74 20 6a 6f 69 6e atural left join
08c0: 0a 09 09 28 73 65 6c 65 63 74 20 73 6f 75 72 63 ...(select sourc
08d0: 65 5f 69 64 2c 20 63 6f 75 6e 74 28 2a 29 20 61 e_id, count(*) a
08e0: 73 20 61 63 74 69 76 69 74 79 0a 09 09 09 66 72 s activity....fr
08f0: 6f 6d 20 72 73 73 74 67 5f 70 6f 73 74 20 77 68 om rsstg_post wh
0900: 65 72 65 20 0a 09 09 09 20 20 28 0a 09 09 09 09 ere .... (.....
0910: 09 28 68 78 6d 20 3e 20 68 78 6d 28 6e 6f 77 28 .(hxm > hxm(now(
0920: 29 29 20 2d 20 33 30 20 61 6e 64 20 68 78 6d 20 )) - 30 and hxm
0930: 3c 20 68 78 6d 28 6e 6f 77 28 29 29 20 2b 20 33 < hxm(now()) + 3
0940: 30 29 0a 09 09 09 09 09 6f 72 20 28 68 78 6d 20 0)......or (hxm
0950: 3c 20 33 30 20 61 6e 64 20 68 78 6d 20 3c 20 68 < 30 and hxm < h
0960: 78 6d 28 6e 6f 77 28 29 29 20 2b 20 33 30 20 2d xm(now()) + 30 -
0970: 20 31 34 34 30 29 0a 09 09 09 09 09 6f 72 20 28 1440)......or (
0980: 68 78 6d 20 3e 20 31 34 31 30 20 61 6e 64 20 68 hxm > 1410 and h
0990: 78 6d 20 3e 20 31 34 34 30 20 2b 20 68 78 6d 28 xm > 1440 + hxm(
09a0: 6e 6f 77 28 29 29 20 2d 20 33 30 29 0a 09 09 09 now()) - 30)....
09b0: 20 20 29 0a 09 09 09 09 61 6e 64 20 70 6f 73 74 ).....and post
09c0: 65 64 20 3c 20 6e 6f 77 28 29 20 2d 20 69 6e 74 ed < now() - int
09d0: 65 72 76 61 6c 20 27 31 20 68 6f 75 72 27 0a 09 erval '1 hour'..
09e0: 09 09 09 61 6e 64 20 70 6f 73 74 65 64 20 3e 20 ...and posted >
09f0: 6e 6f 77 28 29 20 2d 20 69 6e 74 65 72 76 61 6c now() - interval
0a00: 20 27 37 20 64 61 79 73 27 0a 09 09 09 67 72 6f '7 days'....gro
0a10: 75 70 20 62 79 20 73 6f 75 72 63 65 5f 69 64 29 up by source_id)
0a20: 20 61 73 20 61 63 74 0a 09 09 77 68 65 72 65 20 as act...where
0a30: 65 6e 61 62 6c 65 64 0a 09 09 6f 72 64 65 72 20 enabled...order
0a40: 62 79 20 6e 65 78 74 5f 66 65 74 63 68 3b 0a by next_fetch;.