Skip to content

Commit

Permalink
wip: Work on adding package versions
Browse files Browse the repository at this point in the history
  • Loading branch information
KallynGowdy committed Nov 12, 2024
1 parent 467c43e commit 50bee8f
Show file tree
Hide file tree
Showing 8 changed files with 625 additions and 211 deletions.
60 changes: 59 additions & 1 deletion src/aux-common/common/PolicyPermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const HUME_RESOURCE_KIND = 'ai.hume';
export const WEBHOOK_RESOURCE_KIND = 'webhook';
export const NOTIFICATION_RESOURCE_KIND = 'notification';
export const PACKAGE_RESOURCE_KIND = 'package';
export const PACKAGE_VERSION_RESOURCE_KIND = 'package.version';

/**
* The possible types of resources that can be affected by permissions.
Expand All @@ -44,6 +45,7 @@ export type ResourceKinds =
| 'webhook'
| 'notification'
| 'package'
| 'package.version'
| 'loom'
| 'ai.sloyd'
| 'ai.hume';
Expand Down Expand Up @@ -226,6 +228,20 @@ export type PackageActionKinds =
| 'list'
| 'run';

/**
* The possible types of actions that can be performed on package.version resources.
*
* @dochash types/permissions
* @docname PackageVersionActionKinds
*/
export type PackageVersionActionKinds =
| 'create'
| 'read'
| 'update'
| 'delete'
| 'list'
| 'run';

/**
* The possible types of permissions that can be added to policies.
*
Expand All @@ -247,7 +263,8 @@ export type AvailablePermissions =
| HumePermission
| WebhookPermission
| NotificationPermission
| PackagePermission;
| PackagePermission
| PackageVersionPermission;

export const SUBJECT_TYPE_VALIDATION = z.enum(['user', 'inst', 'role']);

Expand Down Expand Up @@ -336,6 +353,15 @@ export const PACKAGE_ACTION_KINDS_VALIDATION = z.enum([
RUN_ACTION,
]);

export const PACKAGE_VERSION_ACTION_KINDS_VALIDATION = z.enum([
CREATE_ACTION,
READ_ACTION,
UPDATE_ACTION,
DELETE_ACTION,
LIST_ACTION,
RUN_ACTION,
]);

export const RESOURCE_KIND_VALIDATION = z.enum([
DATA_RESOURCE_KIND,
FILE_RESOURCE_KIND,
Expand Down Expand Up @@ -838,6 +864,37 @@ type ZodPackagePermissionAssertion = HasType<
PackagePermission
>;

/**
* Defines an interface that describes common options for all permissions that affect package.version resources.
*
* @dochash types/permissions
* @docname PackageVersionPermission
*/
export interface PackageVersionPermission extends Permission {
/**
* The kind of the permission.
*/
resourceKind: 'package.version';

/**
* The action that is allowed.
* If null, then all actions are allowed.
*/
action: PackageVersionActionKinds | null;
}
export const PACKAGE_VERSION_PERMISSION_VALIDATION =
PERMISSION_VALIDATION.extend({
resourceKind: z.literal(PACKAGE_VERSION_RESOURCE_KIND),
action: PACKAGE_VERSION_ACTION_KINDS_VALIDATION.nullable(),
});
type ZodPackageVersionPermission = z.infer<
typeof PACKAGE_VERSION_PERMISSION_VALIDATION
>;
type ZodPackageVersionPermissionAssertion = HasType<
ZodPackageVersionPermission,
PackageVersionPermission
>;

