Skip to content

Commit 8170a02

Browse files
committed
Add support for cloud status
1 parent 228c0f7 commit 8170a02

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

src/apphandler.js

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,144 @@ export class AppHandler {
10351035
return res.status(500).send('error uploading ipynb ' + error)
10361036
}
10371037
})
1038+
app.get(path + '/cloudstatus', async (req, res) => {
1039+
if (!req.token.role.includes('administrator'))
1040+
return res.status(401).send('unauthorized')
1041+
const lectureuuid = req.token.course.lectureuuid
1042+
1043+
let lectcursor = 0
1044+
try {
1045+
// ok, what do we need:
1046+
// First status of all routers their capacity and their usage
1047+
const getRouterDetails = async () => {
1048+
const routercol = this.mongo.collection('avsrouters')
1049+
1050+
const cursor = routercol.find(
1051+
{},
1052+
{
1053+
projection: {
1054+
_id: 0,
1055+
url: 1,
1056+
region: 1,
1057+
numClients: 1,
1058+
localClients: 1,
1059+
remoteClients: 1,
1060+
primaryRealms: 1 // Realm is lecture id
1061+
}
1062+
}
1063+
)
1064+
1065+
const routers = []
1066+
while (await cursor.hasNext()) {
1067+
const {
1068+
region,
1069+
localClients,
1070+
remoteClients,
1071+
numClients,
1072+
primaryRealms,
1073+
url
1074+
} = await cursor.next()
1075+
const isPrimary = (primaryRealms || []).includes(lectureuuid)
1076+
1077+
routers.push({
1078+
url,
1079+
isPrimary,
1080+
region,
1081+
numClients: numClients ?? 0,
1082+
numLocalClients: localClients?.length ?? 0,
1083+
numRemoteClients: remoteClients?.length ?? 0,
1084+
primaryLectureNum: primaryRealms?.length ?? 0
1085+
})
1086+
}
1087+
1088+
return routers
1089+
}
1090+
1091+
// second, status of all concurrently running lectures
1092+
const getLectDetails = async () => {
1093+
const lecturescol = this.mongo.collection('lectures')
1094+
const lectureDetails = []
1095+
do {
1096+
const scanret = await this.redis.scan(lectcursor, {
1097+
MATCH: 'lecture:????????-????-????-????-????????????:notescreens',
1098+
COUNT: 40
1099+
})
1100+
const lectuuids = scanret.keys.map((el) => el.slice(8, 44))
1101+
const nowborder1 = Date.now() - 20 * 60 * 1000
1102+
const nowborder2 = Date.now() - 5 * 60 * 1000 - 10 * 1000
1103+
// perfect, we can now get the number of clients for each of them
1104+
const addLectureDetails = await Promise.all(
1105+
lectuuids.map(async (uuid) => {
1106+
const pnumberOfNotescreens = this.redis
1107+
.sMembers('lecture:' + uuid + ':notescreens')
1108+
.then((screens) => {
1109+
return Promise.all(
1110+
screens.map((screen) => {
1111+
return this.redis.hmGet(
1112+
'lecture:' + uuid + ':notescreen:' + screen,
1113+
['active', 'lastaccess']
1114+
)
1115+
})
1116+
)
1117+
})
1118+
.then((screens) => {
1119+
const scr = screens.filter((el) =>
1120+
el
1121+
? nowborder1 - Number(el[1]) < 0 && el[0] !== '0'
1122+
: false
1123+
)
1124+
return scr.length
1125+
})
1126+
const pnumberOfIdents = this.redis
1127+
.hGetAll('lecture:' + uuid + ':idents')
1128+
.then((identobj) => {
1129+
return Object.values(identobj)
1130+
.map((value) => JSON.parse(value))
1131+
.filter((el) => nowborder2 - Number(el.lastaccess) < 0)
1132+
.length
1133+
})
1134+
1135+
const plecturedoc = lecturescol.findOne(
1136+
{ uuid: uuid },
1137+
{
1138+
projection: {
1139+
_id: 0,
1140+
title: 1,
1141+
coursetitle: 1
1142+
}
1143+
}
1144+
)
1145+
const [numberOfNotescreens, numberOfIdents, lecturedoc] =
1146+
await Promise.all([
1147+
pnumberOfNotescreens,
1148+
pnumberOfIdents,
1149+
plecturedoc
1150+
])
1151+
// Title and course name would also be great
1152+
return {
1153+
uuid,
1154+
numberOfNotescreens,
1155+
numberOfIdents,
1156+
title: lecturedoc.title,
1157+
coursetitle: lecturedoc.coursetitle
1158+
}
1159+
})
1160+
)
1161+
lectureDetails.push(...addLectureDetails)
1162+
lectcursor = scanret.cursor
1163+
} while (lectcursor !== 0)
1164+
return lectureDetails
1165+
}
1166+
const [routerDetails, lectureDetails] = await Promise.all([
1167+
getRouterDetails(),
1168+
getLectDetails()
1169+
])
1170+
return res.status(200).json({ routerDetails, lectureDetails })
1171+
} catch (error) {
1172+
console.log('Problem getting statistics', error)
1173+
return res.status(500).send('Problem getting statistics: ' + error)
1174+
}
1175+
})
10381176
}
10391177

10401178
async getLectureDetails(uuid) {

0 commit comments

Comments
 (0)