1 /**
2 Copyright: Copyright (c) 2020, Joakim Brännström. All rights reserved.
3 License: MPL-2
4 Author: Joakim Brännström (joakim.brannstrom@gmx.com)
5 
6 This Source Code Form is subject to the terms of the Mozilla Public License,
7 v.2.0. If a copy of the MPL was not distributed with this file, You can obtain
8 one at http://mozilla.org/MPL/2.0/.
9 */
10 module dextool.plugin.mutate.backend.report.html.trend;
11 
12 import logger = std.experimental.logger;
13 import std.format : format;
14 
15 import arsd.dom : Element, Link;
16 
17 import dextool.plugin.mutate.backend.database : Database;
18 import dextool.plugin.mutate.backend.report.analyzers : reportTrendByCodeChange,
19     reportMutationScoreHistory;
20 import dextool.plugin.mutate.backend.report.html.constants;
21 import dextool.plugin.mutate.backend.type : Mutation;
22 
23 void makeTrend(ref Database db, string tag, Element root, const(Mutation.Kind)[] kinds) @trusted {
24     import std.datetime : SysTime;
25     import dextool.plugin.mutate.backend.report.html.tmpl : TimeScalePointGraph;
26 
27     DashboardCss.h2(root.addChild(new Link(tag, null)).setAttribute("id", tag[1 .. $]), "Trend");
28 
29     auto base = root.addChild("div");
30 
31     auto ts = TimeScalePointGraph("ScoreHistory");
32 
33     const history = reportMutationScoreHistory(db);
34     if (history.data.length > 2 && history.estimate.x != SysTime.init) {
35         foreach (v; history.data) {
36             ts.put("Score", TimeScalePointGraph.Point(v.timeStamp, v.score.get));
37         }
38         ts.setColor("Score", "blue");
39 
40         ts.put("Trend", TimeScalePointGraph.Point(history.estimate.x, history.estimate.avg));
41         ts.put("Trend", TimeScalePointGraph.Point(history.data[$ - 1].timeStamp,
42                 history.data[$ - 1].score.get));
43         ts.put("Trend", TimeScalePointGraph.Point(history.estimate.predX,
44                 history.estimate.predScore));
45         ts.setColor("Trend", history.estimate.posTrend ? "green" : "red");
46 
47         ts.html(base, TimeScalePointGraph.Width(80));
48         base.addChild("p")
49             .appendHtml(
50                     "<i>trend</i> is a prediction of how the mutation score will change based on previous scores.");
51     }
52 
53     const codeChange = reportTrendByCodeChange(db, kinds);
54     if (codeChange.sample.length > 2) {
55         ts = TimeScalePointGraph("ScoreByCodeChange");
56         foreach (v; codeChange.sample) {
57             ts.put("Score", TimeScalePointGraph.Point(v.timeStamp, v.value.get));
58         }
59         ts.setColor("Score", "purple");
60         ts.html(base, TimeScalePointGraph.Width(80));
61         base.addChild("p").appendHtml(
62                 "<i>code change</i> is a prediction of how the mutation score will change based on the latest code changes.");
63     }
64 }