-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Providers schema #53
Providers schema #53
Changes from 1 commit
53a4eae
19aecda
8af84cb
0e103e4
c2a7157
4d2c3c9
e62f776
e000657
37dc1d8
5d990c2
d59ab65
4239bcc
906de83
9280ad0
f85468d
16f7930
0035587
4ba6544
d36ae3b
5c7d0b3
5697d6e
2349588
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,24 @@ | ||
import Ajv from "ajv"; | ||
import { | ||
gpuPropertyList, | ||
CENTML_CLOUD_PROVIDERS_URL, | ||
deploymentScatterGraphColorSize, | ||
} from "../data/properties"; | ||
import {fetchingURLErrors} from '../utils/utils'; | ||
import { cloudProviderSchema } from "../schema/CloudProvidersSchema"; | ||
|
||
const ajv = new Ajv({ allErrors: true }); // to report all validation errors (rather than failing on the first errors) | ||
const validate = ajv.compile(cloudProviderSchema); | ||
|
||
class ResponseBuffer { | ||
constructor() { | ||
this.instanceId = 0; | ||
this.instanceArray = []; | ||
this.cloudProviders = {}; | ||
this.errors = []; | ||
} | ||
} | ||
export const loadJsonFiles = async (habitatData, additionalProviders) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function |
||
const buffer = new ResponseBuffer(); | ||
let instanceId = 0; | ||
const instanceArray = []; | ||
const cloudProviders = {}; | ||
const errors = []; | ||
|
||
// const buffer = new cloudProviderAndInstancesBuilder(); | ||
let urlList = [ | ||
"https://deepview-explorer-public.s3.amazonaws.com/vscode-cloud-providers/providers.json", | ||
CENTML_CLOUD_PROVIDERS_URL, | ||
]; | ||
const additionalList = additionalProviders ? additionalProviders.split(","):[]; | ||
urlList = urlList.concat(additionalList); | ||
|
@@ -34,7 +33,7 @@ export const loadJsonFiles = async (habitatData, additionalProviders) => { | |
const valid = validate(respJsonData); | ||
if (valid) { | ||
for (const cloudProvider of respJsonData) { | ||
buffer.cloudProviders[cloudProvider.name.toLocaleLowerCase()] = { | ||
cloudProviders[cloudProvider.name.toLocaleLowerCase()] = { | ||
name: cloudProvider.name, | ||
logo: cloudProvider.logo, | ||
color: cloudProvider.color, | ||
|
@@ -49,8 +48,8 @@ export const loadJsonFiles = async (habitatData, additionalProviders) => { | |
item.name.toLocaleLowerCase() === | ||
instanceData.gpu.toLocaleLowerCase() | ||
); | ||
buffer.instanceArray.push({ | ||
id: buffer.instanceId, | ||
instanceArray.push({ | ||
id: instanceId, | ||
x: found_in_habitat[1], // msec | ||
y: (instanceData.cost / 3.6e6) * found_in_habitat[1], // cost per msec * habitatData = cost per 1 iteration | ||
info: { | ||
|
@@ -64,34 +63,22 @@ export const loadJsonFiles = async (habitatData, additionalProviders) => { | |
fill: cloudProvider.color, | ||
z: deploymentScatterGraphColorSize.NORMALSIZE, | ||
}); | ||
buffer.instanceId += 1; | ||
instanceId += 1; | ||
} | ||
} | ||
} else { | ||
buffer.errors.push({ | ||
msg: `invalid data format from url: ${resp.url}`, | ||
invalidFields: validate.errors.map((err) => ({ | ||
field: err.instancePath, | ||
err: err.message, | ||
})), | ||
}); | ||
errors.push(fetchingURLErrors("schemaValidationErrors",resp,validate)); | ||
} | ||
} catch (error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Identical blocks of code found in 2 locations. Consider refactoring. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could encapsulate this into a helper function |
||
buffer.errors.push({ | ||
msg: `error reading from url: ${resp.url}`, | ||
code: `status: ${resp.statusText} | code: ${resp.status}`, | ||
}); | ||
errors.push(fetchingURLErrors("noJsonResponseFromUrl",resp,null)); | ||
} | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Identical blocks of code found in 2 locations. Consider refactoring. |
||
buffer.errors.push({ | ||
msg: `error reading from url: ${resp.url}`, | ||
code: `status: ${resp.statusText} | code: ${resp.status}`, | ||
}); | ||
errors.push(fetchingURLErrors(null,resp,null)); | ||
} | ||
} | ||
return { | ||
cloudProviders: Object.keys(buffer.cloudProviders).length > 0 ? buffer.cloudProviders: null, | ||
instanceArray: buffer.instanceArray.length > 0 ? buffer.instanceArray:null, | ||
errors: buffer.errors.length > 0 ? buffer.errors:null, | ||
cloudProviders: Object.keys(cloudProviders).length > 0 ? cloudProviders: null, | ||
instanceArray: instanceArray.length > 0 ? instanceArray:null, | ||
errors: errors.length > 0 ? errors:null, | ||
}; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
loadJsonFiles
has 77 lines of code (exceeds 25 allowed). Consider refactoring.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could refactor by wrapping some repeated logic with helper functions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved logic to utils.js