Skip to content

Commit

Permalink
fix(msar): snapshot —all working after simplified api bindings
Browse files Browse the repository at this point in the history
Close #167 with major regression, excising the batched snapshots (which need to return).
  • Loading branch information
battis committed Jan 3, 2025
1 parent 169fd96 commit 697852a
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 43 deletions.
2 changes: 1 addition & 1 deletion packages/msar/src/common/output/args/parse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type Parsed = {
outputOptions: { outputPath?: string; pretty: boolean };
outputOptions: { outputPath?: string; pretty?: boolean };
};

export function parse(values: Record<string, string>): Parsed {
Expand Down
2 changes: 1 addition & 1 deletion packages/msar/src/workflows/Download/Spider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Downloader, Options as DownloaderOptions } from './Downloader.js';
export type BaseOptions = {
include?: RegExp[];
exclude?: RegExp[];
haltOnError: boolean;
haltOnError?: boolean;
};

type TraverseOptions = BaseOptions & {
Expand Down
2 changes: 1 addition & 1 deletion packages/msar/src/workflows/Download/args/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export type Parsed = common.args.Parsed & {
downloadOptions: {
include?: RegExp[];
exclude?: RegExp[];
haltOnError: boolean;
haltOnError?: boolean;
};
};

Expand Down
50 changes: 13 additions & 37 deletions packages/msar/src/workflows/Snapshot/Manager/All.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import cli from '@battis/qui-cli';
import cliProgress from 'cli-progress';
import { api } from 'datadirect-puppeteer';
import { api, PuppeteerSession } from 'datadirect-puppeteer';
import crypto from 'node:crypto';
import fs from 'node:fs/promises';
import path from 'node:path';
Expand Down Expand Up @@ -33,48 +33,29 @@ export async function snapshot({
termsOffered,
year,
groupsPath,
batchSize,
outputOptions,
ignoreErrors,
quit,
...options
}: Options) {
if (!year) {
throw new Error(`year must be defined`);
}

const { ignoreErrors, batchSize } = options;
const { outputPath, pretty } = outputOptions;

const session = await api.init(url, {
const session = await PuppeteerSession.Fetchable.init(url, {
credentials,
...puppeteerOptions
});

cli.log.info(
`Snapshot temporary files will be saved to ${cli.colors.url(TEMP)}`
);
const associations = cleanSplit(association);
const terms = cleanSplit(termsOffered);
/*
* FIXME refactoring broke `msar snapshot --all`
* Filtering is now returning zero matches
* ```sh
* Task Terminated with exit code 1
* Snapshot temporary files will be saved to /tmp/msar/snapshot/0bb0250d-8a4c-4c6a-bb28-da5846eb0e76
* 0 groups match filters
* node:internal/fs/promises:948
* const result = await PromisePrototypeThen(
* ^
* Error: ENOENT: no such file or directory, scandir '/tmp/msar/snapshot/0bb0250d-8a4c-4c6a-bb28-da5846eb0e76'
* at async Object.readdir (node:internal/fs/promises:948:18)
* at async Module.snapshot (file:///path/tp/myschoolapp-reporting/packages/myschoolapp-reporting/dist/workflows/Snapshot/Manager/All.js:80:22)
* at async file:///path/to/myschoolapp-reporting/packages/myschoolapp-reporting/dist/bin/commands/snapshot.js:21:9 {
* errno: -2,
* code: 'ENOENT',
* syscall: 'scandir',
* path: '/tmp/msar/snapshot/0bb0250d-8a4c-4c6a-bb28-da5846eb0e76'
* }
* ```
*/
const groups = (
await api.datadirect.groupFinderByYear({
session,
...options,
payload: {
schoolYearLabel: year
}
Expand All @@ -88,9 +69,6 @@ export async function snapshot({
true
))
);
cli.log.info(
`Snapshot temporary files will be saved to ${cli.colors.url(TEMP)}`
);
cli.log.info(`${groups.length} groups match filters`);
const progressBars = new cliProgress.MultiBar({});
const progress = progressBars.create(groups.length, 0);
Expand All @@ -111,21 +89,19 @@ export async function snapshot({

const errors: typeof groups = [];

// FIXME msar snapshot --all needs to return to batched snapshots
for (let i = 0; i < groups.length; i++) {
try {
const tempPath = path.join(TEMP, `${pad(i)}.json`);
const snapshot = await Single.snapshot({
session,
groupId: groups[i].lead_pk,
outputOptions,
ignoreErrors,
batchSize,
...options,
groupId: groups[i].lead_pk,
quit: true
});
const tempPath = path.join(TEMP, `${pad(i)}.json`);
await common.output.writeJSON(tempPath, snapshot);
common.output.writeJSON(tempPath, snapshot);
progressBars.log(
`Wrote snapshot ${snapshot?.SectionInfo?.Teacher}'s ${snapshot?.SectionInfo?.SchoolYear} ${snapshot?.SectionInfo?.GroupName} ${snapshot?.SectionInfo?.Block} to ${cli.colors.url(tempPath)}\n`
`Wrote snapshot ${snapshot?.SectionInfo?.Teacher}'s ${snapshot?.SectionInfo?.SchoolYear} ${snapshot?.SectionInfo?.GroupName} ${snapshot?.SectionInfo?.Block} to ${cli.colors.url(outputPath)}\n`
);
} catch (error) {
if (ignoreErrors) {
Expand Down
4 changes: 2 additions & 2 deletions packages/msar/src/workflows/Snapshot/Manager/Single.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export type Context = {

export type Options = SnapshotOptions &
Context &
common.output.args.Parsed &
Partial<common.output.args.Parsed> &
common.workflow.args.Parsed;

export async function snapshot({
Expand Down Expand Up @@ -132,7 +132,7 @@ export async function snapshot({
snapshot.Metadata.Elapsed =
snapshot.Metadata.Finish.getTime() - snapshot.Metadata.Start.getTime();

if (outputOptions.outputPath) {
if (outputOptions?.outputPath) {
const { outputPath, pretty } = outputOptions;
let basename = 'snapshot';
if (snapshot.SectionInfo) {
Expand Down
2 changes: 1 addition & 1 deletion packages/msar/src/workflows/Snapshot/args/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as common from '../../../common.js';

let defaultYear = `${new Date().getFullYear()} - ${new Date().getFullYear() + 1}`;
if (new Date().getMonth() <= 6) {
defaultYear = `${new Date().getFullYear() - 1} - ${new Date().getFullYear()}`;
defaultYear = `${new Date().getFullYear() - 1} - ${new Date().getFullYear()}`;
}

export const defaults = {
Expand Down

0 comments on commit 697852a

Please sign in to comment.