-
Notifications
You must be signed in to change notification settings - Fork 2
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 #88 from xerosanyam/alpha
Alpha
- Loading branch information
Showing
23 changed files
with
437 additions
and
66 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Turso commands | ||
|
||
``` | ||
turso db shell <libsql://db-url.io> | ||
``` |
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,50 @@ | ||
import { drizzle } from 'drizzle-orm/libsql'; | ||
import { createClient } from '@libsql/client'; | ||
|
||
// picks value from .env | ||
export const turso_client = createClient({ | ||
url: process.env.TURSO_CONNECTION_URL!, | ||
authToken: process.env.TURSO_AUTH_TOKEN!, | ||
}); | ||
|
||
export const db = drizzle(turso_client, | ||
// { logger: true } | ||
); | ||
|
||
import { activityTable, cardTable } from "$lib/db/turso.schema"; | ||
|
||
async function insertActivityFromCards() { | ||
// Fetch all rows from the card table | ||
const cards = await db.select().from(cardTable).all(); | ||
|
||
// Loop through each card and insert rows into the activity table | ||
for (const card of cards) { | ||
const activityInsertData = { | ||
userId: card.userId, | ||
cardId: card.id, | ||
}; | ||
|
||
// Insert activity for creation | ||
await db.insert(activityTable).values({ | ||
id: crypto.randomUUID(), | ||
action: 'INSERT', | ||
createdAt: card.createdAt, | ||
...activityInsertData, | ||
}); | ||
|
||
// If createdAt and updatedAt are different, insert activity for update | ||
if (card.createdAt !== card.updatedAt) { | ||
await db.insert(activityTable).values({ | ||
id: crypto.randomUUID(), | ||
action: 'UPDATE', | ||
createdAt: card.updatedAt, | ||
...activityInsertData, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
// Run the script | ||
insertActivityFromCards() | ||
.then(() => console.log('Activity records inserted successfully.')) | ||
.catch((error) => console.error('Error inserting activity records:', error)); |
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,46 @@ | ||
import { calculateMaxStreak, calculateStreak, type info } from "$lib/algo.utils"; | ||
import { describe, expect, test } from "vitest"; | ||
|
||
describe('streak algo', () => { | ||
test('should calculate current streak', () => { | ||
const inputArray = [ | ||
{ date: '2021-01-01T00:00:00.000Z' }, | ||
{ date: '2021-01-02T00:00:00.000Z' }, | ||
{ date: '2021-01-03T00:00:00.000Z' }, | ||
{ date: '2021-01-04T00:00:00.000Z' }, | ||
//break | ||
{ date: '2021-01-06T00:00:00.000Z' }, | ||
{ date: '2021-01-07T00:00:00.000Z' }, | ||
]; | ||
const streak = calculateStreak(inputArray, new Date('2021-01-07')); | ||
expect(streak).toBe(2); | ||
}) | ||
|
||
test.skip('should calculate max streak', () => { | ||
const inputArray = [ | ||
{ date: '2021-01-01T00:00:00.000Z' }, | ||
{ date: '2021-01-02T00:00:00.000Z' }, | ||
{ date: '2021-01-03T00:00:00.000Z' }, | ||
{ date: '2021-01-04T00:00:00.000Z' }, | ||
//break | ||
{ date: '2021-01-06T00:00:00.000Z' }, | ||
{ date: '2021-01-07T00:00:00.000Z' }, | ||
]; | ||
const streak = calculateMaxStreak(inputArray) | ||
expect(streak).toHaveProperty('maxStreak', 4) | ||
expect(streak).toHaveProperty('startDate', '2020-12-31') | ||
expect(streak).toHaveProperty('endDate', '2021-01-03'); | ||
}) | ||
test('should calculate max streak', () => { | ||
const inputArray: info = []; | ||
const streak = calculateMaxStreak(inputArray) | ||
expect(streak).toHaveProperty('maxStreak', 0); | ||
}) | ||
test('should calculate max streak', () => { | ||
const inputArray = [ | ||
{ date: '2021-01-01' } | ||
]; | ||
const streak = calculateMaxStreak(inputArray) | ||
expect(streak).toHaveProperty('maxStreak', 1); | ||
}) | ||
}) |
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,57 @@ | ||
export type info = { | ||
date: string; | ||
}[] | ||
|
||
export const calculateStreak = (inputArray: info, currentDate: Date) => { | ||
const dateSet = new Set( | ||
inputArray.map((item) => new Date(item.date).toLocaleDateString('en-CA')) | ||
); | ||
|
||
let streak = 0; | ||
|
||
while (dateSet.has(currentDate.toLocaleDateString('en-CA'))) { | ||
streak++; | ||
currentDate.setDate(currentDate.getDate() - 1); | ||
} | ||
|
||
return streak; | ||
} | ||
|
||
function isOneDayDifference(date1: string, date2: string) { | ||
const d1 = new Date(date1); | ||
const d2 = new Date(date2); | ||
|
||
d1.setHours(0, 0, 0, 0); | ||
d2.setHours(0, 0, 0, 0); | ||
|
||
const diffInDays = (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24); | ||
|
||
return Math.abs(diffInDays) === 1; | ||
} | ||
|
||
|
||
|
||
export const calculateMaxStreak = (inputArray: info) => { | ||
const dateSet = new Set( | ||
inputArray.map((item) => new Date(item.date).toLocaleDateString('en-CA')) | ||
); | ||
let maxStreak = dateSet.size >= 1 ? 1 : 0; | ||
let startDate = ''; | ||
let endDate = ''; | ||
|
||
let sortedDates = [...dateSet].toSorted((item1, item2) => (new Date(item1).getTime() - new Date(item2).getTime())) | ||
let left = 0; | ||
for (let right = 1; right < sortedDates.length; right++) { | ||
if (isOneDayDifference(sortedDates[right], sortedDates[right - 1])) { | ||
if (maxStreak < (right - left + 1)) { | ||
maxStreak = right - left + 1; | ||
startDate = sortedDates[left]; | ||
endDate = sortedDates[right]; | ||
} | ||
|
||
} else { | ||
left = right | ||
} | ||
} | ||
return { maxStreak, startDate, endDate }; | ||
} |
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
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,43 @@ | ||
<script lang="ts"> | ||
import { onMount } from 'svelte'; | ||
export let text = ''; | ||
let isUrl = false; | ||
let isImageUrl = false; | ||
onMount(() => { | ||
isUrl = isValidUrl(text); | ||
if (isUrl) { | ||
checkIfImageUrl(text).then((result: boolean) => { | ||
isImageUrl = result; | ||
}); | ||
} | ||
}); | ||
function isValidUrl(string: string) { | ||
try { | ||
new URL(string); | ||
return true; | ||
} catch (_) { | ||
return false; | ||
} | ||
} | ||
function checkIfImageUrl(url: string): Promise<boolean> { | ||
return new Promise((resolve) => { | ||
const img = new Image(); | ||
img.onload = () => resolve(true); | ||
img.onerror = () => resolve(false); | ||
img.src = url; | ||
}); | ||
} | ||
</script> | ||
|
||
{#if isImageUrl} | ||
<img src={text} alt="content" /> | ||
{:else if isUrl} | ||
<a class="underline" href={text} target="_blank" rel="noopener noreferrer">{text}</a> | ||
{:else} | ||
{text} | ||
{/if} |
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,59 @@ | ||
<script lang="ts"> | ||
import { onMount } from 'svelte'; | ||
import CalHeatmap from 'cal-heatmap'; | ||
import Tooltip from 'cal-heatmap/plugins/Tooltip'; | ||
import 'cal-heatmap/cal-heatmap.css'; | ||
export let data; | ||
let cal; | ||
let divElement: HTMLDivElement; | ||
const currentDate = new Date(); | ||
currentDate.setMonth(currentDate.getMonth() - 12); | ||
onMount(() => { | ||
cal = new CalHeatmap(); | ||
cal.paint( | ||
{ | ||
itemSelector: divElement, | ||
range: 13, // show 13 months (current month + 12 months back) | ||
domain: { | ||
type: 'month', | ||
gutter: 4, | ||
label: { text: 'MMM', textAlign: 'start', position: 'top' } | ||
}, | ||
subDomain: { type: 'ghDay', radius: 2, width: 8, height: 8, gutter: 2 }, | ||
date: { | ||
start: currentDate, | ||
highlight: [ | ||
new Date() // Highlight today | ||
], | ||
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone | ||
}, | ||
data: { | ||
source: data, | ||
x: 'date', | ||
y: (d) => +d['count'] | ||
}, | ||
scale: { | ||
color: { | ||
type: 'threshold', | ||
range: ['#4dd05a', '#37a446', '#166b34', '#14432a'], | ||
domain: [10, 20, 30] | ||
} | ||
} | ||
}, | ||
[ | ||
[ | ||
Tooltip, | ||
{ | ||
text: function (date, value, dayjsDate) { | ||
return (value ? value + ' items' : 'No data') + ' on ' + dayjsDate.format('LL'); | ||
} | ||
} | ||
] | ||
] | ||
); | ||
}); | ||
</script> | ||
|
||
<div bind:this={divElement}></div> |
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 @@ | ||
declare module 'cal-heatmap'; |
Oops, something went wrong.