-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve charts partially resolves #80 #91
Changes from all commits
55f7cef
c76af91
ddf3aa3
f64b708
c03fa19
e443e4c
6bce263
f137559
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,27 @@ | ||
import { getCovidTrends } from '../../../services/api/api-chart'; | ||
import { addDays, Now } from '../../../utils/dateUtils'; | ||
import { CountrySituationInfo } from '../../../models/covid19.models'; | ||
import { Transform } from '../../../services/domain/chart'; | ||
import { | ||
Transform, | ||
enrichWithTitle, | ||
enrichWithType, | ||
} from '../../../services/domain/chart'; | ||
import { catchAsyncError } from '../../../utils/catchError'; | ||
import { getRequestedCountry } from '../../../services/domain/countries'; | ||
import { logger } from '../../../utils/logger'; | ||
import { CallBackQueryHandlerWithCommandArgument } from '../models'; | ||
import * as TelegramBot from 'node-telegram-bot-api'; | ||
import { Frequency } from './../../../models/constants'; | ||
|
||
export const trendsByCountryResponse: CallBackQueryHandlerWithCommandArgument = async ( | ||
bot: TelegramBot, | ||
message: TelegramBot.Message, | ||
chatId: number, | ||
requestedCountry?: string | undefined | ||
requestedCountry?: string | undefined, | ||
requestedFrequency?: Frequency | undefined | ||
): Promise<TelegramBot.Message> => { | ||
const ferequency = requestedFrequency || Frequency.Weekly; | ||
|
||
const [err, [foundCountry, foundSituation]] = await catchAsyncError( | ||
getRequestedCountry(requestedCountry) | ||
); | ||
|
@@ -25,12 +33,44 @@ export const trendsByCountryResponse: CallBackQueryHandlerWithCommandArgument = | |
|
||
return bot.sendMessage(chatId, err.message); | ||
} | ||
const lastWeekSituation = foundSituation.filter( | ||
(c: CountrySituationInfo) => { | ||
|
||
let startDate: Date; | ||
let hasFilter = true; | ||
switch (ferequency) { | ||
case Frequency.Weekly: | ||
startDate = addDays(Now, -7); | ||
break; | ||
case Frequency.Monthly: | ||
startDate = addDays(Now, -30); | ||
break; | ||
case Frequency.WholePeriod: | ||
hasFilter = false; | ||
break; | ||
} | ||
|
||
let periodSituation = foundSituation; | ||
if (hasFilter) { | ||
periodSituation = periodSituation.filter((c: CountrySituationInfo) => { | ||
const date = new Date(c.date); | ||
return date < Now && date > addDays(Now, -7); | ||
} | ||
return date < Now && date > startDate; | ||
}); | ||
} | ||
|
||
const frequencyName = | ||
ferequency === Frequency.WholePeriod | ||
? 'Whole period' | ||
: capitalize(ferequency); | ||
|
||
let model = enrichWithTitle( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is that really a model? Maybe a chart? Does it have a type? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TypesTypesTypesTypesTypesTypeskriptkriptkriptkriptkriptkriptThere was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @danbilokha There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @danbilokha try to hover your mouse on the variable. And you'll see the macig. |
||
Transform(periodSituation), | ||
`${frequencyName} trends for ${capitalize(requestedCountry)}` | ||
); | ||
if (ferequency === Frequency.Weekly) { | ||
model = enrichWithType(model, 'barStacked'); | ||
} | ||
|
||
return bot.sendPhoto(chatId, getCovidTrends(Transform(lastWeekSituation))); | ||
return bot.sendPhoto(chatId, getCovidTrends(model)); | ||
}; | ||
|
||
const capitalize = (input: string): string => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that function should go to utils There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lovely utils |
||
input.charAt(0).toUpperCase() + input.substring(1).toLowerCase(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { CallBackQueryHandlerWithCommandArgument } from '../../models'; | ||
import * as TelegramBot from 'node-telegram-bot-api'; | ||
import { LogCategory } from '../../../../models/constants'; | ||
import { logger } from '../../../../utils/logger'; | ||
import { noResponse } from '../../botResponse/noResponse'; | ||
|
||
export const withTwoArgumentsAfterCommand = ( | ||
handlerFn: CallBackQueryHandlerWithCommandArgument | ||
): CallBackQueryHandlerWithCommandArgument => { | ||
return ( | ||
bot: TelegramBot, | ||
message: TelegramBot.Message, | ||
chatId: number, | ||
ikCbData?: string | ||
): Promise<TelegramBot.Message> => { | ||
try { | ||
const [arg1, arg2] = splitArgument(ikCbData); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. talked |
||
|
||
return handlerFn.call( | ||
this, | ||
bot, | ||
message, | ||
chatId, | ||
(arg1 && arg1.toLowerCase()) || ikCbData, | ||
arg2 && arg2.toLowerCase() | ||
); | ||
} catch (err) { | ||
logger.error( | ||
`Error happend inside withTwoArgumentsAfterCommand() for ${chatId} with message: ${message.text} and ikCbData: ${ikCbData}`, | ||
err, | ||
LogCategory.Command, | ||
chatId | ||
); | ||
|
||
return noResponse(this.bot, message, chatId); | ||
} | ||
}; | ||
}; | ||
|
||
const splitArgsRegexp = new RegExp( | ||
'(?<firstArg>[^ ]+)[\\s.,;]+(?<secondArg>[^ ]+)' | ||
); | ||
function splitArgument(argument: string): [string, string] { | ||
const match = splitArgsRegexp.exec(argument); | ||
if (!match) { | ||
return [undefined, undefined]; | ||
} | ||
|
||
/* tslint:disable:no-string-literal */ | ||
return [match.groups['firstArg'], match.groups['secondArg']]; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,8 +48,14 @@ export enum Continents { | |
Other = 'Other', | ||
} | ||
|
||
export enum Frequency { | ||
Weekly = 'weekly', | ||
Monthly = 'monthly', | ||
WholePeriod = 'wholeperiod', | ||
} | ||
|
||
export enum Status { | ||
Confirmed = 'confirmed', | ||
Confirmed = 'active', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed !== active There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My bad 😞 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @danbilokha Do you say I'm showing wrong data to user There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename for consistency, I am not sure how you use that for showing the data |
||
Deaths = 'deaths', | ||
Recovered = 'recovered', | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export const addDays = (date: string | Date, days: number) => { | ||
export const addDays = (date: string | Date, days: number): Date => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks 👍 really appreciate typings There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TypesTypesTypesTypesTypesTypeskriptkriptkriptkriptkriptkript |
||
const result = new Date(date); | ||
result.setDate(result.getDate() + days); | ||
return result; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know that you just followed the pattern, but I recently realized that It means
requestedFrequency might be present and might not
AND
when it presents it could be
Frequency
and could beundefined
.If that's interned, then go for that :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@danbilokha
Could you please paste what I should do with it?
Are you saying that undefined is redundant?
And I should remove it to be
requestedFrequency?: Frequency
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just told you how it works so you were aware of that.