[Rt-commit] rt branch, 4.6/canned-reports, updated. rt-4.4.1-158-ga65ba64

Dustin Collins strega at bestpractical.com
Thu Dec 22 13:38:51 EST 2016


The branch, 4.6/canned-reports has been updated
       via  a65ba64df71d33f5b151924bce72e94241ef5aa9 (commit)
      from  5d85527229c7903d003363dbd093ca877d2b7c7e (commit)

Summary of changes:
 lib/RT/CannedReport.pm                 |  4 +--
 lib/RT/CannedReport/FieldByField.pm    | 60 ++++++++++++----------------------
 share/html/CannedReport/Elements/Graph | 57 ++++++++++++--------------------
 share/html/Helpers/CannedReports       | 40 ++++++++---------------
 share/static/js/canned-reports.js      | 41 ++++++++++++-----------
 5 files changed, 78 insertions(+), 124 deletions(-)

- Log -----------------------------------------------------------------
commit a65ba64df71d33f5b151924bce72e94241ef5aa9
Author: Dustin Collins <strega at bestpractical.com>
Date:   Thu Dec 22 13:38:47 2016 -0500

    Update graph dynamically
    
    The Graph element implements a javascript function updateGraphData() which is called to update the graph within the browser. This function is within the graph element in anticipation of addition graph elements.

diff --git a/lib/RT/CannedReport.pm b/lib/RT/CannedReport.pm
index 120cb50..5ed394e 100644
--- a/lib/RT/CannedReport.pm
+++ b/lib/RT/CannedReport.pm
@@ -71,7 +71,7 @@ sub Reports {
 }
 
 #The user selectable options available for the provided report name