export const AVAILABLE_PERMISSIONS_VALIDATION = z.discriminatedUnion(
'resourceKind',
[
Expand All @@ -853,6 +910,7 @@ export const AVAILABLE_PERMISSIONS_VALIDATION = z.discriminatedUnion(
WEBHOOK_PERMISSION_VALIDATION,
NOTIFICATION_PERMISSION_VALIDATION,
PACKAGE_PERMISSION_VALIDATION,
PACKAGE_VERSION_PERMISSION_VALIDATION,
]
);

Expand Down
56 changes: 0 additions & 56 deletions src/aux-records/packages/MemoryPackageRecordsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import {
PackageRecordsStore,
PackageRecord,
PackageSubscriptionMetrics,
PackageVersion,
PackageRecordVersion,
ListedPackageVersion,
} from './PackageRecordsStore';
import { SubscriptionFilter } from '../MetricsStore';

Expand All @@ -16,76 +13,23 @@ export class MemoryPackageRecordsStore
extends MemoryCrudRecordsStore<PackageRecord>
implements PackageRecordsStore
{
private _packageVersions: PackageRecordVersion[] = [];

async addPackageVersion(version: PackageRecordVersion): Promise<void> {
const index = this._packageVersions.findIndex(
(v) =>
v.recordName === version.recordName &&
v.address === version.address &&
v.version.major === version.version.major &&
v.version.minor === version.version.minor &&
v.version.patch === version.version.patch &&
v.version.tag === version.version.tag
);

if (index >= 0) {
throw new Error('Version already exists');
}

this._packageVersions.push(version);
}

async listPackageVersions(
recordName: string,
address: string
): Promise<ListedPackageVersion[]> {
const versions = this._packageVersions.filter(
(v) => v.recordName === recordName && v.address === address
);
return versions.map((v) => ({
recordName: v.recordName,
address: v.address,
version: v.version,
sha256: v.sha256,
auxSha256: v.auxSha256,
scriptSha256: v.scriptSha256,
entitlements: v.entitlements,
sizeInBytes: v.sizeInBytes,
createdAtMs: v.createdAtMs,
}));
}

async getSubscriptionMetrics(
filter: SubscriptionFilter
): Promise<PackageSubscriptionMetrics> {
const info = await super.getSubscriptionMetrics(filter);

let totalItems = 0;
let totalPackageVersions = 0;
let totalPackageVersionBytes = 0;

const records = filter.ownerId
? await this.store.listRecordsByOwnerId(filter.ownerId)
: await this.store.listRecordsByStudioId(filter.studioId);
for (let record of records) {
totalItems += this.getItemRecord(record.name).size;

for (let version of this._packageVersions) {
if (version.recordName !== record.name) {
continue;
}

totalPackageVersions++;
totalPackageVersionBytes += version.sizeInBytes;
}
}

return {
...info,
totalItems,
totalPackageVersions,
totalPackageVersionBytes,
};
}
}
154 changes: 0 additions & 154 deletions src/aux-records/packages/PackageRecordsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,6 @@ import { SubscriptionFilter } from '../MetricsStore';
* Defines a store that contains notification records.
*/
export interface PackageRecordsStore extends CrudRecordsStore<PackageRecord> {
/**
* Adds the given package version to the store.
* @param version The version of the package to save.
*/
addPackageVersion(version: PackageRecordVersion): Promise<void>;

/**
* Gets the list of package versions for the given record name and address.
* The resulting list will be sorted by the version in descending order.
* @param recordName The name of the record.
* @param address The address of the package.
*/
listPackageVersions(
recordName: string,
address: string
): Promise<ListedPackageVersion[]>;

/**
* Gets the item metrics for the subscription of the given user or studio.
* @param filter The filter to use.
Expand All @@ -50,146 +33,9 @@ export interface PackageRecord extends CrudRecord {
// description: string | null;
}

export interface PackageRecordVersion {
/**
* The name of the record that this package version is stored in.
*/
recordName: string;

/**
* The address of the package that this version is stored under.
*/
address: string;

/**
* The version of the package.
*/
version: PackageVersion;

/**
* The aux that is recorded in the version.
*/
aux: StoredAux;

/**
* The SHA-256 hash of the package version.
*/
sha256: string;

/**
* The SHA-256 hash of the aux.
*/
auxSha256: string;

/**
* The SHA-256 of the scripts that are stored in the aux.
*/
scriptSha256: string;

/**
* The list of entitlements that the package requires.
*/
entitlements: string[];

/**
* The readme of the package.
*/
readme: string;

/**
* The size of the package version in bytes.
*/
sizeInBytes: number;

/**
* The unix time in miliseconds that this package version was created at.
*/
createdAtMs: number;
}

export interface ListedPackageVersion {
/**
* The name of the record that the package versions are stored in.
*/
recordName: string;

/**
* The address of the package that the versions are stored under.
*/
address: string;

/**
* The version of the package.
*/
version: PackageVersion;

/**
* The SHA-256 hash of the package version.
*/
sha256: string;

/**
* The SHA-256 hash of the aux.
*/
auxSha256: string;

/**
* The SHA-256 of the scripts that are stored in the aux.
*/
scriptSha256: string;

/**
* The list of entitlements that the package requires.
*/
entitlements: string[];

/**
* The size of the version in bytes.
*/
sizeInBytes: number;

/**
* The unix time in miliseconds that this version was created at.
*/
createdAtMs: number;
}

export interface PackageVersion {
/**
* The major version of the package.
*/
major: number;

/**
* The minor version of the package.
*/
minor: number;

/**
* The patch version of the package.
*/
patch: number;

/**
* The pre-release version of the package.
* If empty or null, then this is a stable release.
*/
tag: string | null;
}

export interface PackageSubscriptionMetrics extends CrudSubscriptionMetrics {
/**
* The total number of packages stored in the subscription.
*/
totalItems: number;

/**
* The total number of package versions stored in the subscription.
*/
totalPackageVersions: number;

/**
* The total number of bytes stored in package versions in the subscription.
*/
totalPackageVersionBytes: number;
}
Loading

0 comments on commit 50bee8f

Please sign in to comment.