-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.js
173 lines (154 loc) · 5.25 KB
/
admin.js
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
162
163
164
165
166
167
168
169
170
171
172
173
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const fs = require("fs");
const dotenv = require("dotenv");
dotenv.config({ path: ".env.local" });
dotenv.config();
const path = require("path");
const { cursorTo } = require("readline");
const router = express.Router();
const Sentiment = require("sentiment");
const uri = process.env.ATLAS_URI;
if (!uri) {
console.error("MongoDB URI is not defined in the environment variables");
process.exit(1);
}
async function connectToDatabase() {
try {
await mongoose.connect(uri);
console.log("Connected to MongoDB.\nApp is running.");
} catch (err) {
console.error("Error connecting to MongoDB: ", err);
process.exit(1);
}
}
connectToDatabase();
const app = express();
const sentiment = new Sentiment();
app.use(bodyParser.urlencoded({ extended: true }));
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
const GitHubUserSchema = new mongoose.Schema({
CertID: { type: String, required: true, unique: true },
Name: { type: String, required: true },
Class: { type: String, enum: ["BE", "TE", "SE"], required: true },
LastAccessed: { type: Date, default: null },
Feedback: { type: String, default: null },
});
const MembershipUserSchema = new mongoose.Schema({
MemberID: { type: String, required: true },
Name: { type: String, required: true },
Email: { type: String, required: true },
PositionGroup: {
type: String,
enum: ["Executive", "Team Lead", "Member"],
default: "Member",
required: true,
},
Position: { type: String, required: true },
AcademicYear: {
type: String,
enum: ["2022-23", "2023-24", "2024-25"],
required: true,
},
Class: { type: String, enum: ["BE", "TE", "SE"], required: true },
CertID: { type: String, unique: true },
LastAccessed: { type: Date, default: null },
});
app.use(express.static(__dirname + "/public"));
const MembershipUser = mongoose.model(
"Members",
MembershipUserSchema,
"Members"
);
const GitHub_101_User = mongoose.model(
"GitHub_101",
GitHubUserSchema,
"GitHub_101"
);
app.get('/admin', async (req, res) => {
const pageMembership = parseInt(req.query.pageMembership) || 1; // Membership pagination
const pageGitHub = parseInt(req.query.pageGitHub) || 1; // GitHub pagination
const limit = parseInt(req.query.limit) || 10;
try {
const totalMembershipUsers = await MembershipUser.countDocuments();
const recentMembership = await MembershipUser.countDocuments({ LastAccessed: { $exists: true, $ne: null, $ne: "" } });
const totalPagesMembership = Math.ceil(recentMembership / limit);
const recentMembershipGetters = await MembershipUser.find({
LastAccessed: {
$exists: true,
$ne: null,
$ne: ""
}
})
.sort({ LastAccessed: -1 })
.skip((pageMembership - 1) * limit)
.limit(limit);
const totalGitHubUsers = await GitHub_101_User.countDocuments();
const recentGithub = await MembershipUser.countDocuments({ LastAccessed: { $exists: true, $ne: null, $ne: "" } });
const totalPagesGitHub = Math.ceil(recentGithub / limit);
const recentGitHubGetters = await GitHub_101_User.find({
LastAccessed: {
$exists: true,
$ne: null,
$ne: ""
}
})
.sort({ LastAccessed: -1 })
.skip((pageGitHub - 1) * limit)
.limit(limit);
const enrichedGitHubGetters = recentGitHubGetters.map(user => {
const analysis = user.Feedback ? sentiment.analyze(user.Feedback) : { score: 0 };
return {
...user.toObject(), // Convert Mongoose document to plain object
sentimentScore: analysis.score,
sentimentClass: classifySentiment(analysis)
};
});
res.render('admin', {
recentMembershipGetters,
membersWithCertificates: await MembershipUser.countDocuments({ LastAccessed: { $exists: true, $ne: null, $ne: "" } }),
totalMembershipUsers,
recentGitHubGetters: enrichedGitHubGetters,
githubUsersWithCertificates: await GitHub_101_User.countDocuments({ LastAccessed: { $exists: true, $ne: null, $ne: "" } }),
totalGitHubUsers,
currentPageMembership: pageMembership,
totalPagesMembership,
recentMembership,
currentPageGitHub: pageGitHub,
totalPagesGitHub,
recentGithub,
limit
});
} catch (error) {
console.error("Error fetching admin stats:", error);
res.status(500).send("Internal Server Error");
}
});
const classifySentiment = (analysis) => {
const score = analysis.score;
if (score > 0) return 'Positive';
else if (score < 0) return 'Negative';
else return 'Neutral';
};
const PORT = process.env.PORT || 3001;
app.listen(PORT, () => {
console.log(`Server running on: http://localhost:${PORT}/admin`);
});
// http://localhost:3001/admin?page=1&limit=1
process.on("SIGINT", async () => {
await mongoose.connection.close();
console.log("MongoDB connection closed.");
process.exit(0);
});
process.on("SIGTSTP", () => {
console.log("Received SIGTSTP (CTRL+Z). Triggering SIGINT.");
process.kill(process.pid, "SIGINT"); // Send SIGINT to self
});
/**
* const recentGitHubGetters = await GitHub_101_User.find()
.sort({ LastAccessed: -1 })
.skip((pageGitHub - 1) * limit)
.limit(limit);
*/