This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
appointment.js
271 lines (231 loc) · 7.87 KB
/
appointment.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
'use strict';
require('dotenv').config();
const validator = require('validator');
const express = require('express');
const useragent = require('express-useragent');
const app = express();
const { MongoClient } = require("mongodb");
const ObjectId = require('mongodb').ObjectId;
const dbAppointmentsCollection = 'appointments';
const dbAppointmentSlotsCollection = 'appointment-slots';
const UserManagementClient = require("./user-management-client");
const { MeterProvider } = require('@opentelemetry/sdk-metrics-base');
const { DiagConsoleLogger, DiagLogLevel, diag } = require('@opentelemetry/api');
const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus');
// Optional and only needed to see the internal diagnostic logging (during development)
//diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);
const exporter = new PrometheusExporter(
{
host: process.env.HOST,
port: process.env.PROMETHEUS_PORT,
startServer: true,
},
() => {
console.log(
`Prometheus scrape endpoint: ${process.env.HOST}:${process.env.PROMETHEUS_PORT}${PrometheusExporter.DEFAULT_OPTIONS.endpoint}`,
);
},
);
const meter = new MeterProvider({
exporter,
interval: 5000,
}).getMeter('appointment-observer');
const apiTotalRequestsMetric = meter.createValueObserver('api_req_total', {
description: 'Sync value observer for the total API requests',
});
const apppointmentsAvailableMetric = meter.createValueObserver('appointments_available', {
description: 'Sync value observer for the number of available appointments',
});
const apiAppointmentsBookedMetric = meter.createValueObserver('api_appointments_booked', {
description: 'Sync value observer for the number of booked appointments',
});
const apiAppointmentsAttendedMetric = meter.createValueObserver('api_appointments_attended', {
description: 'Sync value observer for the number of attended appointments',
});
const apppointmentsPendingMetric = meter.createValueObserver('appointments_pending', {
description: 'Sync value observer for the number of pending appointments',
});
meter.createBatchObserver((observerBatchResult) => {
const appointments = db.collection(dbAppointmentsCollection);
Promise.all([
getAppointmentSlots(),
appointments.count()
])
.then(([availableSlots, appointmentCount]) => {
console.log('Batching metrics');
observerBatchResult.observe({ app: 'appointment' }, [
apiTotalRequestsMetric.observation(appointmentRequests),
apiAppointmentsBookedMetric.observation(appointmentsBooked),
apiAppointmentsAttendedMetric.observation(appointmentsAttended),
apppointmentsAvailableMetric.observation(availableSlots),
apppointmentsPendingMetric.observation(appointmentCount)
]);
});
}, {
maxTimeoutUpdateMS: 2500,
}
);
var appointmentRequests = 0;
var appointmentsBooked = 0;
var appointmentsAttended = 0;
// Appointment counter
let appointmentSlotsCollection = null;
const incAppointmentSlots = (inc) => {
return appointmentSlotsCollection.findOneAndUpdate(
{
_id: 1,
},
{
$inc: {
available: inc
}
},
{
upsert: true,
returnDocument: "after"
}
);
};
const decAppointmentSlots = () => {
return appointmentSlotsCollection.findOneAndUpdate(
{
_id: 1,
available: {
$gt: 0
}
},
{
$inc: {
available: -1
}
}
);
};
const getAppointmentSlots = () => {
return new Promise((resolve) => {
setTimeout(() => {
appointmentSlotsCollection.findOne({ _id: 1 })
.then(appointmentSlots => {
resolve(appointmentSlots.available);
});
}, 2000);
});
};
const userManagement = new UserManagementClient(process.env.USER_MANAGEMENT_URI);
app.use(useragent.express())
app.use(express.json())
app.get('/healthz', (req, res) => {
res.send('OK');
});
// Appointments API
app.post('/appointments/book', (req, res) => {
appointmentRequests++;
if (req.body.medicalId && req.body.userId) {
decAppointmentSlots()
.then(result => {
console.log(result);
if (!result.value) {
throw new Error("No appointments available");
}
console.log(req.body);
var date = new Date().setHours(0,0,0,0);
const newAppointment = { medicalId: req.body.medicalId, userId: req.body.userId, date: date};
const appointments = db.collection(dbAppointmentsCollection);
return appointments.findOneAndUpdate({ medicalId: req.body.medicalId }, { $set: newAppointment }, { upsert: true });
})
.then(result => {
console.log(result);
if (result.lastErrorObject.updatedExisting) {
// existing appointment
res.send({ appointmentId: result.value._id.toString(), newBooking: false });
} else {
// new appointment
appointmentsBooked++;
res.send({ appointmentId: result.lastErrorObject.upserted.toString(), newBooking: true });
}
})
.catch((error) => {
// handle error
console.log(error);
res.send({ error: error.message });
});
} else {
res.send("medicalId and userId are required");
}
});
app.post('/appointments/attend', (req, res) => {
appointmentRequests++;
const data = req.body;
console.log(data);
if (data.medicalId && data.perscription && validator.isHexadecimal(data.appointmentId) && data.appointmentId.length == 24) {
const appointments = db.collection(dbAppointmentsCollection);
// find appointment by id
var appointmentId = ObjectId(data.appointmentId);
appointments.findOne({ _id: appointmentId })
.then(appointment => {
if (!appointment) {
throw new Error("Appointment not found");
}
// find user by id and add medical data
userManagement.updateMedicalData(appointment.userId, data.medicalId, data.perscription);
})
.then(userId => {
// delete appointment
return appointments.deleteOne({ _id: appointmentId });
})
.then(result => {
appointmentsAttended++;
res.send({ appointmentId: data.appointmentId, updated: result.acknowledged });
})
.catch((error) => {
// handle error
console.log(error);
res.send({ appointmentId: data.appointmentId, updated: false, error: error.message });
});
} else {
res.status(400).send("appointmentId, medicalId and perscription are required");
}
});
app.post('/appointments/add', (req, res) => {
appointmentRequests++;
const data = req.body;
if (data.appointments && data.appointments !== NaN && data.appointments > 0) {
incAppointmentSlots(data.appointments)
.then(result => {
console.log(result);
res.send({ available: result.value.available, added: data.appointments});
})
.catch((error) => {
// handle error
console.log(error);
res.send({ error: error.message });
});
} else {
res.status(400).send("appointments is required");
}
});
// Mongo Connection
const dbClient = new MongoClient(process.env.DB_URI, { useUnifiedTopology: true });
let db = null;
async function connectDB() {
try {
await dbClient.connect();
db = await dbClient.db(process.env.DB_NAME);
db.command({ ping: 1 });
console.log("Connected successfully to mongo server");
// Create index
await db.collection(dbAppointmentsCollection).createIndex({ medicalId: 1 });
// Initialise appointment counter
appointmentSlotsCollection = db.collection(dbAppointmentSlotsCollection);
} catch (e) {
console.error(e);
}
}
async function main() {
await connectDB().catch(console.dir);
incAppointmentSlots(1);
app.listen(process.env.HTTP_PORT, process.env.HOST, () => {
console.log(`Server running at ${process.env.HOST}:${process.env.HTTP_PORT}`);
});
}
main();