Skip to content
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

Adds a script to find examples of particular blocks using specific properties from Flow Cloud #389

Merged
merged 7 commits into from
Nov 2, 2023
93 changes: 93 additions & 0 deletions docs/extract-example-block-usage-from-flows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* This script is designed to explore the configuration used for particular blocks in flows saved to Flow Cloud.
* It can optionally filter by a specific config property, allowing for more targeted analysis.
*
* Why It's Useful:
* - Helps identify patterns or common configurations for specific block types.
* - Assists in auditing and understanding how a particular property is being used across multiple flows.
*
* How to Use:
* 1. Modify the variables below to specify the block type and property of interest (if any).
* 2. Run the script using Node.js.
* 3. Check the generated JSON file for the grouped data.
*/


let inputFilePath = 'flows.json';
let outputFilePath = 'flow-block-config-analysis.json';
lukestanley marked this conversation as resolved.
Show resolved Hide resolved
let blockType = 'form'; // Replace with the block type you're interested in
let propertyOfInterest = null; // E.g: 'uiSchema' - replace with the property you're interested in or leave as null


const fs = require('fs');
const https = require('https');
const { URL } = require('url');

const readJsonFile = (filePath) => JSON.parse(fs.readFileSync(filePath, 'utf8'));

const writeJsonFile = (data, filePath) => fs.writeFileSync(filePath, JSON.stringify(data, null, 4));

// Function to generate grouped JSON based on block type and optionally a property of interest
const generateGroupedJson = (inputFilePath, outputFilePath, blockType, propertyOfInterest = null) => {
const flowsData = readJsonFile(inputFilePath);
let enhancedFlowsWithBlocks = [];
let groupedByAdapter = {};

// Iterate through the flows to collect blocks and their metadata
for (const flow of flowsData) {
let flowBlocks = []; // To store the blocks with the property of interest for this flow

if (flow.blocks) {
for (const block of flow.blocks) {
if (block.type === blockType && (!propertyOfInterest || (block[propertyOfInterest] && Object.keys(block[propertyOfInterest]).length > 0))) {
lukestanley marked this conversation as resolved.
Show resolved Hide resolved
flowBlocks.push(block);
}
}
}

if (flowBlocks.length > 0) {
// Add metadata
const blockMetadata = {
adapterName: flow.adapterName,
id: flow.id,
title: flow.title,
url: `https://app.kendra.io/${flow.adapterName}/${flow.id}`
lukestanley marked this conversation as resolved.
Show resolved Hide resolved
};

// Add the blocks and metadata to the enhanced list
const enhancedFlowEntry = { meta: blockMetadata, blocks: flowBlocks };
enhancedFlowsWithBlocks.push(enhancedFlowEntry);
}
}

// Group the flows by 'adapterName'
for (const entry of enhancedFlowsWithBlocks) {
const adapterName = entry.meta.adapterName;
if (!groupedByAdapter[adapterName]) {
groupedByAdapter[adapterName] = [];
}
groupedByAdapter[adapterName].push(entry);
}

// Write the grouped data to the output file
writeJsonFile(groupedByAdapter, outputFilePath);
};

// Main function to execute the script
(async () => {
// Check if 'flows.json' exists in the current directory
if (!fs.existsSync(inputFilePath)) {
// Download the JSON file using Node.js built-in https
const url = new URL('https://app.kendra.io/flows');
const file = fs.createWriteStream(inputFilePath);
https.get(url, (response) => {
response.pipe(file);
file.on('finish', () => {
file.close();
generateGroupedJson(inputFilePath, outputFilePath, blockType, propertyOfInterest);
});
});
} else {
generateGroupedJson(inputFilePath, outputFilePath, blockType, propertyOfInterest);
}
})();