Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
161 lines (128 loc) · 4.61 KB
/
Copy pathindex.js
File metadata and controls
161 lines (128 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const express = require('express');
const { MongoClient } = require('mongodb');
const cors = require('cors'); // Import the cors package
const app = express();
const port = 3000;
const mongoUrl = 'mongodb://127.0.0.1:27017';
const dbName = 'quantdataDB';
const collectionName = 'options';
const { optionsPipeline } = require("./mongoPipeline");
// Use the cors middleware
app.use(cors()); // This allows requests from all origins. You can configure it for specific origins if needed.
app.use(express.json());
let db;
async function connectToMongo() {
const client = new MongoClient(mongoUrl, { useUnifiedTopology: true });
try {
await client.connect();
db = client.db(dbName);
console.log('Connected to MongoDB');
} catch (error) {
console.error('Error connecting to MongoDB:', error);
}
}
connectToMongo();
app.get('/get_options', async (req, res) => {
try {
const searchTickerList = Object.values(req.query);
const collection = db.collection(collectionName);
let optionsData;
let pipeline = []
if (searchTickerList.length == 0) {
optionsData = await collection.find({}).sort({ tradeId: -1 }).limit(20).toArray();
} else {
optionsData = await collection.find({ ticker: { $in: searchTickerList } }).sort({ tradeId: -1 }).limit(10).toArray();
pipeline.push({
$match: {
$and: [
{
ticker: { $in: searchTickerList },
},
],
}
})
}
pipeline = [...pipeline, ...optionsPipeline];
let result = await collection.aggregate(pipeline).toArray();
if (result.length > 0) {
const [callResult, putResult] = [result[0], result[1]];
res.json({
optionsData: optionsData.reverse(),
callTotalVolume: callResult.totalVolume,
putTotalVolume: putResult.totalVolume,
callTotalPremiumPriceInCents: callResult.totalPremiumPriceInCents,
putTotalPremiumPriceInCents: putResult.totalPremiumPriceInCents
});
} else {
res.json({
optionsData: [],
callTotalVolume: 0,
putTotalVolume: 0,
callTotalPremiumPriceInCents: 0,
putTotalPremiumPriceInCents: 0
});
}
} catch (error) {
console.error('Error fetching data from options collection:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.get('/get_news', async (req, res) => {
try {
let collectionName = "news"
const collection = db.collection(collectionName);
const searchTickerList = Object.values(req.query);
if (searchTickerList.length == 0) {
newsData = await collection.find({}).sort({ _id: -1 }).limit(10).toArray();
} else {
newsData = await collection.find({
"tickerMetadata": {
$elemMatch: { "ticker": { $in: searchTickerList } }
}
}).sort({ _id: -1 }).limit(10).toArray();
}
res.json({ newsData: newsData });
} catch (error) {
console.error('Error fetching data from news collection:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.get('/get_alerts', async (req, res) => {
try {
let collectionName = "alerts"
const collection = db.collection(collectionName);
let alertsData = await collection.find({}).sort({ _id: -1 }).limit(10).toArray();
alertsData = alertsData.reverse();
res.json({ alertsData: alertsData });
} catch (error) {
console.error('Error fetching data from alerts collection:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.get('/get_app_gainers_losers', async (req, res) => {
try {
let collectionName = "app_gainers_losers"
const collection = db.collection(collectionName);
let appGainersLosersData = await collection.find({}).sort({ _id: -1 }).limit(10).toArray();
appGainersLosersData = appGainersLosersData.reverse();
res.json({ appGainersLosersData: appGainersLosersData[0] });
} catch (error) {
console.error('Error fetching data from app_gainers_losers collection:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.get('/get_equities', async (req, res) => {
try {
let collectionName = "equities"
const collection = db.collection(collectionName);
let equitiesData = await collection.find({}).sort({ _id: -1 }).limit(10).toArray();
equitiesData = equitiesData.reverse();
res.json({ equitiesData: equitiesData });
} catch (error) {
console.error('Error fetching data from equities collection:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});