Skip to content

Commit

Permalink
2024-09-16T14-06-45
Browse files Browse the repository at this point in the history
  • Loading branch information
sspilleman committed Sep 16, 2024
1 parent 98d7801 commit 6f190c9
Show file tree
Hide file tree
Showing 18 changed files with 5,439 additions and 53,580 deletions.
19 changes: 6 additions & 13 deletions src/lib/components/Header.svelte
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
<script lang="ts">
import { parseRatecard, parseUsage, parseComputation } from '$lib/parsers/index';
import { computation } from '$lib/stores/computation';
import { usage, ratecard, db } from '$lib/db';
import { usage, ratecard, db, computation } from '$lib/db';
import { Indicator } from 'flowbite-svelte';
import { DarkMode } from 'flowbite-svelte';
import Logo from '$lib/components/Logo.svelte';
const accept = `text/csv`;
// const accept = `text/csv, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet`;
// const load = async () => {
// const c = await parseSampleComputation();
// if (c) computation.set(c);
// const r = await parseSampleRatecard();
// if (r) ratecard.set(r);
// const u = await parseSampleUsage();
// if (u) usage.set(u);
// };
// load();
const change = async (e: null | EventTarget) => {
let files: FileList = (e as HTMLInputElement).files as FileList;
// await upload(files);
Expand All @@ -39,7 +28,11 @@
}
} else if (file.type === 'text/csv' && name.includes('computation')) {
const c = await parseComputation(file);
if (c) computation.set(c);
console.log(c);
if (c) {
await db.computation.clear();
console.log(await db.computation.bulkAdd(c));
}
} else {
alert(`incorect filename or type combo: ${file.name}, ${file.type}`);
}
Expand Down
4 changes: 1 addition & 3 deletions src/lib/components/chart/highcharts.css
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@
stroke-width: 0;
}
.highcharts-title {
/* fill: var(--highcharts-neutral-color-80); */
fill: rgb(255 75 170 / var(--tw-text-opacity));
/* font-size: 1.2em; */
fill: #93ff96;
font-size: 1em;
font-weight: 700;
}
Expand Down
13 changes: 10 additions & 3 deletions src/lib/db.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import Dexie, { type EntityTable } from 'dexie';
import { liveQuery } from 'dexie';
import type { Quote, Rate, Usage } from '$lib/interfaces/index';
import type { Computation, Quote, Rate, Usage } from '$lib/interfaces/index';

const db = new Dexie('spm') as Dexie & {
rates: EntityTable<Rate, 'id'>;
quotes: EntityTable<Quote, 'id'>;
usage: EntityTable<Usage, 'Product Part'>;
computation: EntityTable<Computation, 'SKU'>;
};

db.version(1).stores({ rates: 'id', quotes: 'id', usage: '++id' });
db.version(1).stores({
rates: 'id',
quotes: 'id',
usage: '++id',
computation: '++id,SKU'
});

const ratecard = liveQuery(() => db.rates.toArray());
const usage = liveQuery(() => db.usage.toArray());
const quotes = liveQuery(() => db.quotes.toArray());
const computation = liveQuery(() => db.computation.toArray());

export { db, quotes, ratecard, usage };
export { computation, db, quotes, ratecard, usage };
33 changes: 27 additions & 6 deletions src/lib/interfaces/Computation.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
export type ComputationSourceColumns =
| 'Subscription Plan Number'
| 'Subscription Id'
| 'SKU/Product'
| 'UOM'
| 'Usage Quantity'
| 'Compute Type'
| 'Metered Service Date'
| 'Currency'
| 'Computed Amount'
| 'Computed Amount CD'
| 'Overage'
| 'Data Center'
| 'Unit Rate Card';

