Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API EndPoint for BAN Address scoring #323

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/api/legacy-routes.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const Papa = require('papaparse')
const {snakeCase, mapKeys} = require('lodash')
const fetch = require('../util/fetch.cjs')
const currentDate = require('../util/local-current-date.cjs')
const {computeFilteredStats} = require('../models/ban.cjs')
const {computeFilteredStats, computeScoringStats} = require('../models/ban.cjs')
const {
getCommune,
getCommuneData,
Expand Down Expand Up @@ -414,6 +414,15 @@ app.post(
}
}))

app.get(
'/ban/stats-scoring',
w(async (req, res) => {
const scores = await computeScoringStats()
return res
.send(scores)
})
)

// BAN Carto Tiles
app.get(
'/tiles/ban/:z/:x/:y.pbf',
Expand Down
127 changes: 126 additions & 1 deletion lib/models/ban.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,129 @@ async function computeFilteredStats(codesCommune) {
return {total, ban, bal}
}

module.exports = {computeStats, computeFilteredStats}
async function computeScoringStats() {
const toPercent = (value, total) => Math.round((value / total) * 100)

async function getStatistics() {
const guichetAddr = 'ign-api-gestion-ign'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sur l'explorer le guichet adresse est sourcé avec

Suggested change
const guichetAddr = 'ign-api-gestion-ign'
const guichetAddr = 'ign-api-gestion-municipal_administration'

const adresseCollection = mongo.db.collection('numeros')

// Total des documents 'adresse'
const totalDocuments = await adresseCollection.countDocuments()

// Nb-bal: Nombre de documents 'Adresse' avec source 'bal'
const nbBal = await adresseCollection.countDocuments({sources: 'bal'})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sur l'adresse, le champ sources est un array

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normalement, quand on test sur une unique valeur, il me semble que cette syntaxe est OK ?
(Mais on peut effectivement utiliser {sources: {$all: ['bal']}} pour être plus explicite ?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C'est bien de l'avoir en tète. Mais pas un souci sur une condition égalité /in simple (et pour les sources bal normalement il est seul)


// Score-5: Nombre de documents 'Adresse' avec source 'bal', certificationCommune à true, parcelles non vides et banId non null
const score5 = await adresseCollection.countDocuments({
sources: 'bal',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idem pour le champ sources

certificationCommune: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Le champ certificationCommune n'existe pas sur l'adresse. Il s'agit du champ certifie, certificationCommune étant un champ d'adressesOriginales, le champ qui permet de garder la source originale de l'adresse.

parcelles: {$not: {$size: 0}},
banId: {$ne: null},
})

// Score-4: Nombre de documents 'Adresse' avec source 'bal', certificationCommune à true ou parcelles non vides et banId non null (mais pas les deux à la fois)
const score4 = await adresseCollection.countDocuments({
sources: 'bal',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idem pour le champ sources

$or: [
{certificationCommune: true},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idem pour certificationCommune

{parcelles: {$not: {$size: 0}}, banId: {$ne: null}},
],
$nor: [
{certificationCommune: true, parcelles: {$not: {$size: 0}}, banId: {$ne: null}},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idem pour certificationCommune

]
})

// Score-3: Nombre de documents 'Adresse' avec source 'bal', certificationCommune à false, parcelles vides et banId non null
const score3 = await adresseCollection.countDocuments({
sources: 'bal',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idem pour le champ sources

certificationCommune: {$ne: true},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idem pour certificationCommune

parcelles: {$size: 0},
banId: {$ne: null},
})

// Score-3-Legacy: Nombre de documents 'Adresse' avec source 'bal', certificationCommune à true ou parcelles non vides et banId à null (mais pas les deux à la fois)
const score3Legacy = await adresseCollection.countDocuments({
sources: 'bal',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idem pour le champ sources

$or: [
{certificationCommune: true},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idem pour certificationCommune

{parcelles: {$not: {$size: 0}}, banId: null}
],
$nor: [
{certificationCommune: true, parcelles: {$not: {$size: 0}}, banId: null}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idem pour certificationCommune

]
})

// Score-2: Nombre de documents 'Adresse' avec source 'bal', certificationCommune à false, parcelles vides et banId à null
const score2 = await adresseCollection.countDocuments({
sources: 'bal',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idem pour le champ sources

certificationCommune: {$ne: true},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idem pour certificationCommune

parcelles: {$size: 0},
banId: null,
})

// Score-1: Nombre de documents 'Adresse' avec source 'Guichet Adresse' et 'certifie' à true
const score1 = await adresseCollection.countDocuments({sources: guichetAddr, certifie: true})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idem pour le champ sources


const score0 = totalDocuments - score1 - score2 - score3 - score4 - score5
const score0Legacy = totalDocuments - score1 - score2 - score3Legacy - score4 - score5

const nbBalPercent = toPercent(nbBal, totalDocuments)
const score5Percent = toPercent(score5, totalDocuments)
const score4Percent = toPercent(score4, totalDocuments)
const score3Percent = toPercent(score3, totalDocuments)
const score3LegacyPercent = toPercent(score3Legacy, totalDocuments)
const score2Percent = toPercent(score2, totalDocuments)
const score1Percent = toPercent(score1, totalDocuments)
const score0Percent = 100 - score1Percent - score2Percent - score3Percent - score4Percent - score5Percent
const score0LegacyPercent = 100 - score1Percent - score2Percent - score3LegacyPercent - score4Percent - score5Percent

return {
'pre-score': {
total: totalDocuments,
'source-bal': nbBal,
score0: score0Legacy,
score1,
score2,
score3: score3Legacy,
score4,
score5,
},
score: {
total: totalDocuments,
'source-bal': nbBal,
score0,
score1,
score2,
score3,
score4,
score5,
},
'pre-score-percent': {
total: totalDocuments,
'source-bal': nbBalPercent,
score0: score0LegacyPercent,
score1: score1Percent,
score2: score2Percent,
score3: score3LegacyPercent,
score4: score4Percent,
score5: score5Percent,
},
'score-percent': {
total: totalDocuments,
'source-bal': nbBalPercent,
score0: score0Percent,
score1: score1Percent,
score2: score2Percent,
score3: score3Percent,
score4: score4Percent,
score5: score5Percent,
}

}
}

return getStatistics()
}

module.exports = {computeStats, computeFilteredStats, computeScoringStats}
Loading