From b62564121d70c04e3b0937cd478faefb2888c76c Mon Sep 17 00:00:00 2001
From: srosse <stephane.rosse@frentix.com>
Date: Wed, 20 Feb 2019 19:12:33 +0100
Subject: [PATCH] OO-3895: in weekly stats use WW format for smooth ordering

---
 .../statistic/_spring/statisticContext.xml    |  6 +-
 .../org/olat/upgrade/OLATUpgrade_12_5_17.java | 98 +++++++++++++++++++
 .../olat/upgrade/_spring/upgradeContext.xml   |  1 +
 .../WeeklyStatisticUpdateManagerTest.java     |  7 +-
 4 files changed, 103 insertions(+), 9 deletions(-)
 create mode 100644 src/main/java/org/olat/upgrade/OLATUpgrade_12_5_17.java

diff --git a/src/main/java/org/olat/course/statistic/_spring/statisticContext.xml b/src/main/java/org/olat/course/statistic/_spring/statisticContext.xml
index 748555763b4..2b85b50782c 100644
--- a/src/main/java/org/olat/course/statistic/_spring/statisticContext.xml
+++ b/src/main/java/org/olat/course/statistic/_spring/statisticContext.xml
@@ -164,7 +164,7 @@ insert into o_stat_daily (businesspath, resid, day, value)
 				<value>
 update o_stat_weekly set value = value + delta.cnt
 from (select businesspath as tempbusinesspath,
-  (date_part('year', temptable.creationdate) || '-' || date_part('week', temptable.creationdate)) as w,
+  (date_part('year', temptable.creationdate) || '-' || to_char(temptable.creationdate, 'WW'))  as w,
   count(*) cnt
   from o_stat_temptable as temptable
   group by tempbusinesspath, w) as delta 
