<commit_before>﻿namespace Surge.Core.Transmission

type TransmissionServerStats = { DownloadSpeed : int64
                                 UploadSpeed : int64 }

type TransmissionServerGet = { ServerVersion : int
                               DefaultDownloadLocation : string
                               SpaceRemaining : int64 }

module internal Deserialise =
    open FSharp.Data.JsonExtensions
    open Surge.Core.Models
    open Surge.Core.Network
    open Surge.Core.Transmission
    open Surge.Core.Transmission.Files
    open System
    open System.Collections.Generic
    open FSharp.Data
        
    let DeserialiseStats (stats : string) : TransmissionServerStats =
        let stats' = (SessionStats.Parse stats).Arguments
        { DownloadSpeed = stats'.DownloadSpeed
          UploadSpeed = stats'.UploadSpeed }

    let DeserializeGet (get : string) : TransmissionServerGet =
        let get' = (SessionGet.Parse get)
        let args = get'.Arguments
        { ServerVersion = args.RpcVersion
          DefaultDownloadLocation = args.DownloadDir
          SpaceRemaining =
            match args.FreeSpace with
            | Some(value) -> value
            | None -> match args.DownloadDirFreeSpace with
                      | Some(value) -> value
                      | None -> (int64)0 }

    let GenerateServer (stats : TransmissionServerStats) (get : TransmissionServerGet) =
        { DownloadSpeed = stats.DownloadSpeed
          UploadSpeed = stats.UploadSpeed
          ServerVersion = get.ServerVersion
          DefaultDownloadLocation = get.DefaultDownloadLocation
          SpaceRemaining = get.SpaceRemaining }

    let TorrentListToDictionary seq =
        let dictionary = Dictionary<int, Torrent>()
        seq |> Seq.iter(fun t -> dictionary.Add(t.ID, t))
        dictionary

    let DeserialiseTorrents torrentData torrentDataTracker =
        let jsonDeserialise (torrentData : string) =
            (TorrentGet.Parse torrentData).Arguments.Torrents

        let unixTime = int (DateTime.UtcNow - DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds
        
        let generateTorrent(jsonTorrent : TorrentGet.Torrent) =
            let generateFiles = match Seq.length torrentDataTracker with
                                | 0 -> true
                                | _ ->
                                    let result = torrentDataTracker |> Seq.filter (fun e -> e.Id = jsonTorrent.Id)
                                    match Seq.isEmpty result with
                                    | true -> true
                                    | false -> not (Seq.head result).HasFiles

            let files =
                Array.zip jsonTorrent.Files jsonTorrent.FileStats<commit_msg>Use a nicer pattern match on two options<commit_after>﻿namespace Surge.Core.Transmission

type TransmissionServerStats = { DownloadSpeed : int64
                                 UploadSpeed : int64 }

type TransmissionServerGet = { ServerVersion : int
                               DefaultDownloadLocation : string
                               SpaceRemaining : int64 }

module internal Deserialise =
    open FSharp.Data.JsonExtensions
    open Surge.Core.Models
    open Surge.Core.Network
    open Surge.Core.Transmission
    open Surge.Core.Transmission.Files
    open System
    open System.Collections.Generic
    open FSharp.Data
        
    let DeserialiseStats (stats : string) : TransmissionServerStats =
        let stats' = (SessionStats.Parse stats).Arguments
        { DownloadSpeed = stats'.DownloadSpeed
          UploadSpeed = stats'.UploadSpeed }

    let DeserializeGet (get : string) : TransmissionServerGet =
        let get' = (SessionGet.Parse get)
        let args = get'.Arguments
        { ServerVersion = args.RpcVersion
          DefaultDownloadLocation = args.DownloadDir
          SpaceRemaining =
            match (args.FreeSpace, args.DownloadDirFreeSpace) with
            | (Some(value), None) | (None, Some(value)) -> value
            | _ -> (int64)0
        }

    let GenerateServer (stats : TransmissionServerStats) (get : TransmissionServerGet) =
        { DownloadSpeed = stats.DownloadSpeed
          UploadSpeed = stats.UploadSpeed
          ServerVersion = get.ServerVersion
          DefaultDownloadLocation = get.DefaultDownloadLocation
          SpaceRemaining = get.SpaceRemaining }

    let TorrentListToDictionary seq =
        let dictionary = Dictionary<int, Torrent>()
        seq |> Seq.iter(fun t -> dictionary.Add(t.ID, t))
        dictionary

    let DeserialiseTorrents torrentData torrentDataTracker =
        let jsonDeserialise (torrentData : string) =
            (TorrentGet.Parse torrentData).Arguments.Torrents

        let unixTime = int (DateTime.UtcNow - DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds
        
        let generateTorrent(jsonTorrent : TorrentGet.Torrent) =
            let generateFiles = match Seq.length torrentDataTracker with
                                | 0 -> true
                                | _ ->
                                    let result = torrentDataTracker |> Seq.filter (fun e -> e.Id = jsonTorrent.Id)
                                    match Seq.isEmpty result with
                                    | true -> true
                                    | false -> not (Seq.head result).HasFiles

            let files =
                Array.zip jsonTorrent.Files jsonTorrent.FileStats
{'fraction_non_alphanumeric': 0.0711783145860225, 'fraction_numerical': 0.00700719159137009, 'mean_word_length': 1.8323759791122716, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 38, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5994886', 'n_tokens_mistral': 1375, 'n_tokens_neox': 1287, 'n_words': 325}