Skip to content

Commit b1b84ce

Browse files
committed
resolve comments
1 parent 7f0c068 commit b1b84ce

File tree

14 files changed

+178
-68
lines changed

14 files changed

+178
-68
lines changed

Agents/TrajectoryQueryAgent/trajectoryqueryagent/src/main/java/uk/ac/cam/cares/jps/agent/trajectoryqueryagent/TrajectoryQueryAgent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ private void createGeoserver() {
261261
geoServerClient.createPostGISDataStore(workspaceName, "trajectory", dbName, schema);
262262
geoServerClient.createPostGISLayer(workspaceName, dbName, "trajectoryUserIdByActivity", geoServerVectorSettings);
263263
}
264-
264+
265265
}
266266

267267
/**

Agents/TrajectoryQueryAgent/trajectoryqueryagent/src/main/resources/functions.sql

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ BEGIN
99
FROM time_series_quantities
1010
WHERE data_iri = iri;
1111

12+
1213
RETURN column_name_result;
1314
END;
1415
$$
1516
LANGUAGE plpgsql;
1617

18+
1719
CREATE OR REPLACE FUNCTION get_table_name(iri VARCHAR)
1820
RETURNS VARCHAR AS
1921
$$
@@ -25,11 +27,13 @@ BEGIN
2527
FROM time_series_quantities
2628
WHERE data_iri = iri;
2729

30+
2831
RETURN table_name_result;
2932
END;
3033
$$
3134
LANGUAGE plpgsql;
3235

36+
3337
CREATE OR REPLACE FUNCTION get_time_series(iri VARCHAR)
3438
RETURNS VARCHAR AS
3539
$$
@@ -41,11 +45,13 @@ BEGIN
4145
FROM time_series_quantities
4246
WHERE data_iri = iri;
4347

48+
4449
RETURN time_series_result;
4550
END;
4651
$$
4752
LANGUAGE plpgsql;
4853

54+
4955
CREATE OR REPLACE FUNCTION get_user_id(device TEXT)
5056
RETURNS VARCHAR AS
5157
$$
@@ -54,15 +60,15 @@ DECLARE
5460
BEGIN
5561
-- Check if the schema 'timeline' exists
5662
IF EXISTS (
57-
SELECT 1
58-
FROM pg_catalog.pg_namespace
63+
SELECT 1
64+
FROM pg_catalog.pg_namespace
5965
WHERE nspname = 'timeline'
6066
) THEN
6167
-- Check if the table 'smartPhone' exists in the 'timeline' schema
6268
IF EXISTS (
63-
SELECT 1
64-
FROM pg_catalog.pg_tables
65-
WHERE schemaname = 'timeline'
69+
SELECT 1
70+
FROM pg_catalog.pg_tables
71+
WHERE schemaname = 'timeline'
6672
AND tablename = 'smartPhone'
6773
) THEN
6874
-- Perform the query to get the user_id
@@ -79,20 +85,23 @@ BEGIN
7985
RETURN NULL;
8086
END IF;
8187

88+
8289
RETURN user_result;
8390
END;
8491
$$
8592
LANGUAGE plpgsql;
8693

8794

95+
96+
8897
CREATE OR REPLACE FUNCTION get_location_table(
8998
device_id_array TEXT[]
9099
)
91100
RETURNS TABLE (
92-
"time" bigint,
93-
"geom" geometry,
94-
"speed" double precision,
95-
"altitude" double precision,
101+
"time" bigint,
102+
"geom" geometry,
103+
"speed" double precision,
104+
"altitude" double precision,
96105
"bearing" double precision,
97106
"session_id" character varying,
98107
"device_id" TEXT,
@@ -106,6 +115,7 @@ BEGIN
106115
query := query || ' UNION ALL ';
107116
END IF;
108117

118+
109119
query := query || format(
110120
'SELECT time, %I AS geom, %I AS speed, %I AS altitude, %I AS bearing, %I AS session_id, %L AS device_id, %L AS user_id FROM %I WHERE time_series_iri=%L',
111121
get_column_name(get_point_iri(device_id_array[i])),
@@ -120,9 +130,11 @@ BEGIN
120130
);
121131
END LOOP;
122132

133+
123134
RETURN QUERY EXECUTE query;
124135
END $$ LANGUAGE plpgsql;
125136

137+
126138
CREATE OR REPLACE FUNCTION get_activity_table(
127139
device_id_array TEXT[]
128140
)
@@ -141,9 +153,12 @@ BEGIN
141153
query := query || ' UNION ALL ';
142154
END IF;
143155

156+
144157
device_id := device_id_array[i];
145158

146159

160+
161+
147162
query := query || format(
148163
'SELECT time, %I AS activity_type, %I ::INTEGER AS confidence_level, %L AS device_id, %L AS user_id FROM %I WHERE time_series_iri = %L',
149164
get_column_name(get_activity_type_iri(device_id)),
@@ -155,10 +170,13 @@ BEGIN
155170
);
156171
END LOOP;
157172

173+
158174
RETURN QUERY EXECUTE query;
159175
END $$ LANGUAGE plpgsql;
160176

161177

178+
179+
162180
-- used by timeline app only
163181
CREATE OR REPLACE FUNCTION get_device_ids(id VARCHAR)
164182
RETURNS TEXT AS
@@ -172,30 +190,33 @@ BEGIN
172190
FROM timeline."smartPhone" sp
173191
WHERE sp.user_id = id
174192
AND EXISTS (
175-
SELECT 1
193+
SELECT 1
176194
FROM devices d
177195
WHERE d.device_id = sp.phone_id
178196
);
179197

198+
180199
RETURN phone_id_list;
181200
END;
182201
$$
183202
LANGUAGE plpgsql;
184203

185204

205+
206+
186207
CREATE OR REPLACE FUNCTION fill_activity_types(activity_types varchar[], times bigint[])
187208
RETURNS TABLE (
188209
"time" bigint,
189210
"activity_type" VARCHAR
190211
) AS $$
191212
DECLARE
192-
result varchar[] := '{}';
193-
last_valid varchar := '';
194-
activity varchar;
195-
activity_time bigint;
196-
i integer := 1;
213+
result varchar[] := '{}';
214+
last_valid varchar := '';
215+
activity varchar;
216+
activity_time bigint;
217+
i integer := 1;
197218
BEGIN
198-
219+
199220
FOREACH activity IN ARRAY activity_types
200221
LOOP
201222
IF activity <> 'others' THEN
@@ -204,22 +225,26 @@ BEGIN
204225
END IF;
205226
END LOOP;
206227

228+
207229
FOREACH activity IN ARRAY activity_types
208230
LOOP
209231
IF activity <> 'others' THEN
210232
last_valid := activity;
211233
END IF;
212234

235+
213236
result := array_append(result, last_valid);
214237
END LOOP;
215238

239+
216240
FOR i IN 1..array_length(times, 1)
217241
LOOP
218242
activity_time := times[i];
219-
RETURN QUERY SELECT activity_time, result[i];
243+
RETURN QUERY SELECT activity_time, result[i];
220244
END LOOP;
221245

222-
RETURN;
246+
247+
RETURN;
223248
END;
224249
$$
225-
LANGUAGE plpgsql
250+
LANGUAGE plpgsql

Agents/TrajectoryQueryAgent/trajectoryqueryagent/src/main/resources/line_layer_user_id_by_activity.sql

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
WITH distinct_devices AS (
2-
SELECT
2+
SELECT
33
array_agg(device_id::text) AS device_list
4-
FROM
5-
(SELECT DISTINCT device_id
6-
FROM devices
4+
FROM
5+
(SELECT DISTINCT device_id
6+
FROM devices
77
WHERE sensor_class = 'Activity') AS distinct_devices
88
),
99

10+
1011
timeseries AS (
1112
SELECT
1213
*
@@ -15,6 +16,7 @@ timeseries AS (
1516
ORDER BY time
1617
),
1718

19+
1820
activity_data AS (
1921
SELECT
2022
*
@@ -23,23 +25,25 @@ activity_data AS (
2325
ORDER BY time
2426
),
2527

28+
2629
joined_data AS (
27-
SELECT
28-
t.*,
30+
SELECT
31+
t.*,
2932
a.activity_type,
3033
a.confidence_level,
3134
LAG(t.time) OVER (PARTITION BY t.user_id ORDER BY t.time) AS prev_time
32-
FROM
35+
FROM
3336
timeseries t
34-
LEFT JOIN
35-
activity_data a
36-
ON
37-
ABS(t.time - a.time) <= 5000
37+
LEFT JOIN
38+
activity_data a
39+
ON
40+
ABS(t.time - a.time) <= 5000
3841
ORDER BY t.time
3942
),
4043

44+
4145
fixed_activity_data AS (
42-
SELECT
46+
SELECT
4347
time,
4448
speed,
4549
altitude,
@@ -48,18 +52,20 @@ fixed_activity_data AS (
4852
session_id,
4953
user_id,
5054
confidence_level,
51-
COALESCE(activity_type, 'others') AS activity_type
55+
COALESCE(activity_type, 'unknown') AS activity_type
5256
FROM joined_data
5357
WHERE time <> prev_time
5458
),
5559

60+
5661
temp_activity_table AS (
5762
SELECT * FROM fill_activity_types(
5863
(SELECT array_agg(activity_type ORDER BY time) FROM fixed_activity_data),
59-
(SELECT array_agg(time ORDER BY time) FROM fixed_activity_data)
64+
(SELECT array_agg(time ORDER BY time) FROM fixed_activity_data)
6065
)
6166
),
6267

68+
6369
filled_activity_data AS (
6470
SELECT
6571
fixed.time AS time,
@@ -72,23 +78,25 @@ filled_activity_data AS (
7278
temp.activity_type AS activity_type,
7379
fixed.confidence_level AS confidence_level
7480
FROM fixed_activity_data AS fixed
75-
JOIN temp_activity_table AS temp
81+
JOIN temp_activity_table AS temp
7682
ON fixed.time = temp.time
7783
ORDER BY time
7884
),
7985

86+
8087
change_marked AS (
8188
SELECT
8289
*,
8390
CASE
8491
WHEN
8592
LAG(activity_type) OVER (PARTITION BY user_id ORDER BY time) IS DISTINCT FROM activity_type
8693
THEN 1
87-
ELSE 0
94+
ELSE 0
8895
END AS change_flag
8996
FROM filled_activity_data
9097
),
9198

99+
92100
change_marked_union AS (
93101
SELECT * FROM change_marked
94102
UNION ALL
@@ -98,15 +106,17 @@ change_marked_union AS (
98106
ORDER BY time, change_flag
99107
),
100108

109+
101110
numbered_activity_data AS (
102-
SELECT
111+
SELECT
103112
*,
104113
SUM(change_flag) OVER (PARTITION BY user_id ORDER BY time ROWS UNBOUNDED PRECEDING) AS id
105114
FROM change_marked_union cm
106115
WHERE activity_type IS NOT NULL
107116
ORDER BY cm.time, cm.change_flag
108117
)
109118

119+
110120
SELECT
111121
id,
112122
MIN(na.time) AS start_time,

Agents/utils/GeoServerJwtProxy/src/main/java/uk/ac/cam/cares/GeoServerJwtProxy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class GeoServerJwtProxy extends HttpServlet {
3939
protected void doGet(HttpServletRequest request, HttpServletResponse response)
4040
throws ServletException, IOException {
4141
String token = null;
42-
42+
System.out.println(request);
4343
Iterator<String> headerIterator = request.getHeaders("Authorization").asIterator();
4444
while (headerIterator.hasNext() && token == null) {
4545
String headerValue = headerIterator.next();

0 commit comments

Comments
 (0)