File size: 8,203 Bytes
3f268e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 * Copyright 2017 Google Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

const cwd = process.cwd();
const fs = require('fs');
const moduleAnalyse = require(cwd + '/src/analyse');
const global = {
  ws: {},
  states: {},
  repositories: {},
  series: {},
  debug: false,
  version: undefined,
  config: require(cwd + '/configs/dana.js'),
  projectsConfig: {},
  projects: {},
  admin: {},
  emailIsEnabled: true,
  comparesConfig: {},
};

if (!fs.existsSync(cwd + '/configs/db')) {
  fs.mkdirSync(cwd + '/configs/db');
}

const ModuleFiles = require(cwd + '/src/files');
global.admin = new ModuleFiles(cwd + '/configs/db/admin', 200);

if (global.admin.existsSync('projects')) {
  global.projectsConfig = global.admin.readSync('projects');
}

let k = Object.keys(global.projectsConfig);
for (let ii = 0; ii < k.length; ii++) {
  global.projects[k[ii]] = {};
  if (!fs.existsSync(cwd + '/configs/db/' + k[ii])) {
    fs.mkdirSync(cwd + '/configs/db/' + k[ii]);
  }
  global.projects[k[ii]].series =
    new ModuleFiles(cwd + '/configs/db/' + k[ii] + '/series', 10);
  global.projects[k[ii]].comments =
    new ModuleFiles(cwd + '/configs/db/' + k[ii] + '/comments', 10);
  global.projects[k[ii]].infos =
    new ModuleFiles(cwd + '/configs/db/' + k[ii] + '/infos', 200);
}

if (global.config === undefined) {
  console.log('ERROR Invalid configuration file, undefined');
  process.exit(2);
}

if (global.admin.existsSync('compares')) {
  global.comparesConfig = global.admin.readSync('compares');
}

