Metrics Coding Development Manual

2025-12-28 07:20:56
Sanplex Content
33
Last edited by WANG JING on 2025-12-28 07:20:56
Share links
Summary : A developer guide for implementing custom Report metrics in Sanplex using PHP templates and calculation examples.

Template Definition

<?php
/**
 * [Metric Name].
 * [Metric Name (English)].
 *
 * Scope: [Scope]
 * Object: [Object]
 * Purpose: [Purpose]
 * Metric Name: [Metric Name]
 * Unit: [Unit]
 * Description: [Explain what this metric means]
 * Definition: [Explain how the metric is defined, calculation rules, etc.]
 */
class xxx_of_xxx_in_xxx extends baseCalc
{
    /**
     * Temporary calculation results for the metric.
     */
    public $result = array();
    /**
     * Get the PDO statement handle for a custom data source.
     */
    public function getStatement()
    {
        return $this->dao->XXX->query();
    }
    /**
     * Calculate the metric.
     *
     * @param object $row One row of data from the data source
     */
    public function calculate($row)
    {
         * $this->result += 1;  }
    /**
     * Aggregate and return the calculated metric results.
     *
     * @param array $options Filter options. If not provided, return all metric data.
     */
    public function getResult($options = array())
    {
        $records = $this->getRecords(array('value'));
        return $this->filterByOptions($records, $options);
    }
} 

Example

  • Task hours consumed in projects closed annually (global)
<?php
/**
 * Task hours consumed in projects closed annually (global).
 * Consume of annual closed project.
 *
 * Scope: global
 * Object: project
 * Purpose: hour
 * Metric Name: Task hours consumed in projects closed annually (global)
 * Unit: h
 * Description: This metric represents the total actual hours consumed to complete tasks in projects that were closed within a given year (global scope). It helps assess workload input and resource utilization efficiency during task execution. A high value may indicate the need to review workflows and resource allocation to improve efficiency and schedule control.
 * Definition: Sum of consumed hours of all project tasks; project status is closed; closed date is within the specified year; exclude deleted projects.
 */
class consume_of_annual_closed_project extends baseCalc
{
    public $dataset = null;
    public $fieldList = array();
    public $result = array();
    public function getStatement()
    {
        $task = $this->dao->select('SUM(consumed) as consumed, project')
            ->from(TABLE_TASK)
            ->where('deleted')->eq('0')
            ->andWhere('parent')->ne('-1')
            ->groupBy('project')
            ->get();
        return $this->dao->select('t1.id as project, LEFT(t1.closedDate, 4) as year, t2.consumed')
            ->from(TABLE_PROJECT)->alias('t1')
            ->leftJoin("($task)")->alias('t2')->on('t1.id = t2.project')
            ->where('t1.type')->eq('project')
            ->andWhere('t1.status')->eq('closed')
            ->andWhere('t1.deleted')->eq('0')
            ->andWhere('t1.closedDate IS NOT NULL')
            ->andWhere('LEFT(t1.closedDate, 4)')->ne('0000')
            ->query();
    }
    public function calculate($data)
    {
        $project  = $data->project;
        $year     = $data->year;
        $consumed = $data->consumed;
        if(!isset($this->result[$year])) $this->result[$year] = array();
        $this->result[$year][$project] = round($consumed, 2);
    }
    public function getResult($options = array())
    {
        $records = array();
        foreach($this->result as $year => $projects)
        {
            foreach($projects as $project => $value)
            {
                $records[] = array('project' => $project, 'year' => $year, 'value' => $value);
            }
        }
        return $this->filterByOptions($records, $options);
    }
} 
Write a Comment
Comment will be posted after it is reviewed.