Skip to content

Commit

Permalink
Update requests endpoint to store values for the previous day
Browse files Browse the repository at this point in the history
  • Loading branch information
mzur committed Apr 3, 2024
1 parent 7cc02f1 commit e1ec516
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/Http/Controllers/RequestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class RequestController extends Controller
* @apiDefine kpi-tpken KPI Token
* The request must provide the token configured in the KPI module.
*
* @api {post} /kpis
* @api {post} /kpis Save actions/visits KPIs
* @apiPermission kpi-token
* @apiDescription Save actions/visits KPIs
* @apiDescription The submitted values are stored for the previous day.
* @apiParam {Number} visits The number of GET requests to `/` (without bots).
* @apiParam {Number} actions The number of PUT/POST/DELETE requests (without bots and heartbeat).
*
Expand Down
6 changes: 3 additions & 3 deletions src/Requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class Requests
public static function save($visits, $actions)
{
DB::transaction(function () use ($visits, $actions) {
$today = Carbon::today()->toDateString();
DB::table('kpis_actions')->insert(['date' => $today, 'value' => $actions]);
DB::table('kpis_visits')->insert(['date' => $today, 'value' => $visits]);
$yesterday = Carbon::yesterday()->toDateString();
DB::table('kpis_actions')->insert(['date' => $yesterday, 'value' => $actions]);
DB::table('kpis_visits')->insert(['date' => $yesterday, 'value' => $visits]);
});
}

Expand Down
6 changes: 3 additions & 3 deletions tests/Http/Controllers/RequestControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public function testStore()
['visits' => '94556', 'actions' => '21602']
)->assertStatus(200);

$date = Carbon::now()->toDateString();
$date = Carbon::yesterday()->toDateString();

$actions = DB::table('kpis_actions')->where('date', '=', $date)->pluck('value')[0];
$visits = DB::table('kpis_visits')->where('date', '=', $date)->pluck('value')[0];
$actions = DB::table('kpis_actions')->where('date', '=', $date)->first()->value;
$visits = DB::table('kpis_visits')->where('date', '=', $date)->first()->value;

$this->assertEquals(94556, $visits);
$this->assertEquals(21602, $actions);
Expand Down
4 changes: 2 additions & 2 deletions tests/RequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function testSave()
{
Requests::save(100, 50);

$date = Carbon::now()->toDateString();
$date = Carbon::yesterday()->toDateString();

$actions = DB::table('kpis_actions')->where('date', '=', $date)->pluck('value');
$visits = DB::table('kpis_visits')->where('date', '=', $date)->pluck('value');
Expand All @@ -30,7 +30,7 @@ public function testSaveBigInt(){

Requests::save($maxBigInt, $maxBigInt);

$date = Carbon::now()->toDateString();
$date = Carbon::yesterday()->toDateString();

$actions = DB::table('kpis_actions')->where('date', '=', $date)->pluck('value');
$visits = DB::table('kpis_visits')->where('date', '=', $date)->pluck('value');
Expand Down

0 comments on commit e1ec516

Please sign in to comment.