11import Component from " @glimmer/component" ;
22import { tracked } from " @glimmer/tracking" ;
3- import { hash } from " @ember/helper" ;
3+ import { fn , hash } from " @ember/helper" ;
4+ import { on } from " @ember/modifier" ;
45import { action } from " @ember/object" ;
56import { service } from " @ember/service" ;
7+ import { eq } from " truth-helpers" ;
68import DatePicker from " discourse/components/date-picker" ;
79import { ajax } from " discourse/lib/ajax" ;
810import i18n from " discourse-common/helpers/i18n" ;
@@ -16,7 +18,8 @@ export default class AiUsage extends Component {
1618 @tracked data = this .args .model ;
1719 @tracked selectedFeature;
1820 @tracked selectedModel;
19- @tracked period = " day" ;
21+ @tracked selectedPeriod = " month" ;
22+ @tracked isCustomDateActive = false ;
2023
2124 @action
2225 async fetchData () {
@@ -26,17 +29,11 @@ export default class AiUsage extends Component {
2629 end_date: moment (this .endDate ).format (" YYYY-MM-DD" ),
2730 feature: this .selectedFeature ,
2831 model: this .selectedModel ,
29- period: this .period ,
3032 },
3133 });
3234 this .data = response;
3335 }
3436
35- @action
36- async onDateChange () {
37- await this .fetchData ();
38- }
39-
4037 @action
4138 async onFilterChange () {
4239 await this .fetchData ();
@@ -108,39 +105,117 @@ export default class AiUsage extends Component {
108105 return this ._cachedModels ;
109106 }
110107
108+ get periodOptions () {
109+ return [
110+ { id: " day" , name: " Last 24 Hours" },
111+ { id: " week" , name: " Last Week" },
112+ { id: " month" , name: " Last Month" },
113+ ];
114+ }
115+
116+ @action
117+ setPeriodDates (period ) {
118+ const now = moment ();
119+
120+ switch (period) {
121+ case " day" :
122+ this .startDate = now .clone ().subtract (1 , " day" ).toDate ();
123+ this .endDate = now .toDate ();
124+ break ;
125+ case " week" :
126+ this .startDate = now .clone ().subtract (7 , " days" ).toDate ();
127+ this .endDate = now .toDate ();
128+ break ;
129+ case " month" :
130+ this .startDate = now .clone ().subtract (30 , " days" ).toDate ();
131+ this .endDate = now .toDate ();
132+ break ;
133+ }
134+ }
135+
136+ @action
137+ onPeriodSelect (period ) {
138+ this .selectedPeriod = period;
139+ this .isCustomDateActive = false ;
140+ this .setPeriodDates (period);
141+ this .fetchData ();
142+ }
143+
144+ @action
145+ onCustomDateClick () {
146+ this .isCustomDateActive = true ;
147+ this .selectedPeriod = null ;
148+ }
149+
150+ @action
151+ onDateChange () {
152+ this .isCustomDateActive = true ;
153+ this .selectedPeriod = null ;
154+ this .fetchData ();
155+ }
156+
111157 <template >
112158 <div class =" ai-usage" >
113159 <div class =" ai-usage__filters" >
114- <div class =" ai-usage__filters-dates" >
115- <DatePicker
116- @ value ={{this .startDate }}
117- @ onChange ={{this .onDateChange }}
118- class =" ai-usage__date-picker"
119- />
120- <DatePicker
121- @ value ={{this .endDate }}
122- @ onChange ={{this .onDateChange }}
123- class =" ai-usage__date-picker"
124- />
125160
126- <div class =" ai-usage__filters-row" >
127- <ComboBox
128- @ value ={{this .selectedFeature }}
129- @ content ={{this .availableFeatures }}
130- @ onChange ={{this .onFeatureChanged }}
131- @ options ={{hash none =" discourse_ai.usage.all_features" }}
132- class =" ai-usage__feature-selector"
133- />
134-
135- <ComboBox
136- @ value ={{this .selectedModel }}
137- @ content ={{this .availableModels }}
138- @ onChange ={{this .onModelChanged }}
139- @ options ={{hash none =" discourse_ai.usage.all_models" }}
140- class =" ai-usage__model-selector"
141- />
161+ <div class =" ai-usage__filters-dates" >
162+ <div class =" ai-usage__period-buttons" >
163+ {{#each this . periodOptions as | option | }}
164+ <button
165+ type =" button"
166+ class =" btn
167+ {{if
168+ ( eq this . selectedPeriod option.id )
169+ ' btn-primary'
170+ ' btn-default'
171+ }} "
172+ {{on " click" ( fn this . onPeriodSelect option.id ) }}
173+ >
174+ {{option.name }}
175+ </button >
176+ {{/each }}
177+ <button
178+ type =" button"
179+ class =" btn
180+ {{if this . isCustomDateActive ' btn-primary' ' btn-default' }} "
181+ {{on " click" this . onCustomDateClick}}
182+ >
183+ Custom...
184+ </button >
142185 </div >
143186
187+ {{#if this . isCustomDateActive }}
188+ <div class =" ai-usage__custom-date-pickers" >
189+ <DatePicker
190+ @ value ={{this .startDate }}
191+ @ onChange ={{this .onDateChange }}
192+ class =" ai-usage__date-picker"
193+ />
194+ <DatePicker
195+ @ value ={{this .endDate }}
196+ @ onChange ={{this .onDateChange }}
197+ class =" ai-usage__date-picker"
198+ />
199+ </div >
200+ {{/if }}
201+ </div >
202+
203+ <div class =" ai-usage__filters-row" >
204+ <ComboBox
205+ @ value ={{this .selectedFeature }}
206+ @ content ={{this .availableFeatures }}
207+ @ onChange ={{this .onFeatureChanged }}
208+ @ options ={{hash none =" discourse_ai.usage.all_features" }}
209+ class =" ai-usage__feature-selector"
210+ />
211+
212+ <ComboBox
213+ @ value ={{this .selectedModel }}
214+ @ content ={{this .availableModels }}
215+ @ onChange ={{this .onModelChanged }}
216+ @ options ={{hash none =" discourse_ai.usage.all_models" }}
217+ class =" ai-usage__model-selector"
218+ />
144219 </div >
145220
146221 {{#if this . data }}
0 commit comments