Skip to content

Commit

Permalink
chore: update API
Browse files Browse the repository at this point in the history
  • Loading branch information
timeowilliams committed Dec 2, 2024
1 parent 8dd7f0a commit bfe340b
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 57 deletions.
103 changes: 92 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@typescript-eslint/parser": "^5.62.0",
"electron": "33.0.2",
"eslint": "^8.57.1",
"eslint-import-resolver-typescript": "^3.6.3",
"eslint-plugin-import": "^2.31.0",
"eslint-plugin-solid": "^0.14.3",
"prettier": "^3.3.3",
Expand Down
60 changes: 29 additions & 31 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import {
Tray,
nativeImage
} from 'electron'
import path, { join } from 'path'
import path from 'path'
import dayjs from 'dayjs'
import fs from 'fs'
import schedule from 'node-schedule'
import { scheduleJob } from 'node-schedule'
import dotenv from 'dotenv'
import Store from 'electron-store'
import { StoreSchema, SiteTimeTracker, DeepWorkHours, MessageType, User, AppIcon } from './types'
import { StoreSchema, SiteTimeTracker, DeepWorkHours, User, AppIcon } from './types'
import {
updateSiteTimeTracker,
getBrowserURL,
Expand All @@ -29,15 +29,15 @@ import {
import { getApplicationIcons } from './childProcess'
import { checkForUpdates, getIconPath, updateIconBasedOnProgress } from './utils'
import log from 'electron-log/node.js'

export interface TypedStore extends Store<StoreSchema> {
get<K extends keyof StoreSchema>(key: K): StoreSchema[K]
get<K extends keyof StoreSchema>(key: K, defaultValue: StoreSchema[K]): StoreSchema[K]
set(key: string, value: any): void
delete<K extends keyof StoreSchema>(key: K): void
clear(): void
}
const API_BASE_URL = 'https://backend-production-5eec.up.railway.app'
//const API_BASE_URL = 'https://backend-production-5eec.up.railway.app'
const API_BASE_URL = 'http://localhost:5000'
const store = new Store<StoreSchema>() as TypedStore
let currentSiteTimeTrackers: SiteTimeTracker[] = store.get('siteTimeTrackers', [])
let monitoringInterval: NodeJS.Timeout | null = null
Expand Down Expand Up @@ -226,7 +226,7 @@ export function startActivityMonitoring(): void {
} catch (error) {
console.error('Error getting active window or URL:', error)
}
}, 5000) // Run the monitoring function every 5 seconds
}, 30000) // Run the monitoring function every 5 seconds
log.info('Activity monitoring started.', today.format('dddd, HH:mm'))
}
}
Expand Down Expand Up @@ -410,26 +410,21 @@ export function getSiteTrackers(): SiteTimeTracker[] {
return currentSiteTimeTrackers
}

schedule.scheduleJob('0 0 0 * * *', async () => {
scheduleJob('0 0 0 * * *', async () => {
log.info('Scheduled daily reset at midnight')
stopActivityMonitoring()
await checkAndSendMissedEmails()
await resetCounters('daily')
startActivityMonitoring()
log.info('new reset date is ', store.get('lastResetDate'))
})

// Schedule a weekly reset for 11:55 PM on Sunday
schedule.scheduleJob('55 23 * * 0', () => {
log.info('Scheduled weekly reset at 11:55 PM on Sunday')
stopActivityMonitoring()
checkAndSendMissedEmails()
resetCounters('weekly')
// Check if today is Sunday and perform weekly reset if true
if (dayjs().day() === 0) {
log.info('Performing weekly reset on Sunday')
await resetCounters('weekly')
}
startActivityMonitoring()
log.info('Weekly counters have been reset')
})

schedule.scheduleJob('0 0 12 * * *', () => {
scheduleJob('0 0 12 * * *', () => {
log.info('Scheduled daily reset at 12 PM')
stopActivityMonitoring()
checkAndSendMissedEmails()
Expand All @@ -438,14 +433,15 @@ schedule.scheduleJob('0 0 12 * * *', () => {
})

// TODO: For testing only
// schedule.scheduleJob('* * * * *', () => {
// log.info('Scheduled daily reset at 12 PM')
// stopActivityMonitoring()
// checkAndSendMissedEmails()
// resetCounters('daily')
// stopActivityMonitoring()
// log.info('new reset date is ', store.get('lastResetDate'))
// })
scheduleJob('* * * * *', () => {
log.info('Scheduled daily reset at 12 PM')
stopActivityMonitoring()
// checkAndSendMissedEmails()
sendDailyEmail()
// resetCounters('daily')
stopActivityMonitoring()
log.info('new reset date is ', store.get('lastResetDate'))
})

// Log unhandled promise rejections
process.on('unhandledRejection', (error) => {
Expand Down Expand Up @@ -643,17 +639,19 @@ async function sendDailyEmail(): Promise<boolean> {
title: tracker.title.slice(0, 100), // Truncate long titles
url: tracker.url.slice(0, 200), // Truncate long URLs
timeSpent: tracker.timeSpent,
iconUrl: null // Optionally remove icon URLs if they're large
iconUrl: tracker.iconUrl
}))
}

log.info('Payload size:', JSON.stringify(dailyData).length);

try {
const response = await fetch(`${API_BASE_URL}/api/v1/activity/send-daily`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(dailyData)
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
dailyData
})
})
console.log('Email sent status:', response.status)
if (response.status === 200) {
Expand Down
3 changes: 1 addition & 2 deletions src/productivityUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,14 @@ export function updateSiteTimeTracker(
// log.info(`Using cached icon: ${iconPath}`)
} else {
iconUrl = 'https://cdn-icons-png.freepik.com/512/7022/7022186.png'
log.info(`Using default icon for app: ${appName}`)
}
}

// Find an existing tracker or create a new one
let tracker = timeTrackers.find((t) => t.url === trackerKey)
if (tracker) {
log.info('Updating existing tracker', tracker.title, tracker.timeSpent)
tracker.timeSpent += 5
tracker.timeSpent += 30
tracker.lastActiveTimestamp = currentTime
tracker.iconUrl = iconUrl
} else {
Expand Down
14 changes: 2 additions & 12 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ export interface User {
language: string
}

export enum MessageType {
RESET_DAILY,
RESET_WEEKLY,
RESET_TRACKERS,
UPDATE_DATA,
GET_DATA,
REPLY_DATA,
SET_USER_INFO
}

export interface StoreSchema {
unproductiveSites?: string[]
unproductiveApps?: string[]
Expand All @@ -42,7 +32,7 @@ export interface SiteTimeTracker {
timeSpent: number
lastActiveTimestamp: number
iconUrl?: string
type: TrackerType
type: TrackerType
}

export type DeepWorkHours = {
Expand Down Expand Up @@ -89,4 +79,4 @@ export interface AppIcon {
export enum TrackerType {
Website = 'website',
App = 'app'
}
}
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export function updateIconBasedOnProgress(
}
const isIconPathChanged = iconPath !== newIconPath
if (isIconPathChanged) {
log.info('Comparison of icon paths is ', iconPath, newIconPath)
//TODO: Add if errors occur from clients log.info('Comparison of icon paths is ', iconPath, newIconPath)
// app.dock.setIcon(iconPath)
new Notification({
title: 'DeepFocus',
Expand Down

0 comments on commit bfe340b

Please sign in to comment.