forked from clearlydefined/operations
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request clearlydefined#80 from qtomlinson/qt/auto-detect-s…
…chema-versions Add auto detect schema versions
- Loading branch information
Showing
11 changed files
with
363 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// (c) Copyright 2024, SAP SE and ClearlyDefined contributors. Licensed under the MIT license. | ||
// SPDX-License-Identifier: MIT | ||
const { callFetch } = require('./fetch') | ||
|
||
const defaultTools = ['licensee', 'reuse', 'scancode'] | ||
|
||
class HarvestResultFetcher { | ||
constructor(apiBaseUrl, coordinates, fetch = callFetch) { | ||
this.apiBaseUrl = apiBaseUrl | ||
this._fetch = fetch | ||
this._coordinates = coordinates | ||
} | ||
|
||
async fetchToolVersions(tools = defaultTools) { | ||
const listHarvestResultApi = `${this.apiBaseUrl}/harvest/${this._coordinates}?form=list` | ||
const harvestResultUrls = await this._fetch(listHarvestResultApi).then(r => r.json()) | ||
return tools.flatMap(tool => | ||
harvestResultUrls | ||
.filter(url => url.includes(`/${tool}/`)) | ||
.map(url => url.substring(`${this._coordinates}/${tool}/`.length)) | ||
.map(version => [tool, version]) | ||
) | ||
} | ||
|
||
async _pollForCompletion(poller) { | ||
try { | ||
const completed = await poller.poll() | ||
console.log(`Completed ${this._coordinates}: ${completed}`) | ||
return completed | ||
} catch (error) { | ||
if (error.code === 'ECONNREFUSED') throw error | ||
console.log(`Error polling for ${this._coordinates}: ${error}`) | ||
return false | ||
} | ||
} | ||
|
||
async pollForToolVersionsComplete(poller, startTime, tools) { | ||
const statuses = new Map() | ||
poller.with(async () => { | ||
const toolVersions = await this.fetchToolVersions(tools) | ||
return this.isHarvestComplete(toolVersions, startTime, statuses) | ||
}) | ||
const completed = await this._pollForCompletion(poller) | ||
if (!completed) throw new Error(`Schema versions not detected`) | ||
return [...statuses.entries()].map(([k, v]) => [k, v.toolVersion]) | ||
} | ||
|
||
async pollForHarvestComplete(poller, toolVersions, startTime) { | ||
const statuses = new Map() | ||
poller.with(async () => this.isHarvestComplete(toolVersions, startTime, statuses)) | ||
return this._pollForCompletion(poller) | ||
} | ||
|
||
async isHarvestComplete(toolVersions, startTime, statuses = new Map()) { | ||
const harvestChecks = (toolVersions || []).map(async ([tool, toolVersion]) => { | ||
const completed = statuses.get(tool)?.completed || (await this.isHarvestedbyTool(tool, toolVersion, startTime)) | ||
if (completed) statuses.set(tool, { toolVersion, completed }) | ||
return tool | ||
}) | ||
return Promise.all(harvestChecks).then(tools => tools.every(tool => statuses.get(tool)?.completed)) | ||
} | ||
|
||
async isHarvestedbyTool(tool, toolVersion, startTime = 0) { | ||
const harvested = await this.fetchHarvestResult(tool, toolVersion) | ||
if (!harvested._metadata) return false | ||
const fetchedAt = new Date(harvested._metadata.fetchedAt) | ||
console.log(`${this._coordinates} ${tool}, ${toolVersion} fetched at ${fetchedAt}`) | ||
return fetchedAt.getTime() > startTime | ||
} | ||
|
||
async fetchHarvestResult(tool, toolVersion) { | ||
return this._fetch(`${this.apiBaseUrl}/harvest/${this._coordinates}/${tool}/${toolVersion}?form=raw`).then(r => | ||
r.headers.get('Content-Length') === '0' ? Promise.resolve({}) : r.json() | ||
) | ||
} | ||
} | ||
|
||
module.exports = HarvestResultFetcher |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.