Skip to content

Commit 68f2ca1

Browse files
committed
Merge branch 'refactor-DAPS-1522-Version-Router-Logging-Improvements' of github.com:ORNL/DataFed into refactor-DAPS-1522-Version-Router-Logging-Improvements
2 parents 78c4054 + 4c18905 commit 68f2ca1

File tree

6 files changed

+202
-25
lines changed

6 files changed

+202
-25
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ if( INSTALL_FOXX )
241241
RESULT_VARIABLE _res)
242242
if (NOT \${_res} EQUAL \"0\")
243243
message( FATAL_ERROR \"out: \${_out} install_foxx failed: \${_err}\")
244-
else()
245-
message( \"\${_out} install_foxx failed: \${_err}\")
244+
else()
245+
message( \"\${_out} install_foxx completed: \${_err}\")
246246
endif()"
247247
)
248248
endif()

core/database/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ if( ENABLE_FOXX_TESTS )
2929
add_test(NAME foxx_support COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_foxx.sh" -t "unit_support:")
3030
add_test(NAME foxx_user_router COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_foxx.sh" -t "unit_user_router:")
3131
add_test(NAME foxx_version_router COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_foxx.sh" -t "unit_version_router:")
32+
add_test(NAME foxx_tag_router COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_foxx.sh" -t "unit_tag_router:")
3233
add_test(NAME foxx_task_router COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_foxx.sh" -t "unit_task_router:")
3334
add_test(NAME foxx_authz_router COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_foxx.sh" -t "unit_authz_router:")
3435
add_test(NAME foxx_query_router COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_foxx.sh" -t "unit_query_router:")
@@ -55,6 +56,7 @@ if( ENABLE_FOXX_TESTS )
5556
set_tests_properties(foxx_path PROPERTIES FIXTURES_REQUIRED Foxx)
5657
set_tests_properties(foxx_user_router PROPERTIES FIXTURES_REQUIRED "Foxx;FoxxDBFixtures")
5758
set_tests_properties(foxx_version_router PROPERTIES FIXTURES_REQUIRED Foxx)
59+
set_tests_properties(foxx_tag_router PROPERTIES FIXTURES_REQUIRED Foxx)
5860
set_tests_properties(foxx_query_router PROPERTIES FIXTURES_REQUIRED Foxx)
5961
set_tests_properties(foxx_task_router PROPERTIES FIXTURES_REQUIRED Foxx)
6062
set_tests_properties(foxx_unit_user_token PROPERTIES FIXTURES_REQUIRED Foxx)

core/database/foxx/api/tag_router.js

Lines changed: 92 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,48 @@ const joi = require("joi");
77

88
const g_db = require("@arangodb").db;
99
const g_lib = require("./support");
10+
const logger = require("./lib/logger");
1011

12+
const basePath = "tag";
1113
module.exports = router;
1214

1315
//==================== TAG API FUNCTIONS
1416

1517
router
1618
.post("/search", function (req, res) {
19+
let client = null;
20+
let result = null;
21+
let tot = null;
1722
try {
23+
client = req.queryParams.client
24+
? g_lib.getUserFromClientID(req.queryParams.client)
25+
: null;
26+
logger.logRequestStarted({
27+
client: client?._id,
28+
correlationId: req.headers["x-correlation-id"],
29+
httpVerb: "POST",
30+
routePath: basePath + "/search",
31+
status: "Started",
32+
description: `Search for tags by name (${req.queryParams?.name?.trim()})`,
33+
});
1834
var name = req.queryParams.name.trim();
1935
if (name.length < 3)
2036
throw [error.ERR_INVALID_PARAM, "Input is too short for tag search."];
2137

22-
var off = req.queryParams.offset ? req.queryParams.offset : 0,
23-
cnt = req.queryParams.count ? req.queryParams.count : 50,
24-
result = g_db._query(
25-
"for t in tagview search analyzer(t._key in tokens(@name,'tag_name'), 'tag_name') let s = BM25(t) sort s desc limit @off,@cnt return {name: t._key, count: t.count}",
26-
{
27-
name: name,
28-
off: off,
29-
cnt: cnt,
30-
},
31-
{
32-
fullCount: true,
33-
},
34-
),
35-
tot = result.getExtra().stats.fullCount;
38+
var off = req.queryParams.offset ? req.queryParams.offset : 0;
39+
var cnt = req.queryParams.count ? req.queryParams.count : 50;
40+
result = g_db._query(
41+
"for t in tagview search analyzer(t._key in tokens(@name,'tag_name'), 'tag_name') let s = BM25(t) sort s desc limit @off,@cnt return {name: t._key, count: t.count}",
42+
{
43+
name: name,
44+
off: off,
45+
cnt: cnt,
46+
},
47+
{
48+
fullCount: true,
49+
},
50+
);
51+
tot = result.getExtra().stats.fullCount;
3652

3753
result = result.toArray();
3854
result.push({
@@ -44,7 +60,34 @@ router
4460
});
4561

4662
res.send(result);
63+
logger.logRequestSuccess({
64+
client: client?._id,
65+
correlationId: req.headers["x-correlation-id"],
66+
httpVerb: "POST",
67+
routePath: basePath + "/search",
68+
status: "Success",
69+
description: `Search for tags by name(${req.queryParams?.name?.trim()})`,
70+
extra: {
71+
requestedName: name,
72+
returnedCount: result?.length - 1, // subtract the paging object
73+
total_found: tot,
74+
},
75+
});
4776
} catch (e) {
77+
logger.logRequestFailure({
78+
client: client?._id,
79+
correlationId: req.headers["x-correlation-id"],
80+
httpVerb: "POST",
81+
routePath: basePath + "/search",
82+
status: "Failure",
83+
description: `Search for tags by name(${req.queryParams?.name?.trim()})`,
84+
extra: {
85+
requestedName: name,
86+
returnedCount: result?.length - 1, // subtract the paging object
87+
total_found: tot,
88+
},
89+
error: e,
90+
});
4891
g_lib.handleException(e, res);
4992
}
5093
})
@@ -56,7 +99,21 @@ router
5699

57100
router
58101
.post("/list/by_count", function (req, res) {
102+
let client = null;
103+
let tot = null;
59104
try {
105+
client = req.queryParams.client
106+
? g_lib.getUserFromClientID(req.queryParams.client)
107+
: null;
108+
logger.logRequestStarted({
109+
client: client?._id,
110+
correlationId: req.headers["x-correlation-id"],
111+
httpVerb: "POST",
112+
routePath: basePath + "/list/by_count",
113+
status: "Started",
114+
description: "List tags by count",
115+
});
116+
60117
g_db._executeTransaction({
61118
collections: {
62119
read: ["tag"],
@@ -76,7 +133,7 @@ router
76133
},
77134
);
78135

79-
var tot = result.getExtra().stats.fullCount;
136+
tot = result.getExtra().stats.fullCount;
80137
result = result.toArray();
81138
result.push({
82139
paging: {
@@ -87,9 +144,29 @@ router
87144
});
88145

89146
res.send(result);
147+
logger.logRequestSuccess({
148+
client: client?._id,
149+
correlationId: req.headers["x-correlation-id"],
150+
httpVerb: "POST",
151+
routePath: basePath + "/list/by_count",
152+
status: "Success",
153+
description: "List tags by count",
154+
extra: { total_tags: tot },
155+
});
90156
},
91157
});
92158
} catch (e) {
159+
logger.logRequestFailure({
160+
client: client?._id,
161+
correlationId: req.headers["x-correlation-id"],
162+
httpVerb: "POST",
163+
routePath: basePath + "/list/by_count",
164+
status: "Failure",
165+
description: "List tags by count",
166+
extra: { total_tags: tot },
167+
error: e,
168+
});
169+
93170
g_lib.handleException(e, res);
94171
}
95172
})
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"use strict";
2+
// NOTE: completion of tests requires successful run of user_fixture.js script
3+
4+
// Need to pull enum from support
5+
const g_lib = require("../api/support");
6+
7+
// Integration test of API
8+
const { expect } = require("chai");
9+
const request = require("@arangodb/request");
10+
const { baseUrl } = module.context;
11+
const { db } = require("@arangodb");
12+
13+
const tag_base_url = `${baseUrl}/tag`;
14+
15+
describe("unit_tag_router: the Foxx microservice topic_router /search endpoint", () => {
16+
after(function () {
17+
const collections = ["tag"];
18+
collections.forEach((name) => {
19+
let col = db._collection(name);
20+
if (col) col.truncate();
21+
});
22+
});
23+
24+
beforeEach(() => {
25+
const collections = ["tag"];
26+
collections.forEach((name) => {
27+
let col = db._collection(name);
28+
if (col) {
29+
col.truncate(); // truncate after ensuring collection exists
30+
} else {
31+
db._create(name); // create if it doesn’t exist
32+
}
33+
});
34+
});
35+
36+
it("should successfully run the search route", () => {
37+
db.tag.save({
38+
name: "testName",
39+
});
40+
41+
// arrangesudo journalctl -u arangodb3.service -f
42+
// TODO: make encoded query params less hard coded
43+
const request_string = `${tag_base_url}/search?name=testName`;
44+
// act
45+
const response = request.post(request_string);
46+
// assert
47+
expect(response.status).to.equal(200);
48+
});
49+
50+
it("should successfully run the list by count route", () => {
51+
db.tag.save({
52+
name: "testName",
53+
});
54+
55+
// arrangesudo journalctl -u arangodb3.service -f
56+
// TODO: make encoded query params less hard coded
57+
const request_string = `${tag_base_url}/list/by_count`;
58+
// act
59+
const response = request.post(request_string);
60+
// assert
61+
expect(response.status).to.equal(200);
62+
});
63+
});

scripts/install_foxx.sh

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,38 @@ fi
161161

162162
url="${local_DATABASE_API_SCHEME}://${local_DATAFED_DATABASE_HOST}:${local_DATABASE_PORT}/_api/database/user"
163163
# Do not output to /dev/null we need the output
164-
code=$(LD_LIBRARY_PATH="${DATAFED_DEPENDENCIES_INSTALL_PATH}:$LD_LIBRARY_PATH" curl ${local_CURL_SSL_ARG} -s -o /dev/null -w "%{http_code}" --user "$basic_auth" "$url")
165164

166-
if [[ "$code" != "200" ]]; then
167-
echo "ERROR - Attempting to connect to database at $url"
168-
echo " HTTP code is: $code"
169-
exit 1
170-
fi
165+
max_retries=3
166+
retry_delay=5
167+
168+
for attempt in $(seq 1 $max_retries); do
169+
set +e
170+
code=$(LD_LIBRARY_PATH="${DATAFED_DEPENDENCIES_INSTALL_PATH}:$LD_LIBRARY_PATH" \
171+
curl ${local_CURL_SSL_ARG} -s -o /dev/null -w "%{http_code}" \
172+
--user "$basic_auth" "$url")
173+
error_code=$?
174+
set -e
175+
176+
if [[ "$error_code" -eq 0 && "$code" -eq 200 ]]; then
177+
echo "INFO - Attempting to connect to database at $url"
178+
echo " HTTP code is: $code"
179+
echo " Succeeded"
180+
break
181+
fi
182+
183+
if [[ "$attempt" -lt "$max_retries" ]]; then
184+
echo "WARNING - Attempting to connect to database at $url"
185+
echo " HTTP code is: $code"
186+
echo " Failed"
187+
echo " Retrying in ${retry_delay}s..."
188+
sleep "$retry_delay"
189+
else
190+
echo "ERROR - Attempting to connect to database at $url"
191+
echo " HTTP code is: $code"
192+
echo " Failed after $max_retries"
193+
exit 1
194+
fi
195+
done
171196

172197
url2="${local_DATABASE_API_SCHEME}://${local_DATAFED_DATABASE_HOST}:${local_DATABASE_PORT}/_api/database"
173198
# We are now going to initialize the DataFed database in Arango, but only if sdms database does

web/datafed-ws.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,12 +592,22 @@ the registration page.
592592

593593
a_resp.redirect("/ui/register");
594594
} else {
595+
if (reply.user.length > 1) {
596+
logger.warn(
597+
"ui/authn",
598+
getCurrentLineNumber(),
599+
"More than one user was returned from DataFed, this can happen if a user has registered two or more separate accounts with DataFed and has since linked their identities from a third party identity manager. DataFed will select the first identity when logging in.",
600+
);
601+
}
602+
let username = reply.user[0]?.uid?.replace(/^u\//, "");
595603
logger.info(
596604
"/ui/authn",
597605
getCurrentLineNumber(),
598606
"User: " +
599607
uid +
600-
" verified, acc:" +
608+
" verified, mapped to: " +
609+
username +
610+
" acc:" +
601611
xfr_token.access_token +
602612
", ref: " +
603613
xfr_token.refresh_token +
@@ -606,7 +616,7 @@ the registration page.
606616
);
607617

608618
// Store only data needed for active session
609-
a_req.session.uid = uid;
619+
a_req.session.uid = username;
610620
a_req.session.reg = true;
611621

612622
let redirect_path = "/ui/main";

0 commit comments

Comments
 (0)