From 13938ecf211060a3f50747ac7fed343b71a6aa4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wieland=20Sch=C3=B6bl?= Date: Tue, 24 Sep 2019 10:44:40 +0200 Subject: [PATCH] feat: boost academic terms dynamically --- config/default-b-tu.ts | 41 ++++++++++++++++++++++++++++++++ config/default-fb-fh.ts | 42 +++++++++++++++++++++++++++++++++ config/default-ks-ug.ts | 42 +++++++++++++++++++++++++++++++++ config/default.ts | 45 ++++++++++++++++++------------------ config/elasticsearch-b-tu.ts | 15 +++--------- config/elasticsearch.ts | 6 +++-- src/common.ts | 10 ++++++++ 7 files changed, 164 insertions(+), 37 deletions(-) create mode 100644 config/default-b-tu.ts create mode 100644 config/default-fb-fh.ts create mode 100644 config/default-ks-ug.ts diff --git a/config/default-b-tu.ts b/config/default-b-tu.ts new file mode 100644 index 00000000..01a6113a --- /dev/null +++ b/config/default-b-tu.ts @@ -0,0 +1,41 @@ +// tslint:disable:no-default-export +// tslint:disable:no-magic-numbers +import {SCConfigFile} from '@openstapps/core'; +import {RecursivePartial} from '@openstapps/logger/lib/common'; +import * as moment from 'moment'; +import {inRangeInclusive} from '../src/common'; + +const ssRange = [4, 9]; +const wsRange = [10, 3]; +const month = moment() + .month(); +const year = moment() + .year(); +const wsYearOffset = (month < wsRange[0] ? -1 : 0); +const wsAcronym = `WS ${year + wsYearOffset}/${(year + 1 + wsYearOffset) + .toString() + .slice(-2)}`; +const ssAcronym = `SS ${year + (month <= wsRange[1] ? -1 : 0)}`; + +/** + * This is the default configuration for the technical university of Berlin + */ +const config: RecursivePartial = { + internal: { + boostings: { + default: [ + { + factor: 1, + fields: { + 'academicTerms.acronym': { + [ssAcronym]: inRangeInclusive(month, ssRange) ? 1.1 : 1.05, + [wsAcronym]: inRangeInclusive(month, wsRange) ? 1.1 : 1.05, + }, + }, + }, + ], + }, + }, +}; + +export default config; diff --git a/config/default-fb-fh.ts b/config/default-fb-fh.ts new file mode 100644 index 00000000..e9354a62 --- /dev/null +++ b/config/default-fb-fh.ts @@ -0,0 +1,42 @@ +// tslint:disable:no-default-export +// tslint:disable:no-magic-numbers +import {SCConfigFile} from '@openstapps/core'; +import {RecursivePartial} from '@openstapps/logger/lib/common'; +import * as moment from 'moment'; +import {inRangeInclusive} from '../src/common'; + +const ssRange = [4, 9]; +const wsRange = [10, 3]; +const month = moment() + .month(); +const year = moment() + .year(); +const wsYearOffset = (month < wsRange[0] ? -1 : 0); +const wsAcronym = `WS ${(year + wsYearOffset).toString() + .slice(-2)}/${(year + 1 + wsYearOffset).toString() + .slice(-2)}`; +const ssAcronym = `SS ${(year + (month <= wsRange[1] ? -1 : 0)).toString() + .slice(-2)}`; + +/** + * This is the default configuration for the university of Kassel + */ +const config: RecursivePartial = { + internal: { + boostings: { + default: [ + { + factor: 1, + fields: { + 'academicTerms.acronym': { + [ssAcronym]: inRangeInclusive(month, ssRange) ? 1.1 : 1.05, + [wsAcronym]: inRangeInclusive(month, wsRange) ? 1.1 : 1.05, + }, + }, + }, + ], + }, + }, +}; + +export default config; diff --git a/config/default-ks-ug.ts b/config/default-ks-ug.ts new file mode 100644 index 00000000..cbe9290f --- /dev/null +++ b/config/default-ks-ug.ts @@ -0,0 +1,42 @@ +// tslint:disable:no-default-export +// tslint:disable:no-magic-numbers +import {SCConfigFile} from '@openstapps/core'; +import {RecursivePartial} from '@openstapps/logger/lib/common'; +import * as moment from 'moment'; +import {inRangeInclusive} from '../src/common'; + +const ssRange = [4, 9]; +const wsRange = [10, 3]; +const month = moment() + .month(); +const year = moment() + .year(); +const wsYearOffset = (month < wsRange[0] ? -1 : 0); +const wsAcronym = `WiSe ${(year + wsYearOffset).toString() + .slice(-2)}/${(year + 1 + wsYearOffset).toString() + .slice(-2)}`; +const ssAcronym = `SoSe ${(year + (month <= wsRange[1] ? -1 : 0)).toString() + .slice(-2)}`; + +/** + * This is the default configuration for the university of Kassel + */ +const config: RecursivePartial = { + internal: { + boostings: { + default: [ + { + factor: 1, + fields: { + 'academicTerms.acronym': { + [ssAcronym]: inRangeInclusive(month, ssRange) ? 1.1 : 1.05, + [wsAcronym]: inRangeInclusive(month, wsRange) ? 1.1 : 1.05, + }, + }, + }, + ], + }, + }, +}; + +export default config; diff --git a/config/default.ts b/config/default.ts index 06551a32..e71f421d 100644 --- a/config/default.ts +++ b/config/default.ts @@ -1,3 +1,5 @@ +// tslint:disable:no-default-export +// tslint:disable:no-magic-numbers import {SCConfigFile, SCThingType} from '@openstapps/core'; /** @@ -184,10 +186,6 @@ const config: Partial = { { factor: 1, fields: { - 'academicTerms.acronym': { - 'SS 2018': 1.05, - 'WS 2018/19': 1.1, - }, 'categories': { 'course': 1.08, 'integrated course': 1.08, @@ -225,6 +223,25 @@ const config: Partial = { type: SCThingType.Dish, }, ], + dining: [ + { + factor: 1, + fields: { + 'categories': { + 'cafe': 2, + 'canteen': 2, + 'restaurant': 2, + 'restroom': 1.2, + 'student canteen': 2, + }, + }, + type: SCThingType.Building, + }, + { + factor: 2, + type: SCThingType.Dish, + }, + ], place: [ { factor: 2, @@ -239,28 +256,10 @@ const config: Partial = { type: SCThingType.Room, }, ], - dining: [ - { - factor: 1, - fields: { - 'categories': { - 'cafe': 2, - 'restaurant': 2, - 'canteen': 2, - 'student canteen': 2, - 'restroom': 1.2, - }, - }, - type: SCThingType.Building, - }, - { - factor: 2, - type: SCThingType.Dish, - }, - ], }, }, uid: 'b-tu', }; +// tslint:disable-next-line:no-default-export export default config; diff --git a/config/elasticsearch-b-tu.ts b/config/elasticsearch-b-tu.ts index c93e9d61..c356c27d 100644 --- a/config/elasticsearch-b-tu.ts +++ b/config/elasticsearch-b-tu.ts @@ -1,17 +1,8 @@ +// tslint:disable:no-default-export +// tslint:disable:no-magic-numbers +import {RecursivePartial} from '@openstapps/logger/lib/common'; import {ElasticsearchConfigFile} from '../src/storage/elasticsearch/common'; -/** - * A partial type which is recursive - * - * Copied and only modified array type from `[]` to `Array<>` from https://stackoverflow.com/a/51365037 - */ -type RecursivePartial = { - [P in keyof T]?: - T[P] extends Array<(infer U)> ? Array> : - T[P] extends object ? RecursivePartial : - T[P]; -}; - /** * This is the database configuration for the technical university of berlin */ diff --git a/config/elasticsearch.ts b/config/elasticsearch.ts index e91c5cb9..f82002d2 100644 --- a/config/elasticsearch.ts +++ b/config/elasticsearch.ts @@ -1,3 +1,5 @@ +// tslint:disable:no-default-export +// tslint:disable:no-magic-numbers import {ElasticsearchConfigFile} from '../src/storage/elasticsearch/common'; /** @@ -8,8 +10,8 @@ import {ElasticsearchConfigFile} from '../src/storage/elasticsearch/common'; * To select your university specific configuration which is merged from this default file and your university specific * file, you have to supply the `NODE_APP_INSTANCE` environment variable with your license plate * - * To select a differen database you have to supply the `NODE_CONFIG_ENV` environment variable with a database name that - * is implemented in the backend + * To select a different database you have to supply the `NODE_CONFIG_ENV` environment variable with a database name + * that is implemented in the backend * * To get more information about the meaning of specific fields please use your IDE to read the TSDoc documentation. */ diff --git a/src/common.ts b/src/common.ts index ba5c48c6..79640d53 100644 --- a/src/common.ts +++ b/src/common.ts @@ -20,6 +20,16 @@ import {readFileSync} from 'fs'; import {resolve} from 'path'; import {BackendTransport} from './notification/backend-transport'; +/** + * Evaluates if a number is within the given range + * + * @param num The number that should be checked + * @param range Array of two numbers representing a range (inclusive interval) + */ +export function inRangeInclusive(num: number, range: number[]): boolean { + return num >= range[0] && num <= range[1]; +} + /** * Instance of the transport for sending mails */