How to see the metrics' values with PMD Api? - pmd

I am using the PMD Java API in order to create some statistics.
I am interested in seeing what are the values of some metrics of interest, for example:
CouplingBetweenObjects, ExcessiveClassLength, CyclomaticComplexity, etc.
The problem is that I could not find any report format which displays the metric's value explicitly.
I could parse the output and deduce the value for some of those rules, but I cannot do that for others.
Does anyone know what to do in order to get that value?
For example, this is a json report format:
{
"beginline": 1,
"begincolumn": 1,
"endline": 181,
"endcolumn": 2,
"description": "High amount of different objects as members denotes a high coupling",
"rule": "CouplingBetweenObjects",
"ruleset": "Design",
"priority": 3,
"externalInfoUrl": "https://pmd.github.io/pmd-6.46.0/pmd_rules_java_design.html#couplingbetweenobjects"
},
or
{
"beginline": 10,
"begincolumn": 1,
"endline": 44,
"endcolumn": 1,
"description": "The class \u0027Factory\u0027 has a total cyclomatic complexity of 13 (highest 12).",
"rule": "CyclomaticComplexity",
"ruleset": "Design",
"priority": 3,
"externalInfoUrl": "https://pmd.github.io/pmd-6.46.0/pmd_rules_java_design.html#cyclomaticcomplexity"
},
The cyclomatic complexity can be seen that it's 13 and I can write some code to get that number, but I cannot do that for the coupling between objects.

Related

MongoDB find lowest missing value

I have a collection in MongoDB that looks something like:
{
"foo": "something",
"tag": 0,
},
{
"foo": "bar",
"tag": 1,
},
{
"foo": "hello",
"tag": 0,
},
{
"foo": "world",
"tag": 3,
}
If we consider this example, there are entries in the collection with tag of value 0, 1 or 3 and these aren't unique values, tag value can be repeated. My goal is to find that 2 is missing. Is there a way to do this with a query?
Query1
in the upcoming mongodb 5.2 we will have sort on arrays that could do this query easier without set operation but this will be ok also
group and find the min,max and all the values
take the range(max-min)
the missing are (setDifference range_above tags)
and from them you take only the smallest => 2
Test code here
aggregate(
[{"$group":
{"_id":null,
"min":{"$min":"$tag"},
"max":{"$max":"$tag"},
"tags":{"$addToSet":"$tag"}}},
{"$project":
{"_id":0,
"missing":
{"$min":
{"$setDifference":
[{"$range":[0, {"$subtract":["$max", "$min"]}]}, "$tags"]}}}}])
Query2
in Mongodb 5 (the current version) we can use also $setWindowFields
sort by tag, add the dense-rank(same values=same rank), and the min
then find the difference of tag-min
and then filter those that this difference < rank
and find the max of them (max of the tag that are ok)
increase 1 to find the one missing
*test it before using it to be sure, i tested it 3-4 times seemed ok,
for big collection if you have many different tags, this is better i think. (the above addtoset can cause memory problems)
Test code here
aggregate(
[{"$setWindowFields":
{"output":{"rank":{"$denseRank":{}}, "min":{"$first":"$tag"}},
"sortBy":{"tag":1}}},
{"$set":{"difference":{"$subtract":["$tag", "$min"]}}},
{"$match":{"$expr":{"$lt":["$difference", "$rank"]}}},
{"$group":{"_id":null, "last":{"$max":"$tag"}}},
{"$project":{"_id":0, "missing":{"$add":["$last", 1]}}}])

Data structure MongoDB for Insurance policies