export interface Computation {
'Computed Quantity': number;
'Line Net Amount Increased Precision': number;
'Metered service date': Date;
'Net Unit Price': number;
'Product Name': string;
'Product Part': string;
'Subscription Plan Number': string;
'Subscription Id': string;
SKU: string;
Product: string;
UOM: string;
'Usage Quantity': number;
'Compute Type': string;
'Metered Service Date': Date;
Currency: string;
'Computed Amount': number;
'Computed Amount CD': number;
Overage: boolean;
'Data Center': string;
'Unit Rate Card': number;
}
10 changes: 10 additions & 0 deletions src/lib/interfaces/Rate.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
export type RateSourceColumns =
| 'Active'
| 'End Date'
| 'Max Quantity'
| 'Net Unit Price UOM'
| 'Net Unit Price'
| 'Product Name'
| 'Product Part'
| 'Start Date'
| 'UOM';
export interface Rate {
id: number;
Active: boolean;
Expand Down
2 changes: 2 additions & 0 deletions src/lib/interfaces/Usage.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export type UsageSourceColumns = 'Product' | 'Starting Date' | 'UOM' | 'Usage Quantity';

export interface Usage {
'Starting Date': number;
'Usage Quantity': number;
Expand Down
6 changes: 3 additions & 3 deletions src/lib/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type { Computation } from './Computation';
export type { Rate } from './Rate';
export type { Usage } from './Usage';
export type { Computation, ComputationSourceColumns } from './Computation';
export type { Rate, RateSourceColumns } from './Rate';
export type { Usage, UsageSourceColumns } from './Usage';
export type { Line, Quote } from './Quote';
44 changes: 17 additions & 27 deletions src/lib/parsers/computation.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,29 @@
import type { Computation } from '$lib/interfaces/index';
import type { Computation, ComputationSourceColumns } from '$lib/interfaces/index';
import { readFile } from '$lib/utils';
import * as d3 from 'd3';
import { cleanName, parseMDYDate } from './helpers';
import { cleanName, parseYMDDate } from './helpers';

type SourceColumns =
| 'Computed Quantity'
| 'Line Net Amount Increased Precision'
| 'Metered service date'
| 'Net Unit Price'
| 'Product'
| 'UOM'
| 'Usage Quantity';

const rowCoverter = (r: d3.DSVRowString<SourceColumns>) => {
const pp = r['Product'].split(` - `);
const rowCoverter = (r: d3.DSVRowString<ComputationSourceColumns>) => {
const pp = r['SKU/Product'].split(` - `);
const [part, name] = [pp[0], pp.slice(1).join(` - `)];
return {
'Computed Quantity': parseFloat(r['Computed Quantity']),
'Line Net Amount Increased Precision': parseFloat(r['Line Net Amount Increased Precision']),
'Metered service date': parseMDYDate(r['Metered service date']),
'Net Unit Price': parseFloat(r['Net Unit Price']),
'Product Name': cleanName(name),
'Product Part': part,
'Subscription Plan Number': r['Subscription Plan Number'],
'Subscription Id': r['Subscription Id'],
SKU: part,
Product: cleanName(name),
UOM: r['UOM'],
'Usage Quantity': parseFloat(r['Usage Quantity'])
'Usage Quantity': parseFloat(r['Usage Quantity']),
'Compute Type': r['Compute Type'],
'Metered Service Date': parseYMDDate(r['Metered Service Date']),
Currency: r['Currency'],
'Computed Amount': parseFloat(r['Computed Amount']),
'Computed Amount CD': parseFloat(r['Computed Amount CD']),
Overage: r['Overage'] === 'false' ? false : true,
'Data Center': r['Data Center'],
'Unit Rate Card': parseFloat(r['Unit Rate Card'])
} as Computation;
};

export const parseSampleComputation = async () => {
const url = 'https://s3.spilleman.nl/shared/computation.csv';
const rows: Computation[] = await d3.csv(url, rowCoverter);
rows.pop();
return rows;
};

export const parseComputation = async (file: File) => {
const txt = await readFile(file);
const rows = await d3.csvParse(txt, rowCoverter);
Expand Down
19 changes: 15 additions & 4 deletions src/lib/parsers/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const parseMDYDate = (value: string) => {
const date = new Date('1972-09-10T00:00:00');
const date = new Date('1972-09-10T02:00:00');
const r = /^(\d{2})-(\d{2})-(\d{4})$/.exec(value);
if (r && r.length === 4) {
date.setDate(parseInt(r[2], 10));
Expand All @@ -9,11 +9,22 @@ export const parseMDYDate = (value: string) => {
return date;
};

export const parseYMDDate = (value: string) => {
const date = new Date('1972-09-10T02:00:00');
const r = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value);
if (r && r.length === 4) {
date.setDate(parseInt(r[3], 10));
date.setMonth(parseInt(r[2], 10) - 1);
date.setFullYear(parseInt(r[1], 10));
}
return date;
};

export const cleanName = (value: string) =>
value
.replace(/Oracle Cloud Infrastructure - /, '')
.replace(/Oracle Cloud Infrastructure /, '')
.replace(/Oracle /, '');
.replace(/^Oracle Cloud Infrastructure *-* */, '')
.replace(/^Cloud Infrastructure *-* */, '')
.replace(/^Oracle /, '');

export const splitValues = (line: string) => {
const result: string[] = [];
Expand Down
6 changes: 3 additions & 3 deletions src/lib/parsers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { parseComputation, parseSampleComputation } from './computation';
export { parseRatecard, parseSampleRatecard } from './ratecard';
export { parseUsage, parseSampleUsage } from './usage';
export { parseComputation } from './computation';
export { parseRatecard } from './ratecard';
export { parseUsage } from './usage';
23 changes: 2 additions & 21 deletions src/lib/parsers/ratecard.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
import type { Rate } from '$lib/interfaces/index';
import type { Rate, RateSourceColumns } from '$lib/interfaces/index';
import { readFile } from '$lib/utils';
import * as d3 from 'd3';
import { cleanName, parseMDYDate } from './helpers';

type SourceColumns =
| 'Active'
| 'End Date'
| 'Max Quantity'
| 'Net Unit Price UOM'
| 'Net Unit Price'
| 'Product Name'
| 'Product Part'
| 'Start Date'
| 'UOM';

let id = 0;

const rowCoverter = (r: d3.DSVRowString<SourceColumns>) => {
const rowCoverter = (r: d3.DSVRowString<RateSourceColumns>) => {
return {
id: ++id,
'End Date': parseMDYDate(r['End Date']),
Expand All @@ -30,14 +19,6 @@ const rowCoverter = (r: d3.DSVRowString<SourceColumns>) => {
} as Rate;
};

export const parseSampleRatecard = async () => {
const url = 'https://s3.spilleman.nl/shared/ratecard.csv';
id = 0;
const rows = await d3.csv(url, rowCoverter);
rows.pop();
return rows;
};

export const parseRatecard = async (file: File) => {
const txt = await readFile(file);
id = 0;
Expand Down
13 changes: 2 additions & 11 deletions src/lib/parsers/usage.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import type { Usage } from '$lib/interfaces/index';
import type { Usage, UsageSourceColumns } from '$lib/interfaces/index';
import { readFile } from '$lib/utils';
import * as d3 from 'd3';
import { cleanName } from './helpers';

type SourceColumns = 'Product' | 'Starting Date' | 'UOM' | 'Usage Quantity';

const rowCoverter = (r: d3.DSVRowString<SourceColumns>) => {
const rowCoverter = (r: d3.DSVRowString<UsageSourceColumns>) => {
const pp = r['Product'].split(` - `);
const [part, name] = [pp[0], pp.slice(1).join(` - `)];
// console.log(r);
Expand All @@ -18,13 +16,6 @@ const rowCoverter = (r: d3.DSVRowString<SourceColumns>) => {
} as Usage;
};

export const parseSampleUsage = async (): Promise<d3.DSVParsedArray<Usage>> => {
const url = 'https://s3.spilleman.nl/shared/usage.csv';
const rows = await d3.csv(url, rowCoverter);
rows.pop();
return rows;
};

export const parseUsage = async (file: File) => {
const txt = await readFile(file);
const rows = await d3.csvParse(txt, rowCoverter);
Expand Down
11 changes: 0 additions & 11 deletions src/lib/stores/computation.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<script lang="ts">
import { ChartLineUpOutline, EuroOutline, ClipboardOutline } from 'flowbite-svelte-icons';
import { computation } from '$lib/stores/computation';
import { ratecard, usage } from '$lib/db';
import { ratecard, usage, computation, db } from '$lib/db';
</script>

<svelte:head>
Expand Down
Loading

0 comments on commit 6f190c9

Please sign in to comment.