Artifact
660469de7c1c365968636ca6f58e6d8e38028e192afcb7c28b0d72fa6ee88244:
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 20 73 6f 75 72 as..select sour
0520: 63 65 5f 69 64 2c 20 63 6f 61 6c 65 73 63 65 28 ce_id, coalesce(
0530: 6c 61 73 74 5f 73 63 72 61 70 65 20 2b 20 6d 61 last_scrape + ma
0540: 6b 65 5f 69 6e 74 65 72 76 61 6c 28 30 2c 30 2c ke_interval(0,0,
0550: 30 2c 30 2c 30 2c 28 36 30 20 2f 20 28 63 6f 61 0,0,0,(60 / (coa
0560: 6c 65 73 63 65 28 61 63 74 69 76 69 74 79 2c 20 lesce(activity,
0570: 31 29 2f 37 20 2b 20 31 29 20 29 3a 3a 69 6e 74 1)/7 + 1) )::int
0580: 65 67 65 72 29 2c 20 6e 6f 77 28 29 20 2d 20 69 eger), now() - i
0590: 6e 74 65 72 76 61 6c 20 27 31 20 6d 69 6e 75 74 nterval '1 minut
05a0: 65 27 29 20 61 73 20 6e 65 78 74 5f 66 65 74 63 e') as next_fetc
05b0: 68 2c 20 6f 77 6e 65 72 0a 09 09 66 72 6f 6d 20 h, owner...from
05c0: 72 73 73 74 67 5f 73 6f 75 72 63 65 20 6e 61 74 rsstg_source nat
05d0: 75 72 61 6c 20 6c 65 66 74 20 6a 6f 69 6e 0a 09 ural left join..
05e0: 09 28 73 65 6c 65 63 74 20 73 6f 75 72 63 65 5f .(select source_
05f0: 69 64 2c 20 63 6f 75 6e 74 28 2a 29 20 61 73 20 id, count(*) as
0600: 61 63 74 69 76 69 74 79 0a 09 09 09 66 72 6f 6d activity....from
0610: 20 72 73 73 74 67 5f 70 6f 73 74 20 77 68 65 72 rsstg_post wher
0620: 65 20 0a 09 09 09 09 68 6f 75 72 20 3d 20 65 78 e .....hour = ex
0630: 74 72 61 63 74 28 27 68 6f 75 72 27 20 66 72 6f tract('hour' fro
0640: 6d 20 6e 6f 77 28 29 29 3a 3a 73 6d 61 6c 6c 69 m now())::smalli
0650: 6e 74 0a 09 09 09 09 61 6e 64 20 70 6f 73 74 65 nt.....and poste
0660: 64 20 3e 20 6e 6f 77 28 29 20 2d 20 69 6e 74 65 d > now() - inte
0670: 72 76 61 6c 20 27 37 20 64 61 79 73 27 0a 09 09 rval '7 days'...
0680: 09 09 61 6e 64 20 70 6f 73 74 65 64 20 3c 20 6e ..and posted < n
0690: 6f 77 28 29 20 2d 20 69 6e 74 65 72 76 61 6c 20 ow() - interval
06a0: 27 31 20 68 6f 75 72 27 0a 09 09 09 67 72 6f 75 '1 hour'....grou
06b0: 70 20 62 79 20 73 6f 75 72 63 65 5f 69 64 29 20 p by source_id)
06c0: 61 73 20 61 63 74 0a 09 09 77 68 65 72 65 20 65 as act...where e
06d0: 6e 61 62 6c 65 64 0a 09 09 6f 72 64 65 72 20 62 nabled...order b
06e0: 79 20 6e 65 78 74 5f 66 65 74 63 68 3b 0a 0a 63 y next_fetch;..c
06f0: 72 65 61 74 65 20 6f 72 20 72 65 70 6c 61 63 65 reate or replace
0700: 20 66 75 6e 63 74 69 6f 6e 20 68 78 6d 28 74 69 function hxm(ti
0710: 6d 65 73 74 61 6d 70 74 7a 29 20 72 65 74 75 72 mestamptz) retur
0720: 6e 73 20 73 6d 61 6c 6c 69 6e 74 0a 09 61 73 20 ns smallint..as
0730: 24 24 20 73 65 6c 65 63 74 28 65 78 74 72 61 63 $$ select(extrac
0740: 74 28 27 68 6f 75 72 27 20 66 72 6f 6d 20 24 31 t('hour' from $1
0750: 29 20 2a 20 65 78 74 72 61 63 74 28 27 6d 69 6e ) * extract('min
0760: 75 74 65 27 20 66 72 6f 6d 20 24 31 29 29 3b 20 ute' from $1));
0770: 24 24 0a 09 6c 61 6e 67 75 61 67 65 20 73 71 6c $$..language sql
0780: 20 69 6d 6d 75 74 61 62 6c 65 20 72 65 74 75 72 immutable retur
0790: 6e 73 20 6e 75 6c 6c 20 6f 6e 20 6e 75 6c 6c 20 ns null on null
07a0: 69 6e 70 75 74 3b 0a 0a 63 72 65 61 74 65 20 6f input;..create o
07b0: 72 20 72 65 70 6c 61 63 65 20 76 69 65 77 20 72 r replace view r
07c0: 73 73 74 67 5f 6f 72 64 65 72 20 61 73 0a 09 73 sstg_order as..s
07d0: 65 6c 65 63 74 20 73 6f 75 72 63 65 5f 69 64 2c elect source_id,
07e0: 20 63 6f 61 6c 65 73 63 65 28 6c 61 73 74 5f 73 coalesce(last_s
07f0: 63 72 61 70 65 20 2b 20 6d 61 6b 65 5f 69 6e 74 crape + make_int
0800: 65 72 76 61 6c 28 30 2c 30 2c 30 2c 30 2c 30 2c erval(0,0,0,0,0,
0810: 28 36 30 20 2f 20 28 63 6f 61 6c 65 73 63 65 28 (60 / (coalesce(
0820: 61 63 74 69 76 69 74 79 2c 20 31 29 2f 37 20 2b activity, 1)/7 +
0830: 20 31 29 20 29 3a 3a 69 6e 74 65 67 65 72 29 2c 1) )::integer),
0840: 20 6e 6f 77 28 29 20 2d 20 69 6e 74 65 72 76 61 now() - interva
0850: 6c 20 27 31 20 6d 69 6e 75 74 65 27 29 20 61 73 l '1 minute') as
0860: 20 6e 65 78 74 5f 66 65 74 63 68 2c 20 6f 77 6e next_fetch, own
0870: 65 72 0a 09 09 66 72 6f 6d 20 72 73 73 74 67 5f er...from rsstg_
0880: 73 6f 75 72 63 65 20 6e 61 74 75 72 61 6c 20 6c source natural l
0890: 65 66 74 20 6a 6f 69 6e 0a 09 09 28 73 65 6c 65 eft join...(sele
08a0: 63 74 20 73 6f 75 72 63 65 5f 69 64 2c 20 63 6f ct source_id, co
08b0: 75 6e 74 28 2a 29 20 61 73 20 61 63 74 69 76 69 unt(*) as activi
08c0: 74 79 0a 09 09 09 66 72 6f 6d 20 72 73 73 74 67 ty....from rsstg
08d0: 5f 70 6f 73 74 20 77 68 65 72 65 20 0a 09 09 09 _post where ....
08e0: 20 20 28 0a 09 09 09 09 09 28 68 78 6d 20 3e 20 (......(hxm >
08f0: 68 78 6d 28 6e 6f 77 28 29 29 20 2d 20 33 30 20 hxm(now()) - 30
0900: 61 6e 64 20 68 78 6d 20 3c 20 68 78 6d 28 6e 6f and hxm < hxm(no
0910: 77 28 29 29 20 2b 20 33 30 29 0a 09 09 09 09 09 w()) + 30)......
0920: 6f 72 20 28 68 78 6d 20 3c 20 33 30 20 61 6e 64 or (hxm < 30 and
0930: 20 68 78 6d 20 3c 20 68 78 6d 28 6e 6f 77 28 29 hxm < hxm(now()
0940: 29 20 2b 20 33 30 20 2d 20 31 34 34 30 29 0a 09 ) + 30 - 1440)..
0950: 09 09 09 09 6f 72 20 28 68 78 6d 20 3e 20 31 34 ....or (hxm > 14
0960: 31 30 20 61 6e 64 20 68 78 6d 20 3e 20 31 34 34 10 and hxm > 144
0970: 30 20 2b 20 68 78 6d 28 6e 6f 77 28 29 29 20 2d 0 + hxm(now()) -
0980: 20 33 30 29 0a 09 09 09 20 20 29 0a 09 09 09 09 30).... ).....
0990: 61 6e 64 20 70 6f 73 74 65 64 20 3c 20 6e 6f 77 and posted < now
09a0: 28 29 20 2d 20 69 6e 74 65 72 76 61 6c 20 27 31 () - interval '1
09b0: 20 68 6f 75 72 27 0a 09 09 09 09 61 6e 64 20 70 hour'.....and p
09c0: 6f 73 74 65 64 20 3e 20 6e 6f 77 28 29 20 2d 20 osted > now() -
09d0: 69 6e 74 65 72 76 61 6c 20 27 37 20 64 61 79 73 interval '7 days
09e0: 27 0a 09 09 09 67 72 6f 75 70 20 62 79 20 73 6f '....group by so
09f0: 75 72 63 65 5f 69 64 29 20 61 73 20 61 63 74 0a urce_id) as act.
0a00: 09 09 77 68 65 72 65 20 65 6e 61 62 6c 65 64 0a ..where enabled.
0a10: 09 09 6f 72 64 65 72 20 62 79 20 6e 65 78 74 5f ..order by next_
0a20: 66 65 74 63 68 3b 0a fetch;.