Skip to content

Commit

Permalink
[Task IAC-805] Made data collection REST calls to execute in serial o…
Browse files Browse the repository at this point in the history
…rder in order not to overload vRO, code refactoring

Signed-off-by: Alexander Kantchev <[email protected]>
  • Loading branch information
akantchev committed Feb 5, 2024
1 parent 509d209 commit eb6090a
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 125 deletions.
2 changes: 1 addition & 1 deletion extension/src/client/command/RunAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ class ActionRunner {
if (!fs.existsSync(storagePath)) {
fs.mkdirSync(storagePath)
}

// exec
await this.mavenProxy.copyDependency(
"com.vmware.pscoe.o11n",
"exec",
Expand Down
47 changes: 26 additions & 21 deletions extension/src/client/command/TriggerCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import { AutoWire, Logger, sleep } from "@vmware/vrdt-common"
import { remote } from "@vmware/vro-language-server"
import * as vscode from "vscode"
import { LanguageClient } from "vscode-languageclient"
import { CollectionStatus } from "packages/node/vro-language-server/src/server/request/collection/ServerCollection"

Check warning on line 10 in extension/src/client/command/TriggerCollection.ts

View workflow job for this annotation

GitHub Actions / Node.js v14.x on ubuntu-latest

Unable to resolve path to module 'packages/node/vro-language-server/src/server/request/collection/ServerCollection'

Check warning on line 10 in extension/src/client/command/TriggerCollection.ts

View workflow job for this annotation

GitHub Actions / Node.js v16.x on ubuntu-latest

Unable to resolve path to module 'packages/node/vro-language-server/src/server/request/collection/ServerCollection'

Check warning on line 10 in extension/src/client/command/TriggerCollection.ts

View workflow job for this annotation

GitHub Actions / Node.js v14.x on ubuntu-latest

Unable to resolve path to module 'packages/node/vro-language-server/src/server/request/collection/ServerCollection'

Check warning on line 10 in extension/src/client/command/TriggerCollection.ts

View workflow job for this annotation

GitHub Actions / Node.js v16.x on ubuntu-latest

Unable to resolve path to module 'packages/node/vro-language-server/src/server/request/collection/ServerCollection'

Check warning on line 10 in extension/src/client/command/TriggerCollection.ts

View workflow job for this annotation

GitHub Actions / Node.js v14.x on windows-latest

Unable to resolve path to module 'packages/node/vro-language-server/src/server/request/collection/ServerCollection'

Check warning on line 10 in extension/src/client/command/TriggerCollection.ts

View workflow job for this annotation

GitHub Actions / Node.js v14.x on windows-latest

Unable to resolve path to module 'packages/node/vro-language-server/src/server/request/collection/ServerCollection'

Check warning on line 10 in extension/src/client/command/TriggerCollection.ts

View workflow job for this annotation

GitHub Actions / Node.js v16.x on windows-latest

Unable to resolve path to module 'packages/node/vro-language-server/src/server/request/collection/ServerCollection'

Check warning on line 10 in extension/src/client/command/TriggerCollection.ts

View workflow job for this annotation

GitHub Actions / Node.js v16.x on windows-latest

Unable to resolve path to module 'packages/node/vro-language-server/src/server/request/collection/ServerCollection'

import { Commands, LanguageServerConfig } from "../constants"
import { LanguageServices } from "../lang"
Expand Down Expand Up @@ -40,33 +42,36 @@ export class TriggerCollection extends Command<void> {
location: vscode.ProgressLocation.Window,
title: "vRO hint collection"
},
progress => {
async progress => {
return new Promise<void>(async (resolve, reject) => {
await languageClient.sendRequest(remote.server.triggerVroCollection, false)
let status = await languageClient.sendRequest(remote.server.giveVroCollectionStatus)

while (status && !status.finished) {
this.logger.info("Collection status:", status)
progress.report(status)
await sleep(LanguageServerConfig.SleepTime)
status = await languageClient.sendRequest(remote.server.giveVroCollectionStatus)
}

this.logger.info("Collection finished:", status)
const status: CollectionStatus = await this.triggerVroDataCollection(languageClient, progress)
if (status.error !== undefined) {
await vscode.commands.executeCommand(Commands.EventCollectionError, status.error)
if (status.data.hintsPluginBuild === 0) {
vscode.window.showErrorMessage(
"The vRO Hint plug-in is not installed on the configured vRO server"
)
}
} else {
await vscode.commands.executeCommand(Commands.EventCollectionSuccess)
reject(new Error(`Failed to trigger data collection from vRO: ${status.error}`))
}

resolve()
})
}
)
}

private async triggerVroDataCollection(languageClient: LanguageClient, progress: any): Promise<CollectionStatus> {
await languageClient.sendRequest(remote.server.triggerVroCollection, false)
let status = await languageClient.sendRequest(remote.server.giveVroCollectionStatus)

// wait for status change
while (status && !status.finished) {
this.logger.info("Collection status:", status)
progress.report(status)
await sleep(LanguageServerConfig.SleepTime)
status = await languageClient.sendRequest(remote.server.giveVroCollectionStatus)
}
// check for error response
if (status.error !== undefined) {
await vscode.commands.executeCommand(Commands.EventCollectionError, status.error)
return status
}
await vscode.commands.executeCommand(Commands.EventCollectionSuccess)

return status
}
}
16 changes: 8 additions & 8 deletions extension/src/client/ui/StatusBarController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,6 @@ export class StatusBarController implements Registrable, vscode.Disposable {
this.collectionStatus.dispose()
}

