require 'spec_helper'
require "elasticsearch"


string = %{

{
  "**me": {
    "duration": "0:3:12::400",
    "env_state": " Country Info:Saturday, January 9, 2021 1:03:19 PM EST-UnitedStates @=>  iNetworkTestScore:0 iVideoNetworkTestScore:0  ||  bStunTestDone:1 bNatPreservePort:1 bPortMapped:0 port:52466 useStaticPort:0 || Running on Mac OS X 10.16.0 on plugged in || Processor count =8.  Mac Model:MacBookPro11,3",
    "inSession": "false",
    "info": "*Me cid: **me name:Henry Wasserman uiSession:142874782 encgroup:0 randport:0 ONENAT:0",
    "ip": "qhaLocal: 192.168.1.176 qhaRemote:108.49.216.11 qhaNatAddr:  v4: 192.168.1.176   v6: ",
    "my_route": {
      "ARSComDisable": "false",
      "AutoMiFastestArs": "true",
      "GroupCast": "false",
      "P2PDisable": "false",
      "Route": "PolicyOverride a=0 p=0 d=1",
      "StickyPath": "true",
      "UseSessHostArs": "false",
      "VideoP2P": "false",
      "WarmARSCon": "true",
      "closest_ars": {
        "ars_address": "34.86.170.59",
        "ars_id": "29",
        "ars_port": "3478",
        "is_cached": "1",
        "latency": {
          "jitter": "0.00013688831463026563",
          "jitter_mean": "0.25",
          "jitter_stdev": "1.681702971458435",
          "ping_latency": "29.777878888657725",
          "ping_latency_mean": "31.078125",
          "ping_latency_stdev": "3.8009238243103027"
        },
        "loc": {
          "city": "Ashburn",
          "continent": "NA",
          "country": "US"
        },
        "rank": "1"
      }
    },
    "pt": "usPort: 52466 usNatPort:0 usIpv6Port:0  eLocation: 0 getUserAddress=b:0 a: p:0",
    "qmAudioStatus": {
      "acpu": "1.2415982484817505",
      "audio_in": "Built-in Microph",
      "audio_in_name": "Using audio device 0: Core Audio Built-in Microph chans in:2 out:0 default srate:44100",
      "audio_in_type": "Core Audio",
      "audio_out": "Built-in Output",
      "audio_out_name": "Using audio device 1: Core Audio Built-in Output chans in:0 out:2 default srate:44100",
      "audio_out_type": "Core Audio",
      "cpu": "13.982522010803223",
      "framesize": "2.5",
      "in_latency": "3.99092960357666",
      "input_iio_jitter": "-0.5572032928466797",
      "input_jitter": "189.1237335205078",
      "input_median": "334.2256774902344",
      "io_out_latency": "8.911563873291016",
      "io_out_latency_str": "Expected Latency =   8.91 +/- 1.00  ms  [Raw/PaBuff/PaRing Latency:  8.91 / 8.91 / 0.00 ms]",
      "latency_predict": "true",
      "out_latency": "4.920634746551514",
      "output_iio_jitter": "-0.2445507049560547",
      "output_jitter": "189.45669555664062",
      "output_median": "334.2256774902344",
      "output_name": "1",
      "samplerate": "44100"
    },
    "qmNetworkStatus": {
      "ars_sort_order": {
        "APAC": "6",
        "CA": "2",
        "EU": "5",
        "NA": "1",
        "SA": "4",
        "UK": "3"
      },
      "closest_ars": {
        "ars_address": "34.86.170.59",
        "ars_id": "29",
        "ars_port": "3478",
        "is_cached": "1",
        "latency": {
          "jitter": "0.00013688831463026563",
          "jitter_mean": "0.25",
          "jitter_stdev": "1.681702971458435",
          "ping_latency": "29.777878888657725",
          "ping_latency_mean": "31.078125",
          "ping_latency_stdev": "3.8009238243103027"
        },
        "loc": {
          "city": "Ashburn",
          "continent": "NA",
          "country": "US"
        },
        "rank": "1"
      },
      "last_ars_connection": {
        "ars_address": "34.86.170.59",
        "ars_id": "29"
      },
      "wifi": "0"
    }
  },
  "*TrackPlays": {
    "backtrack_plays": "0",
    "jamtrack_plays": "0"
  },
  "06952d1b-1ba0-4d13-8e82-f5438e030d07": {
    "duration": "0:1:2::806",
    "env_state": {
      "addr": "108.49.216.11",
      "audio_status": {
        "acpu": "11.18483829498291",
        "aipt_violation": "0",
        "aopt_violation": "0",
        "audio_in": "Focusrite USB ASIO",
        "audio_in_name": "Using audio device 13: ASIO Focusrite USB ASIO chans in:2 out:2 default srate:48000",
        "audio_in_type": "ASIO",
        "audio_out_type": "",
        "cpu": "19.173519134521484",
        "dect_violation": "0",
        "framesize": "2",
        "in_latency": "3.9791665077209473",
        "input_iio_jitter": "0.026166439056396484",
        "input_jitter": "0.4325855076313019",
        "input_median": "500.4284973144531",
        "io_out_latency": "7.4583330154418945",
        "io_out_latency_str": "Expected Latency =   7.46 +/- 0.50  ms  [Raw/PaBuff/PaRing Latency:  8.96 / 8.96 / 0.00 ms]",
        "latency_predict": "true",
        "out_latency": "3.9791665077209473",
        "output_iio_jitter": "0.10657310485839844",
        "output_jitter": "0.4325855076313019",
        "output_median": "500.4284973144531",
        "output_name": "13",
        "samplerate": "48000"
      },
      "init_net_status": {
        "PeerOnWifi": "false",
        "ars_con_active": "0",
        "ars_id": "29",
        "ars_ping": "64.74291229248047",
        "ars_ping_var": "5.7877397537231445",
        "audio_bitrate_rx": "256.379150390625",
        "audio_bitrate_tx": "250",
        "audio_rx_pk": "414.31451416015625",
        "audio_tx_pk": "418.3467712402344",
        "audiojq_median": "6.952920436859131",
        "audiojq_var": "2.021310567855835",
        "chat_bitrate_rx": "0",
        "chat_bitrate_tx": "46.875",
        "con_address": "108.49.216.11",
        "con_port": "52466",
        "global_ipv4": "",
        "jitter_median": "1.9524338245391846",
        "jitter_var": "3.3287851810455322",
        "local_ipv4": "192.168.1.176",
        "meOnWifi": "false",
        "media_bitrate_rx": "0",
        "media_bitrate_tx": "0",
        "my_pref_rx_rate": "400",
        "net_bitrate_rx": "369.2587585449219",
        "net_bitrate_tx": "386.48248291015625",
        "p2p_ping": "58",
        "p2p_ping_var": "6.145358562469482",
        "path_opt": "true",
        "ping": "58",
        "ping_var": "6.145358562469482",
        "pkt_loss": "0",
        "preferred_path": "p2p",
        "preferred_path_votes": "51",
        "rx_audio_gcast": "false",
        "rx_video_gcast": "false",
        "tx_audio_gcast": "false",
        "tx_video_gcast": "false",
        "using_ars_path": "0",
        "video_rtcpbw_rx": "0",
        "video_rtcpbw_tx": "0",
        "video_rtcpkt_rx": "0",
        "video_rtcpkt_tx": "0",
        "video_rtpbw_rx": "0",
        "video_rtpbw_tx": "0",
        "video_rtpkt_rx": "0",
        "video_rtpkt_tx": "0",
        "webcam": "false",
        "wifi": "false",
        "wifi_to_user": "false"
      },
      "port": "52466",
      "route": {
        "ARSComDisable": "false",
        "AutoMiFastestArs": "true",
        "GroupCast": "false",
        "P2PDisable": "false",
        "Route": "PolicyOverride a=0 p=0 d=1",
        "StickyPath": "true",
        "UseSessHostArs": "false",
        "VideoP2P": "false",
        "WarmARSCon": "true",
        "closest_ars": {
          "ars_address": "104.44.140.200",
          "ars_id": "105",
          "ars_port": "3478",
          "is_cached": "1",
          "latency": {
            "jitter": "0.00002490835534986728",
            "jitter_mean": "0.046875",
            "jitter_stdev": "0.21137107908725739",
            "ping_latency": "2.3750707535878344",
            "ping_latency_mean": "2.59375",
            "ping_latency_stdev": "0.6782041788101196"
          },
          "loc": {
            "city": "San Antonio",
            "continent": "NA",
            "country": "US"
          },
          "rank": "1"
        }
      },
      "state": " Country Info:Saturday, January 9, 2021 12:51:22 PM Central Standard Time-UnitedStates @=>  iNetworkTestScore:0 iVideoNetworkTestScore:0  ||  bStunTestDone:1 bNatPreservePort:1 bPortMapped:0 port:55200 useStaticPort:0 || Processor count =4/4. CPU_BRAND:= GenuineIntel CPUIFO:= Intel(R) Core(TM) i5-6300HQ CPU @ 2.30GHz "
    },
    "inSession": "true",
    "info": "cid: 06952d1b-1ba0-4d13-8e82-f5438e030d07 name:Stewart Swift uiSession:2743772497 encgroup:0 randport:0 ONENAT:0",
    "ip": "qhaLocal: 192.168.1.105 qhaRemote:68.73.196.221 qhaNatAddr:  v4: 192.168.1.105   v6: 2600:1700:1881:d890:1ad:ef5b:4b68:c624 2600:1700:1881:d890:1844:c238:627e:1c22 2600:1700:1881:d890::49 ",
    "peerEnv": "Peer STATE YES: Receiving audio from Me:  Country Info:Saturday, January 9, 2021 12:51:22 PM Central Standard Time-UnitedStates @=>  iNetworkTestScore:0 iVideoNetworkTestScore:0  ||  bStunTestDone:1 bNatPreservePort:1 bPortMapped:0 port:55200 useStaticPort:0 || Processor count =4/4. CPU_BRAND:= GenuineIntel CPUIFO:= Intel(R) Core(TM) i5-6300HQ CPU @ 2.30GHz  ==> 108.49.216.11@52466",
    "ps": "bLatencyTestOnly:0  bInSession:1 getPktRecvRateGroup:0 overRide:0",
    "pt": "usPort: 55200 usNatPort:0 usIpv6Port:55200  eLocation: 4 getUserAddress=b:1 a:68.73.196.221 p:55200",
    "ra": "YES <== RECEIVING AUDIO FROM PEER"
  },
  "65c57483-7605-4ee0-a754-4acb60e29d0b": {
    "duration": "0:3:4::745",
    "env_state": {
      "addr": "108.49.216.11",
      "audio_status": {
        "acpu": "0.8916316628456116",
        "aipt_violation": "0",
        "aopt_violation": "0",
        "audio_in": "Microphone (USB Audio CODEC)",
        "audio_in_name": "Using audio device 27: Windows WDM-KS Microphone (USB Audio CODEC) chans in:2 out:0 default srate:48000",
        "audio_in_type": "Windows WDM-KS",
        "audio_out": "Speakers (USB Audio CODEC)",
        "audio_out_name": "Using audio device 26: Windows WDM-KS Speakers (USB Audio CODEC) chans in:0 out:2 default srate:48000",
        "audio_out_type": "Windows WDM-KS",
        "cpu": "2.6178781986236572",
        "dect_violation": "0",
        "framesize": "10",
        "in_latency": "10",
        "input_iio_jitter": "-0.013723373413085938",
        "input_jitter": "0.3255210220813751",
        "input_median": "100.09182739257812",
        "io_out_latency": "19",
        "io_out_latency_str": "Expected Latency =   19.00 +/- 2.00  ms  [Raw/PaBuff/PaRing Latency:  20.00 / 20.00 / 0.00 ms]",
        "latency_predict": "true",
        "out_latency": "10",
        "output_iio_jitter": "-0.017871856689453125",
        "output_jitter": "0.3255210220813751",
        "output_median": "100.09182739257812",
        "output_name": "26",
        "samplerate": "48000"
      },
      "init_net_status": {
        "PeerOnWifi": "false",
        "ars_con_active": "0",
        "ars_id": "29",
        "audio_bitrate_rx": "258.8125",
        "audio_bitrate_tx": "250",
        "audio_rx_pk": "111.00000762939453",
        "audio_tx_pk": "114.00000762939453",
        "audiojq_median": "4.997518539428711",
        "audiojq_var": "1.698746919631958",
        "chat_bitrate_rx": "0",
        "chat_bitrate_tx": "46.875",
        "con_address": "108.49.216.11",
        "con_port": "52466",
        "global_ipv4": "",
        "jitter_median": "10.055049896240234",
        "jitter_var": "4.532331943511963",
        "local_ipv4": "192.168.1.176",
        "meOnWifi": "false",
        "media_bitrate_rx": "0",
        "media_bitrate_tx": "0",
        "my_pref_rx_rate": "100",
        "net_bitrate_rx": "293.09375",
        "net_bitrate_tx": "298.765625",
        "p2p_ping": "45",
        "p2p_ping_var": "9.957050323486328",
        "path_opt": "true",
        "ping": "45",
        "ping_var": "9.957050323486328",
        "pkt_loss": "0",
        "preferred_path": "p2p",
        "preferred_path_votes": "36",
        "rx_audio_gcast": "false",
        "rx_video_gcast": "false",
        "tx_audio_gcast": "false",
        "tx_video_gcast": "false",
        "using_ars_path": "0",
        "video_rtcpbw_rx": "0",
        "video_rtcpbw_tx": "0",
        "video_rtcpkt_rx": "0",
        "video_rtcpkt_tx": "0",
        "video_rtpbw_rx": "0",
        "video_rtpbw_tx": "0",
        "video_rtpkt_rx": "0",
        "video_rtpkt_tx": "0",
        "webcam": "false",
        "wifi": "false",
        "wifi_to_user": "false"
      },
      "port": "52466",
      "route": {
        "ARSComDisable": "false",
        "AutoMiFastestArs": "true",
        "GroupCast": "false",
        "P2PDisable": "false",
        "Route": "PolicyOverride a=0 p=0 d=1",
        "StickyPath": "true",
        "UseSessHostArs": "false",
        "VideoP2P": "false",
        "WarmARSCon": "true",
        "closest_ars": {
          "ars_address": "151.139.47.26",
          "ars_id": "46",
          "is_cached": "1",
          "latency": {
            "jitter": "0.00014753539093772635",
            "jitter_mean": "1.6484375",
            "jitter_stdev": "4.0573811531066895",
            "ping_latency": "15.16867496264098",
            "ping_latency_mean": "16.3984375",
            "ping_latency_stdev": "3.100755453109741"
          },
          "loc": {
            "city": "Chicago",
            "continent": "NA",
            "country": "US"
          },
          "rank": "1"
        }
      },
      "state": " Country Info:Saturday, January 9, 2021 12:32:56 PM Central Standard Time-UnitedStates @=>  iNetworkTestScore:0 iVideoNetworkTestScore:0  ||  bStunTestDone:1 bNatPreservePort:1 bPortMapped:1 port:59904 useStaticPort:0 || Processor count =4/8. CPU_BRAND:= GenuineIntel CPUIFO:= Intel(R) Core(TM) i7-3770S CPU @ 3.10GHz "
    },
    "inSession": "true",
    "info": "cid: 65c57483-7605-4ee0-a754-4acb60e29d0b name:Brian Daugherty: Voice uiSession:321276815 encgroup:0 randport:0 ONENAT:0",
    "ip": "qhaLocal: 10.0.0.90 qhaRemote:98.222.36.205 qhaNatAddr:98.222.36.205  v4: 10.0.0.90   v6: 2601:248:8300:3760:b5c0:1145:c327:972d 2601:248:8300:3760:d0d2:5897:b484:4cd5 2601:248:8300:3760::f020 ",
    "peerEnv": "Peer STATE YES: Receiving audio from Me:  Country Info:Saturday, January 9, 2021 12:32:56 PM Central Standard Time-UnitedStates @=>  iNetworkTestScore:0 iVideoNetworkTestScore:0  ||  bStunTestDone:1 bNatPreservePort:1 bPortMapped:1 port:59904 useStaticPort:0 || Processor count =4/8. CPU_BRAND:= GenuineIntel CPUIFO:= Intel(R) Core(TM) i7-3770S CPU @ 3.10GHz  ==> 108.49.216.11@52466",
    "ps": "bLatencyTestOnly:0  bInSession:1 getPktRecvRateGroup:2 overRide:0",
    "pt": "usPort: 59904 usNatPort:59904 usIpv6Port:59904  eLocation: 5 getUserAddress=b:1 a:98.222.36.205 p:59904",
    "ra": "YES <== RECEIVING AUDIO FROM PEER"
  },
  "8a7ceb38-6cdf-447d-bee3-89bc08644104": {
    "duration": "0:3:4::679",
    "env_state": {
      "addr": "108.49.216.11",
      "audio_status": {
        "acpu": "4.287694931030273",
        "aipt_violation": "0",
        "aopt_violation": "0",
        "audio_in": "UMC ASIO Driver",
        "audio_in_name": "Using audio device 15: ASIO UMC ASIO Driver chans in:2 out:4 default srate:48000",
        "audio_in_type": "ASIO",
        "cpu": "21.86724090576172",
        "dect_violation": "0",
        "framesize": "2.5",
        "in_latency": "1.6666666269302368",
        "input_iio_jitter": "-6.493330001831055",
        "input_jitter": "0.43091174960136414",
        "input_median": "400.5474548339844",
        "io_out_latency": "4.166666507720947",
        "io_out_latency_str": "Expected Latency =   4.17 +/- 1.00  ms  [Raw/PaBuff/PaRing Latency:  4.17 / 6.50 / 0.00 ms]",
        "latency_predict": "true",
        "out_latency": "2.5",
        "output_iio_jitter": "12.542228698730469",
        "output_jitter": "0.43239912390708923",
        "output_median": "400.3656311035156",
        "output_name": "15",
        "samplerate": "48000"
      },
      "init_net_status": {
        "PeerOnWifi": "false",
        "ars_con_active": "0",
        "ars_id": "29",
        "audio_bitrate_rx": "256.1216125488281",
        "audio_bitrate_tx": "312.5",
        "audio_rx_pk": "409.81964111328125",
        "audio_tx_pk": "415.8316650390625",
        "audiojq_median": "6.6145405769348145",
        "audiojq_var": "1.4825245141983032",
        "chat_bitrate_rx": "0",
        "chat_bitrate_tx": "62.5",
        "con_address": "108.49.216.11",
        "con_port": "52466",
        "global_ipv4": "",
        "jitter_median": "2.458285093307495",
        "jitter_var": "2.247823715209961",
        "local_ipv4": "192.168.1.176",
        "meOnWifi": "false",
        "media_bitrate_rx": "0",
        "media_bitrate_tx": "0",
        "my_pref_rx_rate": "400",
        "net_bitrate_rx": "362.02093505859375",
        "net_bitrate_tx": "448.4046325683594",
        "p2p_ping": "126.27545928955078",
        "p2p_ping_var": "3.98777437210083",
        "path_opt": "true",
        "ping": "126.27545928955078",
        "ping_var": "3.98777437210083",
        "pkt_loss": "0",
        "preferred_path": "p2p",
        "preferred_path_votes": "38",
        "rx_audio_gcast": "false",
        "rx_video_gcast": "false",
        "tx_audio_gcast": "false",
        "tx_video_gcast": "false",
        "using_ars_path": "0",
        "video_rtcpbw_rx": "0",
        "video_rtcpbw_tx": "0",
        "video_rtcpkt_rx": "0",
        "video_rtcpkt_tx": "0",
        "video_rtpbw_rx": "0",
        "video_rtpbw_tx": "0",
        "video_rtpkt_rx": "0",
        "video_rtpkt_tx": "0",
        "webcam": "false",
        "wifi": "false",
        "wifi_to_user": "false"
      },
      "port": "52466",
      "route": {
        "ARSComDisable": "false",
        "AutoMiFastestArs": "true",
        "GroupCast": "false",
        "P2PDisable": "false",
        "Route": "PolicyOverride a=0 p=0 d=1",
        "StickyPath": "true",
        "UseSessHostArs": "false",
        "VideoP2P": "false",
        "WarmARSCon": "true",
        "closest_ars": {
          "ars_address": "151.139.186.5",
          "ars_id": "79",
          "ars_port": "3478",
          "is_cached": "1",
          "latency": {
            "jitter": "0.0586243615518995",
            "jitter_mean": "3.53125",
            "jitter_stdev": "6.304682731628418",
            "ping_latency": "33.22995250684622",
            "ping_latency_mean": "34.1171875",
            "ping_latency_stdev": "2.5237035751342773"
          },
          "loc": {
            "city": "Warsaw",
            "continent": "EU",
            "country": "PL"
          },
          "rank": "1"
        }
      },
      "state": " Country Info:Saturday, January 9, 2021 7:35:54 PM Central European Standard Time-UnitedStates @=>  iNetworkTestScore:0 iVideoNetworkTestScore:0  ||  bStunTestDone:1 bNatPreservePort:1 bPortMapped:1 port:12000 useStaticPort:1 || Processor count =6/6. CPU_BRAND:= GenuineIntel CPUIFO:= Intel(R) Core(TM) i5-9600K CPU @ 3.70GHz "
    },
    "inSession": "true",
    "info": "cid: 8a7ceb38-6cdf-447d-bee3-89bc08644104 name:Nadir the bass uiSession:3989597311 encgroup:0 randport:0 ONENAT:0",
    "ip": "qhaLocal: 192.168.0.12 qhaRemote:77.77.236.50 qhaNatAddr:77.77.236.50  v4: 192.168.0.12   v6: ",
    "peerEnv": "Peer STATE YES: Receiving audio from Me:  Country Info:Saturday, January 9, 2021 7:35:54 PM Central European Standard Time-UnitedStates @=>  iNetworkTestScore:0 iVideoNetworkTestScore:0  ||  bStunTestDone:1 bNatPreservePort:1 bPortMapped:1 port:12000 useStaticPort:1 || Processor count =6/6. CPU_BRAND:= GenuineIntel CPUIFO:= Intel(R) Core(TM) i5-9600K CPU @ 3.70GHz  ==> 108.49.216.11@52466",
    "ps": "bLatencyTestOnly:0  bInSession:1 getPktRecvRateGroup:0 overRide:0",
    "pt": "usPort: 12000 usNatPort:12000 usIpv6Port:0  eLocation: 5 getUserAddress=b:1 a:77.77.236.50 p:12000",
    "ra": "YES <== RECEIVING AUDIO FROM PEER"
  }
}

}

