-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from IvyZ23/master
merge: complete TypeScript implementation (Ivy Zhu)
- Loading branch information
Showing
15 changed files
with
2,301 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* calculate.js | ||
* Calculate various metrics on the timeline follow-back data. | ||
*/ | ||
|
||
import { UseEvent, CalendarEvent } from "./state"; | ||
import { CVT_MS_DAY } from "./constants"; | ||
|
||
|
||
function calc_use_days(eventlist: Array<CalendarEvent>, category: string, include_unknown: boolean): Array<string> { | ||
|
||
let use_days: Set<string> = new Set(); | ||
let use_days_amount: Set<string> = new Set(); | ||
|
||
for (let event of eventlist) { | ||
if((event as UseEvent).properties.category === category){ | ||
use_days.add(event.date.substring(0,10)); | ||
|
||
if ((event as UseEvent).properties.amount != "unknown") { | ||
use_days_amount.add(event.date.substring(0,10)); | ||
} | ||
} | ||
} | ||
|
||
if (include_unknown) { | ||
return Array.from(use_days); | ||
} else { | ||
return Array.from(use_days_amount); | ||
} | ||
|
||
} | ||
|
||
export function calc_days_used(eventlist: Array<CalendarEvent>, category: string) { | ||
|
||
let use_days = calc_use_days(eventlist, category, true); | ||
|
||
return use_days.length; | ||
} | ||
|
||
// Used to calculate number of days where amount used is known. | ||
export function calc_days_used_amount(eventlist: Array<CalendarEvent>, category: string) { | ||
|
||
let use_days = calc_use_days(eventlist, category, false); | ||
|
||
return use_days.length; | ||
} | ||
|
||
export function calc_total_occasions(eventlist: Array<CalendarEvent>, category: string) { | ||
|
||
let total = 0; | ||
|
||
for (let event of eventlist) { | ||
if((event as UseEvent).properties.category === category){ | ||
total+=Number((event as UseEvent).properties.times) | ||
} | ||
} | ||
|
||
return total; | ||
} | ||
|
||
/* For a particular substance and unit of that substance, return the total | ||
* amount used. | ||
*/ | ||
export function calc_total_units(eventlist: Array<CalendarEvent>, substance: string, units: string) { | ||
|
||
let total = 0; | ||
|
||
for (let event of eventlist) { | ||
if ((event as UseEvent).properties.substance == substance && | ||
(event as UseEvent).properties.units == units && | ||
(event as UseEvent).properties.amount != "unknown") | ||
total = total + Number((event as UseEvent).properties.amount); | ||
} | ||
|
||
return total; | ||
} | ||
|
||
export function calc_days_since_last_use(eventlist: Array<CalendarEvent>, category: string, date : string) { | ||
let use_days_str: Array<string> = calc_use_days(eventlist, category, true); | ||
|
||
if (use_days_str.length < 1) | ||
return NaN; | ||
|
||
let last_day = use_days_str.reduce(function(acc, cur) { | ||
let d = new Date(cur); | ||
return (d > acc) ? d : acc; | ||
}, new Date(use_days_str[0])); | ||
|
||
return (((new Date(date)).valueOf() - last_day.valueOf()) / CVT_MS_DAY) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,12 @@ | |
* | ||
* Defines various constants used elsewhere. | ||
* | ||
* Copyright (c) 2023, Michael Pascale <[email protected]> | ||
* Last modified: 2023-07-26 | ||
* Copyright (c) 2024, Michael Pascale <[email protected]> | ||
* Last modified: 2024-08-26 | ||
*/ | ||
|
||
export const VERSION = '3.0.0'; | ||
|
||
// Time conversion factors. | ||
export const CVT_MS_S = 1000; | ||
export const CVT_S_MIN = 60; | ||
|
@@ -17,14 +19,14 @@ export const CVT_MS_DAY = CVT_MS_S * CVT_S_MIN * CVT_MIN_HR * CVT_HR_DAY; | |
export const CAL_HEIGHT_DIFF = 205; | ||
|
||
|
||
export const COLOR_WHITE = '#FFFFFF' | ||
export const COLOR_TEXT = '#15141A' | ||
export const COLOR_WHITE = '#FFFFFF'; | ||
export const COLOR_TEXT = '#15141A'; | ||
|
||
export const COLOR_PURPLE = '#485FC7' | ||
export const COLOR_ORANGE = '#E67975' | ||
export const COLOR_YELLOW = '#F7DBA7' | ||
export const COLOR_GREEN = '#041F1E' | ||
export const COLOR_BROWN = '#5A352A' | ||
export const COLOR_PURPLE = '#485FC7'; | ||
export const COLOR_ORANGE = '#E67975'; | ||
export const COLOR_YELLOW = '#F7DBA7'; | ||
export const COLOR_GREEN = '#041F1E'; | ||
export const COLOR_BROWN = '#5A352A'; | ||
|
||
// Regular Expressions for input validation. | ||
export const RGX_INTEGER = /^\d+$/; | ||
|
Oops, something went wrong.