private onConfigurationOrProfilesChanged() {
const currentProfileName = this.config.hasActiveProfile() ? this.config.activeProfile.get("id") : undefined

if (this.verifyConfiguration() && currentProfileName !== this.profileName && this.env.hasRelevantProject()) {
vscode.commands.executeCommand(Commands.TriggerServerCollection)
}
}

verifyConfiguration(): boolean {
this.profileName = this.config.hasActiveProfile() ? this.config.activeProfile.get("id") : undefined
this.logger.info(`Verifying configuration for active profile ${this.profileName}`)
Expand Down Expand Up @@ -96,6 +88,14 @@ export class StatusBarController implements Registrable, vscode.Disposable {
return false
}

private onConfigurationOrProfilesChanged() {
const currentProfileName = this.config.hasActiveProfile() ? this.config.activeProfile.get("id") : undefined

if (this.verifyConfiguration() && currentProfileName !== this.profileName && this.env.hasRelevantProject()) {
vscode.commands.executeCommand(Commands.TriggerServerCollection)
}
}

private onCollectionStart() {
this.collectionButton.text = "$(watch) "
this.collectionButton.command = Commands.TriggerServerCollection
Expand Down
20 changes: 11 additions & 9 deletions packages/node/vrdt-common/src/rest/VroRestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
ContentChildrenResponse,
ContentLinksResponse,
InventoryElement,
LinkItem,
LogMessage,
Resource,
Version,
Expand Down Expand Up @@ -86,7 +87,7 @@ export class VroRestClient {
this.logger.info("Initial authentication...")
let auth: Auth

await sleep(1000) // to properly initialize the components
await sleep(3000) // to properly initialize the components

let refreshToken = this.refreshToken
switch (this.authMethod.toLowerCase()) {
Expand Down Expand Up @@ -535,18 +536,19 @@ export class VroRestClient {
`categories?isRoot=true&categoryType=${categoryType}`
)
const categories = responseJson.link
.map(child => {
const name = child.attributes.find(att => att.name === "name")
const id = child.attributes.find(att => att.name === "id")
.map((item: LinkItem) => {
const name = item.attributes.find(att => att.name === "name")
const id = item.attributes.find(att => att.name === "id")
return {
name: name ? name.value : undefined,
id: id ? id.value : undefined,
name: name?.value ?? undefined,
id: id?.value ?? undefined,
type: categoryType,
rel: child.rel
rel: item?.rel,
description: item?.description?.value ?? undefined
}
})
.filter(val => {
return val.name !== undefined && val.id !== undefined
.filter(item => {
return item.name !== undefined && item.id !== undefined
}) as ApiElement[]

categories.sort((x, y) => x.name.localeCompare(y.name))
Expand Down
23 changes: 18 additions & 5 deletions packages/node/vrdt-common/src/rest/vro-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,25 @@ export interface InventoryElement extends Resource {
href: string
}

export interface BaseAttribute {
name: string
value?: string
}

export interface TypeAttribute extends BaseAttribute {
type: string
}

export interface LinkItem {
attributes: TypeAttribute[]
rel: string
href: string
description?: BaseAttribute
}

export interface ContentLinksResponse {
link: {
attributes: { name: string; value: string; type: string }[]
rel: string
href: string
}[]
link: LinkItem[]
total?: number
}

export interface ContentChildrenResponse {
Expand Down
4 changes: 3 additions & 1 deletion packages/node/vro-language-server/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ message Action {
}`

export const Timeout = {
ONE_SECOND: 1000
ONE_SECOND: 1000,
THREE_SECONDS: 3000,
FIVE_SECONDS: 5000
}

export enum CompletionPrefixKind {
Expand Down
32 changes: 12 additions & 20 deletions packages/node/vro-language-server/src/server/core/HintLookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,12 @@ class HintStore<T> {
@AutoWire
export class HintLookup implements Disposable {
private readonly logger = Logger.get("HintLookup")

private scriptingApi: HintStore<vmw.pscoe.hints.ScriptingApiPack> = new HintStore()
actions: any = new HintStore()
private configs: HintStore<vmw.pscoe.hints.ConfigurationsPack> = new HintStore()
private vroModulesAndActions: HintModule[]
private vroObjects: vmw.pscoe.hints.IClass[]

private subscriptions: Disposable[] = []
actions: any = new HintStore()

constructor(
private environment: Environment,
Expand All @@ -61,7 +59,7 @@ export class HintLookup implements Disposable {

dispose(): void {
this.logger.debug("Disposing HintLookup")
this.subscriptions.forEach(s => s && s.dispose())
this.subscriptions.forEach(s => s?.dispose())
}

getGlobalActionsPack() {
Expand Down Expand Up @@ -93,14 +91,15 @@ export class HintLookup implements Disposable {
? _.flatMap(this.actions.local[workspaceFolder.uri.fsPath], pack => pack.modules)
: []
const globalModules = _.flatMap(this.actions.global, pack => pack.modules)

return _.unionWith(localModules, globalModules, (x, y) => x.name === y.name)
}

getActionsIn(moduleName: string, workspaceFolder?: WorkspaceFolder): vmw.pscoe.hints.IAction[] {
const module = this.getActionModules(workspaceFolder).find(module => module.name === moduleName)
this.logger.debug(`Module hint: ${JSON.stringify(module, null, 4)}`)

if (module && module.actions) {
if (module?.actions) {
return module.actions.filter(action => !!action)
}

Expand All @@ -116,13 +115,14 @@ export class HintLookup implements Disposable {
? _.flatMap(this.configs.local[workspaceFolder.uri.fsPath], pack => pack.categories)
: []
const globalCategories = _.flatMap(this.configs.global, pack => pack.categories)

return _.unionWith(localCategories, globalCategories, (x, y) => x.path === y.path)
}

getConfigElementsIn(categoryPath: string, workspaceFolder?: WorkspaceFolder): vmw.pscoe.hints.IConfig[] {
const module = this.getConfigCategories(workspaceFolder).find(category => category.path === categoryPath)

if (module && module.configurations) {
if (module?.configurations) {
return module.configurations.filter(config => !!config)
}

Expand All @@ -135,13 +135,10 @@ export class HintLookup implements Disposable {
if (this.vroObjects) {
result.push(...this.vroObjects)
}

for (const api of this.scriptingApi.global) {
for (const cls of api.classes) {
const hasConstructors = !!cls.constructors && cls.constructors.length > 0
if (filter.isInstantiable === undefined) {
result.push(cls)
} else if (hasConstructors === filter.isInstantiable) {
if (filter.isInstantiable === undefined || hasConstructors === filter.isInstantiable) {
result.push(cls)
}
}
Expand All @@ -165,12 +162,15 @@ export class HintLookup implements Disposable {
//
// Event Handlers
//

initialize() {
this.environment.workspaceFolders.forEach(this.load, this)
this.load()
}

refreshForWorkspace(workspaceFolder: WorkspaceFolder): void {
this.load(workspaceFolder)

Check warning on line 171 in packages/node/vro-language-server/src/server/core/HintLookup.ts

View check run for this annotation

Codecov / codecov/patch

packages/node/vro-language-server/src/server/core/HintLookup.ts#L170-L171

Added lines #L170 - L171 were not covered by tests
}

private onDidChangeConfiguration(): void {
this.logger.debug("HintLookup.onDidChangeConfiguration()")
this.load()
Expand All @@ -183,14 +183,9 @@ export class HintLookup implements Disposable {
}
}

refreshForWorkspace(workspaceFolder: WorkspaceFolder): void {
this.load(workspaceFolder)
}

//
// Load proto files
//

private load(workspaceFolder?: WorkspaceFolder): void {
const actionsFile = this.environment.resolveHintFile("actions.pb", workspaceFolder)
if (actionsFile) {
Expand All @@ -208,7 +203,6 @@ export class HintLookup implements Disposable {
)
}
}

const configsFile = this.environment.resolveHintFile("configs.pb", workspaceFolder)
if (configsFile) {
this.loadProtoInScope([configsFile], workspaceFolder, this.configs, vmw.pscoe.hints.ConfigurationsPack)
Expand All @@ -218,7 +212,6 @@ export class HintLookup implements Disposable {
// plugins aren't located in workspace folder
const coreApiFile = this.environment.resolveHintFile("core-api.pb", undefined)
const pluginFiles = this.environment.resolvePluginHintFiles()

if (coreApiFile) {
pluginFiles.push(coreApiFile)
}
Expand All @@ -244,7 +237,6 @@ export class HintLookup implements Disposable {
result.push(hintPack)
}
})

if (!scope) {
target.global = result
} else {
Expand All @@ -259,8 +251,8 @@ export class HintLookup implements Disposable {
this.logger.warn(`Hint file '${filePath}' does not exist`)
return null
}

const buffer = fs.readFileSync(filePath)

return decoder.decode(buffer)
}
}
Loading

0 comments on commit eb6090a

Please sign in to comment.