generated from Ctri-The-Third/PythonTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpaceTraders.sql
2671 lines (2203 loc) · 92.6 KB
/
SpaceTraders.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
--
-- PostgreSQL database dump
--
-- Dumped from database version 13.11 (Debian 13.11-0+deb11u1)
-- Dumped by pg_dump version 15.3
-- Started on 2023-12-22 12:21:00
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
--
-- TOC entry 6 (class 2615 OID 2200)
-- Name: public; Type: SCHEMA; Schema: -; Owner: spacetraders
--
-- *not* creating schema, since initdb creates it
ALTER SCHEMA public OWNER TO spacetraders;
--
-- TOC entry 2 (class 3079 OID 56145)
-- Name: pg_stat_statements; Type: EXTENSION; Schema: -; Owner: -
--
CREATE EXTENSION IF NOT EXISTS pg_stat_statements WITH SCHEMA public;
--
-- TOC entry 3496 (class 0 OID 0)
-- Dependencies: 2
-- Name: EXTENSION pg_stat_statements; Type: COMMENT; Schema: -; Owner:
--
COMMENT ON EXTENSION pg_stat_statements IS 'track planning and execution statistics of all SQL statements executed';
SET default_tablespace = '';
SET default_table_access_method = heap;
--
-- TOC entry 201 (class 1259 OID 40386)
-- Name: agents; Type: TABLE; Schema: public; Owner: spacetraders
--
CREATE TABLE public.agents (
agent_symbol text NOT NULL,
headquarters text,
credits integer,
starting_faction text,
ship_count integer,
last_updated timestamp without time zone
);
ALTER TABLE public.agents OWNER TO spacetraders;
--
-- TOC entry 205 (class 1259 OID 40414)
-- Name: logging; Type: TABLE; Schema: public; Owner: spacetraders
--
CREATE TABLE public.logging (
event_name text,
event_timestamp timestamp without time zone NOT NULL,
agent_name text,
ship_symbol text NOT NULL,
session_id text,
endpoint_name text,
new_credits integer,
status_code integer,
error_code integer,
event_params jsonb,
duration_seconds numeric
);
ALTER TABLE public.logging OWNER TO spacetraders;
--
-- TOC entry 203 (class 1259 OID 40398)
-- Name: transactions; Type: TABLE; Schema: public; Owner: spacetraders
--
CREATE TABLE public.transactions (
waypoint_symbol text,
ship_symbol text NOT NULL,
trade_symbol text,
type text,
units integer,
price_per_unit integer,
total_price numeric,
session_id text,
"timestamp" timestamp without time zone NOT NULL
);
ALTER TABLE public.transactions OWNER TO spacetraders;
--
-- TOC entry 238 (class 1259 OID 40974)
-- Name: mat_session_stats; Type: MATERIALIZED VIEW; Schema: public; Owner: spacetraders
--
CREATE MATERIALIZED VIEW public.mat_session_stats AS
WITH sessions_and_requests AS (
SELECT logging.session_id,
min(logging.event_timestamp) AS session_start,
max(logging.event_timestamp) AS session_end,
count(
CASE
WHEN ((logging.status_code > 0) AND (logging.status_code <> ALL (ARRAY[404, 429, 500]))) THEN 1
ELSE NULL::integer
END) AS requests,
count(
CASE
WHEN (logging.status_code = 429) THEN 1
ELSE NULL::integer
END) AS delayed_requests
FROM public.logging
WHERE (logging.event_timestamp >= (now() - '3 days'::interval))
GROUP BY logging.session_id
), sessions_and_earnings AS (
SELECT t.session_id,
sum(t.total_price) AS earnings
FROM public.transactions t
WHERE ((t.type = 'SELL'::text) AND (t."timestamp" >= (now() - '3 days'::interval)))
GROUP BY t.session_id
), sessions_and_ship_symbols AS (
SELECT DISTINCT logging.ship_symbol,
logging.session_id
FROM public.logging
WHERE (logging.ship_symbol <> 'GLOBAL'::text)
), sessions_and_behaviours AS (
SELECT l.session_id,
(l.event_params ->> 'script_name'::text) AS behaviour_id
FROM public.logging l
WHERE (l.event_name = 'BEGIN_BEHAVIOUR_SCRIPT'::text)
)
SELECT sas.ship_symbol,
sar.session_start,
sar.session_id,
COALESCE(sab.behaviour_id, 'BEHAVIOUR_NOT_RECORDED'::text) AS behaviour_id,
COALESCE(ear.earnings, (0)::numeric) AS earnings,
sar.requests,
sar.delayed_requests,
(COALESCE(ear.earnings, (0)::numeric) / (
CASE
WHEN (sar.requests = 0) THEN (1)::bigint
ELSE sar.requests
END)::numeric) AS cpr
FROM (((sessions_and_requests sar
LEFT JOIN sessions_and_earnings ear ON ((ear.session_id = sar.session_id)))
LEFT JOIN sessions_and_ship_symbols sas ON ((sar.session_id = sas.session_id)))
LEFT JOIN sessions_and_behaviours sab ON ((sar.session_id = sab.session_id)))
ORDER BY sar.session_start DESC
WITH NO DATA;
ALTER TABLE public.mat_session_stats OWNER TO spacetraders;
--
-- TOC entry 202 (class 1259 OID 40392)
-- Name: ships; Type: TABLE; Schema: public; Owner: spacetraders
--
CREATE TABLE public.ships (
ship_symbol text NOT NULL,
agent_name text,
faction_symbol text,
ship_role text,
cargo_capacity integer,
cargo_in_use integer,
last_updated timestamp without time zone,
fuel_capacity integer,
fuel_current integer,
mount_symbols text[],
module_symbols text[]
);
ALTER TABLE public.ships OWNER TO spacetraders;
--
-- TOC entry 255 (class 1259 OID 77508)
-- Name: agent_credits_per_hour; Type: VIEW; Schema: public; Owner: spacetraders
--
CREATE VIEW public.agent_credits_per_hour AS
WITH t_data AS (
SELECT s.agent_name,
sum(
CASE
WHEN (t_1.type = 'SELL'::text) THEN t_1.total_price
ELSE
CASE
WHEN (t_1.type = 'PURCHASE'::text) THEN (t_1.total_price * ('-1'::integer)::numeric)
ELSE NULL::numeric
END
END) AS credits_earned,
date_trunc('hour'::text, t_1."timestamp") AS event_hour
FROM ((public.transactions t_1
JOIN public.ships s ON ((t_1.ship_symbol = s.ship_symbol)))
JOIN public.agents a ON ((s.agent_name = a.agent_symbol)))
GROUP BY s.agent_name, (date_trunc('hour'::text, t_1."timestamp"))
), l_data AS (
SELECT s.agent_name,
date_trunc('hour'::text, mss.session_start) AS event_hour,
round((sum(mss.requests) / (60)::numeric), 2) AS rpm
FROM (public.mat_session_stats mss
JOIN public.ships s ON ((mss.ship_symbol = s.ship_symbol)))
GROUP BY s.agent_name, (date_trunc('hour'::text, mss.session_start))
)
SELECT t.agent_name,
t.credits_earned,
l.rpm,
t.event_hour
FROM (t_data t
LEFT JOIN l_data l ON (((t.agent_name = l.agent_name) AND (t.event_hour = l.event_hour))))
ORDER BY t.event_hour DESC, t.agent_name;
ALTER TABLE public.agent_credits_per_hour OWNER TO spacetraders;
--
-- TOC entry 204 (class 1259 OID 40409)
-- Name: agent_overview; Type: VIEW; Schema: public; Owner: spacetraders
--
CREATE VIEW public.agent_overview AS
SELECT a.agent_symbol,
a.credits,
a.starting_faction,
count(DISTINCT s.ship_symbol) AS ship_count,
a.last_updated
FROM (public.agents a
JOIN public.ships s ON ((s.agent_name = a.agent_symbol)))
GROUP BY a.agent_symbol, a.credits, a.starting_faction, a.last_updated
ORDER BY a.last_updated DESC;
ALTER TABLE public.agent_overview OWNER TO spacetraders;
--
-- TOC entry 279 (class 1259 OID 101804)
-- Name: construction_site_materials; Type: TABLE; Schema: public; Owner: spacetraders
--
CREATE TABLE public.construction_site_materials (
waypoint_symbol text NOT NULL,
trade_symbol text NOT NULL,
required integer,
fulfilled integer
);
ALTER TABLE public.construction_site_materials OWNER TO spacetraders;
--
-- TOC entry 278 (class 1259 OID 101796)
-- Name: construction_sites; Type: TABLE; Schema: public; Owner: spacetraders
--
CREATE TABLE public.construction_sites (
waypoint_symbol text NOT NULL,
is_complete boolean
);
ALTER TABLE public.construction_sites OWNER TO spacetraders;
--
-- TOC entry 207 (class 1259 OID 40439)
-- Name: contract_tradegoods; Type: TABLE; Schema: public; Owner: spacetraders
--
CREATE TABLE public.contract_tradegoods (
contract_id text NOT NULL,
trade_symbol text NOT NULL,
destination_symbol text,
units_required integer,
units_fulfilled integer
);
ALTER TABLE public.contract_tradegoods OWNER TO spacetraders;
--
-- TOC entry 208 (class 1259 OID 40445)
-- Name: contracts; Type: TABLE; Schema: public; Owner: spacetraders
--
CREATE TABLE public.contracts (
id text NOT NULL,
faction_symbol text,
type text,
accepted boolean,
fulfilled boolean,
expiration timestamp without time zone,
deadline timestamp without time zone,
agent_symbol text,
payment_upfront integer,
payment_on_completion integer,
offering_faction text
);
ALTER TABLE public.contracts OWNER TO spacetraders;
--
-- TOC entry 209 (class 1259 OID 40451)
-- Name: contracts_overview; Type: VIEW; Schema: public; Owner: spacetraders
--
CREATE VIEW public.contracts_overview AS
SELECT co.agent_symbol,
ct.trade_symbol,
round((((ct.units_fulfilled)::numeric / (ct.units_required)::numeric) * (100)::numeric), 2) AS progress,
ct.units_required,
ct.units_fulfilled,
co.expiration,
(co.payment_on_completion / ct.units_required) AS payment_per_item,
co.fulfilled
FROM (public.contracts co
JOIN public.contract_tradegoods ct ON ((co.id = ct.contract_id)))
ORDER BY (co.fulfilled = true);
ALTER TABLE public.contracts_overview OWNER TO spacetraders;
--
-- TOC entry 269 (class 1259 OID 101214)
-- Name: export_overview; Type: VIEW; Schema: public; Owner: spacetraders
--
CREATE VIEW public.export_overview AS
SELECT
NULL::text AS system_symbol,
NULL::text AS market_symbol,
NULL::text AS trade_symbol,
NULL::text AS supply,
NULL::text AS activity,
NULL::integer AS purchase_price,
NULL::integer AS sell_price,
NULL::integer AS market_depth,
NULL::bigint AS units_sold_recently,
NULL::text[] AS requirements;
ALTER TABLE public.export_overview OWNER TO spacetraders;
--
-- TOC entry 248 (class 1259 OID 64900)
-- Name: extractions; Type: TABLE; Schema: public; Owner: spacetraders
--
CREATE TABLE public.extractions (
ship_symbol text NOT NULL,
session_id text,
event_timestamp timestamp without time zone NOT NULL,
waypoint_symbol text,
survey_signature text,
trade_symbol text,
quantity integer
);
ALTER TABLE public.extractions OWNER TO spacetraders;
--
-- TOC entry 217 (class 1259 OID 40503)
-- Name: market_tradegood_listings; Type: TABLE; Schema: public; Owner: spacetraders
--
CREATE TABLE public.market_tradegood_listings (
market_symbol text NOT NULL,
trade_symbol text NOT NULL,
supply text,
purchase_price integer,
sell_price integer,
last_updated timestamp without time zone,
market_depth integer,
type text,
activity text
);
ALTER TABLE public.market_tradegood_listings OWNER TO spacetraders;
--
-- TOC entry 267 (class 1259 OID 84842)
-- Name: market_changes; Type: VIEW; Schema: public; Owner: spacetraders
--
CREATE VIEW public.market_changes AS
WITH "extract" AS (
SELECT l.event_timestamp,
(l.event_params ->> 'market_symbol'::text) AS market_symbol,
(l.event_params ->> 'trade_symbol'::text) AS trade_symbol,
(l.event_params ->> 'activity'::text) AS activity,
(l.event_params ->> 'supply'::text) AS supply,
((l.event_params ->> 'purchase_price'::text))::numeric AS current_purchase_price,
((l.event_params ->> 'purchase_price_change'::text))::numeric AS cpp_change,
((l.event_params ->> 'sell_price'::text))::numeric AS current_sell_price,
((l.event_params ->> 'sell_price_change'::text))::numeric AS csp_change,
((l.event_params ->> 'trade_volume'::text))::numeric AS current_trade_volume,
l.event_params
FROM public.logging l
WHERE (l.event_name = 'MARKET_CHANGES'::text)
ORDER BY l.event_timestamp DESC
)
SELECT e.event_timestamp,
e.market_symbol,
e.trade_symbol,
mtl.type,
e.activity,
e.supply,
e.current_purchase_price,
e.cpp_change,
e.current_sell_price,
e.csp_change,
e.current_trade_volume
FROM ("extract" e
JOIN public.market_tradegood_listings mtl ON (((e.market_symbol = mtl.market_symbol) AND (e.trade_symbol = mtl.trade_symbol))));
ALTER TABLE public.market_changes OWNER TO spacetraders;
--
-- TOC entry 213 (class 1259 OID 40480)
-- Name: waypoints; Type: TABLE; Schema: public; Owner: spacetraders
--
CREATE TABLE public.waypoints (
waypoint_symbol text NOT NULL,
type text NOT NULL,
system_symbol text NOT NULL,
x smallint NOT NULL,
y smallint NOT NULL,
checked boolean DEFAULT false NOT NULL,
modifiers text[],
under_construction boolean DEFAULT false NOT NULL
);
ALTER TABLE public.waypoints OWNER TO spacetraders;
--
-- TOC entry 280 (class 1259 OID 101823)
-- Name: hourly_utilisation_of_exports; Type: VIEW; Schema: public; Owner: spacetraders
--
CREATE VIEW public.hourly_utilisation_of_exports AS
WITH exports_in_last_hour AS (
SELECT mtl.market_symbol,
mtl.trade_symbol,
max(COALESCE(mc.current_trade_volume, (mtl.market_depth)::numeric)) AS max_tv
FROM (public.market_tradegood_listings mtl
LEFT JOIN public.market_changes mc ON (((mtl.market_symbol = mc.market_symbol) AND (mtl.trade_symbol = mc.trade_symbol))))
WHERE (((mc.event_timestamp IS NULL) OR (mc.event_timestamp >= (now() - '01:00:00'::interval))) AND ((mc.supply IS NULL) OR (mc.supply = ANY (ARRAY['MODERATE'::text, 'HIGH'::text, 'ABUNDANT'::text]))) AND (mtl.type = 'EXPORT'::text))
GROUP BY mtl.market_symbol, mtl.trade_symbol
), utilisation_in_last_hour AS (
SELECT elh.market_symbol,
elh.trade_symbol,
elh.max_tv,
sum(
CASE
WHEN (t.type = 'PURCHASE'::text) THEN t.units
ELSE NULL::integer
END) AS goods_exported
FROM (exports_in_last_hour elh
LEFT JOIN public.transactions t ON (((t.waypoint_symbol = elh.market_symbol) AND (t.trade_symbol = elh.trade_symbol))))
WHERE ((t."timestamp" IS NULL) OR (t."timestamp" >= (now() - '01:00:00'::interval)))
GROUP BY elh.market_symbol, elh.trade_symbol, elh.max_tv
)
SELECT w.system_symbol,
mt.market_symbol,
mt.trade_symbol,
COALESCE(uilh.max_tv, (mt.market_depth)::numeric) AS max_tv,
uilh.goods_exported
FROM ((public.market_tradegood_listings mt
LEFT JOIN public.waypoints w ON ((w.waypoint_symbol = mt.market_symbol)))
LEFT JOIN utilisation_in_last_hour uilh ON (((mt.market_symbol = uilh.market_symbol) AND (mt.trade_symbol = uilh.trade_symbol))));
ALTER TABLE public.hourly_utilisation_of_exports OWNER TO spacetraders;
--
-- TOC entry 268 (class 1259 OID 101209)
-- Name: import_overview; Type: VIEW; Schema: public; Owner: spacetraders
--
CREATE VIEW public.import_overview AS
SELECT
NULL::text AS system_symbol,
NULL::text AS market_symbol,
NULL::text AS trade_symbol,
NULL::text AS supply,
NULL::text AS activity,
NULL::integer AS purchase_price,
NULL::integer AS sell_price,
NULL::integer AS market_depth,
NULL::bigint AS units_sold_recently;
ALTER TABLE public.import_overview OWNER TO spacetraders;
--
-- TOC entry 210 (class 1259 OID 40456)
-- Name: jump_gates; Type: TABLE; Schema: public; Owner: spacetraders
--
CREATE TABLE public.jump_gates (
waypoint_symbol text NOT NULL,
faction_symbol text,
jump_range integer
);
ALTER TABLE public.jump_gates OWNER TO spacetraders;
--
-- TOC entry 286 (class 1259 OID 101995)
-- Name: jumpgate_connections; Type: TABLE; Schema: public; Owner: spacetraders
--
CREATE TABLE public.jumpgate_connections (
s_waypoint_symbol text,
s_system_symbol text NOT NULL,
d_waypoint_symbol text NOT NULL,
d_system_symbol text NOT NULL
);
ALTER TABLE public.jumpgate_connections OWNER TO spacetraders;
--
-- TOC entry 211 (class 1259 OID 40468)
-- Name: waypoint_charts; Type: TABLE; Schema: public; Owner: spacetraders
--
CREATE TABLE public.waypoint_charts (
waypoint_symbol text NOT NULL,
submitted_by text NOT NULL,
submitted_on timestamp without time zone NOT NULL
);
ALTER TABLE public.waypoint_charts OWNER TO spacetraders;
--
-- TOC entry 212 (class 1259 OID 40474)
-- Name: waypoint_traits; Type: TABLE; Schema: public; Owner: spacetraders
--
CREATE TABLE public.waypoint_traits (
waypoint_symbol text NOT NULL,
trait_symbol text NOT NULL,
name text,
description text
);
ALTER TABLE public.waypoint_traits OWNER TO spacetraders;
--
-- TOC entry 214 (class 1259 OID 40487)
-- Name: jumpgates_scanned; Type: VIEW; Schema: public; Owner: spacetraders
--
CREATE VIEW public.jumpgates_scanned AS
SELECT w.waypoint_symbol,
(count(
CASE
WHEN (wt.trait_symbol = 'UNCHARTED'::text) THEN 1
ELSE NULL::integer
END) > 0) AS uncharted,
(count(
CASE
WHEN (wc.waypoint_symbol IS NOT NULL) THEN 1
ELSE NULL::integer
END) > 0) AS charted,
(count(
CASE
WHEN (jg.waypoint_symbol IS NOT NULL) THEN 1
ELSE NULL::integer
END) > 0) AS scanned
FROM (((public.waypoints w
LEFT JOIN public.waypoint_traits wt ON (((wt.waypoint_symbol = w.waypoint_symbol) AND (wt.trait_symbol = 'UNCHARTED'::text))))
LEFT JOIN public.waypoint_charts wc ON ((wc.waypoint_symbol = w.waypoint_symbol)))
LEFT JOIN public.jump_gates jg ON ((jg.waypoint_symbol = w.waypoint_symbol)))
WHERE (w.type = 'JUMP_GATE'::text)
GROUP BY w.waypoint_symbol;
ALTER TABLE public.jumpgates_scanned OWNER TO spacetraders;
--
-- TOC entry 215 (class 1259 OID 40492)
-- Name: jumpgates_scanned_progress; Type: VIEW; Schema: public; Owner: spacetraders
--
CREATE VIEW public.jumpgates_scanned_progress AS
WITH data AS (
SELECT count(*) AS total_gates,
count(
CASE
WHEN js.scanned THEN 1
ELSE NULL::integer
END) AS scanned_gates,
count(
CASE
WHEN js.charted THEN 1
ELSE NULL::integer
END) AS charted_gates
FROM public.jumpgates_scanned js
)
SELECT 'charted jumpgates scanned'::text AS title,
data.scanned_gates,
data.charted_gates,
CASE
WHEN (data.scanned_gates > 0) THEN round((((data.charted_gates)::numeric / (data.scanned_gates)::numeric) * (100)::numeric), 2)
ELSE NULL::numeric
END AS progress
FROM data;
ALTER TABLE public.jumpgates_scanned_progress OWNER TO spacetraders;
--
-- TOC entry 261 (class 1259 OID 79516)
-- Name: manufacture_relationships; Type: TABLE; Schema: public; Owner: spacetraders
--
CREATE TABLE public.manufacture_relationships (
export_tradegood text NOT NULL,
import_tradegoods text[] NOT NULL
);
ALTER TABLE public.manufacture_relationships OWNER TO spacetraders;
--
-- TOC entry 216 (class 1259 OID 40497)
-- Name: market; Type: TABLE; Schema: public; Owner: spacetraders
--
CREATE TABLE public.market (
symbol text NOT NULL,
system_symbol text
);
ALTER TABLE public.market OWNER TO spacetraders;
--
-- TOC entry 263 (class 1259 OID 79847)
-- Name: market_prices; Type: VIEW; Schema: public; Owner: spacetraders
--
CREATE VIEW public.market_prices AS
SELECT mtl.trade_symbol,
round(avg(mtl.purchase_price) FILTER (WHERE (mtl.type = 'EXPORT'::text)), 2) AS export_price,
round(avg(mtl.sell_price) FILTER (WHERE (mtl.type = 'IMPORT'::text)), 2) AS import_price,
round(avg(((mtl.purchase_price + mtl.sell_price) / 2)), 2) AS galactic_average
FROM public.market_tradegood_listings mtl
GROUP BY mtl.trade_symbol
ORDER BY mtl.trade_symbol;
ALTER TABLE public.market_prices OWNER TO spacetraders;
--
-- TOC entry 218 (class 1259 OID 40513)
-- Name: market_tradegood; Type: TABLE; Schema: public; Owner: spacetraders
--
CREATE TABLE public.market_tradegood (
market_waypoint text NOT NULL,
symbol text NOT NULL,
buy_or_sell text,
name text,
description text,
market_symbol text,
trade_symbol text,
"type " text
);
ALTER TABLE public.market_tradegood OWNER TO spacetraders;
--
-- TOC entry 283 (class 1259 OID 101942)
-- Name: market_tradegoods; Type: VIEW; Schema: public; Owner: spacetraders
--
CREATE VIEW public.market_tradegoods AS
WITH buy_or_sell_vs(buy_or_sell, type) AS (
VALUES ('sell'::text,'EXPORT'::text), ('buy'::text,'IMPORT'::text), ('exchange'::text,'EXCHANGE'::text)
), market_tradegoods AS (
SELECT mt.market_waypoint AS market_symbol,
COALESCE(mt.trade_symbol, mt.symbol) AS trade_symbol,
COALESCE(bs.type, bs.type) AS type,
mt.name,
mt.description
FROM (public.market_tradegood mt
JOIN buy_or_sell_vs bs ON ((mt.buy_or_sell = bs.buy_or_sell)))
)
SELECT market_tradegoods.market_symbol,
market_tradegoods.trade_symbol,
market_tradegoods.type,
market_tradegoods.name,
market_tradegoods.description
FROM market_tradegoods;
ALTER TABLE public.market_tradegoods OWNER TO spacetraders;
--
-- TOC entry 284 (class 1259 OID 101948)
-- Name: mat_market_connctions; Type: MATERIALIZED VIEW; Schema: public; Owner: spacetraders
--
CREATE MATERIALIZED VIEW public.mat_market_connctions AS
WITH mts AS (
SELECT w.system_symbol,
mt.market_symbol,
mt.trade_symbol,
mt.type
FROM (public.market_tradegoods mt
JOIN public.waypoints w ON ((mt.market_symbol = w.waypoint_symbol)))
)
SELECT (mts_e.system_symbol = mts_i.system_symbol) AS intrasolar_only,
mts_e.trade_symbol,
mts_e.system_symbol AS export_system,
mts_e.market_symbol AS export_market,
mts_e.type AS export_type,
mts_i.system_symbol AS import_system,
mts_i.market_symbol AS import_markt,
mts_i.type AS import_type
FROM (mts mts_e
JOIN mts mts_i ON (((mts_e.trade_symbol = mts_i.trade_symbol) AND (mts_e.market_symbol <> mts_i.market_symbol) AND (mts_e.type = ANY (ARRAY['EXPORT'::text, 'EXCHANGE'::text])) AND (mts_i.type = ANY (ARRAY['IMPORT'::text, 'EXCHANGE'::text])))))
WITH NO DATA;
ALTER TABLE public.mat_market_connctions OWNER TO spacetraders;
--
-- TOC entry 249 (class 1259 OID 65641)
-- Name: mat_session_behaviour_types; Type: MATERIALIZED VIEW; Schema: public; Owner: spacetraders
--
CREATE MATERIALIZED VIEW public.mat_session_behaviour_types AS
SELECT l.session_id,
l.event_timestamp AS session_start,
l.ship_symbol,
s.agent_name,
(l.event_params ->> 'script_name'::text) AS behaviour_name
FROM (public.logging l
JOIN public.ships s ON ((l.ship_symbol = s.ship_symbol)))
WHERE (l.event_name = 'BEGIN_BEHAVIOUR_SCRIPT'::text)
WITH NO DATA;
ALTER TABLE public.mat_session_behaviour_types OWNER TO spacetraders;
--
-- TOC entry 256 (class 1259 OID 79002)
-- Name: mat_session_stats_2; Type: MATERIALIZED VIEW; Schema: public; Owner: spacetraders
--
CREATE MATERIALIZED VIEW public.mat_session_stats_2 AS
WITH beginnings AS (
SELECT l.session_id,
l.event_timestamp AS session_start,
l.ship_symbol,
s.agent_name,
(l.event_params ->> 'script_name'::text) AS behaviour_name
FROM (public.logging l
JOIN public.ships s ON ((l.ship_symbol = s.ship_symbol)))
WHERE (l.event_name = 'BEGIN_BEHAVIOUR_SCRIPT'::text)
), ends AS (
SELECT l.session_id,
l.event_timestamp AS session_end
FROM public.logging l
WHERE (l.event_name = 'END_BEHAVIOUR_SCRIPT'::text)
), earnings AS (
SELECT transactions.session_id,
sum(
CASE
WHEN (transactions.type = 'SELL'::text) THEN transactions.total_price
ELSE NULL::numeric
END) AS earnings,
sum(
CASE
WHEN (transactions.type = 'PURCHASE'::text) THEN transactions.total_price
ELSE NULL::numeric
END) AS losses,
sum(
CASE
WHEN (transactions.type = 'SELL'::text) THEN transactions.total_price
ELSE
CASE
WHEN (transactions.type = 'PURCHASE'::text) THEN (transactions.total_price * ('-1'::integer)::numeric)
ELSE NULL::numeric
END
END) AS net_earnings
FROM public.transactions
GROUP BY transactions.session_id
), request_stats AS (
SELECT l.session_id,
count(
CASE
WHEN ((l.status_code > 0) AND (l.status_code < 500) AND (l.status_code <> 429)) THEN 1
ELSE NULL::integer
END) AS requests
FROM public.logging l
GROUP BY l.session_id
)
SELECT b.session_id,
b.session_start,
e.session_end,
(e.session_end - b.session_start) AS duration,
date_part('epoch'::text, (e.session_end - b.session_start)) AS duration_secs,
b.ship_symbol,
b.agent_name,
b.behaviour_name,
ea.earnings,
ea.losses,
ea.net_earnings,
r.requests,
(ea.net_earnings / (r.requests)::numeric) AS cpr,
((ea.net_earnings)::double precision / date_part('epoch'::text, (e.session_end - b.session_start))) AS cps
FROM (((beginnings b
LEFT JOIN ends e ON ((b.session_id = e.session_id)))
LEFT JOIN earnings ea ON ((b.session_id = ea.session_id)))
LEFT JOIN request_stats r ON ((b.session_id = r.session_id)))
WHERE (e.session_id IS NOT NULL)
ORDER BY (e.session_end - b.session_start) DESC
WITH NO DATA;
ALTER TABLE public.mat_session_stats_2 OWNER TO spacetraders;
--
-- TOC entry 206 (class 1259 OID 40428)
-- Name: ship_behaviours; Type: TABLE; Schema: public; Owner: spacetraders
--
CREATE TABLE public.ship_behaviours (
ship_symbol text NOT NULL,
behaviour_id text,
locked_by text,
locked_until timestamp without time zone,
behaviour_params jsonb
);
ALTER TABLE public.ship_behaviours OWNER TO spacetraders;
--
-- TOC entry 276 (class 1259 OID 101735)
-- Name: warnings_activity; Type: VIEW; Schema: public; Owner: spacetraders
--
CREATE VIEW public.warnings_activity AS
SELECT l.ship_symbol,
(max(l.event_timestamp) < (now() - '01:00:00'::interval)) AS warning_active,
max(l.event_timestamp) AS max
FROM public.logging l
WHERE ((l.event_name = 'END_BEHAVIOUR_SCRIPT'::text) AND (l.event_timestamp > (now() - '1 day'::interval)))
GROUP BY l.ship_symbol;
ALTER TABLE public.warnings_activity OWNER TO spacetraders;
--
-- TOC entry 274 (class 1259 OID 101725)
-- Name: warnings_extraction; Type: VIEW; Schema: public; Owner: spacetraders
--
CREATE VIEW public.warnings_extraction AS
SELECT l.ship_symbol,
(max(l.event_timestamp) < (now() - '01:00:00'::interval)) AS warning_active,
max(l.event_timestamp) AS most_recent_extraction
FROM (public.logging l
JOIN public.ship_behaviours sb ON ((l.ship_symbol = sb.ship_symbol)))
WHERE ((l.event_name = ANY (ARRAY['ship_extract'::text, 'ship_siphon'::text])) AND (l.status_code >= 200) AND (l.status_code < 300) AND (l.event_timestamp > (now() - '1 day'::interval)) AND (sb.behaviour_id = ANY (ARRAY['EXTRACT_AND_CHILL'::text, 'EXTRACT_AND_GO_SELL'::text, 'SIPHON_AND_CHILL'::text])))
GROUP BY l.ship_symbol
ORDER BY l.ship_symbol;
ALTER TABLE public.warnings_extraction OWNER TO spacetraders;
--
-- TOC entry 281 (class 1259 OID 101836)
-- Name: warnings_movement; Type: VIEW; Schema: public; Owner: spacetraders
--
CREATE VIEW public.warnings_movement AS
SELECT l.ship_symbol,
(max(l.event_timestamp) < (now() - '01:00:00'::interval)) AS warning_active,
max(l.event_timestamp) AS most_recent_move
FROM (public.logging l
JOIN public.ship_behaviours sb ON ((l.ship_symbol = sb.ship_symbol)))
WHERE ((l.event_name = ANY (ARRAY['ship_move'::text, 'ship_warp'::text])) AND (l.status_code >= 200) AND (l.status_code < 300) AND (l.event_timestamp > (now() - '1 day'::interval)) AND (sb.behaviour_id = ANY (ARRAY['BUY_AND_DELIVER_OR_SELL'::text, 'CONSTRUCT_JUMPGATE'::text, 'CHAIN_TRADES'::text])))
GROUP BY l.ship_symbol
ORDER BY l.ship_symbol;
ALTER TABLE public.warnings_movement OWNER TO spacetraders;
--
-- TOC entry 275 (class 1259 OID 101730)
-- Name: warnings_profits; Type: VIEW; Schema: public; Owner: spacetraders
--
CREATE VIEW public.warnings_profits AS
WITH profits AS (
SELECT tr.ship_symbol,
(sum(
CASE
WHEN (tr.type = 'SELL'::text) THEN tr.total_price
ELSE (0)::numeric
END) - sum(
CASE
WHEN (tr.type = 'PURCHASE'::text) THEN tr.total_price
ELSE (0)::numeric
END)) AS profit
FROM public.transactions tr
WHERE (tr."timestamp" > (now() - '01:00:00'::interval))
GROUP BY tr.ship_symbol
ORDER BY tr.ship_symbol
)
SELECT pr.ship_symbol,
(pr.profit > (0)::numeric) AS warning_active,
pr.profit
FROM (profits pr
JOIN public.ship_behaviours sb ON ((sb.ship_symbol = pr.ship_symbol)))
WHERE (sb.behaviour_id = ANY (ARRAY['MANAGE_SPECIFIC_EXPORT'::text, 'CHAIN_TRADES'::text, 'BUY_AND_DELIVER_OR_SELL_6'::text]))
ORDER BY pr.ship_symbol;
ALTER TABLE public.warnings_profits OWNER TO spacetraders;
--
-- TOC entry 282 (class 1259 OID 101869)
-- Name: mat_ship_warnings; Type: MATERIALIZED VIEW; Schema: public; Owner: spacetraders
--
CREATE MATERIALIZED VIEW public.mat_ship_warnings AS
SELECT s.agent_name,
s.ship_symbol,
wa.warning_active AS activity_warning,
we.warning_active AS extraction_warning,
wp.warning_active AS profit_warning,
wm.warning_active AS movement_warning,
now() AS last_updated
FROM ((((public.ships s
LEFT JOIN public.warnings_activity wa ON ((wa.ship_symbol = s.ship_symbol)))
LEFT JOIN public.warnings_extraction we ON ((s.ship_symbol = we.ship_symbol)))
LEFT JOIN public.warnings_profits wp ON ((s.ship_symbol = wp.ship_symbol)))
LEFT JOIN public.warnings_movement wm ON ((s.ship_symbol = wm.ship_symbol)))
WITH NO DATA;
ALTER TABLE public.mat_ship_warnings OWNER TO spacetraders;
--
-- TOC entry 219 (class 1259 OID 40519)
-- Name: mat_shipyardtypes_to_ship; Type: MATERIALIZED VIEW; Schema: public; Owner: spacetraders
--
CREATE MATERIALIZED VIEW public.mat_shipyardtypes_to_ship AS
SELECT unnest(ARRAY['SATELLITEFRAME_PROBE'::text, 'HAULERFRAME_LIGHT_FREIGHTER'::text, 'EXCAVATORFRAME_MINER'::text, 'COMMANDFRAME_FRIGATE'::text, 'EXCAVATORFRAME_DRONE'::text, 'SATELLITEFRAME_PROBE'::text, 'REFINERYFRAME_HEAVY_FREIGHTER'::text]) AS ship_roleframe,
unnest(ARRAY['SHIP_PROBE'::text, 'SHIP_LIGHT_FREIGHTER'::text, 'SHIP_ORE_HOUND'::text, 'SHIP_COMMAND_FRIGATE'::text, 'SHIP_MINING_DRONE'::text, 'SHIP_PROBE'::text, 'SHIP_REFINING_FREIGHTER'::text]) AS shipyard_type
WITH NO DATA;
ALTER TABLE public.mat_shipyardtypes_to_ship OWNER TO spacetraders;
--
-- TOC entry 230 (class 1259 OID 40599)
-- Name: ship_nav; Type: TABLE; Schema: public; Owner: spacetraders
--
CREATE TABLE public.ship_nav (
ship_symbol text NOT NULL,
system_symbol text NOT NULL,