Skip to content

Commit

Permalink
feat(ts): Mise en place du typage dans fonctions JavaScript (#3)
Browse files Browse the repository at this point in the history
Première étape visant à rendre plus robuste le développement et l'exécution des fonctions JavaScript employées par le map-reduce de MongoDB, en typant puis transpilant ces fonctions à l'aide de TypeScript.

Pour tester la chaine de transpilation (provisoire):

```sh
$ ./test_js_functions.sh
```
  • Loading branch information
adrienjoly authored Apr 23, 2020
1 parent c282f85 commit c31baca
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 29 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Solution logicielle pour la détection anticipée d'entreprises en difficulté
- frontend vuetify
- mongodb

## Dépendances / pré-requis

- `npx` (installé avec [Node.js](https://nodejs.org/)), pour lancer la transpilation des fichiers TypeScript vers JavaScript

## Installation

```bash
Expand Down
43 changes: 20 additions & 23 deletions dbmongo/js/common/raison_sociale.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
function raison_sociale (
denomination_unite_legale,
nom_unite_legale,
nom_usage_unite_legale,
prenom1_unite_legale,
prenom2_unite_legale,
prenom3_unite_legale,
prenom4_unite_legale
){

if (!nom_usage_unite_legale){
var nom_usage_unite_legale = ''
} else {
var nom_usage_unite_legale = nom_usage_unite_legale + '/'
}
var raison_sociale = denomination_unite_legale ||
(nom_unite_legale + '*' + nom_usage_unite_legale +
prenom1_unite_legale + ' ' +
(prenom2_unite_legale || '') + ' ' +
(prenom3_unite_legale || '') + ' ' +
(prenom4_unite_legale || '') + ' ').trim() + '/'

return(raison_sociale)
function raison_sociale(denomination_unite_legale, nom_unite_legale, nom_usage_unite_legale, prenom1_unite_legale, prenom2_unite_legale, prenom3_unite_legale, prenom4_unite_legale) {
if (!nom_usage_unite_legale) {
var nom_usage_unite_legale = "";
}
else {
var nom_usage_unite_legale = nom_usage_unite_legale + "/";
}
var raison_sociale = denomination_unite_legale ||
(nom_unite_legale +
"*" +
nom_usage_unite_legale +
prenom1_unite_legale +
" " +
(prenom2_unite_legale || "") +
" " +
(prenom3_unite_legale || "") +
" " +
(prenom4_unite_legale || "") +
" ").trim() + "/";
return raison_sociale;
}
34 changes: 34 additions & 0 deletions dbmongo/js/common/raison_sociale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
declare function debug(string); // supported by jsc, to print in stdout

function raison_sociale(
denomination_unite_legale: string,
nom_unite_legale: string,
nom_usage_unite_legale: string,
prenom1_unite_legale: string,
prenom2_unite_legale: string,
prenom3_unite_legale: string,
prenom4_unite_legale: string
): string {
if (!nom_usage_unite_legale) {
var nom_usage_unite_legale = "";
} else {
var nom_usage_unite_legale = nom_usage_unite_legale + "/";
}
var raison_sociale =
denomination_unite_legale ||
(
nom_unite_legale +
"*" +
nom_usage_unite_legale +
prenom1_unite_legale +
" " +
(prenom2_unite_legale || "") +
" " +
(prenom3_unite_legale || "") +
" " +
(prenom4_unite_legale || "") +
" "
).trim() + "/";

return raison_sociale;
}
4 changes: 2 additions & 2 deletions dbmongo/js/reduce.algo2/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ function map () {

if (v.scope == "etablissement") {
let o = f.outputs(v, serie_periode)
let output_array = o[0]
let output_indexed = o[1]
let output_array = o[0] // [ OutputValue ] // in chronological order
let output_indexed = o[1] // { Periode -> OutputValue } // OutputValue: cf outputs()

// Les periodes qui nous interessent, triées
var periodes = Object.keys(output_indexed).sort((a,b) => (a >= b))
Expand Down
19 changes: 18 additions & 1 deletion dbmongo/lib/engine/js/loadJS.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)

// Reads all .js files in the current folder
// and encodes them as strings maps in jsFunctions.go
func main() {
func bundleJsFunctions() {
jsRootDir := filepath.Join("..", "..", "js")
folders, err := ioutil.ReadDir(jsRootDir)
if err != nil {
Expand Down Expand Up @@ -54,3 +55,19 @@ func main() {
}
out.Write([]byte("}\n"))
}

func transpileTsFunctions() {
// TODO: also transpile any other TS files
cmd := exec.Command("npx", "typescript", "../../js/common/raison_sociale.ts") // output: dbmongo/js/common/raison_sociale.js
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
}

func main() {
transpileTsFunctions() // convert *.ts files to .js
bundleJsFunctions() // bundle *.js files to jsFunctions.go
}
2 changes: 0 additions & 2 deletions dbmongo/lib/engine/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"github.com/signaux-faibles/gournal"
)

//go:generate go run js/loadJS.go

// Db connecteur exportable
var Db DB

Expand Down
3 changes: 2 additions & 1 deletion dbmongo/lib/engine/reduce.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func Reduce(batch AdminBatch, algo string, types []string) error {
dbTemp := "reduce" + strconv.Itoa(i)

functions := scope["f"].(map[string]bson.JavaScript)
// Injection des fonctions JavaScript pour exécution par MongoDB
job := &mgo.MapReduce{
Map: functions["map"].Code,
Reduce: functions["reduce"].Code,
Expand Down Expand Up @@ -178,7 +179,7 @@ func reduceFinalAggregation(tempDatabase *mgo.Database, tempCollection, outDatab
pipeline = append(pipeline, []bson.M{
bson.M{
"$unwind": bson.M{
"path": "$value",
"path": "$value",
"preserveNullAndEmptyArrays": false,
},
},
Expand Down
7 changes: 7 additions & 0 deletions test_js_functions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
echo "Transpiling TypeScript files, and generating the jsFunctions.go bundle..."
cd dbmongo/lib/engine
go generate -x
cd -
echo "Running tests against the JS files (including the ones transpiled from TS)..."
cd dbmongo/js/test/ && ./test_common.sh
echo "✅ Tests passed."

0 comments on commit c31baca

Please sign in to comment.