From fe93dd565733d64f68d4e6c60cdaaf8e8bcd4d0d Mon Sep 17 00:00:00 2001 From: btopro Date: Wed, 16 Aug 2023 15:55:19 -0400 Subject: [PATCH] failsafe on big course imports --- api/apps/haxcms/convert/elmslnToSite.js | 31 ++++++++++++++++--------- api/services/website/cacheAddress.js | 12 +++++++++- elements/app-hax/app-hax.js | 6 ++--- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/api/apps/haxcms/convert/elmslnToSite.js b/api/apps/haxcms/convert/elmslnToSite.js index db31aba370..1cb69b98e5 100644 --- a/api/apps/haxcms/convert/elmslnToSite.js +++ b/api/apps/haxcms/convert/elmslnToSite.js @@ -28,24 +28,33 @@ export default async function handler(req, res) { site = await resolveSiteData(base); // use legit prop or just pull off the url siteName = (site.metadata && site.metadata.site && site.metadata.site.name) ? site.metadata.site.name : parseURL.pathname.split('/').pop(); + // note location includes base path of site bc of nature of how system builds base URIs + let __fetchOptions = { + method: "GET", + }; + // test for aanda elms as "basic auth" is required to bypass azure + // and defer to app level permissions handling + if (url.includes('.aanda.psu.edu') || url.includes('.ed.science.psu.edu')) { + let buff = Buffer.from(process.env.ELMSLN_VERCEL_SERVICE_AUTH).toString('base64'); + __fetchOptions.headers = {'Authorization': 'Basic ' + buff}; + } + var elapsed; + var all = 0; for await (const item of site.items) { - // note location includes base path of site bc of nature of how system builds base URIs - let __fetchOptions = { - method: "GET", - }; - // test for aanda elms as "basic auth" is required to bypass azure - // and defer to app level permissions handling - if (url.includes('.aanda.psu.edu') || url.includes('.ed.science.psu.edu')) { - let buff = Buffer.from(process.env.ELMSLN_VERCEL_SERVICE_AUTH).toString('base64'); - __fetchOptions.headers = {'Authorization': 'Basic ' + buff}; + elapsed = process.hrtime()[1] / 100000000; + if (all < 55) { + item.contents = await fetch(`http://${process.env.VERCEL_URL}/api/services/website/cacheAddress?q=${base}/${item.location.replace(`/${siteName}/`,'')}`, __fetchOptions).then((d) => d.ok ? d.json() : '').then((r) => r.data); + } + else { + item.contents = `

get source from ${base}/${item.location.replace(`/${siteName}/`,'')}

`; } - item.contents = await fetch(`${base}/${item.location.replace(`/${siteName}/`,'')}`,__fetchOptions).then((d) => d.ok ? d.text(): ''); - console.log(item.contents); if (item.metadata && item.metadata.files) { for await (const file of item.metadata.files) { downloads[file.url] = `${base}/${file.url}`; } } + elapsed = process.hrtime()[1] / 100000000; + all = all+elapsed; } } res = stdResponse(res, { diff --git a/api/services/website/cacheAddress.js b/api/services/website/cacheAddress.js index f30b4440a2..e17f5a8aaa 100644 --- a/api/services/website/cacheAddress.js +++ b/api/services/website/cacheAddress.js @@ -12,7 +12,17 @@ export default async function handler(req, res) { } let content; try { - content = await fetch(req.query.q).then((d) => d.ok ? d.text(): ''); + // note location includes base path of site bc of nature of how system builds base URIs + let __fetchOptions = { + method: "GET", + }; + // test for aanda elms as "basic auth" is required to bypass azure + // and defer to app level permissions handling + if (req.query.q.includes('.aanda.psu.edu') || req.query.q.includes('.ed.science.psu.edu')) { + let buff = Buffer.from(process.env.ELMSLN_VERCEL_SERVICE_AUTH).toString('base64'); + __fetchOptions.headers = {'Authorization': 'Basic ' + buff}; + } + content = await fetch(req.query.q, __fetchOptions).then((d) => d.ok ? d.text(): ''); } catch { content = ''; diff --git a/elements/app-hax/app-hax.js b/elements/app-hax/app-hax.js index 802e2e3f87..57186ca0e3 100644 --- a/elements/app-hax/app-hax.js +++ b/elements/app-hax/app-hax.js @@ -293,7 +293,7 @@ Window size: ${window.innerWidth}x${window.innerHeight} icon: "hax:hax2022", tags: ["Sites", "Administration", "change"], eventName: "super-daemon-run-program", - path: "hax/action/goToSite", + path: "HAX/action/goToSite", value: { name: "Go to site", program: async (input, values) => { @@ -323,9 +323,9 @@ Window size: ${window.innerWidth}x${window.innerHeight} eventName: "super-daemon-element-method", context: [ "*", - "hax/action/goToSite/" + site.metadata.site.name, + "HAX/action/goToSite/" + site.metadata.site.name, ], - path: "hax/action/goToSite/" + site.metadata.site.name, + path: "HAX/action/goToSite/" + site.metadata.site.name, }); } });