Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
UsageTrait
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getUsage
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Traits;
4
5use App\Http\Models\FlyCutUsage;
6use App\Http\Models\FlyGrammarActions;
7use App\Http\Models\RolePlayConversations;
8use App\Http\Models\RolePlayProjects;
9use App\Http\Models\Shortcut;
10use MongoDB\BSON\UTCDateTime;
11
12trait UsageTrait
13{
14    public function getUsage($user_id, $date)
15    {
16        $from = new UTCDateTime(strtotime($date->startOfDay()->toDateTimeString()) * 1000);
17        $to = new UTCDateTime(strtotime($date->endOfDay()->toDateTimeString()) * 1000);
18
19        $flyGrammarCharactersSaved = FlyGrammarActions::where('user_id', $user_id)->where('created_at', '>=', $from)->where('created_at', '<=', $to)->sum('characters_count');
20        $flyGrammarCostSaved = FlyGrammarActions::where('user_id', $user_id)->where('created_at', '>=', $from)->where('created_at', '<=', $to)->sum('cost_saved');
21        $flyGrammarTimeSaved = FlyGrammarActions::where('user_id', $user_id)->where('created_at', '>=', $from)->where('created_at', '<=', $to)->sum('time_saved');
22
23        $charactersTyped = FlyCutUsage::where('user_id', $user_id)->where('created_at', '>=', $from)->where('created_at', '<=', $to)->sum('characters_saved');
24        $costSaved = FlyCutUsage::where('user_id', $user_id)->where('created_at', '>=', $from)->where('created_at', '<=', $to)->sum('cost_saved');
25        $timeSaved = FlyCutUsage::where('user_id', $user_id)->where('created_at', '>=', $from)->where('created_at', '<=', $to)->sum('time_saved');
26
27        return [
28            'time_saved' => $timeSaved + $flyGrammarTimeSaved,
29            'cost_savings' => $costSaved + $flyGrammarCostSaved,
30            'characters_typed' => $charactersTyped + $flyGrammarCharactersSaved,
31            'flycuts_created' => Shortcut::where('user_id', $user_id)->where('user_defined', true)->where('created_at', '>=', $from)->where('created_at', '<=', $to)->count(),
32            'flyplates_added' => Shortcut::where('user_id', $user_id)->where('user_defined', false)->where('created_at', '>=', $from)->where('created_at', '<=', $to)->count(),
33            'sentence_rewrite_count' => FlyCutUsage::where('user_id', $user_id)->where('created_at', '>=', $from)->where('created_at', '<=', $to)->where('feature', 'sentence_rewrite')->count(),
34            'paragraph_rewrite_count' => FlyCutUsage::where('user_id', $user_id)->where('created_at', '>=', $from)->where('created_at', '<=', $to)->where('feature', 'paragraph_rewrite')->count(),
35            'flyengage_count' => FlyCutUsage::where('user_id', $user_id)->where('created_at', '>=', $from)->where('created_at', '<=', $to)->where('feature', 'flyengage')->count(),
36            'flypost_count' => FlyCutUsage::where('user_id', $user_id)->where('created_at', '>=', $from)->where('created_at', '<=', $to)->where('feature', 'flypost')->count(),
37            'flycut_count' => FlyCutUsage::where('user_id', $user_id)->where('created_at', '>=', $from)->where('created_at', '<=', $to)->where('feature', null)->count(),
38            'fly_grammar_actions' => FlyGrammarActions::where('user_id', $user_id)->where('created_at', '>=', $from)->where('created_at', '<=', $to)->count(),
39            'fly_grammar_accepted' => FlyGrammarActions::where('user_id', $user_id)->where('created_at', '>=', $from)->where('created_at', '<=', $to)->where('action_type', '!=', 'autocorrect')->where('action_type', '!=', 'autocomplete')->count(),
40            'fly_grammar_autocorrect' => FlyGrammarActions::where('user_id', $user_id)->where('created_at', '>=', $from)->where('created_at', '<=', $to)->where('action_type', 'autocorrect')->count(),
41            'fly_grammar_autocomplete' => FlyGrammarActions::where('user_id', $user_id)->where('created_at', '>=', $from)->where('created_at', '<=', $to)->where('action_type', 'autocomplete')->count(),
42            'roleplay_personas_created' => RolePlayProjects::withTrashed()->where('user_id', $user_id)->where('created_at', '>=', $from)->where('created_at', '<=', $to)->count(),
43            'roleplay_sessions_practiced' => RolePlayConversations::withTrashed()->where('user_id', $user_id)->where('status', 'done')->where('created_at', '>=', $from)->where('created_at', '<=', $to)->count(),
44            'roleplay_time_practiced' => (int) RolePlayConversations::withTrashed()->where('user_id', $user_id)->where('status', 'done')->where('created_at', '>=', $from)->where('created_at', '<=', $to)->sum('duration'),
45            'roleplay_daily_avg_score' => (float) RolePlayConversations::withTrashed()->where('user_id', $user_id)->where('status', 'done')->where('created_at', '>=', $from)->where('created_at', '<=', $to)->avg('score'),
46        ];
47    }
48}