Refactor duplicate code for calling into media controls
[WebKit-https.git] / Websites / perf.webkit.org / tools / run-analysis.js
1 #!/usr/local/bin/node
2
3 const fs = require('fs');
4 const parseArguments = require('./js/parse-arguments.js').parseArguments;
5 const RemoteAPI = require('./js/remote.js').RemoteAPI;
6 const MeasurementSetAnalyzer = require('./js/measurement-set-analyzer.js').MeasurementSetAnalyzer;
7 const AnalysisResultsNotifier = require('./js/analysis-results-notifier.js').AnalysisResultsNotifier;
8 const Subprocess = require('./js/subprocess.js').Subprocess;
9 const createAdditionalBuildRequestsForTestGroupsWithFailedRequests = require('./js/retry-failed-build-requests').createAdditionalBuildRequestsForTestGroupsWithFailedRequests;
10 require('./js/v3-models.js');
11 global.PrivilegedAPI = require('./js/privileged-api.js').PrivilegedAPI;
12
13 function main(argv)
14 {
15     const options = parseArguments(argv, [
16         {name: '--server-config-json', required: true},
17         {name: '--notification-config-json', required: true},
18         {name: '--analysis-range-in-days', type: parseFloat, default: 10},
19         {name: '--seconds-to-sleep', type: parseFloat, default: 1200},
20         {name: '--max-retry-factor', type: parseFloat, default: 3},
21     ]);
22
23     if (!options)
24         return;
25
26     analysisLoop(options);
27 }
28
29 async function analysisLoop(options)
30 {
31     let secondsToSleep;
32     try {
33         const serverConfig = JSON.parse(fs.readFileSync(options['--server-config-json'], 'utf-8'));
34         const notificationConfig = JSON.parse(fs.readFileSync(options['--notification-config-json'], 'utf-8'));
35         const analysisRangeInDays = options['--analysis-range-in-days'];
36         const maximumRetryFactor = options['--max-retry-factor'];
37         secondsToSleep = options['--seconds-to-sleep'];
38         global.RemoteAPI = new RemoteAPI(serverConfig.server);
39         PrivilegedAPI.configure(serverConfig.slave.name, serverConfig.slave.password);
40
41         const manifest = await Manifest.fetch();
42         const measurementSetList = MeasurementSetAnalyzer.measurementSetListForAnalysis(manifest);
43
44         const endTime = Date.now();
45         const startTime = endTime - analysisRangeInDays * 24 * 3600 * 1000;
46         const analyzer = new MeasurementSetAnalyzer(measurementSetList, startTime, endTime, console);
47
48         console.log(`Start analyzing last ${analysisRangeInDays} days measurement sets.`);
49         await analyzer.analyzeOnce();
50
51         const testGroupsMayNeedMoreRequests = await TestGroup.fetchAllThatMayNeedMoreRequests();
52         await createAdditionalBuildRequestsForTestGroupsWithFailedRequests(testGroupsMayNeedMoreRequests, maximumRetryFactor);
53
54         const testGroupsNeedNotification = await TestGroup.fetchAllWithNotificationReady();
55         const notificationRemoveAPI = new RemoteAPI(notificationConfig.notificationServerConfig);
56         const notificationMessageConfig = notificationConfig.notificationMessageConfig;
57         const notifier = new AnalysisResultsNotifier(notificationMessageConfig.messageTemplate, notificationMessageConfig.finalizeScript,
58             notificationMessageConfig.messageConstructionRules, notificationRemoveAPI, notificationConfig.notificationServerConfig.path, new Subprocess);
59
60         await notifier.sendNotificationsForTestGroups(testGroupsNeedNotification);
61     } catch(error) {
62         console.error(`Failed analyze measurement sets due to ${error}`);
63     }
64
65     console.log(`Sleeping for ${secondsToSleep} seconds.`);
66     setTimeout(() => analysisLoop(options), secondsToSleep * 1000);
67 }
68
69
70 main(process.argv);