Build matrix result

This commit is contained in:
2023-06-30 08:50:41 +09:00
parent a3853af59e
commit ef264faeaf
20 changed files with 380 additions and 138 deletions

View File

@@ -96,7 +96,7 @@ class KissMatrix {
let semaphore = DispatchSemaphore(value: 0)
Task {
let indexResult = try await withThrowingTaskGroup(of: KissIndexResult?.self, returning: [KissIndexResult].self) { taskGroup in
let results = try await withThrowingTaskGroup(of: KissIndexResult?.self, returning: [KissIndexResult].self) { taskGroup in
for indexSet in model.indexSets {
guard let (runUrl, args) = indexSet.build(date: runDate) else {
print("Cannot get command from \(indexSet.name)")
@@ -121,9 +121,7 @@ class KissMatrix {
return taskResult
}
//for result in indexResult {
// print(result)
//}
mergeResult(results)
semaphore.signal()
}
semaphore.wait()
@@ -181,6 +179,40 @@ class KissMatrix {
}
private func mergeResult(_ results: [KissIndexResult]) {
let indexCount = results.count
var mergedOutput = [String: [KissIndexResult.Output]]()
for result in results {
for item in result.output {
if let _ = mergedOutput[item.shortCode] {
mergedOutput[item.shortCode]!.append(item)
}
else {
mergedOutput[item.shortCode] = [item]
}
}
}
var normalized = [KissIndexResult.Output]()
for (productNo, output) in mergedOutput {
let weight = output.reduce(0.0, { $0 + $1.weight }) / Double(indexCount)
let output = KissIndexResult.Output(shortCode: productNo, productName: output.first?.productName, weight: weight)
normalized.append(output)
}
normalized.sort(by: { $0.weight > $1.weight })
let kmis = results.map { $0.kmi }
let matrixResult = KissMatrixResult(code: 200, kmis: kmis, output: normalized)
do {
let jsonData = try JSONEncoder().encode(matrixResult)
try FileHandle.standardOutput.write(contentsOf: jsonData)
} catch {
printError(error)
}
}
private func loadModel(_ jsonFile: String) -> Model? {
do {
let configUrl = URL.currentDirectory().appending(path: jsonFile)