Skip to content

Commit

Permalink
feat(msar): —logRequests, session-based api calls
Browse files Browse the repository at this point in the history
- `—logRequests` flag useful mostly for debugging
- API requests are now made against a specific session (allowing per-tab sessions to be created/destroyed independendently).
  • Loading branch information
battis committed Jan 3, 2025
1 parent ec0aaef commit b41bc90
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 12 deletions.
8 changes: 6 additions & 2 deletions packages/msar/src/common/workflow/args/flags.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import cli from '@battis/qui-cli';

export const defaults = {
ignoreErrors: true
ignoreErrors: true,
logRequests: false
};

export const flags = {
ignoreErrors: {
description: `Continue collecting snapshots even if errors are encountered (default: ${cli.colors.value(defaults.ignoreErrors)}, use ${cli.colors.value('--no-ignoreErrors')} to halt on errors)`,
description: `Continue collecting snapshots even if errors are encountered (default: ${cli.colors.value(defaults.ignoreErrors)}${defaults.ignoreErrors ? `, use ${cli.colors.value('--no-ignoreErrors')} to halt on errors` : ''})`,
default: defaults.ignoreErrors
},
logRequests: {
description: `Log fetch requests and responses for analysis and debugging (default: ${cli.colors.value(defaults.logRequests)})`
}
};
4 changes: 3 additions & 1 deletion packages/msar/src/common/workflow/args/parse.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
export type Parsed = {
ignoreErrors: boolean;
batchSize: number;
logRequests: boolean;
};

export function parse(values: Record<string, any>): Parsed {
return {
ignoreErrors: !!values.ignoreErrors,
batchSize: parseInt(values.batchSize)
batchSize: parseInt(values.batchSize),
logRequests: !!values.logRequests
};
}
10 changes: 8 additions & 2 deletions packages/msar/src/workflows/Snapshot/Area/Assignments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import * as Base from './Base.js';

export type Data = types.Assignment2.UserAssignmentDetailsGetAllData.Response[];

export const snapshot: Base.Snapshot<Data> = async ({ groupId: sectionId }) => {
export const snapshot: Base.Snapshot<Data> = async ({
session,
groupId: sectionId,
logRequests
}) => {
cli.log.debug(`Group ${sectionId}: Start capturing assignments`);

const assignmentList = await api.datadirect.ImportAssignmentsGet({
Expand All @@ -18,11 +22,13 @@ export const snapshot: Base.Snapshot<Data> = async ({ groupId: sectionId }) => {
for (const assignment of assignmentList) {
assignments.push(
await api.Assignment2.UserAssignmentDetailsGetAllData({
session,
payload: {
assignmentIndexId: assignment.assignment_index_id,
studentUserId: -1,
personaId: 3
}
},
logRequests
})
);
}
Expand Down
3 changes: 3 additions & 0 deletions packages/msar/src/workflows/Snapshot/Area/Base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Endpoint, api as types } from 'datadirect';
import { PuppeteerSession } from 'datadirect-puppeteer';

export class StudentDataError extends Error {
public message = 'Student data is not included in this snapshot';
Expand All @@ -9,9 +10,11 @@ export class StudentDataError extends Error {
* Should be able to invoke endpoints on specific session
*/
export type Options = {
session?: PuppeteerSession.Authenticated;
groupId: number;
payload?: types.datadirect.ContentItem.Payload;
ignoreErrors?: boolean;
logRequests?: boolean;
studentData?: boolean;
};

Expand Down
12 changes: 9 additions & 3 deletions packages/msar/src/workflows/Snapshot/Area/BulletinBoard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,26 @@ async function getPossibleContent(leadSectionId: number) {
}

export const snaphot: Base.Snapshot<Data> = async ({
session,
groupId: Id,
payload = { format: 'json' },
ignoreErrors = true,
studentData
studentData,
logRequests
}): Promise<Data | undefined> => {
cli.log.debug(`Group ${Id}: Start capturing bulletin board`);
try {
const BulletinBoard: Data = [];
await getPossibleContent(Id);
const items = await api.datadirect.BulletinBoardContentGet({
session,
payload: {
format: 'json',
sectionId: Id,
associationId: 1,
pendingInd: false
}
},
logRequests
});
for (const item of items) {
const ContentType = possibleContent!.find(
Expand All @@ -64,8 +68,10 @@ export const snaphot: Base.Snapshot<Data> = async ({
item,
possibleContent!,
{
session,
payload: { ...payload, contextValue: Id, contextLabelId: 2 },
pathParams: { Id }
pathParams: { Id },
logRequests
}
)
});
Expand Down
5 changes: 4 additions & 1 deletion packages/msar/src/workflows/Snapshot/Area/GradeBook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@ export type Data = Item[];
export const snapshot: Base.Snapshot<Data> = async ({
groupId: sectionId,
ignoreErrors,
studentData
studentData,
...options
}) => {
cli.log.debug(`Group ${sectionId}: Start capturing gradebook`);
try {
const markingPeriods = await api.datadirect.GradeBookMarkingPeriodList({
...options,
payload: { sectionId }
});
const Gradebook: Data = [];
for (const markingPeriod of markingPeriods) {
const entry: Item = {
markingPeriod,
gradebook: await api.gradebook.hydrategradebook({
...options,
payload: {
sectionId,
markingPeriodId: markingPeriod.MarkingPeriodId
Expand Down
4 changes: 3 additions & 1 deletion packages/msar/src/workflows/Snapshot/Area/SectionInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ export type Data = types.datadirect.SectionInfoView.Item;

export const snapshot: Base.Snapshot<Data> = async ({
groupId: sectionId,
ignoreErrors = true
ignoreErrors = true,
...options
}): Promise<Data | undefined> => {
cli.log.debug(`Group ${sectionId}: Start capturing section info`);
try {
return (
await api.datadirect.SectionInfoView({
...options,
payload: {
format: 'json',
sectionId,
Expand Down
6 changes: 5 additions & 1 deletion packages/msar/src/workflows/Snapshot/Area/Topics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ export const snapshot: Base.Snapshot<Data> = async ({
groupId: Id,
payload,
ignoreErrors = true,
studentData
studentData,
...options
}): Promise<Data | undefined> => {
cli.log.debug(`Group ${Id}: Start capturing topics`);
try {
const Topics: Data = [];
await getPossibleContent();
const topics = await api.datadirect.sectiontopicsget({
...options,
payload: {
// TODO Fix typing to avoid parameter redundancy
active: true,
Expand All @@ -53,6 +55,7 @@ export const snapshot: Base.Snapshot<Data> = async ({
const Content: Item[] = [];
const items: types.datadirect.topiccontentget.Response = (
await api.datadirect.topiccontentget({
...options,
payload: {
format: 'json',
index_id: topic.TopicIndexID,
Expand Down Expand Up @@ -89,6 +92,7 @@ export const snapshot: Base.Snapshot<Data> = async ({
item,
possibleContent!,
{
...options,
payload: {
format: 'json',
...payload,
Expand Down
2 changes: 1 addition & 1 deletion packages/msar/src/workflows/Snapshot/Manager/Single.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NumericDuration } from '@battis/descriptive-types';
import cli from '@battis/qui-cli';
import { api as types } from 'datadirect';
import { api, PuppeteerSession } from 'datadirect-puppeteer';
import { PuppeteerSession } from 'datadirect-puppeteer';
import * as common from '../../../common.js';
import * as Area from '../Area.js';

Expand Down

0 comments on commit b41bc90

Please sign in to comment.