forked from entrepreneur-interet-general/opensignauxfaibles
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers.go
205 lines (174 loc) · 6.09 KB
/
handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"encoding/json"
"errors"
"fmt"
"os"
"sort"
"time"
"github.com/cosiner/flag"
"github.com/globalsign/mgo/bson"
"opensignauxfaibles/lib/base"
"opensignauxfaibles/lib/engine"
"opensignauxfaibles/lib/parsing"
)
type importBatchHandler struct {
Enable bool // set to true by cosiner/flag if the user is running this command
BatchKey string `names:"--batch" arglist:"batch_key" desc:"Identifiant du batch à importer (ex: 1802, pour Février 2018)"`
Parsers []string `names:"--parsers" desc:"Parseurs à employer (ex: altares,cotisation)"` // TODO: tester la population de ce paramètre
NoFilter bool `names:"--no-filter" desc:"Pour procéder à l'importation même si aucun filtre n'est fourni"`
}
func (params importBatchHandler) Documentation() flag.Flag {
return flag.Flag{
Usage: "Importe des fichiers de données",
Desc: `
Effectue l'import de tous les fichiers du batch donné en paramètre.
Il est possible de limiter l'exécution à certains parsers en spécifiant la liste dans le flag "--parsers".
Répond "ok" dans la sortie standard, si le traitement s'est bien déroulé.
`,
}
}
func (params importBatchHandler) IsEnabled() bool {
return params.Enable
}
func (params importBatchHandler) Validate() error {
if params.BatchKey == "" {
return errors.New("paramètre `batch` obligatoire")
}
return nil
}
// Run importBatchHandler traite les demandes d'import par l'API
// on peut demander l'exécution de tous les parsers sans fournir d'option
// ou demander l'exécution de parsers particuliers en fournissant une liste de leurs codes.
func (params importBatchHandler) Run() error {
batch := base.AdminBatch{}
err := engine.Load(&batch, params.BatchKey)
if err != nil {
return errors.New("Batch inexistant: " + err.Error())
}
parsers, err := parsing.ResolveParsers(params.Parsers)
if err != nil {
return err
}
dataChan := engine.InsertIntoCSVs()
err = engine.ImportBatch(batch, parsers, params.NoFilter, dataChan)
if err != nil {
return err
}
printJSON("ok")
return nil
}
type checkBatchHandler struct {
Enable bool // set to true by cosiner/flag if the user is running this command
BatchKey string `names:"--batch" arglist:"batch_key" desc:"Identifiant du batch à vérifier (ex: 1802, pour Février 2018)"`
Parsers []string `names:"--parsers" desc:"Parseurs à employer (ex: altares,cotisation)"`
}
func (params checkBatchHandler) Documentation() flag.Flag {
return flag.Flag{
Usage: "Vérifie la validité d'un batch avant son importation",
Desc: `
Vérifie la validité du batch sur le point d'être importé et des fichiers qui le constituent.
Il est possible de limiter l'exécution à certains parsers en spécifiant la liste dans le flag "--parsers".
Répond avec un propriété JSON "reports" qui contient les rapports textuels de parsing de chaque fichier.
`,
}
}
func (params checkBatchHandler) IsEnabled() bool {
return params.Enable
}
func (params checkBatchHandler) Validate() error {
if params.BatchKey == "" {
return errors.New("paramètre `batch` obligatoire")
}
return nil
}
func (params checkBatchHandler) Run() error {
batch := base.AdminBatch{}
err := engine.Load(&batch, params.BatchKey)
if err != nil {
return errors.New("Batch inexistant: " + err.Error())
}
parsers, err := parsing.ResolveParsers(params.Parsers)
if err != nil {
return err
}
reports, err := engine.CheckBatch(batch, parsers)
if err != nil {
return errors.New("Erreurs détectées: " + err.Error())
}
sort.Strings(reports) // to make sure that parsed files are always listed in the same order
printJSON(bson.M{"reports": reports})
return nil
}
type purgeNotCompactedHandler struct {
Enable bool // set to true by cosiner/flag if the user is running this command
IUnderstandWhatImDoing bool `names:"--i-understand-what-im-doing" desc:"Nécessaire pour confirmer la suppression de données"`
}
func (params purgeNotCompactedHandler) Documentation() flag.Flag {
return flag.Flag{
Usage: "Vide la collection ImportedData",
Desc: `
Vide la collection "ImportedData".
Répond "ok" dans la sortie standard, si le traitement s'est bien déroulé.
`,
}
}
func (params purgeNotCompactedHandler) IsEnabled() bool {
return params.Enable
}
func (params purgeNotCompactedHandler) Validate() error {
if !params.IUnderstandWhatImDoing {
return errors.New("--i-understand-what-im-doing doit être employé pour confirmer la suppression")
}
return nil
}
func (params purgeNotCompactedHandler) Run() error {
startDate := time.Now()
err := engine.PurgeNotCompacted()
if err != nil {
return err
}
engine.LogOperationEvent("PurgeNotCompacted", startDate)
printJSON("ok")
return nil
}
type pruneEntitiesHandler struct {
Enable bool // set to true by cosiner/flag if the user is running this command
BatchKey string `names:"--batch" arglist:"batch_key" desc:"Identifiant du batch à nettoyer (ex: 1802, pour Février 2018)"`
Delete bool `names:"--delete" desc:"Nécessaire pour confirmer la suppression de données"`
}
func (params pruneEntitiesHandler) Documentation() flag.Flag {
return flag.Flag{
Usage: "Compte/supprime les entités hors périmètre",
Desc: `
Compte ou supprime dans la collection "RawData" les entités (établissements et entreprises)
non listées dans le filtre de périmètre du batch spécifié.
Répond avec un propriété JSON "count" qui vaut le nombre d'entités hors périmètre comptées ou supprimées.
`,
}
}
func (params pruneEntitiesHandler) IsEnabled() bool {
return params.Enable
}
func (params pruneEntitiesHandler) Validate() error {
if params.BatchKey == "" {
return errors.New("paramètre `batch` obligatoire")
}
return nil
}
// Count – then delete – companies from RawData that should have been
// excluded by the SIREN Filter.
func (params pruneEntitiesHandler) Run() error {
count, err := engine.PruneEntities(params.BatchKey, params.Delete)
if err == nil {
printJSON(bson.M{"count": count})
}
return err
}
func printJSON(object interface{}) {
res, _ := json.Marshal(object)
fmt.Println(string(res))
}
func parserToCSVMapping() map[string]os.File {
return nil
}