Skip to content

Commit

Permalink
feat: test
Browse files Browse the repository at this point in the history
  • Loading branch information
Bonn committed Oct 12, 2022
1 parent a0c9249 commit 4bbe3c4
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 0 deletions.
8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changelog

All notable changes to `TransferStatistics` will be documented in this file.

## Version 1.0

### Added
- Everything
39 changes: 39 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "xtheme/transfer-statistics",
"description": "Middleware for Transfer-Statistics v2",
"license": "MIT",
"authors": [
{
"name": "xtheme",
"email": "[email protected]",
"homepage": "https://github.com/xtheme"
}
],
"homepage": "https://github.com/xtheme/transfer-statistics",
"keywords": ["Laravel", "TransferStatistics"],
"require": {
"ext-json": "*",
"illuminate/support": "^8.0|^9.0"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"orchestra/testbench": "~7"
},
"autoload": {
"psr-4": {
"Xtheme\\TransferStatistics\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Xtheme\\TransferStatistics\\Tests\\": "tests"
}
},
"extra": {
"laravel": {
"providers": [
"Xtheme\\TransferStatistics\\TransferStatisticsServiceProvider"
]
}
}
}
6 changes: 6 additions & 0 deletions config/transfer-statistics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return [
'enable' => env('APP_DEBUG', false),
'host' => env('TS_HOST', 'http://host.docker.internal:8788'),
];
27 changes: 27 additions & 0 deletions contributing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Contributing

Contributions are welcome and will be fully credited.

Contributions are accepted via Pull Requests on [Github](https://github.com/xtheme/transfer-statistics).

# Things you could do
If you want to contribute but do not know where to start, this list provides some starting points.
- Add license text
- Remove rewriteRules.php
- Set up TravisCI, StyleCI, ScrutinizerCI
- Write a comprehensive ReadMe

## Pull Requests

- **Add tests!** - Your patch won't be accepted if it doesn't have tests.

- **Document any change in behaviour** - Make sure the `readme.md` and any other relevant documentation are kept up-to-date.

- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.

- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.

- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.


**Happy coding**!
5 changes: 5 additions & 0 deletions license.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# The license

Copyright (c) xtheme <[email protected]>

...Add your license text here...
22 changes: 22 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Package">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>src/</directory>
</whitelist>
</filter>
</phpunit>
76 changes: 76 additions & 0 deletions src/TransferStatisticsMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace xtheme\TransferStatistics;

use Closure;
use GuzzleHttp\Client;
use Illuminate\Http\Request;

/**
* TransferStatistics v2 应用监控系统
*/
class TransferStatisticsMiddleware
{
public function handle(Request $request, Closure $next)
{
if (!config('transfer-statistics.enable')) {
return $next($request);
}

$startTime = LARAVEL_START; // 开始时间
$project = config('app.name'); // 应用名
$ip = $request->ip(); // 请求IP
$transfer = $request->getUri(); // 调用

$response = $next($request);

$finishTime = microtime(true); // 结束时间
$costTime = $finishTime - $startTime; // 运行时长

$code = $response->status(); // 状态码
$success = $code < 400; // 是否成功

// 详细信息,自定义设置
$details = [
'time' => date('Y-m-d H:i:s.', (int) $startTime), // 请求时间(包含毫秒时间)
'run_time' => $costTime, // 运行时长
'request' => $request->input(), // 请求参数
];

// 执行上报
try {
// 数据打包 多条 换行 隔开
$data = json_encode([
'time' => date('Y-m-d H:i:s.', (int) $startTime),
'project' => $project,
'ip' => $ip,
'transfer' => $transfer,
'costTime' => $costTime,
'success' => $success ? 1 : 0,
'code' => $code,
'details' => json_encode($details, 320),
], 320) . "\n";

$client = new Client(['verify' => false]);

$client->post(
// 上报地址
config('transfer-statistics.host') . '/report/statistic/transfer',
[
'headers' => [
// 上报认证,不设置默认为当前年份的md5值
'authorization' => md5(date('Y')),
],
'form_params' => [
// 上报数据
'transfer' => $data,
],
]
);
} catch (\Throwable $th) {
//throw $th;
}

return $response;
}
}
51 changes: 51 additions & 0 deletions src/TransferStatisticsServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Xtheme\TransferStatistics;

use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider;
use Xtheme\TransferStatistics\TransferStatisticsMiddleware;

class TransferStatisticsServiceProvider extends ServiceProvider
{
/**
* Perform post-registration booting of services.
*
* @return void
* @throws BindingResolutionException
*/
public function boot(): void
{
// Publishing is only necessary when using the CLI.
if ($this->app->runningInConsole()) {
$this->bootForConsole();
}

$router = $this->app->make(Router::class);
$router->aliasMiddleware('transfer-statistics', TransferStatisticsMiddleware::class);
}

/**
* Register any package services.
*
* @return void
*/
public function register(): void
{
$this->mergeConfigFrom(__DIR__ . '/../config/transfer-statistics.php', 'transfer-statistics');
}

/**
* Console-specific booting.
*
* @return void
*/
protected function bootForConsole(): void
{
// Publishing the configuration file.
$this->publishes([
__DIR__ . '/../config/transfer-statistics.php' => config_path('transfer-statistics.php'),
], 'config');
}
}

0 comments on commit 4bbe3c4

Please sign in to comment.