Petar.petkovic has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/385353 )

Change subject: Fix missing months on CX dashboard chart data
......................................................................

Fix missing months on CX dashboard chart data

- Add missing months to data returned for translation stats.
- Change deprecated frontend logic.

Bug: T176513
Change-Id: I9207663bb0735ce308ad26b1bc428a1d7340f70e
---
M api/ApiQueryTranslatorStats.php
M modules/widgets/translator/ext.cx.translator.js
2 files changed, 40 insertions(+), 21 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ContentTranslation 
refs/changes/53/385353/1

diff --git a/api/ApiQueryTranslatorStats.php b/api/ApiQueryTranslatorStats.php
index 246da33..6d26298 100644
--- a/api/ApiQueryTranslatorStats.php
+++ b/api/ApiQueryTranslatorStats.php
@@ -9,6 +9,7 @@
 
 use ContentTranslation\Translation;
 use ContentTranslation\Translator;
+use ContentTranslation\DateManipulator;
 
 /**
  * @ingroup API ContentTranslationAPI
@@ -39,16 +40,8 @@
                        null, null, 'published', 'month', $translatorId
                );
 
-               $trend = [];
-               foreach ( $publishedStats as $key => $value ) {
-                       $datetime = new DateTime( "@$key" );
-                       $trend[ $datetime->format( 'Y-m-d' ) ] = $value;
-               }
+               $trend = $this->addMissingMonths( $publishedStats );
 
-               // TODO: The $publishedStats does not contain data for all 
months,
-               // if there is not translation in that month. 
ApiQueryContentTranslationLanguageTrend
-               // has utility methods to fill it. But it is not important for 
the graph we render
-               // from the output of this data.
                $result = [
                        'translator' => $user->getName(),
                        'translatorId' => $translatorId,
@@ -57,6 +50,36 @@
                $this->getResult()->addValue( null, $this->getModuleName(), 
$result );
        }
 
+       private function addMissingMonths( $data ) {
+               $dates = array_keys( $data );
+
+               $dm = new DateManipulator( 'month' );
+               $min = $dm->getIntervalIdentifier( min( $dates ) );
+               $max = $dm->getIntervalIdentifier( 0 ); // Now
+
+               $steps = $dm->getSteps( $min, $max, 'month' );
+
+               $out = [];
+               $count = 0;
+
+               foreach ( $steps as $datetime ) {
+                       $id = $datetime->format( 'U' );
+                       $date = $datetime->format( 'Y-m-d' );
+
+                       if ( isset( $data[ $id ] ) ) {
+                               $out[ $date ] = $data[ $id ];
+                               $count = $data[ $id ][ 'count' ];
+                       } else {
+                               $out[ $date ] = [
+                                       'count' => $count,
+                                       'delta' => 0
+                               ];
+                       }
+               }
+
+               return $out;
+       }
+
        public function getAllowedParams() {
                $allowedParams = [
                        'translator' => [
diff --git a/modules/widgets/translator/ext.cx.translator.js 
b/modules/widgets/translator/ext.cx.translator.js
index 6f58b95..67902a6 100644
--- a/modules/widgets/translator/ext.cx.translator.js
+++ b/modules/widgets/translator/ext.cx.translator.js
@@ -65,28 +65,24 @@
                        .addClass( 'cx-translator' )
                        .append( $header, $monthStats, $total, this.$canvas );
                statsRequest.then( function ( stats ) {
-                       var total, monthCount,
+                       var total, thisMonthStats,
                                publishTrend = 
stats.cxtranslatorstats.publishTrend,
                                // Sorted months for ordered display on bar 
chart
                                monthKeys = Object.keys( publishTrend ).sort(),
-                               thisMonthKey = new Date().toISOString().slice( 
0, 7 ) + '-01',
-                               lastMonthKey = Object.keys( publishTrend 
).slice( -1 ).pop();
+                               thisMonthKey = new Date().toISOString().slice( 
0, 7 ) + '-01';
 
-                       // lastMonthKey is for the month with non-zero 
contributions. It may be equal to
-                       // thisMonthKey, but not guaranteed.
-                       if ( !lastMonthKey ) {
-                               // There is no month with non-zero 
contributions.
+                       total = publishTrend[ thisMonthKey ].count || 0;
+                       thisMonthStats = publishTrend[ thisMonthKey ].delta || 
0;
+
+                       // Don't display statistics if there are no 
translations yet
+                       if ( total === 0 ) {
                                self.$widget.remove();
                                return;
                        }
-                       total = publishTrend[ lastMonthKey ].count || 0;
-                       monthCount = publishTrend[ thisMonthKey ] &&
-                               publishTrend[ thisMonthKey ].delta || 0;
 
                        $header.text( mw.msg( 'cx-translator-header' ) );
                        $total.find( '.cx-translator__total-translations-count' 
).text( total );
-                       $monthStats.find( '.cx-translator__month-stats-count' )
-                               .text( monthCount );
+                       $monthStats.find( '.cx-translator__month-stats-count' 
).text( thisMonthStats );
 
                        $.each( monthKeys, function ( i, month ) {
                                self.max = Math.max( self.max, publishTrend[ 
month ].delta );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I9207663bb0735ce308ad26b1bc428a1d7340f70e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ContentTranslation
Gerrit-Branch: master
Gerrit-Owner: Petar.petkovic <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to