From 6f4b05448b7cc82899c1a18b41b668c49dda19b9 Mon Sep 17 00:00:00 2001 From: sloppyjosh Date: Fri, 23 Feb 2024 01:18:00 -0500 Subject: [PATCH] orchestration Logic --- src/index.ts | 29 ++++++++++-- src/workflows/WorkflowOrchestrator.ts | 65 +++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 src/workflows/WorkflowOrchestrator.ts diff --git a/src/index.ts b/src/index.ts index 7ab8e44..a49922e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,29 @@ import dotenv from 'dotenv'; dotenv.config(); -const redditUsername = process.env.redditUsername; -const redditPassword = process.env.redditPassword; -const rdramaApiKey = process.env.rdramaApiKey; +import WorkflowOrchestrator from './workflows/WorkflowOrchestrator'; +import SessionManager from './rdrama/session/SessionManager'; +import { CommentFetcher } from './rdrama/services/CommentFetcher'; +import { CommentParser } from './rdrama/services/CommentParser'; +// Import other necessary services or configurations -console.log(redditUsername, redditPassword, rdramaApiKey); +async function startApplication() { + // Initialize SessionManager or other global configurations + const sessionManager = SessionManager.getInstance(); + if (!process.env.RDRAMA_API_KEY) { + throw new Error('RDRAMA_API_KEY is undefined. Please set this environment variable.'); + } + sessionManager.setAuthorizationToken(process.env.RDRAMA_API_KEY); + + // Initialize services with any required dependencies + const commentFetcher = new CommentFetcher(); + const commentParser = new CommentParser(); + + // Initialize and start your workflow + const workflowOrchestrator = new WorkflowOrchestrator(commentFetcher, commentParser); + await workflowOrchestrator.executeWorkflow(); +} + +startApplication() + .then(() => console.log('Application started successfully.')) + .catch((error) => console.error('Application failed to start:', error)); diff --git a/src/workflows/WorkflowOrchestrator.ts b/src/workflows/WorkflowOrchestrator.ts new file mode 100644 index 0000000..e3c5240 --- /dev/null +++ b/src/workflows/WorkflowOrchestrator.ts @@ -0,0 +1,65 @@ +import { CommentFetcher } from "../rdrama/services/CommentFetcher"; +import { CommentParser } from "../rdrama/services/CommentParser"; +//import { Comment } from "../rdrama/models/Comment"; + +class WorkflowOrchestrator { + constructor( + private commentFetcher: CommentFetcher, + private commentParser: CommentParser, + //private databaseService: DatabaseService, // Responsible for DB operations + //private redditNotifier: RedditNotifier // Handles notifications to Reddit users + ) {} + + /** + * Executes the defined workflow for processing comments. + */ + async executeWorkflow() { + try { + // Fetch comments from the source + const comments = await this.commentFetcher.fetchComments(); + console.log(`Fetched ${comments.length} comments`); + + // Extract and deduplicate usernames from comments + const allUsernames = comments.flatMap(comment => this.commentParser.extractUsernames(comment)); + const uniqueUsernames = [...new Set(allUsernames)]; + console.log(`Extracted ${uniqueUsernames.length} unique usernames`); + + //// Query user information based on usernames + //const userInfo = await this.databaseService.queryUsersInfo(uniqueUsernames); + //console.log(`Queried information for ${userInfo.length} users`); +// + //// Filter users who should be notified + //const usersToNotify = userInfo.filter(user => this.shouldNotifyUser(user)); + //console.log(`Identified ${usersToNotify.length} users to notify`); +// + //// Notify users + //for (const user of usersToNotify) { + // await this.redditNotifier.notifyUser(user); + // console.log(`Notified user: ${user.username}`); + //} + + console.log('Workflow executed successfully.'); + } catch (error) { + console.error('An error occurred during workflow execution:', error); + } + } + + /** + * Determines whether a user should be notified based on certain criteria. + * + * @param user - The user information object. + * @returns A boolean indicating whether the user should be notified. + */ + //private shouldNotifyUser(user: UserInfo): boolean { + // // Placeholder for the actual logic to determine if a user should be notified. + // // This could involve checking the last notification time against the current time, + // // user preferences, or other criteria defined in the business logic. + // + // // Example logic (to be replaced with actual implementation): + // const lastNotifiedTime = new Date(user.lastNotified); // Assuming 'lastNotified' is a Date or string. + // const notificationThreshold = 24 * 60 * 60 * 1000; // 24 hours in milliseconds. + // return (Date.now() - lastNotifiedTime.getTime()) > notificationThreshold; + //} +} + +export default WorkflowOrchestrator; \ No newline at end of file