-
Notifications
You must be signed in to change notification settings - Fork 14
/
100-create-api-views.sql
1402 lines (1338 loc) · 35.4 KB
/
100-create-api-views.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- SPDX-FileCopyrightText: 2021 - 2023 Dusan Mijatovic (dv4all)
-- SPDX-FileCopyrightText: 2021 - 2023 dv4all
-- SPDX-FileCopyrightText: 2022 - 2023 Dusan Mijatovic (dv4all) (dv4all)
-- SPDX-FileCopyrightText: 2022 - 2023 Ewan Cahen (Netherlands eScience Center) <[email protected]>
-- SPDX-FileCopyrightText: 2022 - 2023 Netherlands eScience Center
-- SPDX-FileCopyrightText: 2023 Christian Meeßen (GFZ) <[email protected]>
-- SPDX-FileCopyrightText: 2023 Helmholtz Centre Potsdam - GFZ German Research Centre for Geosciences
--
-- SPDX-License-Identifier: Apache-2.0
-- NOTE1: Moved views to (stable) functions because views do not allow for RLS (row-level-security)
-- SEE issue https://github.com/research-software-directory/RSD-as-a-service/issues/170
-- NOTE2: After creating new function you might need to reload postgREST to be able to access the function
-- Keywords with the count used in software
-- used by search to show existing keywords with the count
CREATE FUNCTION keyword_count_for_software() RETURNS TABLE (
id UUID,
keyword CITEXT,
cnt BIGINT
) LANGUAGE plpgsql STABLE AS
$$
BEGIN
RETURN QUERY
SELECT
keyword.id,
keyword.value AS keyword,
keyword_count.cnt
FROM
keyword
LEFT JOIN
(SELECT
keyword_for_software.keyword,
COUNT(keyword_for_software.keyword) AS cnt
FROM
keyword_for_software
GROUP BY keyword_for_software.keyword
) AS keyword_count ON keyword.id = keyword_count.keyword
;
END
$$;
-- Keywords by software
-- for selecting keywords of specific software
-- using filter ?software=eq.UUID
CREATE FUNCTION keywords_by_software() RETURNS TABLE (
id UUID,
keyword CITEXT,
software UUID
) LANGUAGE plpgsql STABLE AS
$$
BEGIN
RETURN QUERY
SELECT
keyword.id,
keyword.value AS keyword,
keyword_for_software.software
FROM
keyword_for_software
INNER JOIN
keyword ON keyword.id = keyword_for_software.keyword
;
END
$$;
-- Keywords grouped by software for filtering
-- We use array for selecting software with specific keywords
-- We use text value for "wild card" search
CREATE FUNCTION keyword_filter_for_software() RETURNS TABLE (
software UUID,
keywords CITEXT[],
keywords_text TEXT
) LANGUAGE plpgsql STABLE AS
$$
BEGIN
RETURN QUERY
SELECT
keyword_for_software.software AS software,
ARRAY_AGG(
keyword.value
ORDER BY value
) AS keywords,
STRING_AGG(
keyword.value,' '
ORDER BY value
) AS keywords_text
FROM
keyword_for_software
INNER JOIN
keyword ON keyword.id = keyword_for_software.keyword
GROUP BY keyword_for_software.software
;
END
$$;
-- COUNT contributors per software
CREATE FUNCTION count_software_countributors() RETURNS TABLE (software UUID, contributor_cnt BIGINT) LANGUAGE plpgsql STABLE AS
$$
BEGIN
RETURN QUERY SELECT
contributor.software, COUNT(contributor.id) AS contributor_cnt
FROM
contributor
GROUP BY
contributor.software;
END
$$;
-- COUNT mentions per software
CREATE FUNCTION count_software_mentions() RETURNS TABLE (software UUID, mention_cnt BIGINT) LANGUAGE plpgsql STABLE AS
$$
BEGIN
RETURN QUERY SELECT
mention_for_software.software, COUNT(mention) AS mention_cnt
FROM
mention_for_software
GROUP BY
mention_for_software.software;
END
$$;
-- JOIN contributors and mentions counts per software
CREATE FUNCTION count_software_contributors_mentions() RETURNS TABLE (id UUID, contributor_cnt BIGINT, mention_cnt BIGINT) LANGUAGE plpgsql STABLE AS
$$
BEGIN
RETURN QUERY SELECT
software.id, count_software_countributors.contributor_cnt, count_software_mentions.mention_cnt
FROM
software
LEFT JOIN
count_software_countributors() AS count_software_countributors ON software.id=count_software_countributors.software
LEFT JOIN
count_software_mentions() AS count_software_mentions ON software.id=count_software_mentions.software;
END
$$;
-- programming language counts for software
-- used in software filter - language dropdown
CREATE FUNCTION prog_lang_cnt_for_software() RETURNS TABLE (
prog_lang TEXT,
cnt BIGINT
) LANGUAGE plpgsql STABLE AS
$$
BEGIN
RETURN QUERY
SELECT
JSONB_OBJECT_KEYS(languages) AS "prog_lang",
COUNT(software) AS cnt
FROM
repository_url
GROUP BY
JSONB_OBJECT_KEYS(languages)
;
END
$$;
-- programming language filter for software
-- used by software_overview func
CREATE FUNCTION prog_lang_filter_for_software() RETURNS TABLE (
software UUID,
prog_lang TEXT[]
) LANGUAGE plpgsql STABLE AS
$$
BEGIN
RETURN QUERY
SELECT
repository_url.software,
(SELECT
ARRAY_AGG(p_lang)
FROM
JSONB_OBJECT_KEYS(repository_url.languages) p_lang
) AS "prog_lang"
FROM
repository_url
;
END
$$;
-- RELATED SOFTWARE LIST WITH COUNTS
CREATE FUNCTION related_software_for_software(software_id UUID) RETURNS TABLE (
id UUID,
slug VARCHAR,
brand_name VARCHAR,
short_statement VARCHAR,
updated_at TIMESTAMPTZ,
contributor_cnt BIGINT,
mention_cnt BIGINT,
is_published BOOLEAN
) LANGUAGE plpgsql STABLE AS
$$
BEGIN
RETURN QUERY
SELECT
software.id,
software.slug,
software.brand_name,
software.short_statement,
software.updated_at,
count_software_countributors.contributor_cnt,
count_software_mentions.mention_cnt,
software.is_published
FROM
software
LEFT JOIN
count_software_countributors() ON software.id=count_software_countributors.software
LEFT JOIN
count_software_mentions() ON software.id=count_software_mentions.software
INNER JOIN
software_for_software ON software.id=software_for_software.relation
WHERE
software_for_software.origin = software_id
;
END
$$;
-- Software maintainer by software slug
CREATE FUNCTION maintainer_for_software_by_slug() RETURNS TABLE (maintainer UUID, software UUID, slug VARCHAR) LANGUAGE plpgsql STABLE AS
$$
BEGIN
RETURN QUERY SELECT
maintainer_for_software.maintainer, maintainer_for_software.software, software.slug
FROM
maintainer_for_software
LEFT JOIN
software ON software.id = maintainer_for_software.software;
END
$$;
-- Participating organisations by software
-- requires software UUID
CREATE FUNCTION organisations_of_software(software_id UUID) RETURNS TABLE (
id UUID,
slug VARCHAR,
primary_maintainer UUID,
name VARCHAR,
ror_id VARCHAR,
is_tenant BOOLEAN,
website VARCHAR,
rsd_path VARCHAR,
logo_id VARCHAR,
status relation_status,
"position" INTEGER,
software UUID
) LANGUAGE plpgsql STABLE AS
$$
BEGIN
RETURN QUERY
SELECT
organisation.id AS id,
organisation.slug,
organisation.primary_maintainer,
organisation.name,
organisation.ror_id,
organisation.is_tenant,
organisation.website,
organisation_route.rsd_path,
organisation.logo_id,
software_for_organisation.status,
software_for_organisation.position,
software.id AS software
FROM
software
INNER JOIN
software_for_organisation ON software.id = software_for_organisation.software
INNER JOIN
organisation ON software_for_organisation.organisation = organisation.id
LEFT JOIN
organisation_route(organisation.id) ON organisation_route.organisation = organisation.id
WHERE
software.id = software_id
;
END
$$;
-- Software count by organisation
-- BY DEFAULT we return count of approved software
-- IF public is FALSE we return total count (as far as RLS allows)
CREATE FUNCTION software_count_by_organisation(public BOOLEAN DEFAULT TRUE) RETURNS TABLE (
organisation UUID,
software_cnt BIGINT
) LANGUAGE sql STABLE AS
$$
SELECT
list_parent_organisations.organisation_id,
COUNT(DISTINCT software_for_organisation.software) AS software_cnt
FROM
software_for_organisation
CROSS JOIN list_parent_organisations(software_for_organisation.organisation)
WHERE
(NOT public)
OR
(
software_for_organisation.status = 'approved'
AND
software IN (SELECT id FROM software WHERE is_published)
)
GROUP BY list_parent_organisations.organisation_id;
$$;
-- Project count by organisation
-- BY DEFAULT we return count of approved projects
-- IF public is FALSE we return total count (as far as RLS allows)
CREATE FUNCTION project_count_by_organisation(public BOOLEAN DEFAULT TRUE) RETURNS TABLE (
organisation UUID,
project_cnt BIGINT
)LANGUAGE sql STABLE AS
$$
SELECT
list_parent_organisations.organisation_id,
COUNT(DISTINCT project_for_organisation.project) AS project_cnt
FROM
project_for_organisation
CROSS JOIN list_parent_organisations(project_for_organisation.organisation)
WHERE
(NOT public)
OR
(
status = 'approved'
AND
project IN (SELECT id FROM project WHERE is_published)
)
GROUP BY list_parent_organisations.organisation_id;
$$;
-- Direct children count by organisation
CREATE FUNCTION children_count_by_organisation() RETURNS TABLE (
parent UUID,
children_cnt BIGINT
)LANGUAGE sql STABLE AS
$$
SELECT
organisation.parent, COUNT(*) AS children_cnt
FROM
organisation
WHERE
organisation.parent IS NOT NULL
GROUP BY
organisation.parent
;
$$;
-- Software releases by organisation
-- release info is scraped from concept DOI
-- one software belongs to multiple organisations
-- INCLUDES releases of children organisations
CREATE FUNCTION releases_by_organisation() RETURNS TABLE (
organisation_id UUID,
software_id UUID,
software_slug VARCHAR,
software_name VARCHAR,
release_doi CITEXT,
release_tag VARCHAR,
release_date TIMESTAMPTZ,
release_year SMALLINT,
release_authors VARCHAR
) LANGUAGE sql STABLE AS
$$
SELECT DISTINCT
organisation.id AS organisation_id,
software.id AS software_id,
software.slug AS software_slug,
software.brand_name AS software_name,
mention.doi AS release_doi,
mention.version AS release_tag,
mention.doi_registration_date AS release_date,
mention.publication_year AS release_year,
mention.authors AS release_authors
FROM
organisation
CROSS JOIN
list_child_organisations(organisation.id)
INNER JOIN
software_for_organisation ON list_child_organisations.organisation_id = software_for_organisation.organisation
INNER JOIN
software ON software.id = software_for_organisation.software
INNER JOIN
"release" ON "release".software = software.id
INNER JOIN
release_version ON release_version.release_id = "release".software
INNER JOIN
mention ON mention.id = release_version.mention_id
;
$$;
-- Software releases count by organisation
-- DEPENDS ON releases_by_organisation RPC
CREATE FUNCTION release_cnt_by_organisation() RETURNS TABLE (
organisation_id UUID,
release_cnt BIGINT
) LANGUAGE sql STABLE AS
$$
SELECT
releases_by_organisation.organisation_id AS organisation_id,
COUNT(releases_by_organisation.*) AS release_cnt
FROM
organisation
INNER JOIN
releases_by_organisation() ON releases_by_organisation.organisation_id = organisation.id
GROUP BY
releases_by_organisation.organisation_id
;
$$;
-- Software releases count per YEAR by organisation
-- DEPENDS ON releases_by_organisation RPC
CREATE FUNCTION release_cnt_by_year(organisation_id UUID) RETURNS TABLE (
release_year SMALLINT,
release_cnt BIGINT
) LANGUAGE plpgsql STABLE AS
$$
BEGIN RETURN QUERY
SELECT
releases_by_organisation.release_year,
COUNT(releases_by_organisation.*) AS release_cnt
FROM
releases_by_organisation()
WHERE
releases_by_organisation.organisation_id = release_cnt_by_year.organisation_id
GROUP BY
releases_by_organisation.release_year
;
END
$$;
-- Organisations overview
-- we pass public param to count functions to get public/private count
-- public count is default,
-- note! the RLS will limit row selection in any case
CREATE FUNCTION organisations_overview(public BOOLEAN DEFAULT TRUE) RETURNS TABLE (
id UUID,
slug VARCHAR,
parent UUID,
primary_maintainer UUID,
name VARCHAR,
ror_id VARCHAR,
website VARCHAR,
is_tenant BOOLEAN,
rsd_path VARCHAR,
parent_names VARCHAR,
logo_id VARCHAR,
software_cnt BIGINT,
project_cnt BIGINT,
children_cnt BIGINT,
release_cnt BIGINT,
score BIGINT
) LANGUAGE sql STABLE AS
$$
SELECT
organisation.id,
organisation.slug,
organisation.parent,
organisation.primary_maintainer,
organisation.name,
organisation.ror_id,
organisation.website,
organisation.is_tenant,
organisation_route.rsd_path,
organisation_route.parent_names,
organisation.logo_id,
software_count_by_organisation.software_cnt,
project_count_by_organisation.project_cnt,
children_count_by_organisation.children_cnt,
release_cnt_by_organisation.release_cnt,
(
COALESCE(software_count_by_organisation.software_cnt,0) +
COALESCE(project_count_by_organisation.project_cnt,0)
) as score
FROM
organisation
LEFT JOIN
organisation_route(organisation.id) ON organisation_route.organisation = organisation.id
LEFT JOIN
software_count_by_organisation(public) ON software_count_by_organisation.organisation = organisation.id
LEFT JOIN
project_count_by_organisation(public) ON project_count_by_organisation.organisation = organisation.id
LEFT JOIN
children_count_by_organisation() ON children_count_by_organisation.parent = organisation.id
LEFT JOIN
release_cnt_by_organisation() ON release_cnt_by_organisation.organisation_id = organisation.id
;
$$;
-- Software info by organisation
-- NOTE! one software is shown multiple times in this view
-- we filter this view at least by organisation uuid
CREATE FUNCTION software_by_organisation(organisation_id UUID) RETURNS TABLE (
id UUID,
slug VARCHAR,
brand_name VARCHAR,
short_statement VARCHAR,
is_published BOOLEAN,
is_featured BOOLEAN,
status relation_status,
contributor_cnt BIGINT,
mention_cnt BIGINT,
updated_at TIMESTAMPTZ,
organisation UUID
) LANGUAGE plpgsql STABLE AS
$$
BEGIN
RETURN QUERY
SELECT DISTINCT ON (software.id)
software.id,
software.slug,
software.brand_name,
software.short_statement,
software.is_published,
software_for_organisation.is_featured,
software_for_organisation.status,
count_software_countributors.contributor_cnt,
count_software_mentions.mention_cnt,
software.updated_at,
software_for_organisation.organisation
FROM
software
LEFT JOIN
software_for_organisation ON software.id=software_for_organisation.software
LEFT JOIN
count_software_countributors() ON software.id=count_software_countributors.software
LEFT JOIN
count_software_mentions() ON software.id=count_software_mentions.software
WHERE
software_for_organisation.organisation IN (
SELECT list_child_organisations.organisation_id FROM list_child_organisations(organisation_id)
)
;
END
$$;
-- Project info by organisation
-- NOTE! updated by Dusan 2022-07-27
-- we filter this view at least by organisation_id (uuid)
CREATE FUNCTION projects_by_organisation(organisation_id UUID) RETURNS TABLE (
id UUID,
slug VARCHAR,
title VARCHAR,
subtitle VARCHAR,
current_state VARCHAR,
date_start DATE,
updated_at TIMESTAMPTZ,
is_published BOOLEAN,
image_contain BOOLEAN,
is_featured BOOLEAN,
image_id VARCHAR,
organisation UUID,
status relation_status,
keywords citext[]
) LANGUAGE plpgsql STABLE AS
$$
BEGIN
RETURN QUERY
SELECT DISTINCT ON (project.id)
project.id,
project.slug,
project.title,
project.subtitle,
CASE
WHEN project.date_start IS NULL THEN 'Starting'::VARCHAR
WHEN project.date_start > now() THEN 'Starting'::VARCHAR
WHEN project.date_end < now() THEN 'Finished'::VARCHAR
ELSE 'Running'::VARCHAR
END AS current_state,
project.date_start,
project.updated_at,
project.is_published,
project.image_contain,
project_for_organisation.is_featured,
project.image_id,
project_for_organisation.organisation,
project_for_organisation.status,
keyword_filter_for_project.keywords
FROM
project
LEFT JOIN
project_for_organisation ON project.id = project_for_organisation.project
LEFT JOIN
keyword_filter_for_project() ON project.id=keyword_filter_for_project.project
WHERE
project_for_organisation.organisation IN (SELECT list_child_organisations.organisation_id FROM list_child_organisations(organisation_id))
;
END
$$;
-- Participating organisations by project
-- we filter this view by project_id UUID
CREATE FUNCTION organisations_of_project(project_id UUID) RETURNS TABLE (
id UUID,
slug VARCHAR,
primary_maintainer UUID,
name VARCHAR,
ror_id VARCHAR,
is_tenant BOOLEAN,
website VARCHAR,
rsd_path VARCHAR,
logo_id VARCHAR,
status relation_status,
role organisation_role,
"position" INTEGER,
project UUID,
parent UUID
) LANGUAGE plpgsql STABLE AS
$$
BEGIN
RETURN QUERY
SELECT
organisation.id AS id,
organisation.slug,
organisation.primary_maintainer,
organisation.name,
organisation.ror_id,
organisation.is_tenant,
organisation.website,
organisation_route.rsd_path,
organisation.logo_id,
project_for_organisation.status,
project_for_organisation.role,
project_for_organisation.position,
project.id AS project,
organisation.parent
FROM
project
INNER JOIN
project_for_organisation ON project.id = project_for_organisation.project
INNER JOIN
organisation ON project_for_organisation.organisation = organisation.id
LEFT JOIN
organisation_route(organisation.id) ON organisation_route.organisation = organisation.id
WHERE
project.id = project_id
;
END
$$;
-- RELATED PROJECTS for software
-- NOTE! updated by Dusan 2022-07-27
-- filter by software_id
CREATE FUNCTION related_projects_for_software(software_id UUID) RETURNS TABLE (
software UUID,
id UUID,
slug VARCHAR,
title VARCHAR,
subtitle VARCHAR,
current_state VARCHAR,
date_start DATE,
updated_at TIMESTAMPTZ,
is_published BOOLEAN,
image_contain BOOLEAN,
status relation_status,
image_id VARCHAR
) LANGUAGE plpgsql STABLE AS
$$
BEGIN
RETURN QUERY
SELECT
software_for_project.software,
project.id,
project.slug,
project.title,
project.subtitle,
CASE
WHEN project.date_start IS NULL THEN 'Starting'::VARCHAR
WHEN project.date_start > now() THEN 'Starting'::VARCHAR
WHEN project.date_end < now() THEN 'Finished'::VARCHAR
ELSE 'Running'::VARCHAR
END AS current_state,
project.date_start,
project.updated_at,
project.is_published,
project.image_contain,
software_for_project.status,
project.image_id
FROM
project
INNER JOIN
software_for_project ON project.id = software_for_project.project
WHERE
software_for_project.software = software_id
;
END
$$;
-- RELATED SOFTWARE for PROJECT
-- filter by project_id
CREATE FUNCTION related_software_for_project(project_id UUID) RETURNS TABLE (
id UUID,
slug VARCHAR,
brand_name VARCHAR,
short_statement VARCHAR,
updated_at TIMESTAMPTZ,
contributor_cnt BIGINT,
mention_cnt BIGINT,
is_published BOOLEAN,
status relation_status
) LANGUAGE plpgsql STABLE AS
$$
BEGIN
RETURN QUERY
SELECT
software.id,
software.slug,
software.brand_name,
software.short_statement,
software.updated_at,
count_software_countributors.contributor_cnt,
count_software_mentions.mention_cnt,
software.is_published,
software_for_project.status
FROM
software
LEFT JOIN
count_software_countributors() ON software.id=count_software_countributors.software
LEFT JOIN
count_software_mentions() ON software.id=count_software_mentions.software
INNER JOIN
software_for_project ON software.id=software_for_project.software
WHERE
software_for_project.project=project_id
;
END
$$;
-- Project maintainer by slug
-- there are similar functions for software maintainers
CREATE FUNCTION maintainer_for_project_by_slug() RETURNS TABLE (
maintainer UUID,
project UUID,
slug VARCHAR
) LANGUAGE plpgsql STABLE AS
$$
BEGIN
RETURN QUERY
SELECT
maintainer_for_project.maintainer,
maintainer_for_project.project,
project.slug
FROM
maintainer_for_project
LEFT JOIN
project ON project.id = maintainer_for_project.project;
END
$$;
-- Project maintainers list with basic personal info
-- used in the project maintainer list
CREATE FUNCTION maintainers_of_project(project_id UUID) RETURNS TABLE (
maintainer UUID,
name VARCHAR[],
email VARCHAR[],
affiliation VARCHAR[]
) LANGUAGE plpgsql STABLE SECURITY DEFINER AS
$$
DECLARE account_authenticated UUID;
BEGIN
account_authenticated = uuid(current_setting('request.jwt.claims', FALSE)::json->>'account');
IF account_authenticated IS NULL THEN
RAISE EXCEPTION USING MESSAGE = 'Please login first';
END IF;
IF project_id IS NULL THEN
RAISE EXCEPTION USING MESSAGE = 'Please provide a project id';
END IF;
IF NOT project_id IN (SELECT * FROM projects_of_current_maintainer()) AND
CURRENT_USER IS DISTINCT FROM 'rsd_admin' AND (
SELECT rolsuper FROM pg_roles WHERE rolname = CURRENT_USER
) IS DISTINCT FROM TRUE THEN
RAISE EXCEPTION USING MESSAGE = 'You are not a maintainer of this project';
END IF;
RETURN QUERY
SELECT
maintainer_for_project.maintainer,
ARRAY_AGG(login_for_account.name),
ARRAY_AGG(login_for_account.email),
ARRAY_AGG(login_for_account.home_organisation) AS affiliation
FROM
maintainer_for_project
JOIN
login_for_account ON maintainer_for_project.maintainer = login_for_account.account
WHERE maintainer_for_project.project = project_id
GROUP BY maintainer_for_project.maintainer;
RETURN;
END
$$;
-- Keywords with the count used in projects
-- used by search to show existing keywords with the count
CREATE FUNCTION keyword_count_for_projects() RETURNS TABLE (
id UUID,
keyword CITEXT,
cnt BIGINT
) LANGUAGE plpgsql STABLE AS
$$
BEGIN
RETURN QUERY
SELECT
keyword.id,
keyword.value AS keyword,
keyword_count.cnt
FROM
keyword
LEFT JOIN
(SELECT
keyword_for_project.keyword,
COUNT(keyword_for_project.keyword) AS cnt
FROM
keyword_for_project
GROUP BY keyword_for_project.keyword
) AS keyword_count ON keyword.id = keyword_count.keyword
;
END
$$;
-- Keywords by project
-- for selecting keywords of specific project
-- using filter ?project=eq.UUID
CREATE FUNCTION keywords_by_project() RETURNS TABLE (
id UUID,
keyword CITEXT,
project UUID
) LANGUAGE plpgsql STABLE AS
$$
BEGIN
RETURN QUERY
SELECT
keyword.id,
keyword.value AS keyword,
keyword_for_project.project
FROM
keyword_for_project
INNER JOIN
keyword ON keyword.id = keyword_for_project.keyword
;
END
$$;
-- Research domains by project
CREATE FUNCTION research_domain_by_project() RETURNS TABLE (
id UUID,
"key" VARCHAR,
name VARCHAR,
description VARCHAR,
project UUID
) LANGUAGE plpgsql STABLE AS
$$
BEGIN
RETURN QUERY
SELECT
research_domain.id,
research_domain.key,
research_domain.name,
research_domain.description,
research_domain_for_project.project
FROM
research_domain_for_project
INNER JOIN
research_domain ON research_domain.id=research_domain_for_project.research_domain
;
END
$$;
-- Software maintainers list with basic personal info
-- used in the software maintainer list
CREATE FUNCTION maintainers_of_software(software_id UUID) RETURNS TABLE (
maintainer UUID,
name VARCHAR[],
email VARCHAR[],
affiliation VARCHAR[]
) LANGUAGE plpgsql STABLE SECURITY DEFINER AS
$$
DECLARE account_authenticated UUID;
BEGIN
account_authenticated = uuid(current_setting('request.jwt.claims', FALSE)::json->>'account');
IF account_authenticated IS NULL THEN
RAISE EXCEPTION USING MESSAGE = 'Please login first';
END IF;
IF software_id IS NULL THEN
RAISE EXCEPTION USING MESSAGE = 'Please provide a software id';
END IF;
IF NOT software_id IN (SELECT * FROM software_of_current_maintainer()) AND
CURRENT_USER IS DISTINCT FROM 'rsd_admin' AND (
SELECT rolsuper FROM pg_roles WHERE rolname = CURRENT_USER
) IS DISTINCT FROM TRUE THEN
RAISE EXCEPTION USING MESSAGE = 'You are not a maintainer of this software';
END IF;
RETURN QUERY
SELECT
maintainer_for_software.maintainer,
ARRAY_AGG(login_for_account.name),
ARRAY_AGG(login_for_account.email),
ARRAY_AGG(login_for_account.home_organisation) AS affiliation
FROM
maintainer_for_software
JOIN
login_for_account ON maintainer_for_software.maintainer = login_for_account.account
WHERE maintainer_for_software.software = software_id
GROUP BY maintainer_for_software.maintainer;
RETURN;
END
$$;
-- SOFTWARE BY MAINTAINER
-- NOTE! one software is shown multiple times in this view
-- we filter this view at least by organisation uuid
CREATE FUNCTION software_by_maintainer(maintainer_id UUID) RETURNS TABLE (
id UUID,
slug VARCHAR,
brand_name VARCHAR,
short_statement VARCHAR,
is_published BOOLEAN,
updated_at TIMESTAMPTZ,
contributor_cnt BIGINT,
mention_cnt BIGINT
) LANGUAGE plpgsql STABLE AS
$$
BEGIN
RETURN QUERY
SELECT
software.id,
software.slug,
software.brand_name,
software.short_statement,
software.is_published,
software.updated_at,
count_software_countributors.contributor_cnt,
count_software_mentions.mention_cnt
FROM
software
LEFT JOIN
count_software_countributors() ON software.id=count_software_countributors.software
LEFT JOIN
count_software_mentions() ON software.id=count_software_mentions.software
INNER JOIN
maintainer_for_software ON software.id=maintainer_for_software.software
WHERE
maintainer_for_software.maintainer=maintainer_id
;
END
$$;
-- PROJECTS BY MAINTAINER
-- NOTE! updated by Dusan on 2022-07-27
-- we filter this view at least by user acount (uuid)
CREATE FUNCTION projects_by_maintainer(maintainer_id UUID) RETURNS TABLE (
id UUID,
slug VARCHAR,
title VARCHAR,
subtitle VARCHAR,
current_state VARCHAR,
date_start DATE,
updated_at TIMESTAMPTZ,
is_published BOOLEAN,
image_contain BOOLEAN,
image_id VARCHAR
) LANGUAGE plpgsql STABLE AS
$$
BEGIN
RETURN QUERY
SELECT
project.id,
project.slug,
project.title,
project.subtitle,
CASE
WHEN project.date_start IS NULL THEN 'Starting'::VARCHAR
WHEN project.date_start > now() THEN 'Starting'::VARCHAR
WHEN project.date_end < now() THEN 'Finished'::VARCHAR
ELSE 'Running'::VARCHAR
END AS current_state,
project.date_start,
project.updated_at,
project.is_published,
project.image_contain,
project.image_id
FROM
project
INNER JOIN
maintainer_for_project ON project.id = maintainer_for_project.project
WHERE
maintainer_for_project.maintainer = maintainer_id;
END
$$;
-- ORGANISATIONS BY MAINTAINER
-- NOTE! each organisation is shown multiple times in this view
-- we filter this view at least by user acount (maintainer_id uuid) on primary_maintainer or maintainer
CREATE FUNCTION organisations_by_maintainer(maintainer_id UUID) RETURNS TABLE (
id UUID,
slug VARCHAR,
parent UUID,
primary_maintainer UUID,