I am trying to have some feedback about the best way to organize data for an insurance company (and in general).
I am using MongoDB and i'm unsure about a thing.
lets say i have different prices for different coverages on the same policy, on top of that various cases based on the annual income of the company who will sign the policy.
#1. Coverage -> 5.000.000 - Annual income -> 1.000.000 - price -> 500
#2. Coverage -> 5.000.000 - Annual income -> 1.500.000 - price -> 550
Then i can add extensions to that policy (in this case just 4 types)
#1.Basic extension -> 120 - Advanced extension -> 150
#1.Basic extension -> 150 - Advanced extension -> 170
I was thing about saving every policy in a collection, everyone with their characteristics.
So, lets take this one as an example.
Collection: policies
Entry:
id
name: name of the policy as it is written in the docs
prices: [{id: 0, price: 500, income_range: [0, 1.000.000], coverage_range: [0,5.000.000], extensions: [120, 150]}, {{id: 1, price: 550, income_range: [1.000.001, 1.500.000], coverage_range: [0,5.000.000], extensions: [150, 170]}
other_general_stuff
OR
Collection: policies
Entry:
id
name: name of the policy as it is written in the docs
prices: [{id: 0, price: 500, income_range: 0, coverage_range: 0, extensions: l1}, {{id: 1, price: 550, income_range: 1, coverage_range: 0, extensions: l2}
income_ranges: [{id: 0, range: [0, 1.000.000]}, {id: 1, range: [1.000.001, 1.500.000]}]
cover_ranges: [{id: 0, range: [0, 5.000.000]}, {id: 1, range: [5.000.000, 7.000.000]}]
extensions: {l1: {basic: 120, adv: 150}, l2: {basic: 150, adv: 170}}
I mean considering there are different products this i just a tiny example, but what concerns me is for ex. the ranges property, is it ok in this type of db to scructure it like the second example, is there a way of accessing those property in an easy way, or is it better to write the entire thing directly into the 'prices field'.
I have not much experience, so I am trying to figure out what is the best practice to follow.
I know that there are many possible solutions, but is this entirely wrong? Can someone give me a feedback or point me to some nice resources.
Thank you

Dynamic Json Keys in Scala

I'm new to scala (from python) and I'm trying to create a Json object that has dynamic keys. I would like to use some starting number as the top-level key and then combinations involving that number as second-level keys.
From reading the play-json docs/examples, I've seen how to build these nested structures. While that will work for the top-level keys (there are only 17 of them), this is a combinatorial problem and the power set contains ~130k combinations that would be the second-level keys so it isn't feasible to list that structure out. I also saw the use of a case class for structures, however the parameter name becomes the key in those instances which is not what I'm looking for.
Currently, I'm considering using HashMaps with the MultiMap trait so that I can map multiple combinations to the same original starting number and then second-level keys would be the combinations themselves.
I have python code that does this, but it takes 3-4 days to work through up-to-9-number combinations for all 17 starting numbers. The ideal final format would look something like below.
Perhaps it isn't possible to do in scala given the goal of using immutable structures. I suppose using regex on a string of the output might be an option as well. I'm open to any solutions regarding data structures to hold the info and how to approach the problem. Thanks!
{
"2": {
"(2, 3, 4, 5, 6)": {
"best_permutation": "(2, 4, 3, 5, 6)",
"amount": 26.0
},
"(2, 4, 5, 6)": {
"best_permutation": "(2, 5, 4, 6)",
"amount": 21.0
}
},
"3": {
"(3, 2, 4, 5, 6)": {
"best_permutation": "(3, 4, 2, 5, 6)",
"amount": 26.0
},
"(3, 4, 5, 6)": {
"best_permutation": "(3, 5, 4, 6)",
"amount": 21.0
}
}
}
EDIT:
There is no real data source other than the matrix I'm using as my lookup table. I've posted the links to the lookup table I'm using and the program if it might help, but essentially, I'm generating the content myself within the code.
For a given combination, I have a function that basically takes the first value of the combination (which is to be the starting point) and then uses the tail of that combination to generate a permutation.
After that I prepend the starting location to the front of each permutation and then use sliding(2) to work my way through the permutation looking up the amount which is in a breeze.linalg.DenseMatrix by using the two values to index the matrix I've provided below and summing the amounts gathered by indexing the matrix with the two sliding values (subtracting 1 from each value to account for the 0-based indexing).
At this point, it is just a matter of gathering the information (starting_location, combination, best_permutation and the amount) and constructing the nested HashMap. I'm using scala 2.11.8 if it makes any difference.
MATRIX: see here.
PROGRAM:see here.

Update an element in an array which is within an array in mongodb

Schema
{
_id: 'Unique_id',
array_key: [{
'm1': [7, 5, 6, 6, 1, 2, 3]
},
{
'm2': [1, 5, 3, 7, 9, 2, 3]
},
]
}
I want to update any element in array_keys m1 field's array.
{'m1': [7,5,6,6,1,2,3]}
E.g. I want to update 6 at index 3 in that array.
I found this in MongoDB's official documentation
It seems that one is allowed to add value at specific position but not update it (going by that documentation).
~p.s Later I'd try to implement this using mongoose
Update : The array_key in the schema will hold daily work hour for an employee which will be represented in months format on the frontend. m1 and m2 are used in that respect.

Checking data integrity with counts

I have a field with a dictionary in it, mapping people to numbers 0-9.
{peopleDict : {bob: 3, les: 3, meg: 8, sara: 6}}
I also have another field with another dictionary in it, which is supposed to count the number of people assigned to each number.
{countDict : {"3" : 2, "8" : 1, "6" : 1}}
So a document looks like
{peopleDict : {bob: 3, les: 3, meg: 8, sara: 6},
countDict : {"3" : 2, "8" : 1, "6" : 1}}
I am trying to write a query that tests whether countDict actually matches peopleDict for each document. I'm sure there must be a way to do this with aggregate but I'm not quite sure how.
As far as I know, you can't join data from different collections. So if you have them in separate collections you need to analyze data on application level, or redesign data structure to put all of them to single collection.