Skip to content

Commit ba1ce93

Browse files
committed
feat(array stats and local test): stats fonksiyonu eklendi
1 parent f673cd5 commit ba1ce93

File tree

9 files changed

+353
-49
lines changed

9 files changed

+353
-49
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ layer/php/vendor
55

66
# mac info file
77
**/.DS_Store
8-
**/.idea
8+
**/.idea
9+
10+
# Output
11+
serverless-output.yaml

README.MD

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ The basics of using [SAM Cli][1] for AWS Lambda PHP applications.
55
## Deploy
66
1. Install SAM Cli by following the [Quick Start][2]
77
2. Set up your [AWS credentials][3]
8-
3. Create php binary by following steps in [`doc/create_php_binary.md`][4] or use [Php 7.3.1][5] or just put the yml this projects ARN 'arn:aws:lambda:eu-central-1:303814004728:layer:aws-lambda-layer-php-7-3-1:3'
8+
3. Create php binary by following steps in [`doc/create_php_binary.md`][4] or use [Php 7.3.1][5] or just put the yml this projects ARN 'arn:aws:lambda:eu-central-1:303814004728:layer:aws-lambda-layer-php-7-3-1:6'
99
4. Write your serverless application (!) - the default is in `src/handlers/hello.php`
1010
5. Run `./deploy.sh` to deploy to Lambda
1111

@@ -24,10 +24,14 @@ Hello world looks like:
2424
return ["msg" => "Hello from PHP " . PHP_VERSION];
2525
}
2626

27+
## Local Tests
28+
Run `echo "hi" | sam local invoke "HelloPhp73"`
29+
Result: `{"msg":"hello from PHP 7.3.1","eventData":"hi\n","data":null}`
30+
2731

2832
[1]: https://github.com/awslabs/aws-sam-cli
2933
[2]: https://docs.aws.amazon.com/en_us/serverless-application-model/latest/developerguide/serverless-quick-start.html
3034
[3]: https://serverless.com/framework/docs/providers/aws/guide/credentials/
3135
[4]: doc/create_php_binary.md
3236
[5]: layer/php/php
33-
[6]: 'arn:aws:lambda:eu-central-1:303814004728:layer:aws-lambda-layer-php-7-3-1:3'
37+
[6]: 'arn:aws:lambda:eu-central-1:303814004728:layer:aws-lambda-layer-php-7-3-1:6'

layer/php/composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"require": {
3-
"umutc/array-statistics": "^1.0",
43
"aws/aws-sdk-php": "^3.85"
54
}
65
}

layer/php/composer.lock

Lines changed: 1 addition & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

serverless-output.yaml

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,49 @@
11
AWSTemplateFormatVersion: 2010-09-09
22
Description: Magistum Serverless PHP 7.3.1
33
Resources:
4+
ArrayStats:
5+
Properties:
6+
CodeUri: s3://magistum-sam/539b4fe0df7166cc04e0104df34a7449
7+
Description: This is the statistics function thas calculates payload array.
8+
FunctionName:
9+
Fn::Sub: ${AWS::StackName}-array-stats
10+
Handler: array.stats
11+
Layers:
12+
- Fn::Sub: arn:aws:lambda:eu-central-1:303814004728:layer:aws-lambda-layer-php-7-3-1:6
13+
MemorySize: 128
14+
Runtime: provided
15+
Timeout: 4
16+
Tracing: Active
17+
Type: AWS::Serverless::Function
418
HelloPhp73:
519
Properties:
6-
CodeUri: s3://magistum-sam/0dbcebf1080bcffc055b5d6706a137c3
20+
CodeUri: s3://magistum-sam/539b4fe0df7166cc04e0104df34a7449
721
Description: This is the hello world function from lambda php 7.3.1 layer
822
FunctionName:
923
Fn::Sub: ${AWS::StackName}-hello
1024
Handler: hello.hello
1125
Layers:
12-
- Fn::Sub: arn:aws:lambda:eu-central-1:303814004728:layer:aws-lambda-layer-php-7-3-1:3
26+
- Fn::Sub: arn:aws:lambda:eu-central-1:303814004728:layer:aws-lambda-layer-php-7-3-1:6
27+
MemorySize: 128
28+
Runtime: provided
29+
Timeout: 4
30+
Tracing: Active
31+
Type: AWS::Serverless::Function
32+
Info:
33+
Properties:
34+
CodeUri: s3://magistum-sam/539b4fe0df7166cc04e0104df34a7449
35+
Description: This is the statistics function thas calculates payload array.
36+
FunctionName:
37+
Fn::Sub: ${AWS::StackName}-info
38+
Handler: array.info
39+
Layers:
40+
- Fn::Sub: arn:aws:lambda:eu-central-1:303814004728:layer:aws-lambda-layer-php-7-3-1:6
1341
MemorySize: 128
42+
Policies:
43+
- S3ReadPolicy:
44+
BucketName: magistum-sam
1445
Runtime: provided
15-
Timeout: 30
46+
Timeout: 4
1647
Tracing: Active
1748
Type: AWS::Serverless::Function
1849
Transform: AWS::Serverless-2016-10-31