describe "Elasticsearch"do
  let(:me) { FactoryGirl.create(:user, email: 'estest@jamkazam.com') }
  let(:user1) { FactoryGirl.create(:user) }
  let(:connection1) { FactoryGirl.create(:connection, :user => user1) }
  let(:user2) { FactoryGirl.create(:user) }
  let(:connection2) { FactoryGirl.create(:connection, :user => user2) }
  let(:user3) { FactoryGirl.create(:user) }
  let(:connection3) { FactoryGirl.create(:connection, :user => user3) }
  let(:music_session) { FactoryGirl.create(:music_session) }

  it "should save to index" do

    index_settings = { number_of_shards: 1, number_of_replicas: 0 }
    settings = { settings: { index: index_settings } }


    # 06952d1b-1ba0-4d13-8e82-f5438e030d07
    # 65c57483-7605-4ee0-a754-4acb60e29d0b
    # 8a7ceb38-6cdf-447d-bee3-89bc08644104

    # fix the canned body above
    body = JSON.parse(string)

    # overrwite real user IDs with test IDs
    body[connection1.id] = body.delete("06952d1b-1ba0-4d13-8e82-f5438e030d07")
    body[connection2.id] = body.delete("65c57483-7605-4ee0-a754-4acb60e29d0b")
    body[connection3.id] = body.delete("8a7ceb38-6cdf-447d-bee3-89bc08644104")

    client = JamRuby::ElasticSearch.new

    #client.smash_indices
    succeeded = JamRuby::ElasticSearch.new.session_ratings(music_session, me, body)
    #SearchClient.index(id: tag.id, index: "tags_development", body: string)
    succeeded.should be_true
  end
end
