How to export production stats to csv/json from saves?

Place to get help with not working mods / modding interface.
pilp
Manual Inserter
Manual Inserter
Posts: 2
Joined: Sun Feb 23, 2025 4:48 pm
Contact:

How to export production stats to csv/json from saves?

Post by pilp »

For one of my uni subjects i was tasked to find some data in a time series and do something with them in Py (to learn Py). Normally people would get some publicly available data, but i thought of using production statistics from one of my vanilla Factorio save.

Unfortunately i dont know how to export the necessary data from my save.

Does anyone know, how to export production statistics from my save to csv or json?

Im including a copy of my save in case someone needed it.
Its a vanilla playthrough, doesnt have Space age DLC, game version 2.0.15
Attachments
2.0 game.zip
save file of my game
(11.38 MiB) Downloaded 32 times
eugenekay
Filter Inserter
Filter Inserter
Posts: 433
Joined: Tue May 15, 2018 2:14 am
Contact:

Re: How to export production stats to csv/json from saves?

Post by eugenekay »

Sounds like the Stats Saver mod. This covers "Live" data as you play the game.

If you wanted to get the Historical data for an existing Save you would need to iterate through the LuaFlowStatistics using get_flow_count for your chosen items/timeframes; then export using write_file.

Good Luck!
Muche
Smart Inserter
Smart Inserter
Posts: 1006
Joined: Fri Jun 02, 2017 6:20 pm
Contact:

Re: How to export production stats to csv/json from saves?

Post by Muche »

I made the following command (to be run in the console):

Code: Select all

local timescales = {
  [defines.flow_precision_index.five_seconds] = "5s",
  [defines.flow_precision_index.one_minute] = "1min",
  [defines.flow_precision_index.ten_minutes] = "10min",
  [defines.flow_precision_index.one_hour] = "1h",
  [defines.flow_precision_index.ten_hours] = "10h",
  [defines.flow_precision_index.fifty_hours] = "50h",
  [defines.flow_precision_index.two_hundred_fifty_hours] = "250h",
  [defines.flow_precision_index.one_thousand_hours] = "1000h"
}
for surface_name, _ in pairs(game.surfaces) do
  local flowdata = game.forces.player.get_item_production_statistics(surface_name)
  for timescale_index, timescale_name in pairs(timescales) do
    local tbl = {}
    local totals = flowdata.input_counts
    for item_name, _ in pairs(totals) do
      local row = {item_name}
      for i = 1, 300 do
        table.insert(row, flowdata.get_flow_count{name=item_name, category="input", precision_index=timescale_index, sample_index=i, count=true})
      end
      table.insert(tbl, row)
    end
    helpers.write_file("stats-" .. surface_name .. "-" .. timescale_name .. ".json", helpers.table_to_json(tbl), false)
  end
end
game.player.print("Export done")
It should export all item production stats into json files in the script-output folder.
Item consumption stats could also be available (output category of LuaFlowStatistics), fluids/builds/kills production/consumption (see other get_*_statistics of LuaForce, electric network, or pollution stats.
Attachments
127078-2.0 game-stats-productions.zip
(75.75 KiB) Downloaded 22 times
pilp
Manual Inserter
Manual Inserter
Posts: 2
Joined: Sun Feb 23, 2025 4:48 pm
Contact:

Re: How to export production stats to csv/json from saves?

Post by pilp »

Thank you both for you answers.
Muche wrote: Sun Feb 23, 2025 7:42 pm I made the following command (to be run in the console):
Thank you for your code and the file! :D
eugenekay wrote: Sun Feb 23, 2025 6:35 pm If you wanted to get the Historical data for an existing Save you would need to iterate through the LuaFlowStatistics using get_flow_count for your chosen items/timeframes; then export using write_file.
Thank you for your tip. I will check out the LuaFlowStatistics, how it works, so i can write about it in a report accompanying my Py code. :D
eugenekay
Filter Inserter
Filter Inserter
Posts: 433
Joined: Tue May 15, 2018 2:14 am
Contact:

Re: How to export production stats to csv/json from saves?

Post by eugenekay »

Muche wrote: Sun Feb 23, 2025 7:42 pm I made the following command (to be run in the console):
Wow, that looks like a good candidate to put on the Wiki for future reference! I was just spitballing how it could be achieved, and here is a complete implementation. :-D
pandafish
Manual Inserter
Manual Inserter
Posts: 1
Joined: Fri Feb 28, 2025 6:53 am
Contact:

Re: How to export production stats to csv/json from saves?

Post by pandafish »

eugenekay wrote: Sun Feb 23, 2025 8:06 pm
Muche wrote: Sun Feb 23, 2025 7:42 pm I made the following command (to be run in the console):
Wow, that looks like a good candidate to put on the Wiki for future reference! I was just spitballing how it could be achieved, and here is a complete implementation. :-D
Added it (including Muche as a source) to to https://wiki.factorio.com/Production_statistics - This worked wonders. Thanks!
Post Reply

Return to “Modding help”