aiden-shared-calculations-unified 1.0.4 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,99 @@
1
+ /**
2
+ * @fileoverview Aggregates activity data (likes, comments) from social posts.
3
+ * This calculation ONLY uses the social post insights context, not portfolio data.
4
+ */
5
+
6
+ class SocialActivityAggregation {
7
+ constructor() {
8
+ this.tickerActivity = {};
9
+ this.totalLikes = 0;
10
+ this.totalComments = 0;
11
+ this.processed = false; // Flag to ensure this runs only once
12
+ }
13
+
14
+ /**
15
+ * @param {null} todayPortfolio - Not used.
16
+ * @param {null} yesterdayPortfolio - Not used.
17
+ * @param {null} userId - Not used.
18
+ * @param {object} context - Shared context (e.g., mappings).
19
+ * @param {object} todayInsights - Daily instrument insights.
20
+ * @param {object} yesterdayInsights - Yesterday's instrument insights.
21
+ * @param {object} todaySocialPostInsights - Map of { [postId]: postData } for today.
22
+ * @param {object} yesterdaySocialPostInsights - Map of { [postId]: postData } for yesterday.
23
+ */
24
+ async process(todayPortfolio, yesterdayPortfolio, userId, context, todayInsights, yesterdayInsights, todaySocialPostInsights, yesterdaySocialPostInsights) {
25
+ // This process should only run once per day
26
+ if (this.processed || !todaySocialPostInsights) {
27
+ return;
28
+ }
29
+ this.processed = true;
30
+
31
+ const posts = Object.values(todaySocialPostInsights);
32
+
33
+ for (const post of posts) {
34
+ const likeCount = post.likeCount || 0;
35
+ const commentCount = post.commentCount || 0;
36
+
37
+ // 1. Aggregate global counts
38
+ this.totalLikes += likeCount;
39
+ this.totalComments += commentCount;
40
+
41
+ // 2. Aggregate counts per ticker
42
+ if (post.tickers && Array.isArray(post.tickers)) {
43
+ for (const ticker of post.tickers) {
44
+ if (!this.tickerActivity[ticker]) {
45
+ this.tickerActivity[ticker] = {
46
+ totalLikes: 0,
47
+ totalComments: 0,
48
+ totalPosts: 0
49
+ };
50
+ }
51
+ this.tickerActivity[ticker].totalLikes += likeCount;
52
+ this.tickerActivity[ticker].totalComments += commentCount;
53
+ this.tickerActivity[ticker].totalPosts++;
54
+ }
55
+ }
56
+ }
57
+ }
58
+
59
+ async getResult() {
60
+ if (!this.processed) return {};
61
+
62
+ // Calculate average activity per ticker
63
+ const tickerAverageActivity = {};
64
+ for (const ticker in this.tickerActivity) {
65
+ const data = this.tickerActivity[ticker];
66
+ if (data.totalPosts > 0) {
67
+ tickerAverageActivity[ticker] = {
68
+ avgLikesPerPost: data.totalLikes / data.totalPosts,
69
+ avgCommentsPerPost: data.totalComments / data.totalPosts,
70
+ ...data // Include total counts
71
+ };
72
+ } else {
73
+ tickerAverageActivity[ticker] = {
74
+ avgLikesPerPost: 0,
75
+ avgCommentsPerPost: 0,
76
+ ...data
77
+ };
78
+ }
79
+ }
80
+
81
+ return {
82
+ globalActivity: {
83
+ totalLikes: this.totalLikes,
84
+ totalComments: this.totalComments,
85
+ totalPosts: Object.keys(todaySocialPostInsights || {}).length
86
+ },
87
+ tickerActivity: tickerAverageActivity
88
+ };
89
+ }
90
+
91
+ reset() {
92
+ this.tickerActivity = {};
93
+ this.totalLikes = 0;
94
+ this.totalComments = 0;
95
+ this.processed = false;
96
+ }
97
+ }
98
+
99
+ module.exports = SocialActivityAggregation;
@@ -0,0 +1,122 @@
1
+ /**
2
+ * @fileoverview Correlates social post volume spikes with topics from Gemini analysis.
3
+ * This calculation ONLY uses the social post insights context.
4
+ */
5
+
6
+ class SocialEventCorrelation {
7
+ constructor() {
8
+ this.todayTickerVolume = {};
9
+ this.yesterdayTickerVolume = {};
10
+ this.todayTopicCounts = {}; // { "TICKER": { "topic": count } }
11
+ this.processed = false;
12
+ }
13
+
14
+ /**
15
+ * Helper to process a set of posts and populate volume/topic counts.
16
+ * @param {object} socialPostInsights - Map of { [postId]: postData }
17
+ * @param {object} volumeTarget - The object to store volume counts.
18
+ * @param {object} [topicTarget] - Optional. The object to store topic counts.
19
+ */
20
+ _processPosts(socialPostInsights, volumeTarget, topicTarget = null) {
21
+ if (!socialPostInsights) return;
22
+ const posts = Object.values(socialPostInsights);
23
+
24
+ for (const post of posts) {
25
+ if (!post.tickers || !Array.isArray(post.tickers)) continue;
26
+
27
+ for (const ticker of post.tickers) {
28
+ // 1. Aggregate Volume
29
+ volumeTarget[ticker] = (volumeTarget[ticker] || 0) + 1;
30
+
31
+ // 2. Aggregate Topics (if target provided)
32
+ if (topicTarget) {
33
+ if (!topicTarget[ticker]) {
34
+ topicTarget[ticker] = {};
35
+ }
36
+ // New structure: post.sentiment is { overallSentiment: "...", topics: [...] }
37
+ const topics = post.sentiment?.topics;
38
+ if (topics && Array.isArray(topics)) {
39
+ for (const topic of topics) {
40
+ // Normalize topic to lowercase for consistent counting
41
+ const normalizedTopic = topic.toLowerCase();
42
+ topicTarget[ticker][normalizedTopic] = (topicTarget[ticker][normalizedTopic] || 0) + 1;
43
+ }
44
+ }
45
+ }
46
+ }
47
+ }
48
+ }
49
+
50
+ /**
51
+ * @param {null} todayPortfolio - Not used.
52
+ * @param {null} yesterdayPortfolio - Not used.
53
+ * @param {null} userId - Not used.
54
+ * @param {object} context - Shared context (e.g., mappings).
55
+ * @param {object} todayInsights - Daily instrument insights.
56
+ * @param {object} yesterdayInsights - Yesterday's instrument insights.
57
+ * @param {object} todaySocialPostInsights - Map of { [postId]: postData } for today.
58
+ * @param {object} yesterdaySocialPostInsights - Map of { [postId]: postData } for yesterday.
59
+ */
60
+ async process(todayPortfolio, yesterdayPortfolio, userId, context, todayInsights, yesterdayInsights, todaySocialPostInsights, yesterdaySocialPostInsights) {
61
+
62
+ if (this.processed) {
63
+ return; // Run only once
64
+ }
65
+ this.processed = true;
66
+
67
+ // 1. Process yesterday's posts for baseline volume
68
+ this._processPosts(yesterdaySocialPostInsights, this.yesterdayTickerVolume);
69
+
70
+ // 2. Process today's posts for volume AND topic correlation
71
+ this._processPosts(todaySocialPostInsights, this.todayTickerVolume, this.todayTopicCounts);
72
+ }
73
+
74
+ async getResult() {
75
+ if (!this.processed) return {};
76
+
77
+ const results = {};
78
+ const allTickers = new Set([
79
+ ...Object.keys(this.todayTickerVolume),
80
+ ...Object.keys(this.yesterdayTickerVolume)
81
+ ]);
82
+
83
+ for (const ticker of allTickers) {
84
+ const todayVolume = this.todayTickerVolume[ticker] || 0;
85
+ // Use 1 as baseline if yesterday was 0 to avoid -100% change, but 0 for spike calc
86
+ const yesterdayVolumeForCalc = this.yesterdayTickerVolume[ticker] || 0;
87
+
88
+ let volumeSpikePercent = 0;
89
+ if (yesterdayVolumeForCalc > 0) {
90
+ volumeSpikePercent = ((todayVolume - yesterdayVolumeForCalc) / yesterdayVolumeForCalc) * 100;
91
+ } else if (todayVolume > 0) {
92
+ volumeSpikePercent = todayVolume * 100.0; // e.g., 0 to 5 posts is a 500% increase
93
+ }
94
+
95
+ // Only report tickers with activity today
96
+ if (todayVolume === 0) continue;
97
+
98
+ // Correlate topics
99
+ const topics = this.todayTopicCounts[ticker] || {};
100
+ const correlatedEvents = Object.entries(topics)
101
+ .map(([topic, postCount]) => ({ topic, postCount }))
102
+ .sort((a, b) => b.postCount - a.postCount); // Sort by count descending
103
+
104
+ results[ticker] = {
105
+ volume: todayVolume,
106
+ volumeSpikePercent: volumeSpikePercent,
107
+ correlatedEvents: correlatedEvents
108
+ };
109
+ }
110
+
111
+ return results;
112
+ }
113
+
114
+ reset() {
115
+ this.todayTickerVolume = {};
116
+ this.yesterdayTickerVolume = {};
117
+ this.todayTopicCounts = {};
118
+ this.processed = false;
119
+ }
120
+ }
121
+
122
+ module.exports = SocialEventCorrelation;
@@ -0,0 +1,112 @@
1
+ /**
2
+ * @fileoverview Aggregates sentiment data from social posts.
3
+ * This calculation ONLY uses the social post insights context, not portfolio data.
4
+ */
5
+
6
+ class SocialSentimentAggregation {
7
+ constructor() {
8
+ this.tickerSentiments = {};
9
+ this.totalPosts = 0;
10
+ this.sentimentCounts = {
11
+ Bullish: 0,
12
+ Bearish: 0,
13
+ Neutral: 0
14
+ };
15
+ this.processed = false; // Flag to ensure this runs only once
16
+ }
17
+
18
+ /**
19
+ * @param {null} todayPortfolio - Not used.
20
+ * @param {null} yesterdayPortfolio - Not used.
21
+ * @param {null} userId - Not used.
22
+ * @param {object} context - Shared context (e.g., mappings).
23
+ * @param {object} todayInsights - Daily instrument insights.
24
+ * @param {object} yesterdayInsights - Yesterday's instrument insights.
25
+ * @param {object} todaySocialPostInsights - Map of { [postId]: postData } for today.
26
+ * @param {object} yesterdaySocialPostInsights - Map of { [postId]: postData } for yesterday.
27
+ */
28
+ async process(todayPortfolio, yesterdayPortfolio, userId, context, todayInsights, yesterdayInsights, todaySocialPostInsights, yesterdaySocialPostInsights) {
29
+ // This process should only run once per day, triggered by the first "user" (which is just a trigger).
30
+ if (this.processed || !todaySocialPostInsights) {
31
+ return;
32
+ }
33
+ this.processed = true;
34
+
35
+ const posts = Object.values(todaySocialPostInsights);
36
+ this.totalPosts = posts.length;
37
+
38
+ for (const post of posts) {
39
+ const sentiment = post.sentiment || 'Neutral';
40
+
41
+ // 1. Aggregate global sentiment counts
42
+ if (this.sentimentCounts.hasOwnProperty(sentiment)) {
43
+ this.sentimentCounts[sentiment]++;
44
+ }
45
+
46
+ // 2. Aggregate sentiment per ticker
47
+ if (post.tickers && Array.isArray(post.tickers)) {
48
+ for (const ticker of post.tickers) {
49
+ if (!this.tickerSentiments[ticker]) {
50
+ this.tickerSentiments[ticker] = {
51
+ Bullish: 0,
52
+ Bearish: 0,
53
+ Neutral: 0,
54
+ totalPosts: 0
55
+ };
56
+ }
57
+ if (this.tickerSentiments[ticker].hasOwnProperty(sentiment)) {
58
+ this.tickerSentiments[ticker][sentiment]++;
59
+ }
60
+ this.tickerSentiments[ticker].totalPosts++;
61
+ }
62
+ }
63
+ }
64
+ }
65
+
66
+ async getResult() {
67
+ if (!this.processed) return {};
68
+
69
+ // Calculate global sentiment ratio
70
+ let globalSentimentRatio = 0;
71
+ const totalSentiments = this.sentimentCounts.Bullish + this.sentimentCounts.Bearish;
72
+ if (totalSentiments > 0) {
73
+ globalSentimentRatio = (this.sentimentCounts.Bullish / totalSentiments) * 100;
74
+ }
75
+
76
+ // Calculate per-ticker sentiment ratios
77
+ const tickerSentimentRatios = {};
78
+ for (const ticker in this.tickerSentiments) {
79
+ const data = this.tickerSentiments[ticker];
80
+ const totalTickerSentiments = data.Bullish + data.Bearish;
81
+ if (totalTickerSentiments > 0) {
82
+ tickerSentimentRatios[ticker] = {
83
+ sentimentRatio: (data.Bullish / totalTickerSentiments) * 100,
84
+ ...data // Include counts
85
+ };
86
+ } else {
87
+ tickerSentimentRatios[ticker] = {
88
+ sentimentRatio: 50, // Default to neutral if no clear sentiment
89
+ ...data // Include counts
90
+ };
91
+ }
92
+ }
93
+
94
+ return {
95
+ globalSentiment: {
96
+ ...this.sentimentCounts,
97
+ totalPosts: this.totalPosts,
98
+ sentimentRatio: globalSentimentRatio
99
+ },
100
+ tickerSentiment: tickerSentimentRatios
101
+ };
102
+ }
103
+
104
+ reset() {
105
+ this.tickerSentiments = {};
106
+ this.totalPosts = 0;
107
+ this.sentimentCounts = { Bullish: 0, Bearish: 0, Neutral: 0 };
108
+ this.processed = false;
109
+ }
110
+ }
111
+
112
+ module.exports = SocialSentimentAggregation;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aiden-shared-calculations-unified",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "Shared calculation modules for the BullTrackers Computation System.",
5
5
  "main": "index.js",
6
6
  "files": [