-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreate_statistics_schema.php
90 lines (73 loc) · 2.69 KB
/
create_statistics_schema.php
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
<?php
$host = 'localhost';
$port = 5432;
$dbname = 'postgres';
$user = 'postgres';
$password = 'postgres';
$schema = 'paracestamol';
$interval = '3 month'; // Older records will be disregarded in the median test duration calculation
$pdo = new \PDO("pgsql:host={$host};port={$port};dbname={$dbname}", $user, $password);
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$query = <<<HEREDOC
CREATE SCHEMA IF NOT EXISTS $schema;
CREATE TABLE IF NOT EXISTS $schema.test
(
id bigserial,
name text COLLATE pg_catalog."default" UNIQUE NOT NULL,
created_at timestamp without time zone NOT NULL DEFAULT now(),
CONSTRAINT test_pkey PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS $schema.environment
(
id bigserial,
name text COLLATE pg_catalog."default" UNIQUE NOT NULL,
created_at timestamp without time zone NOT NULL DEFAULT now(),
CONSTRAINT environment_pkey PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS $schema.project
(
id bigserial,
name text COLLATE pg_catalog."default" UNIQUE NOT NULL,
created_at timestamp without time zone NOT NULL DEFAULT now(),
CONSTRAINT project_pkey PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS $schema.duration
(
id bigserial,
project_id bigint NOT NULL,
environment_id bigint NOT NULL,
test_id bigint NOT NULL,
duration_seconds integer NOT NULL,
created_at timestamp without time zone NOT NULL DEFAULT now(),
CONSTRAINT duration_pkey PRIMARY KEY (id),
CONSTRAINT fk_project FOREIGN KEY (project_id)
REFERENCES $schema.project (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE CASCADE,
CONSTRAINT fk_envs FOREIGN KEY (environment_id)
REFERENCES $schema.environment (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE CASCADE,
CONSTRAINT fk_test FOREIGN KEY (test_id)
REFERENCES $schema.test (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE CASCADE
);
CREATE INDEX pcml_duration_project_id ON $schema.duration (project_id);
CREATE INDEX pcml_duration_environment_id ON $schema.duration (environment_id);
CREATE INDEX pcml_duration_test_id ON $schema.duration (test_id);
CREATE INDEX pcml_duration_duration_seconds ON $schema.duration (duration_seconds);
CREATE INDEX pcml_duration_created_at ON $schema.duration (created_at);
CREATE OR REPLACE VIEW $schema.duration_median AS
SELECT
project_id,
environment_id,
test_id,
PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY duration_seconds DESC) AS median_duration_seconds
FROM $schema.duration
WHERE created_at > (NOW() - INTERVAL '$interval')
GROUP BY project_id, environment_id, test_id
;
HEREDOC;
$pdo->exec($query);
return 0;