Modeling complex JSON in Ruby - ruby
I have a piece of JSON that is the boxscore of an NBA game as in an example below. Are there any strategies to turn these data into a sane Ruby model (maybe using serializers)? When I pull in these data from a remote API, there are lots of nested data within this JSON such as the Individual Player statistics, Team Statistics, team data, etc. Any solution on tools to use to deal with this JSON with sane Ruby modeling is preferred. Shouldn't matter but I'll be trying to work this modeling into a ruby gem, not a rails app.
Here's an example of one JSON blob I'm dealing with:
[
{
"game_length": "2:38",
"location": "Sleep Train Arena",
"game_time": "10:00 PM",
"season_type": "Regular",
"game_id": 19123,
"scores_by_quarter": [
{
"scores": {
"quarter": "1",
"away_team_score": 23,
"home_team_score": 24
}
},
{
"scores": {
"quarter": "2",
"away_team_score": 27,
"home_team_score": 21
}
},
{
"scores": {
"quarter": "3",
"away_team_score": 23,
"home_team_score": 27
}
},
{
"scores": {
"quarter": "4",
"away_team_score": 21,
"home_team_score": 22
}
},
{
"scores": {
"quarter": "OT",
"away_team_score": 10,
"home_team_score": 4
}
}
],
"team_stats": [
{
"team": {
"team_id": 101,
"team_name": "LA Clippers",
"listings": [
{
"listing": {
"freethrows_attempted": 2,
"freethrows_made": 0,
"field_goals_attempted": 5,
"turnovers": 1,
"total_rebounds": 3,
"blocks": 0,
"steals": 1,
"assists": 1,
"field_goals_made": 2,
"offensive_rebounds": 1,
"player_first_name": "Jared",
"three_points_made": 1,
"player_last_name": "Dudley",
"points": 5,
"personal_fouls": 4,
"three_points_attempted": 3,
"minutes_played": 37,
"technical_fouls": 0,
"player_position": "F",
"player_id": 1292,
"started": "Yes",
"seconds_played": 16,
"defensive_rebounds": 2
}
},
{
"listing": {
"freethrows_attempted": 10,
"freethrows_made": 7,
"field_goals_attempted": 15,
"turnovers": 4,
"total_rebounds": 12,
"blocks": 0,
"steals": 0,
"assists": 1,
"field_goals_made": 7,
"offensive_rebounds": 2,
"player_first_name": "Blake",
"three_points_made": 0,
"player_last_name": "Griffin",
"points": 21,
"personal_fouls": 4,
"three_points_attempted": 0,
"minutes_played": 38,
"technical_fouls": 1,
"player_position": "F",
"player_id": 1590,
"started": "Yes",
"seconds_played": 57,
"defensive_rebounds": 10
}
},
{
"listing": {
"freethrows_attempted": 5,
"freethrows_made": 0,
"field_goals_attempted": 9,
"turnovers": 1,
"total_rebounds": 15,
"blocks": 9,
"steals": 0,
"assists": 2,
"field_goals_made": 5,
"offensive_rebounds": 6,
"player_first_name": "DeAndre",
"three_points_made": 0,
"player_last_name": "Jordan",
"points": 10,
"personal_fouls": 1,
"three_points_attempted": 0,
"minutes_played": 40,
"technical_fouls": 0,
"player_position": "C",
"player_id": 1487,
"started": "Yes",
"seconds_played": 14,
"defensive_rebounds": 9
}
},
{
"listing": {
"freethrows_attempted": 4,
"freethrows_made": 4,
"field_goals_attempted": 9,
"turnovers": 1,
"total_rebounds": 1,
"blocks": 0,
"steals": 1,
"assists": 2,
"field_goals_made": 4,
"offensive_rebounds": 0,
"player_first_name": "J.J.",
"three_points_made": 1,
"player_last_name": "Redick",
"points": 13,
"personal_fouls": 2,
"three_points_attempted": 4,
"minutes_played": 16,
"technical_fouls": 0,
"player_position": "G",
"player_id": 1129,
"started": "Yes",
"seconds_played": 59,
"defensive_rebounds": 1
}
},
{
"listing": {
"freethrows_attempted": 4,
"freethrows_made": 4,
"field_goals_attempted": 12,
"turnovers": 1,
"total_rebounds": 1,
"blocks": 0,
"steals": 1,
"assists": 2,
"field_goals_made": 5,
"offensive_rebounds": 0,
"player_first_name": "Darren",
"three_points_made": 1,
"player_last_name": "Collison",
"points": 15,
"personal_fouls": 1,
"three_points_attempted": 2,
"minutes_played": 40,
"technical_fouls": 0,
"player_position": "G",
"player_id": 1578,
"started": "Yes",
"seconds_played": 7,
"defensive_rebounds": 1
}
},
{
"listing": {
"freethrows_attempted": 4,
"freethrows_made": 4,
"field_goals_attempted": 22,
"turnovers": 2,
"total_rebounds": 7,
"blocks": 0,
"steals": 0,
"assists": 11,
"field_goals_made": 12,
"offensive_rebounds": 2,
"player_first_name": "Jamal",
"three_points_made": 3,
"player_last_name": "Crawford",
"points": 31,
"personal_fouls": 1,
"three_points_attempted": 7,
"minutes_played": 37,
"technical_fouls": 0,
"player_position": "G",
"player_id": 299,
"started": "No",
"seconds_played": 29,
"defensive_rebounds": 5
}
},
{
"listing": {
"freethrows_attempted": 4,
"freethrows_made": 3,
"field_goals_attempted": 5,
"turnovers": 2,
"total_rebounds": 5,
"blocks": 0,
"steals": 1,
"assists": 1,
"field_goals_made": 1,
"offensive_rebounds": 1,
"player_first_name": "Antawn",
"three_points_made": 0,
"player_last_name": "Jamison",
"points": 5,
"personal_fouls": 1,
"three_points_attempted": 0,
"minutes_played": 16,
"technical_fouls": 0,
"player_position": "F",
"player_id": 682,
"started": "No",
"seconds_played": 37,
"defensive_rebounds": 4
}
},
{
"listing": {
"freethrows_attempted": 2,
"freethrows_made": 1,
"field_goals_attempted": 1,
"turnovers": 0,
"total_rebounds": 0,
"blocks": 0,
"steals": 0,
"assists": 0,
"field_goals_made": 1,
"offensive_rebounds": 0,
"player_first_name": "Ryan",
"three_points_made": 0,
"player_last_name": "Hollins",
"points": 3,
"personal_fouls": 2,
"three_points_attempted": 0,
"minutes_played": 10,
"technical_fouls": 0,
"player_position": "C",
"player_id": 1198,
"started": "No",
"seconds_played": 11,
"defensive_rebounds": 0
}
},
{
"listing": {
"freethrows_attempted": 2,
"freethrows_made": 1,
"field_goals_attempted": 4,
"turnovers": 0,
"total_rebounds": 1,
"blocks": 0,
"steals": 0,
"assists": 0,
"field_goals_made": 0,
"offensive_rebounds": 1,
"player_first_name": "Reggie",
"three_points_made": 0,
"player_last_name": "Bullock",
"points": 1,
"personal_fouls": 0,
"three_points_attempted": 3,
"minutes_played": 12,
"technical_fouls": 0,
"player_position": "F",
"player_id": 2408,
"started": "No",
"seconds_played": 3,
"defensive_rebounds": 0
}
},
{
"listing": {
"freethrows_attempted": 0,
"freethrows_made": 0,
"field_goals_attempted": 1,
"turnovers": 0,
"total_rebounds": 1,
"blocks": 0,
"steals": 0,
"assists": 0,
"field_goals_made": 0,
"offensive_rebounds": 0,
"player_first_name": "Willie",
"three_points_made": 0,
"player_last_name": "Green",
"points": 0,
"personal_fouls": 0,
"three_points_attempted": 1,
"minutes_played": 15,
"technical_fouls": 0,
"player_position": "G",
"player_id": 634,
"started": "No",
"seconds_played": 7,
"defensive_rebounds": 1
}
},
{
"listing": {
"player_first_name": "Chris",
"reason": "Coach's Decision",
"player_last_name": "Paul",
"status": "Did Not Play",
"player_id": 1011
}
},
{
"listing": {
"player_first_name": "Matt",
"reason": "Coach's Decision",
"player_last_name": "Barnes",
"status": "Did Not Play",
"player_id": 827
}
},
{
"listing": {
"player_first_name": "Byron",
"reason": "Coach's Decision",
"player_last_name": "Mullens",
"status": "Did Not Play",
"player_id": 1568
}
},
{
"listing": {
"player_first_name": "Maalik",
"reason": "Injured",
"player_last_name": "Wayns",
"status": "Did Not Play",
"player_id": 2344
}
}
],
"totals": {
"freethrows_attempted": 37,
"freethrows_made": 24,
"field_goals_attempted": 83,
"total_rebounds": 46,
"points_off_turnovers": 18,
"blocks": 9,
"illegal_defense": 1,
"steals": 4,
"assists": 20,
"field_goals_made": 37,
"turnoveres": 12,
"offensive_rebounds": 13,
"team_turnovers": 12,
"three_points_made": 6,
"team_rebounds": 14,
"shooting_percentages": {
"field_goal_percentage": 0.446,
"three_point_percentage": 0.3,
"freethrow_percentage": 0.649
},
"points": 104,
"personal_fouls": 16,
"three_points_attempted": 20,
"minutes_played": 265,
"technical_fouls": 2,
"seconds_played": 240,
"non_player_technicals": 0,
"defensive_rebounds": 33
}
}
},
{
"team": {
"team_id": 109,
"team_name": "Sacramento",
"listings": [
{
"listing": {
"freethrows_attempted": 1,
"freethrows_made": 0,
"field_goals_attempted": 13,
"turnovers": 1,
"total_rebounds": 6,
"blocks": 0,
"steals": 0,
"assists": 4,
"field_goals_made": 6,
"offensive_rebounds": 0,
"player_first_name": "Derrick",
"three_points_made": 0,
"player_last_name": "Williams",
"points": 12,
"personal_fouls": 3,
"three_points_attempted": 2,
"minutes_played": 32,
"technical_fouls": 0,
"player_position": "F",
"player_id": 2192,
"started": "Yes",
"seconds_played": 21,
"defensive_rebounds": 6
}
},
{
"listing": {
"freethrows_attempted": 0,
"freethrows_made": 0,
"field_goals_attempted": 7,
"turnovers": 1,
"total_rebounds": 4,
"blocks": 1,
"steals": 0,
"assists": 0,
"field_goals_made": 3,
"offensive_rebounds": 2,
"player_first_name": "Jason",
"three_points_made": 0,
"player_last_name": "Thompson",
"points": 6,
"personal_fouls": 4,
"three_points_attempted": 0,
"minutes_played": 23,
"technical_fouls": 0,
"player_position": "F",
"player_id": 1496,
"started": "Yes",
"seconds_played": 11,
"defensive_rebounds": 2
}
},
{
"listing": {
"freethrows_attempted": 3,
"freethrows_made": 3,
"field_goals_attempted": 19,
"turnovers": 3,
"total_rebounds": 9,
"blocks": 3,
"steals": 0,
"assists": 6,
"field_goals_made": 11,
"offensive_rebounds": 1,
"player_first_name": "DeMarcus",
"three_points_made": 0,
"player_last_name": "Cousins",
"points": 25,
"personal_fouls": 5,
"three_points_attempted": 0,
"minutes_played": 41,
"technical_fouls": 0,
"player_position": "C",
"player_id": 1900,
"started": "Yes",
"seconds_played": 21,
"defensive_rebounds": 8
}
},
{
"listing": {
"freethrows_attempted": 3,
"freethrows_made": 3,
"field_goals_attempted": 12,
"turnovers": 2,
"total_rebounds": 4,
"blocks": 0,
"steals": 2,
"assists": 3,
"field_goals_made": 5,
"offensive_rebounds": 2,
"player_first_name": "Ben",
"three_points_made": 1,
"player_last_name": "McLemore",
"points": 14,
"personal_fouls": 3,
"three_points_attempted": 6,
"minutes_played": 34,
"technical_fouls": 0,
"player_position": "G",
"player_id": 2432,
"started": "Yes",
"seconds_played": 43,
"defensive_rebounds": 2
}
},
{
"listing": {
"freethrows_attempted": 1,
"freethrows_made": 1,
"field_goals_attempted": 12,
"turnovers": 0,
"total_rebounds": 3,
"blocks": 0,
"steals": 0,
"assists": 7,
"field_goals_made": 4,
"offensive_rebounds": 1,
"player_first_name": "Greivis",
"three_points_made": 2,
"player_last_name": "Vasquez",
"points": 11,
"personal_fouls": 2,
"three_points_attempted": 6,
"minutes_played": 35,
"technical_fouls": 0,
"player_position": "G",
"player_id": 1970,
"started": "Yes",
"seconds_played": 58,
"defensive_rebounds": 2
}
},
{
"listing": {
"freethrows_attempted": 0,
"freethrows_made": 0,
"field_goals_attempted": 4,
"turnovers": 1,
"total_rebounds": 3,
"blocks": 0,
"steals": 0,
"assists": 0,
"field_goals_made": 2,
"offensive_rebounds": 1,
"player_first_name": "Marcus",
"three_points_made": 0,
"player_last_name": "Thornton",
"points": 4,
"personal_fouls": 5,
"three_points_attempted": 2,
"minutes_played": 17,
"technical_fouls": 0,
"player_position": "G",
"player_id": 1646,
"started": "No",
"seconds_played": 26,
"defensive_rebounds": 2
}
},
{
"listing": {
"freethrows_attempted": 2,
"freethrows_made": 1,
"field_goals_attempted": 5,
"turnovers": 1,
"total_rebounds": 9,
"blocks": 0,
"steals": 0,
"assists": 0,
"field_goals_made": 3,
"offensive_rebounds": 3,
"player_first_name": "Patrick",
"three_points_made": 0,
"player_last_name": "Patterson",
"points": 7,
"personal_fouls": 4,
"three_points_attempted": 0,
"minutes_played": 26,
"technical_fouls": 0,
"player_position": "F",
"player_id": 1950,
"started": "No",
"seconds_played": 59,
"defensive_rebounds": 6
}
},
{
"listing": {
"freethrows_attempted": 0,
"freethrows_made": 0,
"field_goals_attempted": 1,
"turnovers": 2,
"total_rebounds": 1,
"blocks": 0,
"steals": 0,
"assists": 0,
"field_goals_made": 0,
"offensive_rebounds": 1,
"player_first_name": "Hamady",
"three_points_made": 0,
"player_last_name": "N`diaye",
"points": 0,
"personal_fouls": 3,
"three_points_attempted": 0,
"minutes_played": 4,
"technical_fouls": 0,
"player_position": "C",
"player_id": 1944,
"started": "No",
"seconds_played": 56,
"defensive_rebounds": 0
}
},
{
"listing": {
"freethrows_attempted": 0,
"freethrows_made": 0,
"field_goals_attempted": 2,
"turnovers": 0,
"total_rebounds": 0,
"blocks": 0,
"steals": 1,
"assists": 1,
"field_goals_made": 0,
"offensive_rebounds": 0,
"player_first_name": "Travis",
"three_points_made": 0,
"player_last_name": "Outlaw",
"points": 0,
"personal_fouls": 0,
"three_points_attempted": 1,
"minutes_played": 9,
"technical_fouls": 0,
"player_position": "F",
"player_id": 759,
"started": "No",
"seconds_played": 30,
"defensive_rebounds": 0
}
},
{
"listing": {
"player_first_name": "Chuck",
"reason": "Coach's Decision",
"player_last_name": "Hayes",
"status": "Did Not Play",
"player_id": 1044
}
},
{
"listing": {
"player_first_name": "Carl",
"reason": "Injured",
"player_last_name": "Landry",
"status": "Did Not Play",
"player_id": 1333
}
},
{
"listing": {
"player_first_name": "Ray",
"reason": "Injured",
"player_last_name": "McCallum",
"status": "Did Not Play",
"player_id": 2430
}
},
{
"listing": {
"player_first_name": "Jimmer",
"reason": "Coach's Decision",
"player_last_name": "Fredette",
"status": "Did Not Play",
"player_id": 2111
}
}
],
"totals": {
"freethrows_attempted": 10,
"freethrows_made": 8,
"field_goals_attempted": 91,
"total_rebounds": 44,
"points_off_turnovers": 15,
"blocks": 5,
"illegal_defense": 1,
"steals": 4,
"assists": 24,
"field_goals_made": 42,
"turnoveres": 15,
"offensive_rebounds": 12,
"team_turnovers": 16,
"three_points_made": 6,
"team_rebounds": 6,
"shooting_percentages": {
"field_goal_percentage": 0.462,
"three_point_percentage": 0.261,
"freethrow_percentage": 0.8
},
"points": 98,
"personal_fouls": 33,
"three_points_attempted": 23,
"minutes_played": 265,
"technical_fouls": 1,
"seconds_played": 360,
"non_player_technicals": 0,
"defensive_rebounds": 32
}
}
}
],
"away_team_final_score": 104,
"quarter": 5,
"game_date": "November 29, 2013",
"technicals": [
{
"listing": {
"flagrant": "False",
"team_id": 109,
"time_left": "8:40",
"player_first_name": "",
"quarter": 3,
"player_last_name": "",
"player_id": 0,
"def_3_sec": "True",
"bench": "False"
}
},
{
"listing": {
"flagrant": "False",
"team_id": 101,
"time_left": "6:11",
"player_first_name": "Blake",
"quarter": 3,
"player_last_name": "Griffin",
"player_id": 1590,
"def_3_sec": "False",
"bench": "False"
}
},
{
"listing": {
"flagrant": "False",
"team_id": 101,
"time_left": "5:46",
"player_first_name": "",
"quarter": 3,
"player_last_name": "",
"player_id": 0,
"def_3_sec": "True",
"bench": "False"
}
}
],
"sport": "NBA",
"visiting_team": "LA Clippers",
"status": "FINAL",
"home_team_id": 109,
"title": "LA Clippers at Sacramento",
"attendance": "17317",
"home_team_final_score": 98,
"updated_at": "2013-11-30 06:08:40 UTC",
"visiting_team_id": 101,
"home_team": "Sacramento",
"season_year": "2013",
"officials": "Tony Brothers, David Guthrie, JT Orr"
}
]
You can parse JSON data into two classes, Arrays and Hashes:
Arrays:
data = [1, 2]
data.to_json # => "[1,2]"
JSON.parse(data.to_json) # => [1, 2]
Hashes:
data = {'a' => 1}
data.to_json # => "{\"a\":1}"
JSON.parse(data.to_json) # => {"a"=>1}
Attempting to generate anything else results in an error.
Once you parse the JSON string back into a Ruby object, you'll have one of those two types acting as a wrapper or container for the data you want, so you can use normal Ruby loops to walk through the data if it's an Array, and directly access the values you want if it's a Hash.
While your sample appears complex to you, it can easily be broken down into some simple code that will extract the data. If I parse the data and store it in a variable called data:
data.first returns the hash containing the data, which, in your example is all the data. If there were multiple hashes then iterate over them using:
data.each { |hash| ... }
or:
data.map { |hash| ... }
Once you have a sub-hash you can access things in similar manners, by iterating or using hash accesses:
data.first['scores_by_quarter'].last['scores']['home_team_score']
# => 4
I had to copy your JSON into a file to be able to read it properly(note that this is not necessary, you can supply the JSON stream directly to JSON.parse as it should be properly formatted), then parse it as a JSON to an array of hashes and use OpenStruct to convert them to ruby objects, if you have a large array, then OpenStruct is not very good with performance, explanation can be found here
require 'ostruct'
require 'json'
a = JSON.parse(File.read('temp.json'))
a.map! { |team| OpenStruct.new(team) }
Setters and getters generated by OpenStruct
a.each { |x| p x.methods(false) };
[:game_length, :game_length=, :location, :location=, :game_time, :game_time=, :season_type, :season_type=, :game_id, :game_id=, :scores_by_quarter, :scores_by_quarter=, :team_stats, :team_stats=, :away_team_final_score, :away_team_final_score=, :quarter, :quarter=, :game_date, :game_date=, :technicals, :technicals=, :sport, :sport=, :visiting_team, :visiting_team=, :status, :status=, :home_team_id, :home_team_id=, :title, :title=, :attendance, :attendance=, :home_team_final_score, :home_team_final_score=, :updated_at, :updated_at=, :visiting_team_id, :visiting_team_id=, :home_team, :home_team=, :season_year, :season_year=, :officials, :officials=]
Related
How to use Nifi QueryRecord to lookup for values in a flowfile?
I have a scenario where a flowfile contains the json transformed, I need to now combine fields that are in the json, for example, I want to get the R and PA from the Original.ScoreInfo.Team per ID and get Enriched.Team.Source.HV and Enriched.Team.SourceMapped.HV based on the Team.ID. My desired json looks like: [{ TeamID: 1000, ValueR: 3, ValuePA: 13}, { TeamID: 2000, ValueR: 1, ValuePA: 14} ] Flowfile example: [ { "Mapping": { "ScoreInfo": { "Team": [ { "ID": 1, "Source": { "HV": 1, "ID": 1 } }, { "ID": 3, "Source": { "HV": 2, "ID": 3 } } ] } }, "Original": { "ScoreInfo": { "Team": [ { "HV": 1, "ID": 1, "R": 1, "PA": 8, "H": 2, "BB": 2, "SB": 0, "E": 0, "Score": [ { "Inn": 1, "TB": 2, "R": 0, "H": 0, "BB": 1 }, { "Inn": 2, "TB": 2, "R": 1, "H": 2, "BB": 1 } ] }, { "HV": 2, "ID": 3, "R": 1, "PA": 10, "H": 3, "BB": 0, "SB": 0, "E": 0, "Score": [ { "Inn": 1, "TB": 1, "R": 1, "H": 2, "BB": 0 }, { "Inn": 2, "TB": 1, "R": 0, "H": 0, "BB": 0 } ] } ] } }, "MatchValue": "99999999", "Enriched": { "Team": [ { "ID": 1, "Source": { "HV": 1, "ID": 1 }, "SourceMapped": { "HV": 1000, "ID": 1000 } }, { "ID": 2, "Source": { "HV": 2, "ID": 2 }, "SourceMapped": { "HV": 2000, "ID": 2000 } } ], "MatchValue": "99999999" } } ] I was trying to do it using the QueryRecord and combined with the RPATH but it is returning empty when try this SELECT * FROM FLOWFILE WHERE RPATH(Original, '/ScoreInfo/Team/ID') = RPATH(Enriched, '/Team/ID') Any idea how can I use the QueryRecord to do it?
elasticsearch update doc very slow
Now it even takes about 7 minutes to update a doc. Elasticsearch version: 6.8.6 Deploy via docker Single node, heap size: 8G, 30 shards, a total of 6.2G data, 2 million doc. This is too slow, if anyone can tell me how to solve it, I would appreciate it iostat info Linux 4.15.0-20-generic (max-master) 12/18/2020 _x86_64_ (32 CPU) avg-cpu: %user %nice %system %iowait %steal %idle 15.46 0.00 8.95 0.09 0.00 75.51 Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn sda 222.31 92.21 5448.83 23575235 1393042693 cat _nodes/hot_threads ::: {es-node1}{VxAcSdn8QJKRBu705pFVDA}{ozapTgHcRDaIgu9Cb-tB9w}{10.244.0.19}{10.244.0.19:9300}{ml.machine_memory=66933485568, xpack.installed=true, ml.max_open_jobs=20, ml.enabled=true} Hot threads at 2020-12-18T08:36:29.005Z, interval=500ms, busiestThreads=3, ignoreIdleThreads=true: 100.4% (502ms out of 500ms) cpu usage by thread 'elasticsearch[es-node1][write][T#7]' 2/10 snapshots sharing following 205 elements ... 22.7% (113.6ms out of 500ms) cpu usage by thread 'elasticsearch[es-node1][write][T#4]' 6/10 snapshots sharing following 192 elements ... cat /_nodes/stats/thread_pool?human&pretty { "_nodes": { "total": 1, "successful": 1, "failed": 0 }, "cluster_name": "zs", "nodes": { "VxAcSdn8QJKRBDA": { "timestamp": 1608280589005, "name": "es-node1", "transport_address": "10.44.0.19:9300", "host": "10.44.0.19", "ip": "10.44.0.19:9300", "roles": [ "master", "data", "ingest" ], "attributes": { "ml.machine_memory": "66933485568", "xpack.installed": "true", "ml.max_open_jobs": "20", "ml.enabled": "true" }, "thread_pool": { "analyze": { "threads": 0, "queue": 0, "active": 0, "rejected": 0, "largest": 0, "completed": 0 }, "ccr": { "threads": 0, "queue": 0, "active": 0, "rejected": 0, "largest": 0, "completed": 0 }, "fetch_shard_started": { "threads": 1, "queue": 0, "active": 0, "rejected": 0, "largest": 16, "completed": 167 }, "fetch_shard_store": { "threads": 0, "queue": 0, "active": 0, "rejected": 0, "largest": 0, "completed": 0 }, "flush": { "threads": 1, "queue": 0, "active": 0, "rejected": 0, "largest": 4, "completed": 368 }, "force_merge": { "threads": 0, "queue": 0, "active": 0, "rejected": 0, "largest": 0, "completed": 0 }, "generic": { "threads": 24, "queue": 0, "active": 1, "rejected": 0, "largest": 24, "completed": 10495 }, "get": { "threads": 8, "queue": 0, "active": 0, "rejected": 0, "largest": 8, "completed": 389 }, "index": { "threads": 0, "queue": 0, "active": 0, "rejected": 0, "largest": 0, "completed": 0 }, "listener": { "threads": 3, "queue": 0, "active": 0, "rejected": 0, "largest": 3, "completed": 3 }, "management": { "threads": 5, "queue": 0, "active": 2, "rejected": 0, "largest": 5, "completed": 7194 }, "ml_autodetect": { "threads": 0, "queue": 0, "active": 0, "rejected": 0, "largest": 0, "completed": 0 }, "ml_datafeed": { "threads": 0, "queue": 0, "active": 0, "rejected": 0, "largest": 0, "completed": 0 }, "ml_utility": { "threads": 1, "queue": 0, "active": 0, "rejected": 0, "largest": 1, "completed": 1 }, "refresh": { "threads": 4, "queue": 0, "active": 0, "rejected": 0, "largest": 4, "completed": 51313 }, "rollup_indexing": { "threads": 0, "queue": 0, "active": 0, "rejected": 0, "largest": 0, "completed": 0 }, "search": { "threads": 13, "queue": 0, "active": 1, "rejected": 0, "largest": 13, "completed": 217319 }, "search_throttled": { "threads": 0, "queue": 0, "active": 0, "rejected": 0, "largest": 0, "completed": 0 }, "security-token-key": { "threads": 0, "queue": 0, "active": 0, "rejected": 0, "largest": 0, "completed": 0 }, "snapshot": { "threads": 0, "queue": 0, "active": 0, "rejected": 0, "largest": 0, "completed": 0 }, "warmer": { "threads": 4, "queue": 0, "active": 0, "rejected": 0, "largest": 4, "completed": 399 }, "watcher": { "threads": 0, "queue": 0, "active": 0, "rejected": 0, "largest": 0, "completed": 0 }, "write": { "threads": 8, "queue": 0, "active": 6, "rejected": 0, "largest": 8, "completed": 1401 } } } } }
Vega-lite heatmap "transform" isn't working right
1) There's a X - coordinate and Y - coordinate. There's a Z - value. Z needs to calculate AVG. "data": { "values": [ {"x": 0, "y": 0, "z": 0}, {"x": 1, "y": 0, "z": 1}, {"x": 2, "y": 0, "z": 5}, {"x": 2, "y": 0, "z": 10}, {"x": 4, "y": 0, "z": 16}, {"x": 0, "y": 1, "z": 1}, {"x": 1, "y": 1, "z": 2}, {"x": 2, "y": 1, "z": 5}, {"x": 3, "y": 1, "z": 10}, {"x": 5, "y": 1, "z": 26} ]}, If the data: "encoding": { "x": {"field": "x", "type": "ordinal", "title": "X"}, "y": {"field": "y", "type": "ordinal", "title": "Y"} }, then AVG that's right: Vega Editor If I use "transform" with "groupby" : "transform": [ {"window": [{"op": "count", "field": "x", "as": "x2"}], "groupby": ["y"]}], "encoding": { "x": {"field": "x2", "type": "ordinal", "title": "X"}, "y": {"field": "y", "type": "ordinal", "title": "Y"} }, then AVG it's not true: Vega Editor
The transform is doing exactly what you're telling it to do: {"window": [{"op": "count", "field": "x", "as": "x2"}], "groupby": ["y"]} It groups your data by y. Schematically it looks like this: y=0: x: [0, 1, 2, 2, 4] z: [0, 1, 5, 10, 16] y=1: x: [0, 1, 2, 3, 5] z: [1, 2, 5, 10, 26] Then it does a sliding window and assigns the count to x2: y=0: x: [0, 1, 2, 2, 4] z: [0, 1, 5, 10, 16] x2: [1, 2, 3, 4, 5] y=1: x: [0, 1, 2, 3, 5] z: [1, 2, 5, 10, 26] x2: [1, 2, 3, 4, 5] As you can see, when you create a plot of y vs x2, there is now a single z value at each location, and this is correctly reflected in the chart that you show above. If you want different behavior, you should specify a different transform.
Vega-lite heatmap add text to a variable
"data": { "values": [ {"x": 0, "y": 0, "z": 0}, {"x": 1, "y": 0, "z": 1}, {"x": 2, "y": 0, "z": 5}, {"x": 2, "y": 0, "z": 10}, {"x": 4, "y": 0, "z": 16}, {"x": 0, "y": 1, "z": 1}, {"x": 1, "y": 1, "z": 2}, {"x": 2, "y": 1, "z": 5}, {"x": 3, "y": 1, "z": 10}, {"x": 5, "y": 1, "z": 26} ]}, How to add text to a variable, so like "AVG:"? Vega Editor
You can use a calculate transform to create specifically-formatted text to display in a text mark. For example (vega editor): { "data": { "values": [ {"x": 0, "y": 0, "z": 0}, {"x": 1, "y": 0, "z": 1}, {"x": 2, "y": 0, "z": 5}, {"x": 2, "y": 0, "z": 10}, {"x": 4, "y": 0, "z": 16}, {"x": 0, "y": 1, "z": 1}, {"x": 1, "y": 1, "z": 2}, {"x": 2, "y": 1, "z": 5}, {"x": 3, "y": 1, "z": 10}, {"x": 5, "y": 1, "z": 26} ] }, "transform": [ { "aggregate": [{"op": "mean", "field": "z", "as": "z"}], "groupby": ["x", "y"] }, {"calculate": "'avg: ' + datum.z", "as": "z"} ], "mark": "text", "encoding": { "x": {"field": "x", "type": "ordinal"}, "y": {"field": "y", "type": "ordinal"}, "text": {"field": "z", "type": "nominal"} }, "width": 300, "height": 100 }
MongoDB Too slow at find by id query, for just 100000 items?
I have a collection with 100000(999996) documents and I'm using Laravel for integration, When I want to find a document by its id, it take too long to respond. (something like >80ms) I'm wondering why it must take too long for just 100k items? and _id field is indexed. what configurations I should set for better performance. be aware I have some embeds in my document that embedded by Laravel model. example of one of the documents: { "_id": ObjectId("5d88a2857c254f0256737f43"), "name": "blcy", "coach": null, "line_up": 1, "sponsor_id": "5d131dd37c254f1d007d01f0", "last_online": ISODate("2019-09-23T10:46:29.000Z"), "stadium": 1, "active_stadium": 1, "doctor": 1, "active_doctor": 1, "trainer": 1, "coach_assistant": 1, "active_coach_assistant": 1, "fitness_coach": 1, "active_fitness_coach": 1, "leader": 1, "active_leader": 1, "executive_manager": 1, "active_executive_manager": 1, "strategy": 1, "attack_tactic": 1, "defence_tactic": 1, "ticket_price": 5, "user_id": "5d88a2847c254f0256737f42", "updated_at": ISODate("2019-09-23T10:46:29.000Z"), "created_at": ISODate("2019-09-23T10:46:29.000Z"), "kit": { "bg_color": 10, "fg_color": 20, "fg": 1, "updated_at": ISODate("2019-09-23T10:46:29.000Z"), "created_at": ISODate("2019-09-23T10:46:29.000Z"), "_id": ObjectId("5d88a2857c254f0256737f44") }, "logo": { "bg_color": 10, "fg_color": 20, "fg": 1, "bg": 11, "updated_at": ISODate("2019-09-23T10:46:29.000Z"), "created_at": ISODate("2019-09-23T10:46:29.000Z"), "_id": ObjectId("5d88a2857c254f0256737f45") }, "players": [ { "name": "بردیا رادمان", "player_number": 7, "age": 22, "post": "G", "talent": 2, "shoot": 6, "pass": 2, "speed": 5, "strength": 5, "readiness": 100, "technique": 6, "head": 6, "real_power": 83, "power": 30, "level": 2, "updated_at": ISODate("2019-09-23T10:46:29.000Z"), "created_at": ISODate("2019-09-23T10:46:29.000Z"), "_id": ObjectId("5d88a2857c254f0256737f46"), "in_lineup": true, "position": "G" }, { "name": "سعید قاضی", "player_number": 1, "age": 19, "post": "G", "talent": 5, "shoot": 2, "pass": 2, "speed": 6, "strength": 5, "readiness": 100, "technique": 6, "head": 2, "real_power": 69, "power": 23, "level": 1, "updated_at": ISODate("2019-09-23T10:46:29.000Z"), "created_at": ISODate("2019-09-23T10:46:29.000Z"), "_id": ObjectId("5d88a2857c254f0256737f47") }, { "name": "علیرضا چلبی", "player_number": 47, "age": 19, "post": "G", "talent": 4, "shoot": 4, "pass": 3, "speed": 4, "strength": 3, "readiness": 100, "technique": 4, "head": 3, "real_power": 60, "power": 21, "level": 1, "updated_at": ISODate("2019-09-23T10:46:29.000Z"), "created_at": ISODate("2019-09-23T10:46:29.000Z"), "_id": ObjectId("5d88a2857c254f0256737f48") }, { "name": "آذین اوستا", "player_number": 52, "age": 25, "post": "G", "talent": 4, "shoot": 5, "pass": 6, "speed": 2, "strength": 6, "readiness": 100, "technique": 2, "head": 4, "real_power": 91, "power": 25, "level": 2, "updated_at": ISODate("2019-09-23T10:46:29.000Z"), "created_at": ISODate("2019-09-23T10:46:29.000Z"), "_id": ObjectId("5d88a2857c254f0256737f49") }, { "name": "یاسین صفوی", "player_number": 28, "age": 20, "post": "D", "talent": 4, "shoot": 5, "pass": 4, "speed": 3, "strength": 4, "readiness": 100, "technique": 3, "head": 4, "real_power": 68, "power": 23, "level": 1, "updated_at": ISODate("2019-09-23T10:46:29.000Z"), "created_at": ISODate("2019-09-23T10:46:29.000Z"), "_id": ObjectId("5d88a2857c254f0256737f4a"), "in_lineup": true, "position": "D" }, { "name": "خشایار نوبخت", "player_number": 36, "age": 17, "post": "D", "talent": 2, "shoot": 6, "pass": 5, "speed": 3, "strength": 5, "readiness": 100, "technique": 3, "head": 3, "real_power": 75, "power": 25, "level": 2, "updated_at": ISODate("2019-09-23T10:46:29.000Z"), "created_at": ISODate("2019-09-23T10:46:29.000Z"), "_id": ObjectId("5d88a2857c254f0256737f4b"), "in_lineup": true, "position": "D" }, { "name": "شجاع سحاب", "player_number": 18, "age": 23, "post": "D", "talent": 3, "shoot": 4, "pass": 2, "speed": 2, "strength": 3, "readiness": 100, "technique": 5, "head": 4, "real_power": 53, "power": 20, "level": 1, "updated_at": ISODate("2019-09-23T10:46:29.000Z"), "created_at": ISODate("2019-09-23T10:46:29.000Z"), "_id": ObjectId("5d88a2857c254f0256737f4c"), "in_lineup": true, "position": "D" }, { "name": "رها فهمیده", "player_number": 10, "age": 26, "post": "D", "talent": 3, "shoot": 5, "pass": 5, "speed": 3, "strength": 5, "readiness": 100, "technique": 5, "head": 3, "real_power": 76, "power": 26, "level": 2, "updated_at": ISODate("2019-09-23T10:46:29.000Z"), "created_at": ISODate("2019-09-23T10:46:29.000Z"), "_id": ObjectId("5d88a2857c254f0256737f4d"), "in_lineup": true, "position": "D" }, { "name": "آذین عبدالکریمی", "player_number": 32, "age": 18, "post": "D", "talent": 2, "shoot": 6, "pass": 4, "speed": 2, "strength": 2, "readiness": 100, "technique": 6, "head": 5, "real_power": 59, "power": 25, "level": 1, "updated_at": ISODate("2019-09-23T10:46:29.000Z"), "created_at": ISODate("2019-09-23T10:46:29.000Z"), "_id": ObjectId("5d88a2857c254f0256737f4e"), "in_lineup": true, "position": "D" }, { "name": "محمدرضا طالقانی", "player_number": 22, "age": 26, "post": "D", "talent": 5, "shoot": 3, "pass": 3, "speed": 3, "strength": 4, "readiness": 100, "technique": 6, "head": 2, "real_power": 60, "power": 21, "level": 1, "updated_at": ISODate("2019-09-23T10:46:29.000Z"), "created_at": ISODate("2019-09-23T10:46:29.000Z"), "_id": ObjectId("5d88a2857c254f0256737f4f") }, { "name": "پارسا دستغیب", "player_number": 9, "age": 19, "post": "D", "talent": 1, "shoot": 6, "pass": 2, "speed": 4, "strength": 3, "readiness": 100, "technique": 6, "head": 2, "real_power": 58, "power": 23, "level": 1, "updated_at": ISODate("2019-09-23T10:46:29.000Z"), "created_at": ISODate("2019-09-23T10:46:29.000Z"), "_id": ObjectId("5d88a2857c254f0256737f50") }, { "name": "فرشید پناهیان", "player_number": 33, "age": 26, "post": "M", "talent": 2, "shoot": 2, "pass": 6, "speed": 2, "strength": 4, "readiness": 100, "technique": 3, "head": 6, "real_power": 72, "power": 23, "level": 1, "updated_at": ISODate("2019-09-23T10:46:29.000Z"), "created_at": ISODate("2019-09-23T10:46:29.000Z"), "_id": ObjectId("5d88a2857c254f0256737f51"), "in_lineup": true, "position": "M" }, { "name": "سپهر واعظ", "player_number": 8, "age": 26, "post": "M", "talent": 4, "shoot": 4, "pass": 3, "speed": 3, "strength": 2, "readiness": 100, "technique": 6, "head": 6, "real_power": 72, "power": 24, "level": 1, "updated_at": ISODate("2019-09-23T10:46:29.000Z"), "created_at": ISODate("2019-09-23T10:46:29.000Z"), "_id": ObjectId("5d88a2857c254f0256737f52"), "in_lineup": true, "position": "M" }, { "name": "امیررضا کوشکی", "player_number": 3, "age": 26, "post": "M", "talent": 5, "shoot": 4, "pass": 2, "speed": 6, "strength": 3, "readiness": 100, "technique": 3, "head": 4, "real_power": 62, "power": 22, "level": 1, "updated_at": ISODate("2019-09-23T10:46:29.000Z"), "created_at": ISODate("2019-09-23T10:46:29.000Z"), "_id": ObjectId("5d88a2857c254f0256737f53"), "in_lineup": true, "position": "M" }, { "name": "پدرام جنتی", "player_number": 29, "age": 24, "post": "M", "talent": 3, "shoot": 2, "pass": 4, "speed": 5, "strength": 4, "readiness": 100, "technique": 2, "head": 4, "real_power": 63, "power": 21, "level": 1, "updated_at": ISODate("2019-09-23T10:46:29.000Z"), "created_at": ISODate("2019-09-23T10:46:29.000Z"), "_id": ObjectId("5d88a2857c254f0256737f54") }, { "name": "محمد علیآبادی", "player_number": 13, "age": 17, "post": "M", "talent": 1, "shoot": 2, "pass": 5, "speed": 3, "strength": 3, "readiness": 100, "technique": 4, "head": 4, "real_power": 68, "power": 21, "level": 1, "updated_at": ISODate("2019-09-23T10:46:29.000Z"), "created_at": ISODate("2019-09-23T10:46:29.000Z"), "_id": ObjectId("5d88a2857c254f0256737f55") }, { "name": "سهیل فانی", "player_number": 19, "age": 25, "post": "M", "talent": 1, "shoot": 2, "pass": 3, "speed": 4, "strength": 4, "readiness": 100, "technique": 4, "head": 5, "real_power": 65, "power": 22, "level": 1, "updated_at": ISODate("2019-09-23T10:46:29.000Z"), "created_at": ISODate("2019-09-23T10:46:29.000Z"), "_id": ObjectId("5d88a2857c254f0256737f56") }, { "name": "شهرام گنجی", "player_number": 24, "age": 17, "post": "S", "talent": 4, "shoot": 6, "pass": 3, "speed": 6, "strength": 2, "readiness": 100, "technique": 2, "head": 2, "real_power": 69, "power": 21, "level": 1, "updated_at": ISODate("2019-09-23T10:46:29.000Z"), "created_at": ISODate("2019-09-23T10:46:29.000Z"), "_id": ObjectId("5d88a2857c254f0256737f57"), "in_lineup": true, "position": "S" }, { "name": "امیرمحمد سرافراز", "player_number": 2, "age": 21, "post": "S", "talent": 4, "shoot": 6, "pass": 2, "speed": 5, "strength": 4, "readiness": 100, "technique": 5, "head": 5, "real_power": 90, "power": 27, "level": 2, "updated_at": ISODate("2019-09-23T10:46:29.000Z"), "created_at": ISODate("2019-09-23T10:46:29.000Z"), "_id": ObjectId("5d88a2857c254f0256737f58"), "in_lineup": true, "position": "S" }, { "name": "فربد ارسباران", "player_number": 20, "age": 25, "post": "S", "talent": 2, "shoot": 2, "pass": 2, "speed": 5, "strength": 3, "readiness": 100, "technique": 4, "head": 4, "real_power": 65, "power": 20, "level": 1, "updated_at": ISODate("2019-09-23T10:46:29.000Z"), "created_at": ISODate("2019-09-23T10:46:29.000Z"), "_id": ObjectId("5d88a2857c254f0256737f59") }, { "name": "فرشید الهام", "player_number": 21, "age": 24, "post": "S", "talent": 2, "shoot": 4, "pass": 3, "speed": 6, "strength": 2, "readiness": 100, "technique": 3, "head": 6, "real_power": 78, "power": 24, "level": 2, "updated_at": ISODate("2019-09-23T10:46:29.000Z"), "created_at": ISODate("2019-09-23T10:46:29.000Z"), "_id": ObjectId("5d88a2857c254f0256737f5a") }, { "name": "ساسان طریقت", "player_number": 35, "age": 17, "post": "S", "talent": 5, "shoot": 2, "pass": 5, "speed": 4, "strength": 2, "readiness": 100, "technique": 5, "head": 6, "real_power": 74, "power": 24, "level": 1, "updated_at": ISODate("2019-09-23T10:46:29.000Z"), "created_at": ISODate("2019-09-23T10:46:29.000Z"), "_id": ObjectId("5d88a2857c254f0256737f5b") } ], "league_id": "5d88a2857c254f0256737f5d", "league_name": "league-0-1", "transactions": [ { "amount": 120000, "type": "coin", "cash": 0, "coin": 120000, "description": "1", "updated_at": ISODate("2019-09-23T10:46:29.000Z"), "created_at": ISODate("2019-09-23T10:46:29.000Z"), "_id": ObjectId("5d88a2857c254f0256737f5e") }, { "amount": 100, "type": "cash", "cash": 100, "coin": 120000, "description": "1", "updated_at": ISODate("2019-09-23T10:46:29.000Z"), "created_at": ISODate("2019-09-23T10:46:29.000Z"), "_id": ObjectId("5d88a2857c254f0256737f5f") } ], "cash": 100, "coin": 120000, "fixtures": [ { "league_name": "league-0-1", "home_id": "5d88a2857c254f025673800f", "guest_id": "5d88a2857c254f0256737f43", "week_day": 1, "weather_type": "sunny", "home_goal": 0, "guest_goal": 0, "friendly": false, "fixture_id": "5d89dff87c254f36ee3ae9ab", "updated_at": ISODate("2019-09-24T09:20:56.000Z"), "created_at": ISODate("2019-09-24T09:20:56.000Z"), "_id": ObjectId("5d89dff87c254f36ee3ae9ad") }, { "league_name": "league-0-1", "home_id": "5d88a2857c254f0256737f61", "guest_id": "5d88a2857c254f0256737f43", "week_day": 2, "weather_type": "rainy", "home_goal": 0, "guest_goal": 0, "friendly": false, "fixture_id": "5d89dff87c254f36ee3ae9c0", "updated_at": ISODate("2019-09-24T09:20:56.000Z"), "created_at": ISODate("2019-09-24T09:20:56.000Z"), "_id": ObjectId("5d89dff87c254f36ee3ae9c2") }, { "league_name": "league-0-1", "home_id": "5d88a2857c254f0256737f43", "guest_id": "5d88a2857c254f0256737f7e", "week_day": 3, "weather_type": "sunny", "home_goal": 0, "guest_goal": 0, "friendly": false, "fixture_id": "5d89dff87c254f36ee3ae9c6", "updated_at": ISODate("2019-09-24T09:20:56.000Z"), "created_at": ISODate("2019-09-24T09:20:56.000Z"), "_id": ObjectId("5d89dff87c254f36ee3ae9c7") }, { "league_name": "league-0-1", "home_id": "5d88a2857c254f0256737f9b", "guest_id": "5d88a2857c254f0256737f43", "week_day": 4, "weather_type": "sunny", "home_goal": 0, "guest_goal": 0, "friendly": false, "fixture_id": "5d89dff87c254f36ee3ae9d5", "updated_at": ISODate("2019-09-24T09:20:56.000Z"), "created_at": ISODate("2019-09-24T09:20:56.000Z"), "_id": ObjectId("5d89dff87c254f36ee3ae9d7") }, { "league_name": "league-0-1", "home_id": "5d88a2857c254f0256737f43", "guest_id": "5d88a2857c254f0256737fb8", "week_day": 5, "weather_type": "rainy", "home_goal": null, "guest_goal": null, "friendly": false, "fixture_id": "5d89dff87c254f36ee3ae9e1", "updated_at": ISODate("2019-09-24T09:20:57.000Z"), "created_at": ISODate("2019-09-24T09:20:57.000Z"), "_id": ObjectId("5d89dff97c254f36ee3ae9e2") }, { "league_name": "league-0-1", "home_id": "5d88a2857c254f0256737fd5", "guest_id": "5d88a2857c254f0256737f43", "week_day": 6, "weather_type": "sunny", "home_goal": null, "guest_goal": null, "friendly": false, "fixture_id": "5d89dff97c254f36ee3ae9ea", "updated_at": ISODate("2019-09-24T09:20:57.000Z"), "created_at": ISODate("2019-09-24T09:20:57.000Z"), "_id": ObjectId("5d89dff97c254f36ee3ae9ec") }, { "league_name": "league-0-1", "home_id": "5d88a2857c254f0256737f43", "guest_id": "5d88a2857c254f0256737ff2", "week_day": 7, "weather_type": "sunny", "home_goal": null, "guest_goal": null, "friendly": false, "fixture_id": "5d89dff97c254f36ee3ae9fc", "updated_at": ISODate("2019-09-24T09:20:57.000Z"), "created_at": ISODate("2019-09-24T09:20:57.000Z"), "_id": ObjectId("5d89dff97c254f36ee3ae9fd") } ] } Finding a team like this: $team = Team::find($team['team_id']); EDITED: chunk methods and also cant help me cause i just want to find one document not getting all documents in one row. Thanks.