Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add version number and modification date to JSON & Add single file containing the whole dataset #78

Merged
merged 4 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ struct MetadataObjectModel {
var kurzgeschichten: [Hörspiel]?
var die_dr3i: [Hörspiel]?
var kids: [Hörspiel]?

var dbInfo: DBInfo?
}


Expand Down Expand Up @@ -244,6 +246,12 @@ struct Medium {
var ripLog: String?
var musicBrainzID: String?
}


struct DBInfo {
var version: String
var lastModified: String
}
```

</details>
Expand Down
2 changes: 2 additions & 0 deletions code/dreimetadaten/CollectionType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ enum CollectionType: String, ArgumentEnum {
case die_dr3i
case kids

static let allCasesJSONFile = "Alle.json"

var name: String {
switch self {
case .serie: return "Serie"
Expand Down
8 changes: 6 additions & 2 deletions code/dreimetadaten/Export/JSONExporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ struct JSONExporter {
let webDataURL: URL

func export(to outputDir: URL) throws {
let objectModels = try MetadataObjectModel(fromDatabase: db, withBaseURL: webDataURL).separateByCollectionType()
let objectModel = try MetadataObjectModel(fromDatabase: db, withBaseURL: webDataURL)

for (objectModel, collectionType) in objectModels {
for (objectModel, collectionType) in objectModel.separateByCollectionType(withDBInfo: true) {
let jsonURL = outputDir.appendingPathComponent(collectionType.jsonFile)
let jsonString = try objectModel.jsonString()
try jsonString.write(to: jsonURL, atomically: true, encoding: .utf8)
}

let jsonURL = outputDir.appendingPathComponent(CollectionType.allCasesJSONFile)
let jsonString = try objectModel.jsonString()
try jsonString.write(to: jsonURL, atomically: true, encoding: .utf8)
}
}
8 changes: 7 additions & 1 deletion code/dreimetadaten/Export/WebDataExporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct WebDataExporter {
func export(to outputDir: URL) throws {
try Self.createDirectoryIfNeccessary(at: outputDir)

let objectModels = objectModel.separateByCollectionType()
let objectModels = objectModel.separateByCollectionType(withDBInfo: true)
var referencedFilePaths = Set<String>()

for (objectModel, collectionType) in objectModels {
Expand Down Expand Up @@ -62,6 +62,12 @@ struct WebDataExporter {
}
}

// Export metadata of whole object model as JSON
let jsonURL = outputDir.appendingPathComponent(CollectionType.allCasesJSONFile)
referencedFilePaths.insert(jsonURL.relativePath)
let jsonString = try objectModel.jsonString()
try jsonString.write(to: jsonURL, atomically: true, encoding: .utf8)

// Check referenced files and existing files
var existingFilePaths = Set<String>()
guard let enumerator = FileManager.default.enumerator(at: outputDir, includingPropertiesForKeys: [.isRegularFileKey], options: [.skipsHiddenFiles, .producesRelativePathURLs]) else {
Expand Down
30 changes: 29 additions & 1 deletion code/dreimetadaten/MetadataModel/MetadataObjectModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ struct MetadataObjectModel: Codable {
var kurzgeschichten: [Hörspiel]?
var die_dr3i: [Hörspiel]?
var kids: [Hörspiel]?

var dbInfo: DBInfo?
}


Expand Down Expand Up @@ -132,6 +134,12 @@ extension MetadataObjectModel {
var musicBrainzID: String?
}


struct DBInfo: Codable {
var version: String
var lastModified: String
}

}


Expand All @@ -156,6 +164,10 @@ extension MetadataObjectModel {
}

static let ordering: [String] = [
"dbInfo",
"version",
"lastModified",

"serie",
"spezial",
"kurzgeschichten",
Expand Down Expand Up @@ -297,7 +309,7 @@ extension MetadataObjectModel {

// MARK: - Collection Type Masking

func separateByCollectionType() -> [(objectModel: Self, collectionType: CollectionType)] {
func separateByCollectionType(withDBInfo: Bool = false) -> [(objectModel: Self, collectionType: CollectionType)] {
CollectionType.allCases.map { collectionType in
var maskedObjectModel = Self()
switch collectionType.objectModelKeyPath {
Expand All @@ -308,6 +320,9 @@ extension MetadataObjectModel {
default:
fatalError("Unrecognized type for CollectionType.objectModelKeyPath")
}
if withDBInfo {
maskedObjectModel.dbInfo = dbInfo
}
return (maskedObjectModel, collectionType)
}
}
Expand Down Expand Up @@ -635,6 +650,19 @@ extension MetadataObjectModel {
try addURL(to: hörspiel, as: .kids)
return hörspiel
}


// version
dbInfo = try MetadataRelationalModel.Version
.order(Column("date").desc)
.fetchOne(db)
.map {
let versionNumber = "\($0.major).\($0.minor).\($0.patch)"
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withInternetDateTime]
let timestamp = formatter.string(from: $0.date)
return DBInfo(version: versionNumber, lastModified: timestamp)
}
}


Expand Down
10 changes: 10 additions & 0 deletions code/dreimetadaten/MetadataModel/MetadataRelationalModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ extension MetadataRelationalModel {
var personID: Person.ID
}


struct Version: Codable {
var major: UInt
var minor: UInt
var patch: UInt

var date: Date
}

}


Expand Down Expand Up @@ -249,6 +258,7 @@ extension MetadataRelationalModel.SprechrolleTeil: PersistableFetchableTableReco
extension MetadataRelationalModel.Spricht: PersistableFetchableTableRecord { }
extension MetadataRelationalModel.HörspielBuchautor: PersistableFetchableTableRecord { }
extension MetadataRelationalModel.HörspielSkriptautor: PersistableFetchableTableRecord { }
extension MetadataRelationalModel.Version: PersistableFetchableTableRecord { }


// MARK: - Database Reading
Expand Down
17 changes: 17 additions & 0 deletions metadata/db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ CREATE TABLE "hörspielSkriptautor"(
"personID" INTEGER NOT NULL REFERENCES "person"("personID") ON DELETE CASCADE ON UPDATE CASCADE,
PRIMARY KEY("hörspielID", "personID")
);
CREATE TABLE "version"(
"major" INTEGER NOT NULL CHECK("major" >= 0),
"minor" INTEGER NOT NULL CHECK("minor" >= 0),
"patch" INTEGER NOT NULL CHECK("patch" >= 0),
"date" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY("major", "minor", "patch")
);
CREATE VIEW "alleIDs"("sammlung", "nummer", "hörspielID") AS
SELECT 'serie', nummer, hörspielID FROM serie UNION
SELECT 'spezial', NULL, hörspielID FROM spezial UNION
Expand Down Expand Up @@ -23358,4 +23365,14 @@ INSERT INTO "hörspielSkriptautor" VALUES(405,931);
INSERT INTO "hörspielSkriptautor" VALUES(69,235);
INSERT INTO "hörspielSkriptautor" VALUES(70,235);
INSERT INTO "hörspielSkriptautor" VALUES(74,235);
INSERT INTO version VALUES(2,0,0,'2024-08-05 02:17:04');
INSERT INTO version VALUES(2,0,1,'2024-08-09 21:47:13');
INSERT INTO version VALUES(2,0,2,'2024-08-10 10:34:45');
INSERT INTO version VALUES(2,0,3,'2024-09-09 13:13:00');
INSERT INTO version VALUES(2,0,4,'2024-09-10 00:38:38');
INSERT INTO version VALUES(2,1,0,'2024-09-29 01:36:32');
INSERT INTO version VALUES(2,2,0,'2024-10-07 23:31:09');
INSERT INTO version VALUES(2,2,1,'2024-10-08 12:28:14');
INSERT INTO version VALUES(2,2,2,'2024-10-10 02:30:50');
INSERT INTO version VALUES(2,3,0,'2024-10-11 13:04:56');
COMMIT;
Loading