Changeset 68


Ignore:
Timestamp:
17/11/06 04:19:57 (6 years ago)
Author:
nigel
Message:

Implemented meter_extrapolate. It is incorrectly implemented at this
stage, but it shows something.

Next we need to do some graphing. Preferably CSS graphs.

Location:
meter
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • meter/README

    r67 r68  
    22The meters can then be graphed for usage over days, months, quarters, etc.  
    33The data can also be extracted is csv format. 
     4 
     5TODO 
     6Put in units for meter type 
     7Graph usage 
     8Set a maximum value for the meter so wrap arounds can be accounted for. 
  • meter/meter.module

    r67 r68  
    7272 
    7373/** 
    74  * Implementation of hook_cron(). 
    75  * 
    76  * Closes polls that have exceeded their allowed runtime. 
    77  */ 
    78 function meter_cron() { 
    79   /*$result = db_query('SELECT p.nid FROM {poll} p INNER JOIN {node} n ON p.nid = n.nid WHERE (n.created + p.runtime) < '. time() .' AND p.active = 1 AND p.runtime != 0'); 
    80   while ($poll = db_fetch_object($result)) { 
    81     db_query("UPDATE {poll} SET active = 0 WHERE nid = %d", $poll->nid); 
    82   } 
    83   */ 
    84 } 
    85  
    86 /** 
    8774 * Implementation of hook_delete(). 
    8875 */ 
     
    9582 */ 
    9683function meter_submit(&$node) { 
     84  print "<p>meter_submit</p>"; 
    9785  // Renumber fields 
    9886  /* 
     
    10694 */ 
    10795function meter_validate($node) { 
     96  print "<p>meter_validate</p>"; 
    10897/* 
    10998  if (isset($node->title)) { 
     
    147136 
    148137function meter_insert($node) { 
     138  print "<p>meter_insert</p>"; 
    149139  /* 
    150140  if (!user_access('administer nodes')) { 
     
    231221  $output = '<ul>'; 
    232222  while ($node = db_fetch_object($result)) { 
    233     $output .= '<li>'. l($node->title, "node/$node->nid") .' - '. format_plural($node->votes, '1 vote', '%count votes') .' - '. ($node->active ? t('open') : t('closed')) .'</li>'; 
     223    $output .= '<li>'. l($node->title, "node/$node->nid") .'</li>'; 
    234224  } 
    235225  $output .= '</ul>'; 
    236226  $output .= theme("pager", NULL, 15); 
    237227  return $output; 
    238   //$output = ''; 
    239   //return $output; 
    240228} 
    241229 
     
    395383  $form['#action'] = url('node/'. $node->nid.'/add_reading'); 
    396384 
     385  $res = meter_extrapolate(meter_get_values($node), 1,30); 
     386  foreach ($res as $val){ 
     387    $output .= "<p>$val->to $val->value </p>"; 
     388  } 
     389 
    397390  $output .= drupal_get_form('meter_add_reading', $form); 
    398391 
     
    406399function meter_csv_readings(&$node, $teaser = FALSE, $page = FALSE, $block = FALSE) { 
    407400  $nid = arg(1); 
    408   $results = db_query("SELECT time, value FROM {meter_readings} WHERE nid = %d", $nid); 
     401  $results = meter_get_values($node); 
    409402  drupal_set_header('Content-Type: text/csv; charset=utf-8'); 
    410403  drupal_set_header('Content-Disposition: attachment; filename="meter.csv";'); 
    411404  print "Time, Value\n"; 
     405  foreach($results as $row){ 
     406    print '"'.$row->time.'",'.$row->value."\n"; 
     407  } 
     408 
     409} 
     410 
     411/** 
     412  * Query the DB for the reading values 
     413  */ 
     414function meter_get_values($node){ 
     415  $res = array(); 
     416  $results = db_query("SELECT time, value FROM {meter_readings} WHERE nid = %d ORDER BY time DESC", $node->nid); 
     417 
    412418  while ($row = db_fetch_object($results)){ 
    413     print '"'.$row->time.'",'.$row->value."\n"; 
    414   } 
    415  
     419    $res[] = $row; 
     420  } 
     421  return $res; 
    416422} 
    417423 
     
    442448  } 
    443449} 
     450 
     451/** 
     452  * Average usage over $days, from midday before the last reading 
     453  * @param values - object with date and value 
     454  * @param days - the number of days to average over 
     455  * @param count - the number of entries to calculate 
     456  */ 
     457function meter_extrapolate($values, $days, $count){ 
     458  $return = array(); 
     459 
     460  $last = reset($values); 
     461  $row = $last; 
     462  $target = strtotime($row->time); // Set the first target 
     463 
     464  for ($i = 0 ; $i < $count ; $i++){ 
     465    // Increment target 
     466    $oldTarget = $target; 
     467    $target -= $days*60*60*24; 
     468 
     469    while ( !($row === false) && strtotime($row->time) > $target){ 
     470      $last = $row; 
     471      $row = next($values); 
     472    } 
     473    if ($row === false) { 
     474      break; 
     475    } else { 
     476      $timeBetween = (strtotime( $last->time ) - strtotime( $row->time ))/24/3600; 
     477      $entry = new StdClass(); 
     478      $entry->from = date("d/m/Y H:i", $target); 
     479      $entry->to = date("d/m/Y H:i", $oldTarget); 
     480      $entry->value = ($last->value - $row->value) / $timeBetween; 
     481      $return[$i] = $entry; 
     482    } 
     483  } 
     484  return $return; 
     485} 
Note: See TracChangeset for help on using the changeset viewer.