SonarQube Total LOC number has been increased immediately

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)
    ====================================================================
    => Version: 8.9.1.44547

  • what are you trying to achieve
    ===============================================
    How to calculate the total LOC ? (total projects: 70)
    What can be reason of immediate increase in total LOC ?
    How can we use Sonarqube web API to get LOC data of each project?

  • what have you tried so far to achieve this
    ================================================
    => Based on the Sonarqube documentation, For LOC of individual project , branch having largest LOC will be considered. And sum of all projects’ LOCs will be considered the total LOC.
    => Last week, we have tried noting down the LOC of each project and calculated the sum of all . My calculation summed up to 493 K, but the administration page has different total = 524K.
    => While we were thinking to monitor the change in number , In three days , the administration page shows total increased to 966K which means it’s about to reach our threshold of 1M.

I would be grateful for the response on this ticket/topic.

We have used sonarqube web-api to get “ncloc” Metric value for each of our projects.
We have 69 projects and each of them have unique project key.
Per project key , we fetched “ncloc” value.
We have considered all the projects that comes under “TRK” qualifiers.
sum of all 69 “ncloc” values is : 530586

The administration page shows total LOC : 966,651
The screeshot is attached.

Please help us find the reason of this different total. Are we looking at correct metric value (“ncloc”)?

Hello,

As said in the FAQ of SonarQube 8.9:

What is the impact on my LOCs consumption vs my license?

The LOC of your largest branch are counted toward your license limit. All other branches are ignored.

If you want to manually compute the LOCs consumption you need to take into account the LOCs scanned on your Branches. For each projects, you need to loop on the Branches and get the LOCs. Then take the max of them for each project.
If you used the API api/measures/component or api/measures/component_tree, you need to provide the branch parameter. If you don’t, you will get only the LOCs on your Main Branch.

Alex

@Alexandre_Gigleux
Thank you for the response.

We have tried getting list of projects with qualifier : “TRK”

// this loop is by project_key
for project in project_list
for branch in project
max_loc = max(list all branch locs)
loc_list.append(max_loc)

sum(loc_list)

The sum we got here is now more than the one showing on the administration page.
Script says , total LOC is : 1003746

Admin page says , total LOC is : 966,651

Now to look at the script calculation, The first step we followed was considering each project by its project key.

We have some projects with same project_name , but has different “project_key” .

Question :
The max loc will be considered of all branches per project .
But will it be per project_key or per project_name ?

Thanks

Yes

per project_key

I don’t think you should go in the database and try to guess the schema. It’s better to rely on the API we provide.

Hi @Alexandre_Gigleux ,

Thank you for the help.

We were able to find out the specific project and branch which was causing an immediate increase in LOC.

Disabled that branch and now we are good with the LOC threshold.

FYI, here is a feature we have in our backlog to help users in your situation:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.