-sub Options{
+sub Options {
     my $self = shift;
     my $report = shift;
     return {};
@@ -80,7 +80,7 @@ sub Options{
 #Should return a hash of results
 sub Results {
     my $self = shift;
-    return {};
+    return [];
 }
 
 1;
diff --git a/lib/RT/CannedReport/FieldByField.pm b/lib/RT/CannedReport/FieldByField.pm
index 371a894..0e50731 100644
--- a/lib/RT/CannedReport/FieldByField.pm
+++ b/lib/RT/CannedReport/FieldByField.pm
@@ -72,65 +72,45 @@ sub Options {
     ];
 }
 
-#Placeholder for chosen options
-sub Config {
-    my $self = shift;
-    return {Field1 => "Resolved",
-            Field2 => "Owner",
-            Within => "7 Days", # '__Custom__' to use start & end
-            Start  => "2016/01/01",
-            End    => "2017/01/01",
-    };
-}
-
 #TODO: status shouldn't be hardcoded
 sub Results {
     my $self = shift;
-
-    my %config = %{$self->Config()};
-    my %state;#State machine for building the query
-    #type
-    #   field - add all field values
-    #   count - count tickets
+    my $parameters = shift;
 
     my $collection = RT::Tickets->new(RT->SystemUser);
 
     my $query = "";
 
-    my $f1 = lc $config{Field1};
-    if ($f1 eq "resolved" || $f1 eq "open" || $f1 eq "stalled") {
-        $query .= "status = '$f1'";
-        $state{"f1"}{"type"} = "count";
-    }elsif ($f1 eq "timeworked") {
-        $state{"f1"}{"type"} = "field";
-    }else{
-        $query .= "status = '$f1'";
-        $state{"f1"}{"type"} = "count";
-    }
-
-    my $within = $config{Within};
-    if (!$within || $within eq "__Custom__") {
-        $query .= " AND LastUpdated < '$within'";
-    }else{
-        my $start = $config{Start};
-        my $end = $config{End};
-        $query .= " AND LastUpdated > '$start' AND LastUpdated < '$end'";
+    my $name = lc $parameters->{"name"};
+    if (!$name || $name eq "resolved") { ### DEFAULT ###
+        $query .= "status = 'resolved'";
     }
 
     $collection->FromSQL($query);
     $collection->UnLimit();
     my %results;
-    my $f2 = lc $config{Field2};
-    if (lc $f2 eq "queue") {
+
+    if ($name eq "created") {
+        while (my $ticket = $collection->Next) {
+            $results{$ticket->CreatorObj->Name} += 1;
+        }
+    }elsif ($name eq "time worked") {
         while (my $ticket = $collection->Next) {
-            $results{$ticket->QueueObj->Name} += 1;
+            $results{$ticket->CreatorObj->Name} += $ticket->TimeWorked;
         }
-    }else{#Default
+    }else{#if ($name eq "Resolved") { ### DEFAULT ###
         while (my $ticket = $collection->Next) {
             $results{$ticket->OwnerObj->Name} += 1;
         }
     }
-    return %results;
+
+    my @values;
+    for my $key (keys %results) {
+        push @values, {name => $key, value => $results{$key}};
+    }
+
+    return \@values;
 }
 
+
 1;
diff --git a/share/html/CannedReport/Elements/Graph b/share/html/CannedReport/Elements/Graph
index 49e2c14..d9ea372 100644
--- a/share/html/CannedReport/Elements/Graph
+++ b/share/html/CannedReport/Elements/Graph
@@ -63,48 +63,33 @@ $name
 <script>
 
 var svg = d3.select("#graph"),
-    margin = {top: 20, right: 20, bottom: 30, left: 40},
-    width = +svg.attr("width") - margin.left - margin.right,
-    height = +svg.attr("height") - margin.top - margin.bottom;
+            margin = {top: 20, right: 20, bottom: 30, left: 40},
+            width = +svg.attr("width") - margin.left - margin.right,
+            height = +svg.attr("height") - margin.top - margin.bottom;
 
-var x = d3.scaleBand().rangeRound([0, width]).padding(0.1),
-    y = d3.scaleLinear().rangeRound([height, 0]);
+function updateGraphData(data) {
+    d3.select("#graph").selectAll("*").remove();
 
-var g = svg.append("g")
-    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
+    var x = d3.scaleBand().rangeRound([0, width]).padding(0.1),
+        y = d3.scaleLinear().rangeRound([height, 0]);
+    var g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
 
-d3.tsv("/Helpers/CannedReports?cmd=results&kind=tsv&name=<% $name %>", function(d) {
-  d.resolved = +d.resolved;
-  return d;
-}, function(error, data) {
-  if (error) throw error;
+    x.domain(data.map(function(d) { return d.name; }));
+    y.domain([0, d3.max(data, function(d) { return d.value; })]);
 
-  x.domain(data.map(function(d) { return d.user; }));
-  y.domain([0, d3.max(data, function(d) { return d.resolved; })]);
+    g.append("g")
+    .attr("class", "axis axis--x")
+    .attr("transform", "translate(0," + height + ")")
+    .call(d3.axisBottom(x));
 
-  g.append("g")
-      .attr("class", "axis axis--x")
-      .attr("transform", "translate(0," + height + ")")
-      .call(d3.axisBottom(x));
-
-  g.append("g")
-      .attr("class", "axis axis--y")
-      .call(d3.axisLeft(y).ticks(10, "%"))
-    .append("text")
-      .attr("transform", "rotate(-90)")
-      .attr("y", 6)
-      .attr("dy", "0.71em")
-      .attr("text-anchor", "end")
-      .text("Resolved");
-
-  g.selectAll(".bar")
+    g.selectAll(".bar")
     .data(data)
     .enter().append("rect")
-      .attr("class", "bar")
-      .attr("x", function(d) { return x(d.user); })
-      .attr("y", function(d) { return y(d.resolved); })
-      .attr("width", x.bandwidth())
-      .attr("height", function(d) { return height - y(d.resolved); });
-});
+    .attr("class", "bar")
+    .attr("x", function(d) { return x(d.name); })
+    .attr("y", function(d) { return y(d.value); })
+    .attr("width", x.bandwidth())
+    .attr("height", function(d) { return height - y(d.value); });
+}
 
 </script>
diff --git a/share/html/Helpers/CannedReports b/share/html/Helpers/CannedReports
index 3ac8b9f..e25718f 100644
--- a/share/html/Helpers/CannedReports
+++ b/share/html/Helpers/CannedReports
@@ -46,9 +46,9 @@
 %#
 %# END BPS TAGGED BLOCK }}}
 <%ARGS>
+$cmd => "results"
 $name
-$kind => "tsv"
-$cmd
+$kind => undef
 </%ARGS>
 <%init>
 
@@ -56,42 +56,28 @@ use RT::CannedReports;
 use JSON;
 
 my %object = (
-    Message => loc("Unknown Error"),
-    Code => 500,
-    Content => ["Failed"],
+    message => loc("Unknown Error"),
+    code => 500,
+    content => ["Failed"],
 );
 
 my $reports = RT::CannedReports->new($session{'CurrentUser'});
 my $report = $reports->ReportFromReportName($name);
-
 if ($cmd eq "results") {
     if (ref $report) {
-        my %results = $report->Results();
-        my $resultsOut;
-        if ($kind eq "table") {
+        my $results = $report->Results(\%ARGS);
+        if ($kind && $kind eq "table") {
             my $html = "<table>";
-            for my $key (keys %results) {
+            for my $hashRef (@$results) {
                 $html .= "\n";
-                $html .= "<tr>\n<td>" . $key . "</td>\n<td>" . $results{$key} . "</td>\n</tr>";
+                $html .= "<tr>\n<td>" . $hashRef->{name} . "</td>\n<td>" . $hashRef->{value} . "</td>\n</tr>";
             }
             $html .= "\n</table>";
-            $object{Content} = [$html];
-        }elsif ($kind eq "tsv") {
-            my $largest = 0;
-            for my $value (values %results) {
-                $largest = $value if $value > $largest;
-            }
-            my $tsv = "user\tresolved\n";
-            for my $key (keys %results) {
-                my $value = $results{$key};
-                my $chartValue = (1.0/$largest) * $value;
-                $tsv .= $key . "\t" . $chartValue . "\n";
-            }
-            warn "$tsv";
-            $m->out($tsv);
-            $m->abort();
+            $object{content} = [$html];
+            $object{code} = 200;
         }else{
-            $object{Content} = [\%results];
+            $object{content} = $results;
+            $object{code} = 200;
         }
     }
 }
diff --git a/share/static/js/canned-reports.js b/share/static/js/canned-reports.js
index a60306c..220f3cd 100644
--- a/share/static/js/canned-reports.js
+++ b/share/static/js/canned-reports.js
@@ -1,26 +1,21 @@
 //MARK: Helper
 
-function getReportNames(completion) {
-    submit("/Helpers/CannedReports?cmd=names", null, null, function(code, message, content) {
-           completion(code == 200, content);
-    })
-}
-
-function getReportResults(reportIndex, completion) {
-    submit("/Helpers/CannedReports?cmd=results&i=" + reportIndex, null, null, function(code, message, content) {
-           completion(code == 200, content);
-    })
-}
-
-function getReportResultsTable(reportIndex, completion) {
-    submit("/Helpers/CannedReports?cmd=results&kind=table&i=" + reportIndex, null, null, function(code, message, content) {
+function getReportResults(parameters, kind, completion) {
+    var url = "/Helpers/CannedReports?cmd=results";
+    if (kind) {
+        url += "&kind=" + kind;
+    }
+    for (var key in parameters) {
+        url += "&" + key + "=" + parameters[key];
+    }
+    submit(url, null, null, function(code, message, content) {
            completion(code == 200, content);
     })
 }
 
 function submit(path, pairs, data, completion) {
     jQuery.post(path, pairs, function(object) {
-        completion(object["Code"], object["Message"], object["Content"])
+        completion(object["code"], object["message"], object["content"])
     },'json');
 }
 
@@ -47,9 +42,11 @@ function getQueryParams() {
 var _parameters = getQueryParams();
 var _paramsTimer = {};
 function setParameter(key, value) {
-    _parameters[key] = value;
-    window.clearTimeout(_paramsTimer);
-    _paramsTimer = window.setTimeout(function() {paramsChanged()}, 30);
+    if (_parameters[key] !== value) {
+        _parameters[key] = value;
+        window.clearTimeout(_paramsTimer);
+        _paramsTimer = window.setTimeout(function() {paramsChanged()}, 30);
+    }
 }
 
 function paramsChanged() {
@@ -90,7 +87,13 @@ window.onclick = function(event) {
 //MARK: Graph
 
 function updateGraph() {
-    alert("updateGraph called :)");
+    getReportResults(_parameters, null, function (success, content) {
+        if (success) {
+            updateGraphData(content)
+        }else{
+            alert("Sorry! Graph update failed.");
+        }
+    })
 }
 
 var _graphUpdateTimer = {}

-----------------------------------------------------------------------


More information about the rt-commit mailing list