@@ -4,12 +4,14 @@ import "dotenv/config";
44import fs from "fs/promises" ;
55import path from "path" ;
66import { diff } from "json-diff" ;
7+ import { json2csv } from "json-2-csv" ;
78
89const GREEN_CHECK = "\x1b[32m✔\x1b[0m" ;
910const RED_CROSS = "\x1b[31m✖\x1b[0m" ;
1011
1112let totalEvals = 0 ;
1213let totalSuccesses = 0 ;
14+ let apiResults = [ ]
1315
1416const apiHost = process . env . API_BASE_URL || "https://api.pipedream.com" ;
1517
@@ -41,7 +43,23 @@ function customDiff(original, updated, oldLabel = "expected", newLabel = "actual
4143 return replaceLabels ( result ) ;
4244}
4345
46+ async function exportToCsv ( filePath , limit , threshold ) {
47+ const csvData = json2csv ( apiResults , {
48+ fields : [ "query" , "evalTriggers" , "apiTriggers" , "evalActions" , "apiActions" , "success" ]
49+ } ) ;
50+ const parts = filePath . split ( "/" )
51+ const path = parts [ parts . length - 1 ] . split ( ".json" ) [ 0 ]
52+ await fs . writeFile ( `./csv/${ path } -${ limit } -${ threshold } .csv` , csvData ) ;
53+ }
54+
55+ function arrayToString ( items ) {
56+ if ( items ) return items . join ( "," )
57+ return ""
58+ }
59+
4460async function processEvalFile ( filePath ) {
61+ const limit = 3
62+ const threshold = 0.65
4563 try {
4664 const content = await fs . readFile ( filePath , "utf-8" ) ;
4765 const evalData = JSON . parse ( content ) ;
@@ -53,7 +71,7 @@ async function processEvalFile(filePath) {
5371 } = evalTest ;
5472
5573 const encodedQuery = encodeURIComponent ( query ) ;
56- const apiUrl = `${ apiHost } /v1/components/search?query=${ encodedQuery } ` ;
74+ const apiUrl = `${ apiHost } /v1/components/search?query=${ encodedQuery } &similarity_threshold= ${ threshold } &limit= ${ limit } ` ;
5775
5876 const response = await fetch ( apiUrl , {
5977 headers : {
@@ -63,16 +81,20 @@ async function processEvalFile(filePath) {
6381 } ) ;
6482 const apiData = await response . json ( ) ;
6583
84+
6685 // Compare actual and expected
6786 const apiTriggers = apiData ?. triggers ?? [ ] ;
6887 const apiActions = apiData ?. actions ?? [ ] ;
88+
6989 const triggersMatch =
7090 JSON . stringify ( apiTriggers . sort ( ) ) === JSON . stringify ( triggers . sort ( ) ) ;
7191 const actionsMatch =
7292 JSON . stringify ( apiActions . sort ( ) ) === JSON . stringify ( actions . sort ( ) ) ;
7393
94+ let success = false
7495 if ( triggersMatch && actionsMatch ) {
7596 totalSuccesses ++ ;
97+ success = true
7698 console . log ( `${ GREEN_CHECK } Success for query: "${ query } "` ) ;
7799 } else {
78100 console . log ( `${ RED_CROSS } Failure for query: "${ query } "` ) ;
@@ -82,10 +104,21 @@ async function processEvalFile(filePath) {
82104 actions,
83105 } , apiData ) ) ;
84106 }
107+
108+ const record = {
109+ query : query . replace ( "\"" , "" ) ,
110+ apiTriggers : arrayToString ( apiTriggers ) ,
111+ apiActions : arrayToString ( apiActions ) ,
112+ evalTriggers : arrayToString ( triggers ) ,
113+ evalActions : arrayToString ( actions ) ,
114+ success : success
115+ } ;
116+ apiResults . push ( record )
85117 }
86118 } catch ( error ) {
87119 console . error ( `Error processing file ${ filePath } :` , error . message ) ;
88120 }
121+ await exportToCsv ( filePath , limit , threshold )
89122}
90123
91124async function main ( ) {
0 commit comments