-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e49e23
commit 483bda9
Showing
1 changed file
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import Foundation | ||
|
||
internal protocol EventStorage { | ||
func startNewBatch() throws | ||
func writeEvent(event: Event) throws | ||
func batchReadyFiles() throws -> [URL] | ||
func eventsFrom(fileURL: URL) throws -> [Event] | ||
} | ||
|
||
internal class EventStorageImpl: EventStorage { | ||
let DIRECTORY = "events" | ||
let READYTOSENDEXTENSION = ".ready" | ||
let encoder = JSONEncoder() | ||
let decoder = JSONDecoder() | ||
var folderURL: URL = URL(string: "")! | ||
Check warning on line 15 in Sources/Confidence/EventStorage.swift GitHub Actions / SwiftLint
|
||
var fileURL: URL = URL(string: "")! | ||
Check warning on line 16 in Sources/Confidence/EventStorage.swift GitHub Actions / SwiftLint
|
||
var currentBatch: [Event] = [] | ||
|
||
init() throws { | ||
folderURL = try getFolderURL() | ||
fileURL = folderURL.appendingPathComponent("events-\(Date().currentTime)") | ||
} | ||
|
||
func startNewBatch() throws { | ||
let newPath = URL(string: "\(fileURL)"+"\(READYTOSENDEXTENSION)")! | ||
Check warning on line 25 in Sources/Confidence/EventStorage.swift GitHub Actions / SwiftLint
|
||
try FileManager.default.moveItem(at: fileURL, to: newPath) | ||
fileURL = folderURL.appendingPathComponent("events-\(Date().currentTime)") | ||
currentBatch = [] | ||
} | ||
|
||
func writeEvent(event: Event) throws { | ||
currentBatch.append(event) | ||
let data = try encoder.encode(currentBatch) | ||
try data.write(to: fileURL, options: .atomic) | ||
} | ||
|
||
func batchReadyFiles() throws -> [URL] { | ||
var readyFilesList: [URL] = [] | ||
let directoryContents = try FileManager.default.contentsOfDirectory(atPath: folderURL.absoluteString) | ||
for file in directoryContents { | ||
if file.hasSuffix(READYTOSENDEXTENSION) { | ||
readyFilesList.append(URL(string: file)!) | ||
} | ||
} | ||
return readyFilesList | ||
} | ||
|
||
func eventsFrom(fileURL: URL) throws -> [Event] { | ||
let data = try Data(contentsOf: fileURL) | ||
let events = try decoder.decode([Event].self, from: data) | ||
return events | ||
} | ||
|
||
private func getFolderURL() throws -> URL { | ||
let rootFolderURL = try FileManager.default.url( | ||
for: .cachesDirectory, | ||
in: .userDomainMask, | ||
appropriateFor: nil, | ||
create: true | ||
) | ||
var nestedFolderURL: URL | ||
if #available(iOS 16.0, *) { | ||
nestedFolderURL = rootFolderURL.appending(path: DIRECTORY) | ||
} else { | ||
nestedFolderURL = rootFolderURL.appendingPathComponent(DIRECTORY, isDirectory: true) | ||
} | ||
return nestedFolderURL | ||
} | ||
} | ||
|
||
struct Event: Codable { | ||
let eventDefinition: String | ||
let eventTime: Date | ||
// TODO: fix this to be ConfidenceValue | ||
let payload: [String] | ||
let context: [String] | ||
} | ||
|
||
|
||
extension Date { | ||
var currentTime: String { | ||
let dateFormatter = DateFormatter() | ||
dateFormatter.dateStyle = .short | ||
return dateFormatter.string(from: self) | ||
} | ||
} | ||
|
||