Skip to content

Commit

Permalink
feat: add EventStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
nickybondarenko committed Apr 5, 2024
1 parent 2e49e23 commit 483bda9
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions Sources/Confidence/EventStorage.swift
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

View workflow job for this annotation

GitHub Actions / SwiftLint

Force Unwrapping Violation: Force unwrapping should be avoided (force_unwrapping)

Check warning on line 15 in Sources/Confidence/EventStorage.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
var fileURL: URL = URL(string: "")!

Check warning on line 16 in Sources/Confidence/EventStorage.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Force Unwrapping Violation: Force unwrapping should be avoided (force_unwrapping)

Check warning on line 16 in Sources/Confidence/EventStorage.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
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

View workflow job for this annotation

GitHub Actions / SwiftLint

Force Unwrapping Violation: Force unwrapping should be avoided (force_unwrapping)

Check warning on line 25 in Sources/Confidence/EventStorage.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Operator Usage Whitespace Violation: Operators should be surrounded by a single whitespace when they are being used (operator_usage_whitespace)
try FileManager.default.moveItem(at: fileURL, to: newPath)
fileURL = folderURL.appendingPathComponent("events-\(Date().currentTime)")
currentBatch = []
}

Check warning on line 30 in Sources/Confidence/EventStorage.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
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) {

Check warning on line 41 in Sources/Confidence/EventStorage.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Prefer For-Where Violation: `where` clauses are preferred over a single `if` inside a `for` (for_where)
readyFilesList.append(URL(string: file)!)

Check warning on line 42 in Sources/Confidence/EventStorage.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Force Unwrapping Violation: Force unwrapping should be avoided (force_unwrapping)
}
}
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)
}
}

Check warning on line 87 in Sources/Confidence/EventStorage.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Newline Violation: Files should have a single trailing newline (trailing_newline)

0 comments on commit 483bda9

Please sign in to comment.