Fdans has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/396469 )

Change subject: Fix loading sparse data into widgets
......................................................................


Fix loading sparse data into widgets

When data isn't continuous, widgets behave in hard-to-predict ways like
showing NaNs and incorrect YoY or MoM totals.  This patch addresses most
of that but I'm not really sure what to do when there's data *more* than
a year ago.  The change from the last available datapoint beyond a year
seems relevant, but it's not YoY change.

Bug: T182224
Change-Id: If911e6dceeb9281887e19e705ae716c1ea1b6772
---
M src/components/dashboard/MetricWidget.vue
1 file changed, 38 insertions(+), 6 deletions(-)

Approvals:
  Fdans: Verified; Looks good to me, approved



diff --git a/src/components/dashboard/MetricWidget.vue 
b/src/components/dashboard/MetricWidget.vue
index 7e3e986..4437531 100644
--- a/src/components/dashboard/MetricWidget.vue
+++ b/src/components/dashboard/MetricWidget.vue
@@ -18,8 +18,13 @@
                 <div>
                     <span 
class="subdued">{{getMonthValue(lastMonth.month)}}</span>
                     <span class="change label">
-                        <arrow-icon :value="changeMoM"/>
-                        {{changeMoM}} % month over month
+                        <span v-if="changeMoM">
+                            <arrow-icon :value="changeMoM"/>
+                            {{changeMoM}} % month over month
+                        </span>
+                        <span v-else>
+                            (no data last month)
+                        </span>
                     </span>
                 </div>
                 <metric-bar-widget
@@ -33,16 +38,22 @@
                     :graphModel="graphModel">
                 </metric-line-widget>
                 <div class="ui horizontal small statistic">
-                    <div class="value">
+                    <div class="value" v-if="changeYoY">
                         
{{graphModel.formatNumberForMetric(lastYearAggregation)}}
                     </div>
-                    <div class="change label">
+                    <div class="change label" v-if="changeYoY">
                         <arrow-icon :value="changeYoY"/>
                         {{changeYoY}} % year over year
                     </div>
                 </div>
                 <div class="year total subdued">
-                    Year {{aggregationType}} 
({{monthOneYearAgo.month.getFullYear()}})
+                    <span v-if="changeYoY">
+                        <span v-if="monthOneYearAgo">Year {{aggregationType}} 
({{monthOneYearAgo.month.getFullYear()}})</span>
+                        <span v-else>{{aggregationType}} (all available 
data)</span>
+                    </span>
+                    <span v-else>
+                        (no data last year)
+                    </span>
                 </div>
             </div>
         </div>
@@ -123,7 +134,16 @@
             monthOneYearAgo: function () {
                 if (!this.lastMonth) { return null; }
 
-                return this.graphData[_.indexOf(this.graphData, 
this.lastMonth) - 12];
+                let last = _.indexOf(this.graphData, this.lastMonth);
+                const lastMonth = this.graphData[last].month;
+
+                while (last > 0) {
+                    last--;
+                    if (lastMonth - this.graphData[last].month >= 31536000000) 
{
+                        return this.graphData[last];
+                    }
+                }
+                return null;
             },
             lastYearAggregation: function () {
                 return this.graphModel.getLimitedAggregate(12);
@@ -136,12 +156,24 @@
 
                 const data = this.graphData;
                 const prev = data[data.length - 2];
+
+                if (!prev
+                    || !prev.total
+                    || this.lastMonth.month - prev.month > 2764800000) {
+                    return null;
+                }
+
                 const diff = this.lastMonth.total - prev.total;
                 return ((diff / prev.total) * 100).toFixed(2);
             },
             changeYoY: function () {
                 // TODO: We're showing more than the last year, but reporting 
YoY.  This can be confusing because the YoY might not match up visually with 
the graph (like for Unique Devices in Achinese).
 
+                if (!this.monthOneYearAgo
+                    || !this.monthOneYearAgo.total) {
+                    return null;
+                }
+
                 const diff = this.lastMonth.total - this.monthOneYearAgo.total;
                 return ((diff / this.monthOneYearAgo.total) * 100).toFixed(2);
             },

-- 
To view, visit https://gerrit.wikimedia.org/r/396469
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: If911e6dceeb9281887e19e705ae716c1ea1b6772
Gerrit-PatchSet: 1
Gerrit-Project: analytics/wikistats2
Gerrit-Branch: master
Gerrit-Owner: Milimetric <dandree...@wikimedia.org>
Gerrit-Reviewer: Fdans <fd...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to