1
1
package appunlynx
2
2
3
3
import (
4
+ "errors"
4
5
"os"
6
+ "regexp"
7
+ "strconv"
8
+ "strings"
5
9
6
- "github.com/btcsuite/goleveldb/leveldb/errors"
7
10
"github.com/lca1/unlynx/lib"
8
- "github.com/lca1/unlynx/services/default "
11
+ "github.com/lca1/unlynx/services"
9
12
"go.dedis.ch/onet/v3"
10
13
"go.dedis.ch/onet/v3/app"
11
14
"go.dedis.ch/onet/v3/log"
12
15
"gopkg.in/urfave/cli.v1"
13
- "regexp"
14
- "strconv"
15
- "strings"
16
16
)
17
17
18
18
// BEGIN CLIENT: QUERIER ----------
19
- func startQuery (el * onet.Roster , proofs bool , sum []string , count bool , whereQueryValues []libunlynx.WhereQueryAttribute , predicate string , groupBy []string ) {
20
- client := servicesunlynxdefault .NewUnLynxClient (el .List [0 ], strconv .Itoa (0 ))
19
+ func startQuery (el * onet.Roster , proofs bool , sum []string , count bool , whereQueryValues []libunlynx.WhereQueryAttribute , predicate string , groupBy []string ) error {
20
+ client := servicesunlynx .NewUnLynxClient (el .List [0 ], strconv .Itoa (0 ))
21
21
22
- // Generate Survey Data
23
22
nbrDPs := make (map [string ]int64 )
24
23
//how many data providers for each server
25
24
for _ , server := range el .List {
26
25
nbrDPs [server .String ()] = 1 // 1 DP for each server
27
26
}
28
27
29
- surveyID , err := client .SendSurveyCreationQuery (el , servicesunlynxdefault .SurveyID ("" ), nil , nbrDPs , proofs , true , sum , count , whereQueryValues , predicate , groupBy )
28
+ surveyID , err := client .SendSurveyCreationQuery (el , servicesunlynx .SurveyID ("" ), nil , nbrDPs , proofs , true , sum , count , whereQueryValues , predicate , groupBy )
30
29
if err != nil {
31
- log . Fatal ( "Service did not start." , err )
30
+ return err
32
31
}
33
32
34
33
grp , aggr , err := client .SendSurveyResultsQuery (* surveyID )
35
34
if err != nil {
36
- log . Fatal ( "Service could not output the results." )
35
+ return errors . New ( "service could not output the results: " + err . Error () )
37
36
}
38
37
39
38
// Print Output
@@ -43,9 +42,10 @@ func startQuery(el *onet.Roster, proofs bool, sum []string, count bool, whereQue
43
42
for i := range tabVerify {
44
43
log .Lvl1 (i , ")" , (* grp )[i ], "->" , (* aggr )[i ])
45
44
}
45
+ return nil
46
46
}
47
47
48
- func runUnLynx (c * cli.Context ) error {
48
+ func runUnLynx (c * cli.Context ) {
49
49
tomlFileName := c .String ("file" )
50
50
51
51
proofs := c .Bool ("proofs" )
@@ -58,14 +58,12 @@ func runUnLynx(c *cli.Context) error {
58
58
groupBy := c .String ("groupBy" )
59
59
60
60
el , err := openGroupToml (tomlFileName )
61
- if err != nil {
62
- return err
63
- }
61
+ log .ErrFatal (err , "Could not open group toml." )
64
62
65
- sumFinal , countFinal , whereFinal , predicateFinal , groupByFinal := parseQuery (el , sum , count , whereQueryValues , predicate , groupBy )
66
- startQuery (el , proofs , sumFinal , countFinal , whereFinal , predicateFinal , groupByFinal )
63
+ sumFinal , countFinal , whereFinal , predicateFinal , groupByFinal , err := parseQuery (el , sum , count , whereQueryValues , predicate , groupBy )
67
64
68
- return nil
65
+ err = startQuery (el , proofs , sumFinal , countFinal , whereFinal , predicateFinal , groupByFinal )
66
+ log .ErrFatal (err )
69
67
}
70
68
71
69
func openGroupToml (tomlFileName string ) (* onet.Roster , error ) {
@@ -79,33 +77,30 @@ func openGroupToml(tomlFileName string) (*onet.Roster, error) {
79
77
}
80
78
81
79
if len (el .Roster .List ) <= 0 {
82
- return nil , errors .New ("Empty or invalid unlynx group file:" + tomlFileName )
80
+ return nil , errors .New ("empty or invalid unlynx group file:" + tomlFileName )
83
81
}
84
82
85
83
return el .Roster , nil
86
84
}
87
85
88
- func checkRegex (input , expression , errorMessage string ) {
86
+ func checkRegex (input , expression string ) bool {
89
87
var aux = regexp .MustCompile (expression )
90
-
91
- correct := aux .MatchString (input )
92
-
93
- if ! correct {
94
- log .Fatal (errorMessage )
95
- }
88
+ return aux .MatchString (input )
96
89
}
97
90
98
- func parseQuery (el * onet.Roster , sum string , count bool , where , predicate , groupBy string ) ([]string , bool , []libunlynx.WhereQueryAttribute , string , []string ) {
91
+ func parseQuery (el * onet.Roster , sum string , count bool , where , predicate , groupBy string ) ([]string , bool , []libunlynx.WhereQueryAttribute , string , []string , error ) {
99
92
100
93
if sum == "" || (where != "" && predicate == "" ) || (where == "" && predicate != "" ) {
101
- log . Fatal ( "Wrong query! Please check the sum, where and the predicate parameters" )
94
+ return nil , false , nil , "" , nil , errors . New ( "wrong query! please check the sum, where and the predicate parameters" )
102
95
}
103
96
104
97
sumRegex := "{s[0-9]+(,\\ s*s[0-9]+)*}"
105
98
whereRegex := "{(w[0-9]+(,\\ s*[0-9]+))*(,\\ s*w[0-9]+(,\\ s*[0-9]+))*}"
106
99
groupByRegex := "{g[0-9]+(,\\ s*g[0-9]+)*}"
107
100
108
- checkRegex (sum , sumRegex , "Error parsing the sum parameter(s)" )
101
+ if ! checkRegex (sum , sumRegex ) {
102
+ return nil , false , nil , "" , nil , errors .New ("error parsing the sum parameter(s)" )
103
+ }
109
104
sum = strings .Replace (sum , " " , "" , - 1 )
110
105
sum = strings .Replace (sum , "{" , "" , - 1 )
111
106
sum = strings .Replace (sum , "}" , "" , - 1 )
@@ -120,11 +115,13 @@ func parseQuery(el *onet.Roster, sum string, count bool, where, predicate, group
120
115
}
121
116
122
117
if ! check {
123
- log . Fatal ( "No 'count' attribute in the sum variables" )
118
+ return nil , false , nil , "" , nil , errors . New ( "no 'count' attribute in the sum variables" )
124
119
}
125
120
}
126
121
127
- checkRegex (where , whereRegex , "Error parsing the where parameter(s)" )
122
+ if ! checkRegex (where , whereRegex ) {
123
+ return nil , false , nil , "" , nil , errors .New ("error parsing the where parameter(s)" )
124
+ }
128
125
where = strings .Replace (where , " " , "" , - 1 )
129
126
where = strings .Replace (where , "{" , "" , - 1 )
130
127
where = strings .Replace (where , "}" , "" , - 1 )
@@ -140,20 +137,22 @@ func parseQuery(el *onet.Roster, sum string, count bool, where, predicate, group
140
137
} else { // if it is a value
141
138
value , err := strconv .Atoi (tmp [i ])
142
139
if err != nil {
143
- log . Fatal ( "Something wrong with the where value" )
140
+ return nil , false , nil , "" , nil , err
144
141
}
145
142
146
143
whereFinal = append (whereFinal , libunlynx.WhereQueryAttribute {Name : variable , Value : * libunlynx .EncryptInt (el .Aggregate , int64 (value ))})
147
144
}
148
145
}
149
146
150
- checkRegex (groupBy , groupByRegex , "Error parsing the groupBy parameter(s)" )
147
+ if ! checkRegex (groupBy , groupByRegex ) {
148
+ return nil , false , nil , "" , nil , errors .New ("error parsing the groupBy parameter(s)" )
149
+ }
151
150
groupBy = strings .Replace (groupBy , " " , "" , - 1 )
152
151
groupBy = strings .Replace (groupBy , "{" , "" , - 1 )
153
152
groupBy = strings .Replace (groupBy , "}" , "" , - 1 )
154
153
groupByFinal := strings .Split (groupBy , "," )
155
154
156
- return sumFinal , count , whereFinal , predicate , groupByFinal
155
+ return sumFinal , count , whereFinal , predicate , groupByFinal , nil
157
156
}
158
157
159
158
// CLIENT END: QUERIER ----------
0 commit comments