aboutsummaryrefslogtreecommitdiff
path: root/backend/hist_data
diff options
context:
space:
mode:
authorTerry Truong <terry06890@gmail.com>2022-12-29 16:17:39 +1100
committerTerry Truong <terry06890@gmail.com>2022-12-29 16:20:26 +1100
commit20d69469a4c80a196de23625d0420487b0ed04a6 (patch)
treee3a7aad153e72322c0810c66bda3b70daa8d815a /backend/hist_data
parent4ad7206443660587a15a7b47384b927188155da8 (diff)
Show event-count data on timeline
Backend: Send event-count data to client in EventResponse instance Fix certain usages of gregorian calendar instead of julian Move HistDate, SCALES, etc, into cal.py Frontend: Make App update a unitCountMaps object using event-count data from server Make TimeLine show visual indication of unit counts Add showEventCounts option to store Update unit tests
Diffstat (limited to 'backend/hist_data')
-rw-r--r--backend/hist_data/cal.py53
-rw-r--r--backend/hist_data/gen_disp_data.py19
2 files changed, 53 insertions, 19 deletions
diff --git a/backend/hist_data/cal.py b/backend/hist_data/cal.py
index c0e8772..3b65205 100644
--- a/backend/hist_data/cal.py
+++ b/backend/hist_data/cal.py
@@ -1,6 +1,6 @@
"""
-Provides functions for converting between Julian calendar, Gregorian calendar,
-and Julian day number values. Algorithms were obtained from
+Provides date conversion functions, HistDate, and date scales.
+Algorithms for converting between calendars and Julian day number values were obtained from
https://en.wikipedia.org/wiki/Julian_day#Converting_Gregorian_calendar_date_to_Julian_Day_Number.
"""
@@ -69,3 +69,52 @@ def julianToGregorian(year: int, month: int, day: int) -> tuple[int, int, int]:
def gregorianToJulian(year: int, month: int, day: int) -> tuple[int, int, int]:
return jdnToJulian(gregorianToJdn(year, month, day))
+
+MIN_CAL_YEAR = -4713 # Disallow within-year dates before this year
+MONTH_SCALE = -1;
+DAY_SCALE = -2;
+SCALES: list[int] = [int(x) for x in [1e9, 1e8, 1e7, 1e6, 1e5, 1e4, 1e3, 100, 10, 1, MONTH_SCALE, DAY_SCALE]];
+class HistDate:
+ """
+ Represents a historical date
+ - 'year' may be negative (-1 means 1 BCE)
+ - 'month' and 'day' are at least 1, if given
+ - 'gcal' may be:
+ - True: Indicates a Gregorian calendar date
+ - False: Means the date should, for display, be converted to a Julian calendar date
+ - None: 'month' and 'day' are 1 (used for dates before the Julian period starting year 4713 BCE)
+ """
+ def __init__(self, gcal: bool | None, year: int, month=1, day=1):
+ self.gcal = gcal
+ self.year = year
+ self.month = month
+ self.day = day
+ # Used in unit testing
+ def __eq__(self, other):
+ return isinstance(other, HistDate) and \
+ (self.gcal, self.year, self.month, self.day) == (other.gcal, other.year, other.month, other.day)
+ def __repr__(self):
+ return str(self.__dict__)
+def dbDateToHistDate(n: int, fmt: int, end=False) -> HistDate:
+ if fmt == 0: # year
+ if n >= MIN_CAL_YEAR:
+ return HistDate(True, n, 1, 1)
+ else:
+ return HistDate(None, n)
+ elif fmt == 1 or fmt == 3 and not end: # jdn for julian calendar
+ return HistDate(False, *jdnToJulian(n))
+ else: # fmt == 2 or fmt == 3 and end
+ return HistDate(True, *jdnToGregorian(n))
+def dateToUnit(date: HistDate, scale: int) -> int:
+ if scale >= 1:
+ return date.year // scale
+ elif scale == MONTH_SCALE:
+ if date.gcal == False:
+ return julianToJdn(date.year, date.month, 1)
+ else:
+ return gregorianToJdn(date.year, date.month, 1)
+ else: # scale == DAY_SCALE
+ if date.gcal == False:
+ return julianToJdn(date.year, date.month, date.day)
+ else:
+ return gregorianToJdn(date.year, date.month, date.day)
diff --git a/backend/hist_data/gen_disp_data.py b/backend/hist_data/gen_disp_data.py
index e425efc..a81263f 100644
--- a/backend/hist_data/gen_disp_data.py
+++ b/backend/hist_data/gen_disp_data.py
@@ -10,13 +10,9 @@ parentDir = os.path.dirname(os.path.realpath(__file__))
sys.path.append(parentDir)
import sqlite3
-from cal import gregorianToJdn, jdnToGregorian
+from cal import SCALES, dbDateToHistDate, dateToUnit
-MONTH_SCALE = -1;
-DAY_SCALE = -2;
-SCALES: list[int] = [int(x) for x in [1e9, 1e8, 1e7, 1e6, 1e5, 1e4, 1e3, 100, 10, 1, MONTH_SCALE, DAY_SCALE]];
MAX_DISPLAYED_PER_UNIT = 4
-#
DB_FILE = 'data.db'
def genData(dbFile: str, scales: list[int], maxDisplayedPerUnit: int) -> None:
@@ -36,18 +32,7 @@ def genData(dbFile: str, scales: list[int], maxDisplayedPerUnit: int) -> None:
print(f'At iteration {iterNum}')
# For each scale
for scale in scales:
- # Get unit
- unit: int
- if scale >= 1:
- unit = (eventStart if fmt == 0 else jdnToGregorian(eventStart)[0]) // scale
- elif scale == MONTH_SCALE:
- if fmt == 0:
- unit = gregorianToJdn(eventStart, 1, 1)
- else:
- year, month, day = jdnToGregorian(eventStart)
- unit = eventStart if day == 1 else gregorianToJdn(year, month, 1)
- else: # scale == DAY_SCALE
- unit = eventStart if fmt != 0 else gregorianToJdn(eventStart, 1, 1)
+ unit = dateToUnit(dbDateToHistDate(eventStart, fmt), scale)
# Update maps
counts: list[int]
if (scale, unit) in scaleUnitToCounts: