forked from MichaelJendryke/NyxHyperion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.sql
122 lines (112 loc) · 3.16 KB
/
Database.sql
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
------------
-- TABLES --
------------
--images
CREATE TABLE public.images (
"ID" varchar NOT NULL DEFAULT nextval('user_id_seq'::regclass),
manifest varchar NOT NULL,
file_name varchar NOT NULL,
checksum uuid NULL,
ordernumber int8 NOT NULL,
ordercreated timestamp NOT NULL,
orderexpiration timestamp NOT NULL,
status varchar NOT NULL,
file_size int8 NOT NULL,
noaaid int8 NOT NULL,
CONSTRAINT images_pk PRIMARY KEY (ordernumber,file_name)
)
WITH (
OIDS=FALSE
) ;
--orders
CREATE TABLE public.orders (
user_id int2 NOT NULL DEFAULT nextval('user_id_seq'::regclass),
ordernumber int8 NOT NULL,
status bpchar(12) NOT NULL,
server varchar NOT NULL,
notice varchar NULL,
manifest varchar NULL,
directory varchar NULL,
manifesttotal int4 NULL,
CONSTRAINT ordernumberunique UNIQUE (ordernumber)
)
WITH (
OIDS=FALSE
) ;
-----------
-- VIEWS --
-----------
-- deleteorder
CREATE OR REPLACE VIEW public.deleteorder AS
SELECT orders.ordernumber,
orders.notice,
orders.status,
orders.directory
FROM orders;
--downloadimages
CREATE OR REPLACE VIEW public.downloadimages AS
SELECT i.ordernumber,
i.file_name AS name,
o.directory AS destination,
o.server,
i.checksum
FROM images i
LEFT JOIN orders o ON i.ordernumber = o.ordernumber
WHERE i.status::text <> 'FINISHED'::text
ORDER BY o.ordernumber, i.orderexpiration;
--getmanifest
CREATE OR REPLACE VIEW public.getmanifest AS
SELECT orders.ordernumber,
orders.server,
orders.directory
FROM orders
WHERE orders.status = 'NEW'::bpchar AND orders.directory IS NOT NULL;
--imagesummary
CREATE OR REPLACE VIEW public.imagesummary AS
SELECT i."ID",
i.noaaid AS "NOAA ID",
i.file_name AS "Filename",
i.status AS "Status",
round(i.file_size::numeric / power(1024::double precision, 2::double precision)::numeric, 2) AS "MB"
FROM images i;
--overview
CREATE OR REPLACE VIEW public.overview AS
SELECT o.ordernumber AS "Ordernumber",
o.status AS "Status",
count(i.ordernumber) FILTER (WHERE i.status::text = 'NEW'::text) AS "New",
count(i.ordernumber) FILTER (WHERE i.status::text = 'FINISHED'::text) AS "Downloaded",
count(i.ordernumber) FILTER (WHERE i.status::text = 'ERROR'::text) AS "Error",
count(i.ordernumber) AS "Total",
round((sum(i.file_size)::double precision / power(1024::double precision, 3::double precision))::numeric, 2) AS "GB",
o.directory AS "Destination"
FROM orders o
LEFT JOIN images i ON i.ordernumber = o.ordernumber
GROUP BY o.ordernumber, o.status, o.directory
ORDER BY (count(i.ordernumber));
--processmanifest
CREATE OR REPLACE VIEW public.processmanifest AS
SELECT orders.ordernumber,
orders.directory,
orders.manifest
FROM orders
WHERE orders.status = 'MANIFEST'::bpchar;
----------------
-- PROCEDURES --
----------------
--ordercomplete
CREATE OR REPLACE FUNCTION public.ordercomplete(n bigint)
RETURNS boolean
LANGUAGE plpgsql
AS $function$
BEGIN
if (select manifesttotal from orders where ordernumber = $1) = (select count(I.ordernumber)::integer
from images as I
where ordernumber = $1 and I.status = 'FINISHED'
group by ordernumber)
THEN
return true;
ELSE
RETURN false;
END IF;
end;
$function$;