@@ -174,10 +174,10 @@ from (select businesspath as tempbusinesspath,
 insert into o_stat_weekly (businesspath, resid, week, value)  
   (select temptable.businesspath,
      int8(substring(temptable.businesspath from position(':' in temptable.businesspath) + 1 for position(']' in temptable.businesspath) - position(':' in temptable.businesspath) - 1)) as resid,
-     (date_part('year', temptable.creationdate) || '-' || date_part('week', temptable.creationdate)) as w,
+     (date_part('year', temptable.creationdate) || '-' || to_char(temptable.creationdate, 'WW')) as w,
      count(*) as cnt 
    from o_stat_temptable as temptable
-   left join o_stat_weekly old on (temptable.businesspath=old.businesspath and (date_part('year', temptable.creationdate) || '-' || date_part('week', temptable.creationdate))=old.week)
+   left join o_stat_weekly old on (temptable.businesspath=old.businesspath and (date_part('year', temptable.creationdate) || '-' || to_char(temptable.creationdate, 'WW'))=old.week)
    where temptable.businesspath != '' and old.businesspath is null
    group by temptable.businesspath, w);
 				</value>
diff --git a/src/main/java/org/olat/upgrade/OLATUpgrade_12_5_17.java b/src/main/java/org/olat/upgrade/OLATUpgrade_12_5_17.java
new file mode 100644
index 00000000000..18df9a75ef2
--- /dev/null
+++ b/src/main/java/org/olat/upgrade/OLATUpgrade_12_5_17.java
@@ -0,0 +1,98 @@
+/**
+ * <a href="http://www.openolat.org">
+ * OpenOLAT - Online Learning and Training</a><br>
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); <br>
+ * you may not use this file except in compliance with the License.<br>
+ * You may obtain a copy of the License at the
+ * <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
+ * <p>
+ * Unless required by applicable law or agreed to in writing,<br>
+ * software distributed under the License is distributed on an "AS IS" BASIS, <br>
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
+ * See the License for the specific language governing permissions and <br>
+ * limitations under the License.
+ * <p>
+ * Initial code contributed and copyrighted by<br>
+ * frentix GmbH, http://www.frentix.com
+ * <p>
+ */
+package org.olat.upgrade;
+
+import org.olat.core.CoreSpringFactory;
+import org.olat.core.commons.persistence.DB;
+import org.olat.course.statistic.StatisticUpdateManager;
+import org.springframework.beans.factory.annotation.Autowired;
+
+/**
+ * 
+ * Initial date: 20 févr. 2019<br>
+ * @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
+ *
+ */
+public class OLATUpgrade_12_5_17 extends OLATUpgrade {
+	
+
+	private static final String VERSION = "OLAT_12.5.17";
+	private static final String UPDATE_COURSE_STATS = "UPDATE COURSE STATS";
+	
+	@Autowired
+	private DB dbInstance;
+	
+	public OLATUpgrade_12_5_17() {
+		super();
+	}
+	
+	@Override
+	public String getVersion() {
+		return VERSION;
+	}
+
+	@Override
+	public boolean doPreSystemInitUpgrade(UpgradeManager upgradeManager) {
+		return false;
+	}
+
+	@Override
+	public boolean doPostSystemInitUpgrade(UpgradeManager upgradeManager) {
+		UpgradeHistoryData uhd = upgradeManager.getUpgradesHistory(VERSION);
+		if (uhd == null) {
+			// has never been called, initialize
+			uhd = new UpgradeHistoryData();
+		} else if (uhd.isInstallationComplete()) {
+			return false;
+		}
+		
+		boolean allOk = true;
+		allOk &= updateCourseStatistics(upgradeManager, uhd);
+		
+		uhd.setInstallationComplete(allOk);
+		upgradeManager.setUpgradesHistory(uhd, VERSION);
+		if(allOk) {
+			log.audit("Finished OLATUpgrade_12_5_17 successfully!");
+		} else {
+			log.audit("OLATUpgrade_12_5_17 not finished, try to restart OpenOLAT!");
+		}
+		return allOk;
+	}
+
+	private boolean updateCourseStatistics(UpgradeManager upgradeManager, UpgradeHistoryData uhd) {
+		boolean allOk = true;
+		if (!uhd.getBooleanDataValue(UPDATE_COURSE_STATS)) {
+			if(dbInstance.isPostgreSQL()) {
+				StatisticUpdateManager statisticManager = (StatisticUpdateManager) CoreSpringFactory.getBean("org.olat.course.statistic.StatisticUpdateManager");
+				if(statisticManager != null) {
+					statisticManager.updateStatistics(true, new Runnable() {
+						@Override
+						public void run() {
+							//
+						}
+					});
+				}
+			}
+			uhd.setBooleanDataValue(UPDATE_COURSE_STATS, allOk);
+			upgradeManager.setUpgradesHistory(uhd, VERSION);
+		}
+		return allOk;
+	}
+}
diff --git a/src/main/java/org/olat/upgrade/_spring/upgradeContext.xml b/src/main/java/org/olat/upgrade/_spring/upgradeContext.xml
index 4fd1198df2c..a9e96aee352 100644
--- a/src/main/java/org/olat/upgrade/_spring/upgradeContext.xml
+++ b/src/main/java/org/olat/upgrade/_spring/upgradeContext.xml
@@ -57,6 +57,7 @@
 				<bean id="upgrade_12_4_0" class="org.olat.upgrade.OLATUpgrade_12_4_0"/>
 				<bean id="upgrade_12_4_1" class="org.olat.upgrade.OLATUpgrade_12_4_1"/>
 				<bean id="upgrade_12_5_3" class="org.olat.upgrade.OLATUpgrade_12_5_3"/>
+				<bean id="upgrade_12_5_17" class="org.olat.upgrade.OLATUpgrade_12_5_17"/>
 			</list>
 		</property>
 	</bean>
diff --git a/src/test/java/org/olat/course/statistic/WeeklyStatisticUpdateManagerTest.java b/src/test/java/org/olat/course/statistic/WeeklyStatisticUpdateManagerTest.java
index 7156e620df3..798d8b56b20 100644
--- a/src/test/java/org/olat/course/statistic/WeeklyStatisticUpdateManagerTest.java
+++ b/src/test/java/org/olat/course/statistic/WeeklyStatisticUpdateManagerTest.java
@@ -44,8 +44,7 @@ import org.olat.test.JunitTestHelper;
  */
 public class WeeklyStatisticUpdateManagerTest extends AbstractStatisticUpdateManagerTest {
 
-	private final SimpleDateFormat weeklyFormat = new SimpleDateFormat("yyyy-w");
-	private final SimpleDateFormat weeklyMySQLFormat = new SimpleDateFormat("yyyy-ww");
+	private final SimpleDateFormat weeklyFormat = new SimpleDateFormat("yyyy-ww");
 	
 	private final WeeklyStatisticManager weeklyStatisticManager = new WeeklyStatisticManager();
 
@@ -128,10 +127,6 @@ public class WeeklyStatisticUpdateManagerTest extends AbstractStatisticUpdateMan
 		cal.set(Calendar.SECOND, 0);
 		cal.set(Calendar.MILLISECOND, 0);
 		
-		if(dbInstance.isMySQL()) {
-			weeklyMySQLFormat.setCalendar(cal);
-			return weeklyMySQLFormat.format(cal.getTime());
-		}
 		weeklyFormat.setCalendar(cal);
 		return weeklyFormat.format(cal.getTime());
 	}
-- 
GitLab