danmayer / coverband

Ruby production code coverage collection and reporting (line of code usage)
https://github.com/danmayer/coverband
MIT License
2.5k stars 161 forks source link

Add a JSON Reporter #469

Closed mark-davenport-fountain closed 1 year ago

mark-davenport-fountain commented 1 year ago

I'm proposing adding a JSON route and reporter. This can be used by systems to pull a structured view of the data and then can be easily processed.

I based this off of what was in the html file view.

In my use case I'd like to be able to hit this end point periodically and then store the results to process locally and give feedback during dev about usage of a file that's in production, but having this structured set available for systems to use could be valuable for other use cases as well.

Example output (truncated):


{
  "total_files": 17,
  "lines_of_code": 242,
  "lines_covered": 38,
  "lines_missed": 204,
  "covered_strength": 1.4223140495867765,
  "covered_percent": 15.702479338842975,
  "files": {
    "app/controllers/application_controller.rb": {
      "file_name": "app/controllers/application_controller.rb",
      "never_loaded": false,
      "runtime_percentage": 33.33,
      "lines_of_code": 11,
      "lines_covered": 2,
      "lines_missed": 1,
      "covered_percent": 66.66666666666667,
      "covered_strength": 3.0
    },
    "app/controllers/home_controller.rb": {
      "file_name": "app/controllers/home_controller.rb",
      "never_loaded": false,
      "runtime_percentage": 28.57,
      "lines_of_code": 35,
      "lines_covered": 10,
      "lines_missed": 10,
      "covered_percent": 50.0,
      "covered_strength": 1.6
    }
    .....
  }
}
danmayer commented 1 year ago

hmm, so @mark-davenport-fountain this isn't well documented as it is mostly for the developers of coverband, but I have also used it for tool integration in the past... There are two JSON endpoints

I think these would cover your use cases... to enable and display links to that data add this to your coverband config:

config.web_debug = true

If these don't have the specific data you are after we could modify or add another endpoint... I am more likely to support an API than some of the recent requests to dump static reports... All of these 'extra' options that folks do a bit more of one off processing has in the past added a lot of maintenance burden and isn't frequently used. Take a look at the current endpoints and let me know if they meet your needs...

Otherwise the code and pattern looks good, so I could cleanup and move all the JSON api endpoints more towards this style, if there is enough interest in using the JSON.

mark-davenport-fountain commented 1 year ago

I saw those to endpoints but they didn't meed my needs since they didn't have the data clearly laid out to consume. The enriched_debug_data has it a little closer, but I wanted to have an endpoint that made it clear and simple for how to use the data that is presented in JSON.

For example: I assume the data array has all the information in it, but I'm not sure how to consume it. The web UI has it pretty clearly spelled out so I was looking for something similar to relate to.

    "./config/routes.rb": {
      "first_updated_at": 1676067851,
      "last_updated_at": 1676300488,
      "file_hash": "c6975f2631903dd08b43afec8de92306",
      "data": [
        null,
        17,
        0,
        null,
        null,
        null,
        null,
      ]
    }
  },

Runtime relevant lines is easier to understand, and coverage also is her but still not clear how to tell what they mean.

   {
    "filename": "/Users/mark/fountain/coverband_demo/app/controllers/application_controller.rb",
    "runtime_relavant_lines": null,
    "coverage": [4, 4, 0, null, null, null, 4, 0, null, null, null],
    "last_updated_at": "2023-02-13T09:01:28.000-06:00",
    "first_updated_at": "2023-02-10T16:24:36.000-06:00",
    "never_loaded": false
  },

I could probably use these endpoints if they add some good docs on how to make sense of the responses.

danmayer commented 1 year ago

OK, I think we can take your approach, could you resolve the conflict and ensure that it has all the data for your use cases... If so I am good with pulling this in, after it is merged, I will likely move over all the other json endpoints to follow some of these patterns.

@mark-davenport-fountain let me know if you run into any issues.

mark-davenport-fountain commented 1 year ago

Sounds good. I 'll get this fixed up shortly. I as out on vacation last week. I'll let you know!

mark-davenport-fountain commented 1 year ago

I've got this ready now. @danmayer please take a look, and may want to approve the workflow to run.

danmayer commented 1 year ago

@mark-davenport-fountain approved workflow run

mark-davenport-fountain commented 1 year ago

Looks like I missed running standardrb on one of my files. I've got that done now. Should be green on the next run. @danmayer

mark-davenport-fountain commented 1 year ago

Just a small bump on this @danmayer.

danmayer commented 1 year ago

hmm still another standard error

mark-davenport-fountain commented 1 year ago

Alright - got that one too. I was running into some trouble with standardrb locally. I'm seeing this same scanning error locally as I'm seeing in CI, but I guess it's not causing any actual problems. Looks like to fix it you'd need to unlock and update standardrb. Should be good to go now anyhow.

danmayer commented 1 year ago

merged, thanks I will update the changes and get this in a RC release sometime soon

mark-davenport-fountain commented 1 year ago

Than you @danmayer

danmayer commented 1 year ago

ok so you can install coverband RC release 5.2.6.rc.4 to test out this functionality let me know if you have any issues @mark-davenport-fountain