Skip to content

Commit

Permalink
Printing streamable logs to the console
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiehewitt15 committed Dec 12, 2024
1 parent 11df128 commit c0a0ead
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 14 deletions.
1 change: 1 addition & 0 deletions metadata/compute-results-2024-12-12T13-56-39-791Z.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"Traceback (most recent call last):\r\n File \"data/transformations/algorithm\", line 18, in <module>\r\n \"termsAndConditions\": true\r\nNameError: name 'true' is not defined\r\n"
45 changes: 32 additions & 13 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import { OceanP2P } from './helpers/oceanNode'
import { download } from './helpers/download'
import {
checkComputeStatus,
computeLogsChannel,
computeStart,
delay,
getComputeLogs,
getComputeResult,
saveResults
} from './helpers/freeCompute'
Expand Down Expand Up @@ -316,9 +318,36 @@ export async function activate(context: vscode.ExtensionContext) {
nonce
)
console.log('Compute result received:', computeResponse)
const jobId = computeResponse.jobId // Assuming computeStart returns jobId
const jobId = computeResponse.jobId
console.log('Job ID:', jobId)

computeLogsChannel.show()
computeLogsChannel.appendLine(`Starting compute job with ID: ${jobId}`)

// Start fetching logs periodically

const index = 0

console.log('Generating signature for retrieval...')
progress.report({ message: 'Generating signature for retrieval...' })
const signatureResult = await generateOceanSignature({
privateKey,
consumerAddress: signer.address,
jobId,
index,
nonce
})

const logInterval = setInterval(async () => {
await getComputeLogs(
nodeUrl,
jobId,
signer.address,
nonce,
signatureResult.signature
)
}, 5000)

// Monitor job status
progress.report({ message: 'Monitoring compute job status...' })

Expand All @@ -330,19 +359,9 @@ export async function activate(context: vscode.ExtensionContext) {
progress.report({ message: `${status.statusText}` })

if (status.statusText === 'Job finished') {
// Clear the logging interval
clearInterval(logInterval)
// Generate signature for result retrieval
console.log('Generating signature for result retrieval...')
progress.report({ message: 'Generating signature for result retrieval...' })

const index = 0

const signatureResult = await generateOceanSignature({
privateKey,
consumerAddress: signer.address,
jobId,
index,
nonce
})

// Retrieve results
progress.report({ message: 'Retrieving compute results...' })
Expand Down
43 changes: 42 additions & 1 deletion src/helpers/freeCompute.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ProviderInstance } from '@oceanprotocol/lib'
import * as vscode from 'vscode'
import { Signer } from 'ethers'
import fs from 'fs'
import axios from 'axios'
Expand Down Expand Up @@ -141,3 +141,44 @@ export async function saveResults(
throw error
}
}

// Create output channel for compute logs
export const computeLogsChannel = vscode.window.createOutputChannel('Ocean Compute Logs')

export async function getComputeLogs(
nodeUrl: string,
jobId: string,
consumerAddress: string,
nonce: number,
signature: string
): Promise<void> {
try {
// Make request to get compute logs
const response = await fetch(`${nodeUrl}/directCommand`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
command: 'getComputeStreamableLogs',
jobId,
consumerAddress,
nonce,
signature
})
})

if (!response.ok) {
throw new Error(`Failed to get compute logs: ${response.statusText}`)
}

const logs = await response.json()
if (logs && Array.isArray(logs)) {
logs.forEach((log) => {
computeLogsChannel.appendLine(log)
})
}
} catch (error) {
console.error('Error fetching compute logs:', error)
}
}

0 comments on commit c0a0ead

Please sign in to comment.