Scalr\Stats\CostAnalytics\Usage::getFarmPointData PHP Метод

getFarmPointData() публичный Метод

Get period data for one point on chart
public getFarmPointData ( integer $accountId, string $projectId = null, integer $envId = null, integer | array $farmId = null, integer $farmRoleId = null, string $mode, string $date, string $start, string $end ) : array
$accountId integer Identifier of the Account
$projectId string optional Identifier of the Project
$envId integer optional Identifier of the Environment
$farmId integer | array optional Identifier of the Farm, or the list of the farms which should be excluded
$farmRoleId integer optional Identifier of the Farm Role Id
$mode string The mode (chart)
$date string The UTC date within period ('Y-m-d H:00')
$start string The start date of the period in UTC ('Y-m-d')
$end string The end date of the period in UTC ('Y-m-d')
Результат array
    public function getFarmPointData($accountId, $projectId = null, $envId = null, $farmId = null, $farmRoleId = null, $mode, $date, $start, $end)
    {
        $criteria = [];
        if ($envId) {
            $criteria['envId'] = $envId;
        }
        if ($farmId) {
            $criteria['farmId'] = $farmId;
        }
        if ($farmRoleId) {
            $criteria['farmRoleId'] = $farmRoleId;
        }
        if ($projectId) {
            $criteria['projectId'] = $projectId;
        }
        $chartPoint = null;
        if (!empty($date)) {
            $iterator = ChartPeriodIterator::create($mode, $start, $end ?: null, 'UTC');
            //Interval which is used in the database query for grouping
            $queryInterval = preg_replace('/^1 /', '', $iterator->getInterval());
            //Finds the key for current label
            foreach ($iterator as $chartPoint) {
                /* @var $chartPoint \Scalr\Stats\CostAnalytics\ChartPointInfo */
                if ($chartPoint->dt->format('Y-m-d H:00') == $date) {
                    break;
                }
            }
        } else {
            $queryInterval = $mode == 'day' ? 'hour' : 'day';
        }
        if ($chartPoint === null) {
            $intervalStart = new DateTime($start . " 00:00:00", new DateTimeZone('UTC'));
            $intervalEnd = new DateTime($end . " 23:59:59", new DateTimeZone('UTC'));
        } else {
            $intervalStart = $chartPoint->dt;
            if ($chartPoint->isLastPoint) {
                $intervalEnd = new DateTime($end . " 23:59:59", new DateTimeZone('UTC'));
            } else {
                $iterator->next();
                $intervalEnd = $iterator->current()->dt->modify('-1 second');
            }
        }
        if ($queryInterval == 'hour') {
            $criteria['hourly'] = true;
        }
        //Requests data for the specified period
        $usg = (new AggregationCollection(['distributionType', 'usageType' => ['name', 'displayName'], 'usageItem' => ['envId', 'platform', 'cloudLocation', 'id']], ['cost' => 'sum', 'minUsage' => 'min', 'maxUsage' => 'max', 'usageHours' => 'sum', 'workingHours' => 'sum']))->load($this->getFarmData($accountId, $criteria, $intervalStart, $intervalEnd, ['distributionType', 'usageType', 'usageItem'], true))->calculatePercentage();
        $distrTypes = [];
        if (!empty($usg['data'])) {
            foreach ($usg['data'] as $distrType => $distrUsage) {
                $usageTypesData = [];
                $distrTypesDataPoint = $this->getDetailedPointDataArray($distrType, $distrType, $distrUsage, null, null);
                foreach ($distrUsage['data'] as $usageType => $uv) {
                    $usageTypeDataPoint = ['id' => $usageType, 'name' => $uv['name'], 'displayName' => $uv['displayName'], 'measure' => $this->getMeasure($uv['name'])];
                    if (!empty($uv['data'])) {
                        $usageItemsData = [];
                        foreach ($uv['data'] as $usageItem => $iv) {
                            if ($uv['name'] == UsageTypeEntity::NAME_COMPUTE_BOX_USAGE) {
                                $usageItemName = $this->getInstanceTypeName($usageItem, $iv['envId'], $iv['platform'], $iv['cloudLocation']);
                            } else {
                                $usageItemName = $usageItem;
                            }
                            $usageItemDataPoint = $this->getDetailedPointDataArray($iv['id'], $usageItemName, $iv, null, null);
                            $usageItemDataPoint['costPct'] = !empty($usg['cost']) ? round($usageItemDataPoint['cost'] / $usg['cost'] * 100) : 0;
                            $usageItemDataPoint['min'] = $iv['minUsage'];
                            $usageItemDataPoint['max'] = $iv['maxUsage'];
                            $usageItemDataPoint['avg'] = !empty($iv['workingHours']) ? round($iv['usageHours'] / $iv['workingHours']) : 0;
                            $usageItemDataPoint['hours'] = $iv['usageHours'];
                            $usageItemDataPoint['displayHours'] = $this->getDisplayHours($iv['usageHours']);
                            $usageItemsData[] = $usageItemDataPoint;
                        }
                        $usageTypeDataPoint['usageItems'] = $usageItemsData;
                    }
                    $usageTypesData[] = $usageTypeDataPoint;
                }
                $distrTypesDataPoint['usageTypes'] = $usageTypesData;
                $distrTypes[] = $distrTypesDataPoint;
            }
        }
        return $distrTypes;
    }