<commit_before><?php
/*=========================================================================
 MIDAS Server
 Copyright (c) Kitware SAS. 26 rue <NAME>. 69100 Villeurbanne, FRANCE
 All rights reserved.
 More information http://www.kitware.com

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0.txt

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
=========================================================================*/

/**
 * This component is used to create and manages
 * paraview (pvpython) instances.
 */
class Pvw_ParaviewComponent extends AppComponent
{
  /**
   * Creates a new pvpython instance and a corresponding database record for it.
   * @param item The item dao to visualize
   * @return The pvw_instance dao
   */
  public function createAndStartInstance($item, $meshItems, $appname, $progressDao = null)
    {
    $progressModel = MidasLoader::loadModel('Progress');
    if($progressDao)
      {
      $step = 1;
      $progressDao->setMaximum(5);
      $progressModel->save($progressDao);
      $progressModel->updateProgress($progressDao, $step, 'Checking available ports...');
      }
    $settingModel = MidasLoader::loadModel('Setting');
    $pvpython = $settingModel->getValueByName('pvpython', 'pvw');
    $application = BASE_PATH.'/modules/pvw/apps/'.$appname.'.py';
    if(!is_file($application))
      {
      throw new Zend_Exception('No such application: '.$appname, 400);
      }
    if($progressDao)
      {
      $step++;
      $progressModel->updateProgress($progressDao, $step, 'Checking available ports...');
      }

    // TODO critical section of code between getting the open port and listening on it
    // practical solution: add db setting for 'nextPortToTry'.
    $port = $this->_getNextOpenPort();
    if($port === false)
      {
      throw new Zend_Exception('Maximum number of running instances reached, try again soon', 503);
      }
    if($progressDao)
      {
      $step++;
      $progressModel->updateProgress($progressDao, $step, 'Starting ParaView instance...');
      }

    $instance = MidasLoader::newDao('InstanceDao', 'pvw');
    $instance->setItemId($item->getKey());
    $instance->setPort($port);
    $instance->setSid(''); // todo?
    $instance->setPid(0);
    $instance->setCreationDate(date('c'));
    $instance->setSecret(UtilityComponent::generateRandomString(32, '<KEY>'));

    $instanceModel = MidasLoader::loadModel('Instance', 'pvw');
    $instanceModel->save($instance);

    $dataPath = $this->_createDataDir($item, $meshItems, $instance);

    $cmdArray = array($pvpython, $application,
                      '--port', $port,
                      '--data', $dataPath,
                      '-a', $instance->getSecret(),
                      '--timeout', '900'); // TODO --inactivity-timeout
<commit_msg>BUG: refs #1030. Fix date format for mysql 5.6<commit_after><?php
/*=========================================================================
 MIDAS Server
 Copyright (c) Kitware SAS. 26 rue <NAME>. 69100 Villeurbanne, FRANCE
 All rights reserved.
 More information http://www.kitware.com

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0.txt

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
=========================================================================*/

/**
 * This component is used to create and manages
 * paraview (pvpython) instances.
 */
class Pvw_ParaviewComponent extends AppComponent
{
  /**
   * Creates a new pvpython instance and a corresponding database record for it.
   * @param item The item dao to visualize
   * @return The pvw_instance dao
   */
  public function createAndStartInstance($item, $meshItems, $appname, $progressDao = null)
    {
    $progressModel = MidasLoader::loadModel('Progress');
    if($progressDao)
      {
      $step = 1;
      $progressDao->setMaximum(5);
      $progressModel->save($progressDao);
      $progressModel->updateProgress($progressDao, $step, 'Checking available ports...');
      }
    $settingModel = MidasLoader::loadModel('Setting');
    $pvpython = $settingModel->getValueByName('pvpython', 'pvw');
    $application = BASE_PATH.'/modules/pvw/apps/'.$appname.'.py';
    if(!is_file($application))
      {
      throw new Zend_Exception('No such application: '.$appname, 400);
      }
    if($progressDao)
      {
      $step++;
      $progressModel->updateProgress($progressDao, $step, 'Checking available ports...');
      }

    // TODO critical section of code between getting the open port and listening on it
    // practical solution: add db setting for 'nextPortToTry'.
    $port = $this->_getNextOpenPort();
    if($port === false)
      {
      throw new Zend_Exception('Maximum number of running instances reached, try again soon', 503);
      }
    if($progressDao)
      {
      $step++;
      $progressModel->updateProgress($progressDao, $step, 'Starting ParaView instance...');
      }

    $instance = MidasLoader::newDao('InstanceDao', 'pvw');
    $instance->setItemId($item->getKey());
    $instance->setPort($port);
    $instance->setSid(''); // todo?
    $instance->setPid(0);
    $instance->setCreationDate(date('Y-m-d H:i:s'));
    $instance->setSecret(UtilityComponent::generateRandomString(32, '0123456789abcdef'));

    $instanceModel = MidasLoader::loadModel('Instance', 'pvw');
    $instanceModel->save($instance);

    $dataPath = $this->_createDataDir($item, $meshItems, $instance);

    $cmdArray = array($pvpython, $application,
                      '--port', $port,
                      '--data', $dataPath,
                      '-a', $instance->getSecret(),
                      '--timeout', '900'); // TODO --inactivity-timeout

{'fraction_non_alphanumeric': 0.17122656009837073, 'fraction_numerical': 0.010144482016600061, 'mean_word_length': 3.4386084583901773, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3105229', 'n_tokens_mistral': 1818, 'n_tokens_neox': 1745, 'n_words': 513}