<commit_before>//
//  ReadScheduleJSON.swift
//  RsyncUI
//
//  Created by <NAME> on 19/04/2021.
//

import Combine
import Files
import Foundation

class ReadScheduleJSON: NamesandPaths {
    var schedules: [ConfigurationSchedule]?
    var filenamedatastore = [SharedReference.shared.fileschedulesjson]
    var subscriptons = Set<AnyCancellable>()

    init(_ profile: String?, _ validhiddenID: Set<Int>?) {
        super.init(.configurations)
        print("ReadScheduleJSON")
        filenamedatastore.publisher
            .compactMap { filenamejson -> URL in
                var filename: String = ""
                if let profile = profile, let path = fullpathmacserial {
                    filename = path + "/" + profile + "/" + filenamejson
                } else {
                    if let path = fullpathmacserial {
                        filename = path + "/" + filenamejson
                    }
                }
                return URL(fileURLWithPath: filename)
            }
            .tryMap { url -> Data in
                try Data(contentsOf: url)
            }
            .decode(type: [DecodeSchedule].self, decoder: JSONDecoder())
            .sink { completion in
                switch completion {
                case .finished:
                    // print("The publisher finished normally.")
                    return
                case let .failure(error):
                    let error = error as NSError
                    self.error(errordescription: error.description, errortype: .readerror)
                }
            } receiveValue: { [unowned self] data in
                var schedules = [ConfigurationSchedule]()
                for i in 0 ..< data.count {
                    var schedule = ConfigurationSchedule(data[i])
                    schedule.profilename = profile
                    // Validate that the hidden ID is OK,
                    // schedule != Scheduletype.stopped.rawValue, logs count > 0
                    if let validhiddenID = validhiddenID {
                        if validhiddenID.contains(schedule.hiddenID) {
                            schedules.append(schedule)
                        }
                    }
                }
                self.schedules = schedules
                subscriptons.removeAll()
            }.store(in: &subscriptons)
        // Sorting schedule after hiddenID
        schedules?.sort { schedule1, schedule2 -> Bool in
            if schedule1.hiddenID > schedule2.hiddenID {
                return false
            } else {
                return true
            }
        }
        if SharedReference.shared.checkinput {
            schedules = Reorgschedule().mergerecords(data: schedules)
        }
    }<commit_msg>Update ReadScheduleJSON.swift<commit_after>//
//  ReadScheduleJSON.swift
//  RsyncUI
//
//  Created by <NAME> on 19/04/2021.
//

import Combine
import Files
import Foundation

class ReadScheduleJSON: NamesandPaths {
    var schedules: [ConfigurationSchedule]?
    var filenamedatastore = [SharedReference.shared.fileschedulesjson]
    var subscriptons = Set<AnyCancellable>()

    init(_ profile: String?, _ validhiddenID: Set<Int>?) {
        super.init(.configurations)
        print("ReadScheduleJSON")
        filenamedatastore.publisher
            .compactMap { filenamejson -> URL in
                var filename: String = ""
                if let profile = profile, let path = fullpathmacserial {
                    filename = path + "/" + profile + "/" + filenamejson
                } else {
                    if let path = fullpathmacserial {
                        filename = path + "/" + filenamejson
                    }
                }
                return URL(fileURLWithPath: filename)
            }
            .tryMap { url -> Data in
                try Data(contentsOf: url)
            }
            .decode(type: [DecodeSchedule].self, decoder: JSONDecoder())
            .sink { completion in
                switch completion {
                case .finished:
                    // print("The publisher finished normally.")
                    return
                case let .failure(error):
                    let error = error as NSError
                    self.error(errordescription: error.description, errortype: .readerror)
                }
            } receiveValue: { [unowned self] data in
                var schedules = [ConfigurationSchedule]()
                for i in 0 ..< data.count {
                    var schedule = ConfigurationSchedule(data[i])
                    schedule.profilename = profile
                    // Validate that the hidden ID is OK,
                    // schedule != Scheduletype.stopped.rawValue, logs count > 0
                    if let validhiddenID = validhiddenID {
                        if validhiddenID.contains(schedule.hiddenID) {
                            schedules.append(schedule)
                        }
                    }
                }
                self.schedules = schedules
                subscriptons.removeAll()
            }.store(in: &subscriptons)
        // Sorting schedule after hiddenID
        /* Dont need to sort
         schedules?.sort { schedule1, schedule2 -> Bool in
             if schedule1.hiddenID > schedule2.hiddenID {
                 return false
             } else {
                 return true
             }
         }
         */
        if SharedReference.shared.checkinput {
            schedules = Reorgschedule().mergerecords(data: schedules)
        }
    }
{'fraction_non_alphanumeric': 0.08183453237410072, 'fraction_numerical': 0.005035971223021582, 'mean_word_length': 1.3513742071881607, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7034692', 'n_tokens_mistral': 1382, 'n_tokens_neox': 1265, 'n_words': 329}