src/array.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
require_once __DIR__ . '/lib/ArrayStats.php';
4+
5+
use ArrayStatistics\ArrayStats;
6+
use \ArrayStatistics\GraphItem;
7+
8+
9+
function stats($eventData): array
10+
{
11+
$data = json_decode($eventData);
12+
$response = [];
13+
try {
14+
foreach ($data->points as $key => $point) {
15+
$arrayStats = new ArrayStats($point->results);
16+
$data->points[$key]->stats = (object)[
17+
"_min" => $data->min,
18+
"_max" => $data->max,
19+
"mean" => $arrayStats->mean(),
20+
"median" => $arrayStats->median(),
21+
"mode" => $arrayStats->mode(),
22+
"range" => $arrayStats->range(),
23+
"standard_deviation" => $arrayStats->standard_deviation(),
24+
"frequency" => $arrayStats->frequency(),
25+
"min" => $arrayStats->min(),
26+
"max" => $arrayStats->max(),
27+
"maxGraphDataValue" => 0,
28+
"graphData" => $arrayStats->graphData($data->min, $data->max),
29+
];
30+
$maxGraphDataValue = max(array_map(function(GraphItem $item) {
31+
return $item->getValue();
32+
}, $data->points[$key]->stats->graphData));
33+
$data->points[$key]->stats->maxGraphDataValue = $maxGraphDataValue;
34+
35+
$x = null;
36+
if ($maxGraphDataValue <= 0.0001) $x = 10000;
37+
elseif ($maxGraphDataValue <= 0.001) $x = 1000;
38+
elseif ($maxGraphDataValue <= 0.01) $x = 100;
39+
elseif ($maxGraphDataValue <= 0.1) $x = 10;
40+
41+
if ($x) {
42+
foreach ($data->points[$key]->stats->graphData as $item) {
43+
$item->multiplyValue($x);
44+
}
45+
}
46+
unset($data->points[$key]->results);
47+
unset($stats);
48+
}
49+
$response = $data->points;
50+
unset($data);
51+
} catch (Exception $e) {
52+
}
53+
return $response;
54+
}
55+
56+
function info($eventData): array
57+
{
58+
$response = [
59+
'PHP_VERSION' => PHP_VERSION,
60+
'__DIR__' => __DIR__,
61+
];
62+
return $response;
63+
}

src/handlers/hello.php renamed to src/hello.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ function hello($eventData) : array
33
{
44
$response = ['msg' => 'hello from PHP '.PHP_VERSION];
55
$response['eventData'] = $eventData;
6-
$data = json_decode($eventData);
7-
$response['data'] = $data;
6+
$response['data'] = json_decode($eventData);
87
return $response;
98
}

0 commit comments

Comments
 (0)