// serieUpdateAnalyse (projectId, serieId, analyse)
// -> receiveUpdateAnalyseDone (projectId, serieId, analyse)
function serieUpdateAnalyse(req) {
  let projectId = req.projectId;
  let serieId = req.serieId;
  let analyse = req.analyse;
  if (global.debug) console.log('serieUpdateAnalyse', req);
  if (analyse === undefined) {
    let e = 'serieUpdateAnalyse analyse undefined';
    console.error(e);
    console.log('serverError', e);
    return;
  }
  if (projectId === undefined) {
    let e = 'serieUpdateAnalyse projectId undefined';
    console.error(e);
    console.log('serverError', e);
    return;
  }
  if (global.projects[projectId] === undefined) {
    let e = 'serieUpdateAnalyse projectId not in projects';
    console.error(e, projectId, serieId);
    console.log('serverError', e);
    return;
  }
  if (global.projects[projectId].series === undefined) {
    let e = 'serieUpdateAnalyse projectId series undefined';
    console.error(e, projectId, serieId);
    console.log('serverError', e);
    return;
  }
  if (!global.projects[projectId].series.existsSync(serieId)) {
    let e = 'serieUpdateAnalyse serieId doesnt exist';
    console.error(e, projectId, serieId);
    console.log('serverError', e);
    return;
  }
  let serie = global.projects[projectId].series.readSync(serieId);

  serie.analyse = analyse;

  if (serie.analyse.benchmark) {
    moduleAnalyse.analyse(serie);

    let st = serie.state.analyse;

    // convert an existing database
    if (st === 'none') st = undefined;
    if (st === 'assigned') st = 'regressionAssigned';
    if (st === 'new') st = 'regressionConfirmed';
    if (st === 'wontfix') st = 'regressionIntended';

    if (st !== 'similarNoisy') {
      if (serie.analyseResult.summary.status === 'regression') {
        if (st === undefined)
          serie.state.analyse = 'regressionNeedstriage';
        else if (st.indexOf('regression') === -1)
          serie.state.analyse = 'regressionNeedstriage';
      } else {
        serie.assignee.analyse = undefined;
        if (serie.analyseResult.summary.status === 'improvement') {
          if (st === undefined)
            serie.state.analyse = 'improvementNeedstriage';
          else if (st.indexOf('improvement') === -1)
            serie.state.analyse = 'improvementNeedstriage';
        } else {
          if (st === undefined)
            serie.state.analyse = 'similarNeedstriage';
          else if (st.indexOf('similar') === -1)
            serie.state.analyse = 'similarNeedstriage';
        }
      }
    } else {
      //TODO: noisy management to know if serie is back to a normal mode
    }

    // series management
    let series = global.projects[projectId].infos
      .readSync('benchmarks.series');
    if (series[serieId] === undefined) {
      let e = 'serieUpdateAnalyse series[serieId] undefined';
      console.error(e, projectId, serieId);
      console.log('serverError', e);
      return;
    }
    series[serieId].status = serie.analyseResult.summary;
    series[serieId].state = serie.state.analyse;

    global.projects[projectId].infos.writeSync('benchmarks.series', series);

    //
    // Compare management
    //
    let k = Object.keys(global.comparesConfig);
    for (let ii = 0; ii < k.length; ii++) {
      let compareId = k[ii];
      let cp = global.comparesConfig[compareId];
      if (cp.projectId !== projectId) continue;
      let cpProjectId = cp.projectId;
      if (global.projects[cpProjectId].series) {
        if (global.projects[cpProjectId].series.existsSync(serieId)) {
          let cpSerie = global.projects[cpProjectId].series
            .readSync(serieId);
          let result = moduleAnalyse.benchmarkCompare(cp, serie, cpSerie);
          if (result !== undefined) {

            let st = serie.state.compares[compareId];
            if (result.status === 'lower') {
              if (st === undefined)
                serie.state.compares[compareId] = 'lowerNeedstriage';
              else if (st.indexOf('lower') === -1)
                serie.state.compares[compareId] = 'lowerNeedstriage';
            } else {
              serie.assignee.compares[compareId] = undefined;
              if (serie.analyseResult.summary.status === 'better') {
                if (st === undefined)
                  serie.state.compares[compareId] = 'betterNeedstriage';
                else if (st.indexOf('better') === -1)
                  serie.state.compares[compareId] = 'betterNeedstriage';
              } else {
                if (st === undefined)
                  serie.state.compares[compareId] = 'similarNeedstriage';
                else if (st.indexOf('similar') === -1)
                  serie.state.compares[compareId] = 'similarNeedstriage';
              }
            }

            // serie file
            if (serie.compares === undefined) serie.compares = {};
            serie.compares[compareId].result = result;

            // compares file
            let compare = global.projects[projectId].infos
              .readSync('benchmarks.compare_' + compareId);
            if (compare[serieId] === undefined) {
              let e = 'serieUpdateAnalyse compare[serieId] undefin';
              console.error(e, projectId, serieId);
              console.log('serverError', e);
              return;
            }
            compare[serieId].result = result;
            compare[serieId].state = serie.state.compares[compareId];

            global.projects[projectId].infos
              .writeSync('benchmarks.compare_' + compareId, compare);
          }
        }
      }
    }
  }

  global.projects[projectId].series.writeSync(serieId, serie);
}

var req = {};
var kProjects = Object.keys(global.projects);
for (var ii = 0; ii < kProjects.length; ii++) {
  req.projectId = kProjects[ii];
  let series = global.projects[req.projectId].infos
    .readSync('benchmarks.series');
  var kSeries = Object.keys(series);
  for (var ii = 0; ii < kSeries.length; ii++) {
    req.serieId = kSeries[ii];
    req.analyse = {
      benchmark: {
        range: "5%",
        trend: "higher",
        required: 2
      }
    };
    console.log(req)
    serieUpdateAnalyse(req);
  }
}