Skip to content

Commit

Permalink
feat: use branded UnixTimestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Nov 9, 2024
1 parent baf1be5 commit 68bf943
Show file tree
Hide file tree
Showing 14 changed files with 443 additions and 477 deletions.
3 changes: 2 additions & 1 deletion src/bin/generate-build-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fs from 'node:fs'
import path from 'node:path'
import { UnixTimestamp } from '@naturalcycles/js-lib'
import yargs from 'yargs'
import { appendToBashEnv, appendToGithubEnv, appendToGithubOutput } from '../fs/json2env'
import { runScript } from '../script/runScript'
Expand All @@ -20,7 +21,7 @@ runScript(async () => {
}).argv

const buildInfo = generateBuildInfo({
overrideTimestamp,
overrideTimestamp: overrideTimestamp as UnixTimestamp,
})
console.log(buildInfo)

Expand Down
6 changes: 3 additions & 3 deletions src/fs/del.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { _since, pFilter, pMap } from '@naturalcycles/js-lib'
import { _since, pFilter, pMap, UnixTimestampMillis } from '@naturalcycles/js-lib'
import { dimGrey, yellow } from '../colors/colors'
import { fs2, globby } from '../index'

Expand Down Expand Up @@ -35,7 +35,7 @@ const DEF_OPT: DelOptions = {
* @experimental
*/
export async function del(_opt: DelOptions | DelSingleOption): Promise<void> {
const started = Date.now()
const started = Date.now() as UnixTimestampMillis

// Convert DelSingleOption to DelOptions
if (typeof _opt === 'string') {
Expand Down Expand Up @@ -110,7 +110,7 @@ export async function del(_opt: DelOptions | DelSingleOption): Promise<void> {
}

export function delSync(_opt: DelOptions | DelSingleOption): void {
const started = Date.now()
const started = Date.now() as UnixTimestampMillis

// Convert DelSingleOption to DelOptions
if (typeof _opt === 'string') {
Expand Down
8 changes: 4 additions & 4 deletions src/fs/kpy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'node:path'
import { _since } from '@naturalcycles/js-lib'
import { _since, UnixTimestampMillis } from '@naturalcycles/js-lib'
import { boldWhite, dimGrey, grey, yellow } from '../colors/colors'
import { fs2, globby } from '../index'

Expand Down Expand Up @@ -43,7 +43,7 @@ export interface KpyOptions {
}

export async function kpy(opt: KpyOptions): Promise<void> {
const started = Date.now()
const started = Date.now() as UnixTimestampMillis

kpyPrepare(opt)

Expand Down Expand Up @@ -82,7 +82,7 @@ export async function kpy(opt: KpyOptions): Promise<void> {
}

export function kpySync(opt: KpyOptions): void {
const started = Date.now()
const started = Date.now() as UnixTimestampMillis

kpyPrepare(opt)

Expand Down Expand Up @@ -143,7 +143,7 @@ function kpyLogFilenames(opt: KpyOptions, filenames: string[]): void {
)
}

function kpyLogResult(opt: KpyOptions, filenames: string[], started: number): void {
function kpyLogResult(opt: KpyOptions, filenames: string[], started: UnixTimestampMillis): void {
if (opt.silent || filenames.length === 0) return

console.log(
Expand Down
1 change: 1 addition & 0 deletions src/stream/ndjson/ndjson.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class NDJsonStats {
get rpsTotal(): number {
return Math.round(this.rows / ((this.tookMillis || 1) / 1000))
}

get bpsTotal(): number {
return this.sizeBytes === 0 ? 0 : Math.round(this.sizeBytes / ((this.tookMillis || 1) / 1000))
}
Expand Down
12 changes: 6 additions & 6 deletions src/stream/progressLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
CommonLogger,
localTime,
SimpleMovingAverage,
UnixTimestampMillisNumber,
UnixTimestampMillis,
} from '@naturalcycles/js-lib'
import { boldWhite, dimGrey, hasColors, white, yellow } from '../colors/colors'
import { SizeStack } from './sizeStack'
Expand Down Expand Up @@ -185,8 +185,8 @@ export class ProgressLogger<T> implements Disposable {
logger: CommonLogger
}

private started!: UnixTimestampMillisNumber
private lastSecondStarted!: UnixTimestampMillisNumber
private started!: UnixTimestampMillis
private lastSecondStarted!: UnixTimestampMillis
private sma!: SimpleMovingAverage
private logEvery10!: number
private processedLastSecond!: number
Expand All @@ -196,8 +196,8 @@ export class ProgressLogger<T> implements Disposable {
private sizesZipped?: SizeStack

private start(): void {
this.started = Date.now()
this.lastSecondStarted = Date.now()
this.started = Date.now() as UnixTimestampMillis
this.lastSecondStarted = Date.now() as UnixTimestampMillis
this.sma = new SimpleMovingAverage(10)
this.processedLastSecond = 0
this.progress = 0
Expand Down Expand Up @@ -250,7 +250,7 @@ export class ProgressLogger<T> implements Disposable {

const mem = process.memoryUsage()

const now = Date.now()
const now = Date.now() as UnixTimestampMillis
const batchedProgress = this.progress * chunkSize
const lastRPS =
(this.processedLastSecond * chunkSize) / ((now - this.lastSecondStarted) / 1000) || 0
Expand Down
6 changes: 3 additions & 3 deletions src/stream/transform/transformMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
Promisable,
SKIP,
StringMap,
UnixTimestampMillisNumber,
UnixTimestampMillis,
} from '@naturalcycles/js-lib'
import through2Concurrent = require('through2-concurrent')
import { yellow } from '../../colors/colors'
Expand Down Expand Up @@ -94,7 +94,7 @@ export interface TransformMapStats {
countErrors: number
countIn: number
countOut: number
started: UnixTimestampMillisNumber
started: UnixTimestampMillis
}

export interface TransformMapStatsSummary extends TransformMapStats {
Expand Down Expand Up @@ -141,7 +141,7 @@ export function transformMap<IN = any, OUT = IN>(
logger = console,
} = opt

const started = Date.now()
const started = Date.now() as UnixTimestampMillis
let index = -1
let countOut = 0
let isSettled = false
Expand Down
3 changes: 2 additions & 1 deletion src/stream/transform/transformMapSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Mapper,
Predicate,
SKIP,
UnixTimestampMillis,
} from '@naturalcycles/js-lib'
import { yellow } from '../../colors/colors'
import { AbortableTransform } from '../pipeline/pipeline'
Expand Down Expand Up @@ -88,7 +89,7 @@ export function transformMapSync<IN = any, OUT = IN>(
logger = console,
} = opt

const started = Date.now()
const started = Date.now() as UnixTimestampMillis
let index = -1
let countOut = 0
let isSettled = false
Expand Down
8 changes: 4 additions & 4 deletions src/stream/transform/transformThrottle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import {
_since,
DeferredPromise,
localTime,
NumberOfMilliseconds,
NumberOfSeconds,
pDefer,
PositiveInteger,
UnixTimestampMillis,
} from '@naturalcycles/js-lib'
import { TransformTyped } from '../stream.model'

Expand Down Expand Up @@ -46,7 +46,7 @@ export function transformThrottle<T>(opt: TransformThrottleOptions): TransformTy
const { throughput, interval, debug } = opt

let count = 0
let start: NumberOfMilliseconds
let start: UnixTimestampMillis
let paused: DeferredPromise | undefined
let timeout: NodeJS.Timeout | undefined

Expand All @@ -55,7 +55,7 @@ export function transformThrottle<T>(opt: TransformThrottleOptions): TransformTy
async transform(item: T, _, cb) {
// console.log('incoming', item, { paused: !!paused, count })
if (!start) {
start = Date.now()
start = Date.now() as UnixTimestampMillis
timeout = setTimeout(() => onInterval(this), interval * 1000)
if (debug) {
console.log(`${localTime.now().toPretty()} transformThrottle started with`, {
Expand Down Expand Up @@ -106,7 +106,7 @@ export function transformThrottle<T>(opt: TransformThrottleOptions): TransformTy
}

count = 0
start = Date.now()
start = Date.now() as UnixTimestampMillis
timeout = setTimeout(() => onInterval(transform), interval * 1000)
}
}
4 changes: 2 additions & 2 deletions src/util/buildInfo.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
AnyObject,
BuildInfo,
localTime,
UnixTimestampNumber,
UnixTimestamp,
} from '@naturalcycles/js-lib'
import { fs2 } from '../fs/fs2'
import { git2 } from './git2'
Expand All @@ -12,7 +12,7 @@ export interface GenerateBuildInfoOptions {
/**
* If set - this timestamp will be used, instead of "current time".
*/
overrideTimestamp?: UnixTimestampNumber
overrideTimestamp?: UnixTimestamp
}

export function generateBuildInfo(opt: GenerateBuildInfoOptions = {}): BuildInfo {
Expand Down
12 changes: 6 additions & 6 deletions src/util/exec2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
AnyObject,
AppError,
NumberOfMilliseconds,
UnixTimestampMillisNumber,
UnixTimestampMillis,
} from '@naturalcycles/js-lib'
import { dimGrey, dimRed, hasColors, white } from '../colors/colors'

Expand Down Expand Up @@ -49,7 +49,7 @@ class Exec2 {
opt.log ??= true // by default log should be true, as we are printing the output
opt.logStart ??= opt.log
opt.logFinish ??= opt.log
const started = Date.now()
const started = Date.now() as UnixTimestampMillis
this.logStart(cmd, opt)

const r = cp.spawnSync(cmd, opt.args, {
Expand Down Expand Up @@ -93,7 +93,7 @@ class Exec2 {
const { cwd, env, passProcessEnv = true, timeout } = opt
opt.logStart ??= opt.log ?? false
opt.logFinish ??= opt.log ?? false
const started = Date.now()
const started = Date.now() as UnixTimestampMillis
this.logStart(cmd, opt)

try {
Expand Down Expand Up @@ -146,7 +146,7 @@ class Exec2 {
opt.log ??= true // by default log should be true, as we are printing the output
opt.logStart ??= opt.log
opt.logFinish ??= opt.log
const started = Date.now()
const started = Date.now() as UnixTimestampMillis
this.logStart(cmd, opt)

await new Promise<void>((resolve, reject) => {
Expand Down Expand Up @@ -205,7 +205,7 @@ class Exec2 {
opt.log ??= printWhileRunning // by default log should be true, as we are printing the output
opt.logStart ??= opt.log
opt.logFinish ??= opt.log
const started = Date.now()
const started = Date.now() as UnixTimestampMillis
this.logStart(cmd, opt)
let stdout = ''
let stderr = ''
Expand Down Expand Up @@ -274,7 +274,7 @@ class Exec2 {
private logFinish(
cmd: string,
opt: SpawnOptions | ExecOptions,
started: UnixTimestampMillisNumber,
started: UnixTimestampMillis,
isSuccessful: boolean,
): void {
if (isSuccessful && !opt.logFinish) return
Expand Down
6 changes: 3 additions & 3 deletions src/util/git2.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import cp from 'node:child_process'
import path from 'node:path'
import type { UnixTimestampNumber } from '@naturalcycles/js-lib'
import type { UnixTimestamp } from '@naturalcycles/js-lib'
import { grey } from '../colors/colors'
import { exec2 } from './exec2'

Expand Down Expand Up @@ -88,8 +88,8 @@ class Git2 {
return full ? sha : sha.slice(0, 7)
}

getCurrentCommitTimestamp(): UnixTimestampNumber {
return Number(exec2.exec('git log -1 --format=%ct'))
getCurrentCommitTimestamp(): UnixTimestamp {
return Number(exec2.exec('git log -1 --format=%ct')) as UnixTimestamp
}

getCurrentBranchName(): string {
Expand Down
16 changes: 12 additions & 4 deletions src/validation/joi/joi.shared.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
BaseDBEntity,
NumberEnum,
StringEnum,
UnixTimestamp,
UnixTimestampMillis,
} from '@naturalcycles/js-lib'
import { AlternativesSchema, AnySchema, ArraySchema, ObjectSchema } from 'joi'
import { Joi } from './joi.extensions'
Expand Down Expand Up @@ -119,25 +121,31 @@ const TS_2000 = 946684800 // 2000-01-01
/**
* Between years 1970 and 2050
*/
export const unixTimestampSchema = numberSchema.integer().min(0).max(TS_2500)
export const unixTimestampSchema = numberSchema
.integer()
.min(0)
.max(TS_2500) as NumberSchema<UnixTimestamp>
/**
* Between years 2000 and 2050
*/
export const unixTimestamp2000Schema = numberSchema.integer().min(TS_2000).max(TS_2500)
export const unixTimestamp2000Schema = numberSchema
.integer()
.min(TS_2000)
.max(TS_2500) as NumberSchema<UnixTimestamp>
/**
* Between years 1970 and 2050
*/
export const unixTimestampMillisSchema = numberSchema
.integer()
.min(0)
.max(TS_2500 * 1000)
.max(TS_2500 * 1000) as NumberSchema<UnixTimestampMillis>
/**
* Between years 2000 and 2050
*/
export const unixTimestampMillis2000Schema = numberSchema
.integer()
.min(TS_2000 * 1000)
.max(TS_2500 * 1000)
.max(TS_2500 * 1000) as NumberSchema<UnixTimestampMillis>

// 2
export const verSchema = numberSchema.optional().integer().min(1).max(100)
Expand Down
1 change: 1 addition & 0 deletions src/validation/joi/string.extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export function stringExtensions(joi: typeof Joi): Extension {
}

const DAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

// Based on: https://github.com/ajv-validator
function isValidDate(parts: string[]): boolean {
const year = Number(parts[1])
Expand Down
Loading

0 comments on commit 68bf943

Please sign in to comment.