Skip to content

Commit 86d7ce4

Browse files
committed
refactor(api): enhance analytics integration by adding datasets to wrangler configuration and updating node types for compute usage tracking
1 parent faa65e8 commit 86d7ce4

7 files changed

Lines changed: 30 additions & 0 deletions

File tree

apps/api/src/context.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Workflow } from "@cloudflare/workers-types";
22
import { Fetcher } from "@cloudflare/workers-types";
3+
import { AnalyticsEngineDataset } from "@cloudflare/workers-types";
34
import { JWTTokenPayload } from "@dafthunk/types";
45

56
import { RuntimeParams } from "./runtime/runtime";
@@ -11,6 +12,7 @@ export interface Bindings {
1112
AI: Ai;
1213
KV: KVNamespace;
1314
BROWSER: Fetcher;
15+
COMPUTE: AnalyticsEngineDataset;
1416
WEB_HOST: string;
1517
EMAIL_DOMAIN: string;
1618
JWT_SECRET: string;

apps/api/src/nodes/node-registry.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,4 +326,12 @@ export class NodeRegistry {
326326
!nodeType.compatibility || nodeType.compatibility.includes(workflowType)
327327
);
328328
}
329+
330+
public getNodeType(nodeType: string): NodeType {
331+
const Implementation = this.implementations.get(nodeType);
332+
if (!Implementation) {
333+
throw new Error(`Node type not found: ${nodeType}`);
334+
}
335+
return Implementation.nodeType;
336+
}
329337
}

apps/api/src/nodes/text/llama-3-1-8b-instruct-fast-node.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export class Llama318BInstructFastNode extends ExecutableNode {
1313
description: "Generates text",
1414
category: "Text",
1515
icon: "ai",
16+
usage: 10,
1617
inputs: [
1718
{
1819
name: "prompt",

apps/api/src/nodes/text/llama-3-3-70b-instruct-fp8-fast-node.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export class Llama3370BInstructFastNode extends ExecutableNode {
2020
description: "Generates text using Llama 3.3 70B model with fp8 precision",
2121
category: "Text",
2222
icon: "ai",
23+
usage: 100,
2324
inputs: [
2425
{
2526
name: "prompt",

apps/api/src/runtime/runtime.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ export class Runtime extends WorkflowEntrypoint<Bindings, RuntimeParams> {
158158
async () =>
159159
this.executeNode(
160160
runtimeState,
161+
workflow.id,
161162
nodeIdentifier,
162163
organizationId,
163164
instanceId,
@@ -251,6 +252,7 @@ export class Runtime extends WorkflowEntrypoint<Bindings, RuntimeParams> {
251252
*/
252253
private async executeNode(
253254
runtimeState: RuntimeState,
255+
workflowId: string,
254256
nodeIdentifier: string,
255257
organizationId: string,
256258
executionId: string,
@@ -268,6 +270,15 @@ export class Runtime extends WorkflowEntrypoint<Bindings, RuntimeParams> {
268270
return { ...runtimeState, status: "error" };
269271
}
270272

273+
const nodeType = NodeRegistry.getInstance().getNodeType(node.type);
274+
if (nodeType.usage) {
275+
this.env.COMPUTE.writeDataPoint({
276+
indexes: [organizationId],
277+
blobs: [organizationId, workflowId, node.id],
278+
doubles: [nodeType.usage],
279+
});
280+
}
281+
271282
// Resolve the runnable implementation.
272283
const executable = NodeRegistry.getInstance().createExecutableNode(node);
273284
if (!executable) {

apps/api/wrangler.jsonc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@
6363
"class_name": "Runtime",
6464
},
6565
],
66+
"analytics_engine_datasets": [
67+
{ "binding": "COMPUTE", "dataset": "dafthunk-compute" },
68+
],
6669

6770
/**
6871
* Environment Variables
@@ -114,6 +117,9 @@
114117
"preview_id": "dd17ff67a75c41768dc30078eec37c46",
115118
},
116119
],
120+
"analytics_engine_datasets": [
121+
{ "binding": "COMPUTE", "dataset": "dafthunk-compute" },
122+
],
117123
"vars": {
118124
"WEB_HOST": "https://www.dafthunk.com",
119125
},

packages/types/src/workflow.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export interface NodeType {
9999
description?: string;
100100
category: string;
101101
icon: string;
102+
usage?: number;
102103
inputs: Parameter[];
103104
outputs: Parameter[];
104105
compatibility?: WorkflowType[]; // Optional array of workflow types this node is compatible with

0 commit comments

Comments
 (0)