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 c8057f7
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions Sources/Confidence/EventStorage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import Foundation

internal protocol EventStorage {
func rollover() throws
func writeEvent(event: Event) throws
func batchReadyFiles() throws -> [URL]
func eventsFrom(fileURL: URL) throws -> [Event]
}

internal class EventStorageImpl: EventStorage {
let DIRECTORY = "events"
let EVENT_WRITE_DELIMITER = ",\n"

Check failure on line 12 in Sources/Confidence/EventStorage.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Identifier Name Violation: Variable name 'EVENT_WRITE_DELIMITER' should only contain alphanumeric and other allowed characters (identifier_name)
let READY_TO_SEND_EXTENSION = ".ready"

Check failure on line 13 in Sources/Confidence/EventStorage.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Identifier Name Violation: Variable name 'READY_TO_SEND_EXTENSION' should only contain alphanumeric and other allowed characters (identifier_name)
let encoder = JSONEncoder()
let decoder = JSONDecoder()
var folderURL: 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 fileURL: URL = URL(string: "")!

Check warning on line 17 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 17 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()
try createEventsDirectory(url: folderURL)
fileURL = folderURL.appendingPathComponent("events-\(Date().currentTime)")
}

func rollover() throws {
let newPath = URL(string: "\(fileURL)" + "\(Date().currentTime)")!

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

View workflow job for this annotation

GitHub Actions / SwiftLint

Force Unwrapping Violation: Force unwrapping should be avoided (force_unwrapping)
try FileManager.default.moveItem(at: fileURL, to: newPath)
try FileManager.default.removeItem(at: fileURL)
fileURL = folderURL.appendingPathComponent("events-\(Date().currentTime)")
currentBatch = []
}

Check warning on line 33 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: .withoutOverwriting)
}

func batchReadyFiles() throws -> [URL] {
var readyFilesList: [URL] = []
let directoryContents = try FileManager.default.contentsOfDirectory(atPath: folderURL.absoluteString)
for file in directoryContents {
if file.hasSuffix(READY_TO_SEND_EXTENSION) {

Check warning on line 44 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 45 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: false
)
var nestedFolderURL: URL
if #available(iOS 16.0, *) {
nestedFolderURL = rootFolderURL.appending(path: DIRECTORY)
} else {
nestedFolderURL = rootFolderURL.appendingPathComponent(DIRECTORY, isDirectory: true)
}
return nestedFolderURL
}

private func createEventsDirectory(url: URL) throws {
do {
try FileManager.default.createDirectory(
at: url,
withIntermediateDirectories: false
)
} catch CocoaError.fileWriteFileExists {
//Folder already exists

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

View workflow job for this annotation

GitHub Actions / SwiftLint

Comment Spacing Violation: Prefer at least one space after slashes for comments (comment_spacing)
} catch {
throw error
}
}

}

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 104 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 c8057f7

Please sign in to comment.