-
Notifications
You must be signed in to change notification settings - Fork 87
/
DevFestNantes.kt
415 lines (373 loc) · 15.5 KB
/
DevFestNantes.kt
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package dev.johnoreilly.confetti.backend.import
import com.charleskorn.kaml.*
import dev.johnoreilly.confetti.backend.datastore.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.datetime.*
import kotlinx.serialization.json.Json
import net.mbonnin.bare.graphql.asList
import net.mbonnin.bare.graphql.asMap
import net.mbonnin.bare.graphql.asString
import net.mbonnin.bare.graphql.toAny
import okhttp3.executeAsync
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.OkHttpClient
import okhttp3.Request
import kotlin.time.Duration
import kotlin.time.Duration.Companion.minutes
private val timeZone = "Europe/Paris"
private val okHttpClient = OkHttpClient.Builder()
.build()
private val baseUrl = "https://raw.githubusercontent.com/GDG-Nantes/Devfest2022/master/"
private val json = Json {
ignoreUnknownKeys = true
}
object DevFestNantes {
private suspend fun getUrl(url: String): String {
val request = Request(url.toHttpUrl())
val response = okHttpClient.newCall(request).executeAsync()
return response.use {
check(response.isSuccessful) {
"Cannot get $url: ${response.body.string()}"
}
withContext(Dispatchers.IO) {
response.body.string()
}
}
}
private suspend fun getGithubFile(name: String): String {
return getUrl("$baseUrl$name")
}
private suspend fun getJsonUrl(url: String) = Json.parseToJsonElement(getUrl(url)).toAny()
private suspend fun getJsonGithubFile(name: String) =
Json.parseToJsonElement(getGithubFile(name)).toAny()
private suspend fun getFiles(ref: String): List<Map<String, Any?>> {
return getJsonUrl("https://api.github.com/repos/GDG-Nantes/Devfest2022/git/trees/$ref")
.asMap
.get("tree")
.asList
.map { it.asMap }
}
private val sessionIdsWithoutRoom = mutableSetOf<String>()
private suspend fun listFiles(path: String): List<String> {
var files: List<Map<String, Any?>> = getFiles("master")
path.split("/").forEach { comp ->
val sha = files.first { it.get("path") == comp }.get("sha").asString
files = getFiles(sha)
}
return files.map { it.get("path").asString }
.map { "$path/$it" }
}
private suspend fun listYamls(path: String): List<String> =
listFiles(path).filter { it.endsWith(".yml") }
private fun Map<String, Any?>.startTime(): LocalDateTime {
val day = if (get("key").asString.startsWith("day-1")) {
"2022-10-20"
} else {
"2022-10-21"
}
return LocalDateTime.parse("${day}T${get("start")}")
}
private val UNKNOWN_END = LocalDateTime(0, 1, 1, 0, 0)
internal suspend fun import() {
val slots = getJsonGithubFile("data/slots.json").asMap.get("slots").asList.map { it.asMap }
val categories =
getJsonGithubFile("data/categories.json").asMap.get("categories").asList.map { it.asMap }
val partners = getJsonGithubFile("data/partners.json")
val roomIds = mutableSetOf<String>()
var sessions = listYamls("data/sessions").mapNotNull { session ->
val talk = Yaml.default.parseToYamlNode(getGithubFile(session)).toAny().asMap
if (talk.get("cancelled") == "true") {
return@mapNotNull null
}
val slot = slots.first { it.get("key") == talk.get("slot") }
val localDateTime = slot.startTime()
val roomId = talk.get("room").asString
roomIds.add(roomId)
/**
* construct a valid ID for DataStore
*/
val id = session.substringAfterLast("/")
.replace("_", "")
.substringBeforeLast(".")
.take(64)
DSession(
id = id,
title = talk.get("title").asString,
description = talk.get("abstract").asString,
language = when (talk.get("language").asString.lowercase()) {
"french" -> "fr-FR"
else -> "en-US"
},
speakers = talk.get("speakers").asList.map { it.asString },
tags = talk.get("tags").asList.mapNotNull { tagId ->
categories.firstOrNull { it.get("id") == tagId.asString }
?.get("label")?.asString
},
start = localDateTime,
// The YAMLs do not have an end time
end = UNKNOWN_END,
complexity = talk.get("complexity")?.asString,
feedbackId = talk.get("openfeedbackId")?.asString,
rooms = listOf(roomId),
type = talk.get("talkType")?.asString ?: "talk",
shortDescription = null
)
} + slotSessions(slots)
val sortedSessions = sessions.sortedBy { it.start }
sessions = sessions.map { session ->
if (session.end == UNKNOWN_END) {
val end = sortedSessions.firstOrNull { candidate ->
candidate.start > session.start
&& candidate.start.dayOfMonth == session.start.dayOfMonth
&& candidate.rooms.intersect(session.rooms.toSet()).isNotEmpty()
}?.start ?: (session.start + 40.minutes)
session.copy(end = end)
} else {
session
}
}
sessions = sessions.map {
/**
* Remove the room for lunch & break sessions
* See https://github.com/GDG-Nantes/DevfestNantesMobile/issues/85
*/
if (sessionIdsWithoutRoom.contains(it.id)) {
it.copy(rooms = emptyList())
} else {
it
}
}
val allImages = listFiles("src/images/partners").map { it.substringAfterLast("/") }
val partnerGroups = partners.asMap.entries.map {
DPartnerGroup(
key = it.key,
partners = it.value.asList.map { it.asMap }.map {
val title = it.get("title").asString
val id = it.get("id").asString
val image = allImages.firstOrNull { it.substringBeforeLast(".") == id }
DPartner(
name = title,
logoUrl = (image?.let { "$baseUrl/src/images/partners/$it" })
?: error("No partner image for $id"),
url = it.get("website")?.asString ?: ""
)
}
)
}
val speakers = listYamls("data/speakers").map {
val speaker = Yaml.default.parseToYamlNode(getGithubFile(it)).toAny().asMap
DSpeaker(
id = speaker.get("key").asString,
name = speaker.get("name").asString,
bio = speaker.get("bio")?.asString,
company = speaker.get("company")?.asString,
links = speaker.get("socials").asMap.entries.map {
val handle = it.value.asString.trim()
val url = when (it.key) {
"github" -> "https://github.com/$handle"
"linkedin" -> "https://www.linkedin.com/$handle"
"twitter" -> "https://twitter.com/" + handle.trimStart('@')
"facebook" -> "https://www.facebook.com/$handle"
else -> ""
}
DLink(key = it.key, url = url)
},
photoUrl = speaker.get("photoUrl")?.asString,
companyLogoUrl = speaker.get("companyLogo")?.asString?.let { "${baseUrl}src$it" },
city = speaker.get("city")?.asString,
)
}
val rooms = roomIds.map {
DRoom(
id = it,
name = it,
)
}
val config = DConfig(
id = ConferenceId.DevFestNantes2022.id,
name = "DevFest Nantes",
timeZone = timeZone
)
return DataStore().write(
sessions = sessions.sortedBy { it.start },
rooms = rooms,
speakers = speakers,
partnerGroups = partnerGroups,
config = config,
venues = listOf(
DVenue(
id = "main",
name = "Cité des Congrès de Nantes",
address = "5 rue de Valmy, 44000 Nantes",
description = mapOf(
"en" to "Located in the center of Nantes, the event takes place in the \"Cité des Congrès\" with more than 3000m² of conference rooms, hand's on and networking space…",
"fr" to "Située en plein cœur de ville, La Cité des Congrès de Nantes propose pour le DevFest Nantes plus de 3000m² de salles de conférences, codelabs et lieu de rencontre…",
),
latitude = 47.21308725112951,
longitude = -1.542622837466317,
imageUrl = "https://devfest.gdgnantes.com/static/6328df241501c6e31393e568e5c68d7e/efc43/amphi.webp",
floorPlanUrl = "https://raw.githubusercontent.com/GDG-Nantes/Devfest2022/master/src/images/plan-cite-blanc.png"
)
)
)
}
private fun slotSessions(slots: List<Map<String, Any?>>): List<DSession> {
/**
* Some of the logic that determines Rooms is in code so I just hardcode the rules here
* See https://github.com/GDG-Nantes/Devfest2022/blob/d82c0b815d6e67b61db7cf9faf61c2069d875abc/src/components/schedule/large.tsx#L60
*/
return slots.mapNotNull {
val id = it.get("key").asString
val type = it.get("type").asString
when {
id == "day-2-pause-2" -> {
sessionIdsWithoutRoom.add(id)
DSession(
id = id,
title = "Break",
description = "Break",
language = "fr-FR",
speakers = emptyList(),
tags = emptyList(),
start = it.startTime(),
end = UNKNOWN_END,
rooms = FIRST_6_ROOMS,
type = "break",
complexity = null,
feedbackId = null,
shortDescription = null,
)
}
id == "day-1-party" -> {
sessionIdsWithoutRoom.add(id)
DSession(
id = id,
title = "Party",
description = "Party",
language = "fr-FR",
speakers = emptyList(),
tags = emptyList(),
start = it.startTime(),
end = UNKNOWN_END,
rooms = ALL_ROOMS,
type = "party",
complexity = null,
feedbackId = null,
shortDescription = null,
)
}
type == "opening" -> {
sessionIdsWithoutRoom.add(id)
DSession(
id = id,
title = "Opening",
description = "Opening",
language = "fr-FR",
speakers = emptyList(),
tags = emptyList(),
start = it.startTime(),
end = UNKNOWN_END,
rooms = ALL_ROOMS,
type = "opening",
complexity = null,
feedbackId = null,
shortDescription = null,
)
}
type == "keynote" -> {
DSession(
id = id,
title = "Keynote",
description = "Keynote",
language = "fr-FR",
speakers = emptyList(),
tags = emptyList(),
start = it.startTime(),
end = UNKNOWN_END,
rooms = listOf(ROOM_JULES_VERNE),
type = "keynote",
complexity = null,
feedbackId = null,
shortDescription = null,
)
}
type == "break" -> {
sessionIdsWithoutRoom.add(id)
val notForCodelab =
it.get("display")?.asMap?.containsKey("notForCodelab") ?: false
DSession(
id = id,
title = "Break",
description = "Break",
language = "fr-FR",
speakers = emptyList(),
tags = emptyList(),
start = it.startTime(),
end = UNKNOWN_END,
rooms = if (notForCodelab) FIRST_4_ROOMS else ALL_ROOMS,
type = "break",
complexity = null,
feedbackId = null,
shortDescription = null,
)
}
type == "lunch" -> {
sessionIdsWithoutRoom.add(id)
DSession(
id = id,
title = "Lunch",
description = "Lunch",
language = "fr-FR",
speakers = emptyList(),
tags = emptyList(),
start = it.startTime(),
end = UNKNOWN_END,
rooms = ALL_ROOMS,
type = "lunch",
complexity = null,
feedbackId = null,
shortDescription = null,
)
}
else -> null
}
}
}
private const val ROOM_JULES_VERNE = "Jules Verne"
private const val ROOM_TITAN = "Titan"
private const val ROOM_BELEM = "Belem"
private const val ROOM_TOUR_DE_BRETAGNE = "Tour de Bretagne"
private const val ROOM_LES_MACHINES = "Les Machines"
private const val ROOM_HANGAR = "Hangar"
private const val ROOM_L_ATELIER = "L'Atelier"
private val FIRST_4_ROOMS = listOf(
ROOM_JULES_VERNE,
ROOM_TITAN,
ROOM_BELEM,
ROOM_TOUR_DE_BRETAGNE
)
private val FIRST_6_ROOMS = FIRST_4_ROOMS + listOf(
ROOM_LES_MACHINES,
ROOM_HANGAR,
)
private val ALL_ROOMS = FIRST_6_ROOMS + listOf(
ROOM_L_ATELIER
)
private operator fun LocalDateTime.plus(duration: Duration): LocalDateTime {
return toInstant(TimeZone.UTC).plus(duration).toLocalDateTime(TimeZone.UTC)
}
private fun YamlNode.toAny(): Any? {
return when (this) {
is YamlNull -> null
is YamlScalar -> this.content
is YamlList -> this.items.map {
it.toAny()
}
is YamlMap -> this.entries.map {
it.key.content to it.value.toAny()
}.toMap()
is YamlTaggedNode -> TODO()
}
}
}