Provide HTML cap count

This commit is contained in:
Christoph Hagen 2022-12-16 13:32:33 +01:00
parent 8ec0c303ec
commit 3c5999a892

View File

@ -9,6 +9,9 @@ final class CapServer {
/// The file where the database of caps is stored /// The file where the database of caps is stored
private let dbFile: URL private let dbFile: URL
/// The file to store the HTML info of the cap count
private let htmlFile: URL
private let classifierVersionFile: URL private let classifierVersionFile: URL
@ -70,6 +73,7 @@ final class CapServer {
init(in folder: URL, writers: [String]) throws { init(in folder: URL, writers: [String]) throws {
self.imageFolder = folder.appendingPathComponent("images") self.imageFolder = folder.appendingPathComponent("images")
self.dbFile = folder.appendingPathComponent("caps.json") self.dbFile = folder.appendingPathComponent("caps.json")
self.htmlFile = folder.appendingPathComponent("count.html")
self.classifierVersionFile = folder.appendingPathComponent("classifier.version") self.classifierVersionFile = folder.appendingPathComponent("classifier.version")
self.classifierFile = folder.appendingPathComponent("classifier.mlmodel") self.classifierFile = folder.appendingPathComponent("classifier.mlmodel")
self.writers = Set(writers) self.writers = Set(writers)
@ -83,6 +87,7 @@ final class CapServer {
try loadCaps() try loadCaps()
try updateCounts() try updateCounts()
saveCapCountHTML()
} }
private func loadCaps() throws { private func loadCaps() throws {
@ -127,6 +132,19 @@ final class CapServer {
try data.write(to: dbFile) try data.write(to: dbFile)
} }
private func saveCapCountHTML() {
let count = caps.count
let content =
"""
<body style="margin: 0;">
<div style="display: flex; justify-content: center;">
<div style="font-size: 60px; font-family: 'SF Pro Display',-apple-system,BlinkMacSystemFont,Helvetica,sans-serif; -webkit-font-smoothing: antialiased;">\(count)</div>
</div>
</body>
"""
try? content.data(using: .utf8)!.write(to: htmlFile)
}
// MARK: Paths // MARK: Paths
func folder(of cap: Int) -> URL { func folder(of cap: Int) -> URL {
@ -235,6 +253,7 @@ final class CapServer {
cap.count = 0 cap.count = 0
cap.classifierVersion = nextClassifierVersion cap.classifierVersion = nextClassifierVersion
caps[cap.id] = cap caps[cap.id] = cap
saveCapCountHTML()
log("Added cap \(cap.id) '\(cap.name)'") log("Added cap \(cap.id) '\(cap.name)'")
} }