MDL-59661 analytics: Filearea passed as an argument to dataset_manager
authorDavid Monllao <davidm@moodle.com>
Thu, 10 Aug 2017 10:55:57 +0000 (12:55 +0200)
committerDavid Monllao <david.monllao@gmail.com>
Thu, 17 Aug 2017 18:51:56 +0000 (20:51 +0200)
analytics/classes/dataset_manager.php
analytics/classes/local/analyser/base.php
analytics/classes/local/analyser/by_course.php
analytics/classes/local/analyser/sitewide.php

index a1438ea..052b7ed 100644 (file)
@@ -45,6 +45,11 @@ class dataset_manager {
      */
     const UNLABELLED_FILEAREA = 'unlabelled';
 
+    /**
+     * File area for exported datasets.
+     */
+    const EXPORT_FILEAREA = 'export';
+
     /**
      * Evaluation file file name.
      */
@@ -77,11 +82,11 @@ class dataset_manager {
     protected $evaluation;
 
     /**
-     * Labelled (true) or unlabelled data (false).
+     * The dataset filearea. Must be one of the self::*_FILEAREA options.
      *
-     * @var bool
+     * @var string
      */
-    protected $includetarget;
+    protected $filearea;
 
     /**
      * Constructor method.
@@ -89,16 +94,22 @@ class dataset_manager {
      * @param int $modelid
      * @param int $analysableid
      * @param string $timesplittingid
+     * @param string $filearea
      * @param bool $evaluation
-     * @param bool $includetarget
      * @return void
      */
-    public function __construct($modelid, $analysableid, $timesplittingid, $evaluation = false, $includetarget = false) {
+    public function __construct($modelid, $analysableid, $timesplittingid, $filearea, $evaluation = false) {
+
+        if ($filearea !== self::EXPORT_FILEAREA && $filearea !== self::LABELLED_FILEAREA &&
+                $filearea !== self::UNLABELLED_FILEAREA) {
+            throw new \coding_exception('Invalid provided filearea');
+        }
+
         $this->modelid = $modelid;
         $this->analysableid = $analysableid;
         $this->timesplittingid = $timesplittingid;
         $this->evaluation = $evaluation;
-        $this->includetarget = $includetarget;
+        $this->filearea = $filearea;
     }
 
     /**
@@ -108,8 +119,7 @@ class dataset_manager {
      */
     public function init_process() {
         $lockkey = 'modelid:' . $this->modelid . '-analysableid:' . $this->analysableid .
-            '-timesplitting:' . self::clean_time_splitting_id($this->timesplittingid) .
-            '-includetarget:' . (int)$this->includetarget;
+            '-timesplitting:' . self::clean_time_splitting_id($this->timesplittingid);
 
         // Large timeout as processes may be quite long.
         $lockfactory = \core\lock\lock_config::get_lock_factory('core_analytics');
@@ -132,9 +142,10 @@ class dataset_manager {
 
         // Delete previous file if it exists.
         $fs = get_file_storage();
+
         $filerecord = [
             'component' => 'analytics',
-            'filearea' => self::get_filearea($this->includetarget),
+            'filearea' => $this->filearea,
             'itemid' => $this->modelid,
             'contextid' => \context_system::instance()->id,
             'filepath' => '/analysable/' . $this->analysableid . '/' . self::clean_time_splitting_id($this->timesplittingid) . '/',
@@ -217,7 +228,7 @@ class dataset_manager {
         $fs = get_file_storage();
 
         // Always evaluation.csv and labelled as it is an evaluation file.
-        $filearea = self::get_filearea(true);
+        $filearea = self::LABELLED_FILEAREA;
         $filename = self::get_filename(true);
         $filepath = '/analysable/' . $analysableid . '/' . self::clean_time_splitting_id($timesplittingid) . '/';
         return $fs->get_file(\context_system::instance()->id, 'analytics', $filearea, $modelid, $filepath, $filename);
@@ -231,11 +242,11 @@ class dataset_manager {
      * @param array  $files
      * @param int    $modelid
      * @param string $timesplittingid
+     * @param string $filearea
      * @param bool   $evaluation
-     * @param bool   $includetarget
      * @return \stored_file
      */
-    public static function merge_datasets(array $files, $modelid, $timesplittingid, $evaluation, $includetarget) {
+    public static function merge_datasets(array $files, $modelid, $timesplittingid, $filearea, $evaluation = false) {
 
         $tmpfilepath = make_request_directory() . DIRECTORY_SEPARATOR . 'tmpfile.csv';
 
@@ -297,7 +308,7 @@ class dataset_manager {
 
         $filerecord = [
             'component' => 'analytics',
-            'filearea' => self::get_filearea($includetarget),
+            'filearea' => $filearea,
             'itemid' => $modelid,
             'contextid' => \context_system::instance()->id,
             'filepath' => '/timesplitting/' . self::clean_time_splitting_id($timesplittingid) . '/',
@@ -387,27 +398,9 @@ class dataset_manager {
             $filename = self::EVALUATION_FILENAME;
         } else {
             // Incremental time, the lock will make sure we don't have concurrency problems.
-            $filename = microtime(false) . '.csv';
+            $filename = microtime(true) . '.csv';
         }
 
         return $filename;
     }
-
-    /**
-     * Returns the file area to be used.
-     *
-     * @param bool $includetarget
-     * @return string
-     */
-    protected static function get_filearea($includetarget) {
-
-        if ($includetarget === true) {
-            $filearea = self::LABELLED_FILEAREA;
-        } else {
-            $filearea = self::UNLABELLED_FILEAREA;
-        }
-
-        return $filearea;
-    }
-
 }
index f0136f7..52986a4 100644 (file)
@@ -417,8 +417,13 @@ abstract class base {
             }
         }
 
+        if (!empty($target)) {
+            $filearea = \core_analytics\dataset_manager::LABELLED_FILEAREA;
+        } else {
+            $filearea = \core_analytics\dataset_manager::UNLABELLED_FILEAREA;
+        }
         $dataset = new \core_analytics\dataset_manager($this->modelid, $analysable->get_id(), $timesplitting->get_id(),
-            $this->options['evaluation'], !empty($target));
+            $filearea, $this->options['evaluation']);
 
         // Flag the model + analysable + timesplitting as being analysed (prevent concurrent executions).
         if (!$dataset->init_process()) {
index 424270e..e531bba 100644 (file)
@@ -111,8 +111,13 @@ abstract class by_course extends base {
             }
 
             // Merge all course files into one.
+            if ($includetarget) {
+                $filearea = \core_analytics\dataset_manager::LABELLED_FILEAREA;
+            } else {
+                $filearea = \core_analytics\dataset_manager::UNLABELLED_FILEAREA;
+            }
             $timesplittingfiles[$timesplittingid] = \core_analytics\dataset_manager::merge_datasets($files,
-                $this->modelid, $timesplittingid, $this->options['evaluation'], $includetarget);
+                $this->modelid, $timesplittingid, $filearea, $this->options['evaluation']);
         }
 
         return $timesplittingfiles;
index 1f21aca..9c6fb47 100644 (file)
@@ -57,8 +57,13 @@ abstract class sitewide extends base {
             }
 
             // We use merge but it is just a copy.
+            if ($includetarget) {
+                $filearea = \core_analytics\dataset_manager::LABELLED_FILEAREA;
+            } else {
+                $filearea = \core_analytics\dataset_manager::UNLABELLED_FILEAREA;
+            }
             $files[$timesplittingid] = \core_analytics\dataset_manager::merge_datasets(array($file), $this->modelid,
-                $timesplittingid, $this->options['evaluation'], $includetarget);
+                $timesplittingid, $filearea, $this->options['evaluation']);
         }
 
         return $files;