-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// anomalyDetection.js | ||
|
||
class AnomalyDetection { | ||
constructor() { | ||
this.transactionHistory = []; | ||
this.threshold = { | ||
amount: 1000, // Example threshold for transaction amount | ||
frequency: 5, // Example threshold for transaction frequency | ||
}; | ||
} | ||
|
||
// Log a transaction | ||
logTransaction(userId, amount) { | ||
const transaction = { | ||
userId, | ||
amount, | ||
timestamp: new Date(), | ||
}; | ||
this.transactionHistory.push(transaction); | ||
console.log(`Transaction logged: ${JSON.stringify(transaction)}`); | ||
} | ||
|
||
// Check for anomalies in the transaction history | ||
checkForAnomalies(userId) { | ||
const userTransactions = this.transactionHistory.filter(tx => tx.userId === userId); | ||
const totalAmount = userTransactions.reduce((sum, tx) => sum + tx.amount, 0); | ||
const transactionCount = userTransactions.length; | ||
|
||
const anomalies = []; | ||
if (totalAmount > this.threshold.amount) { | ||
anomalies.push(`High transaction amount: $${totalAmount}`); | ||
} | ||
if (transactionCount > this.threshold.frequency) { | ||
anomalies.push(`High transaction frequency: ${transactionCount} transactions`); | ||
} | ||
|
||
return anomalies; | ||
} | ||
} | ||
|
||
// Example usage | ||
(async () => { | ||
const anomalyDetector = new AnomalyDetection(); | ||
const userId = 'user123'; | ||
|
||
// Simulate logging transactions | ||
anomalyDetector.logTransaction(userId, 500); | ||
anomalyDetector.logTransaction(userId, 600); | ||
anomalyDetector.logTransaction(userId, 200); | ||
anomalyDetector.logTransaction(userId, 300); | ||
anomalyDetector.logTransaction(userId, 1500); // This should trigger an anomaly | ||
|
||
// Check for anomalies | ||
const anomalies = anomalyDetector.checkForAnomalies(userId); | ||
if (anomalies.length > 0) { | ||
console.log(`Anomalies detected for user ${userId}: ${anomalies.join(', ')}`); | ||
} else { | ||
console.log(`No anomalies detected for user ${userId}.`); | ||
} | ||
})(); | ||
|
||
export default AnomalyDetection; | ||
|