aractingi commited on
Commit
a360c63
·
unverified ·
1 Parent(s): 30ba4c3

Remove the bug that assumes only one parquet file exists for the episodes metadata (#11)

Browse files
src/app/[org]/[dataset]/[episode]/fetch-data.ts CHANGED
@@ -481,7 +481,19 @@ async function loadEpisodeDataV3(
481
  // Convert BigInt to number if needed
482
  const fromIndex = Number(episodeMetadata.dataset_from_index || 0);
483
  const toIndex = Number(episodeMetadata.dataset_to_index || fullData.length);
484
- const episodeData = fullData.slice(fromIndex, toIndex);
 
 
 
 
 
 
 
 
 
 
 
 
485
 
486
  if (episodeData.length === 0) {
487
  return { chartDataGroups: [], ignoredColumns: [], task: undefined };
@@ -924,47 +936,50 @@ async function loadEpisodeMetadataV3Simple(
924
  version: string,
925
  episodeId: number,
926
  ): Promise<any> {
927
- const episodesMetadataUrl = buildVersionedUrl(
928
- repoId,
929
- version,
930
- "meta/episodes/chunk-000/file-000.parquet"
931
- );
 
 
 
 
 
 
932
 
933
- try {
934
- const arrayBuffer = await fetchParquetFile(episodesMetadataUrl);
935
- const episodesData = await readParquetAsObjects(arrayBuffer, []);
936
-
937
- if (episodesData.length === 0) {
938
- throw new Error("No episode metadata found");
939
- }
940
-
941
- // Find the row for the requested episode
942
- let episodeRow = null;
943
-
944
- for (let i = 0; i < episodesData.length; i++) {
945
- const row = episodesData[i];
946
- const parsedRow = parseEpisodeRowSimple(row);
947
 
948
- if (parsedRow.episode_index === episodeId) {
949
- episodeRow = row;
950
- break;
 
951
  }
952
- }
953
-
954
- if (!episodeRow) {
955
- // Fallback: if we can't find the exact episode, use the row at index episodeId
956
- if (episodeId < episodesData.length) {
957
- episodeRow = episodesData[episodeId];
958
- } else {
959
- throw new Error(`Episode ${episodeId} not found in metadata`);
 
 
 
 
 
 
960
  }
 
 
 
961
  }
962
-
963
- // Convert the row to a usable format
964
- return parseEpisodeRowSimple(episodeRow);
965
- } catch (error) {
966
- throw error;
967
  }
 
 
 
968
  }
969
 
970
  // Simple parser for episode row - focuses on key fields for episodes
 
481
  // Convert BigInt to number if needed
482
  const fromIndex = Number(episodeMetadata.dataset_from_index || 0);
483
  const toIndex = Number(episodeMetadata.dataset_to_index || fullData.length);
484
+
485
+ // Find the starting index of this parquet file by checking the first row's index
486
+ // This handles the case where episodes are split across multiple parquet files
487
+ let fileStartIndex = 0;
488
+ if (fullData.length > 0 && fullData[0].index !== undefined) {
489
+ fileStartIndex = Number(fullData[0].index);
490
+ }
491
+
492
+ // Adjust indices to be relative to this file's starting position
493
+ const localFromIndex = Math.max(0, fromIndex - fileStartIndex);
494
+ const localToIndex = Math.min(fullData.length, toIndex - fileStartIndex);
495
+
496
+ const episodeData = fullData.slice(localFromIndex, localToIndex);
497
 
498
  if (episodeData.length === 0) {
499
  return { chartDataGroups: [], ignoredColumns: [], task: undefined };
 
936
  version: string,
937
  episodeId: number,
938
  ): Promise<any> {
939
+ // Pattern: meta/episodes/chunk-{chunk_index:03d}/file-{file_index:03d}.parquet
940
+ // Most datasets have all episodes in chunk-000/file-000, but episodes can be split across files
941
+
942
+ let episodeRow = null;
943
+ let fileIndex = 0;
944
+ const chunkIndex = 0; // Episodes are typically in chunk-000
945
+
946
+ // Try loading episode metadata files until we find the episode
947
+ while (!episodeRow) {
948
+ const episodesMetadataPath = `meta/episodes/chunk-${chunkIndex.toString().padStart(3, "0")}/file-${fileIndex.toString().padStart(3, "0")}.parquet`;
949
+ const episodesMetadataUrl = buildVersionedUrl(repoId, version, episodesMetadataPath);
950
 
951
+ try {
952
+ const arrayBuffer = await fetchParquetFile(episodesMetadataUrl);
953
+ const episodesData = await readParquetAsObjects(arrayBuffer, []);
 
 
 
 
 
 
 
 
 
 
 
954
 
955
+ if (episodesData.length === 0) {
956
+ // Empty file, try next one
957
+ fileIndex++;
958
+ continue;
959
  }
960
+
961
+ // Find the row for the requested episode by episode_index
962
+ for (const row of episodesData) {
963
+ const parsedRow = parseEpisodeRowSimple(row);
964
+
965
+ if (parsedRow.episode_index === episodeId) {
966
+ episodeRow = row;
967
+ break;
968
+ }
969
+ }
970
+
971
+ if (!episodeRow) {
972
+ // Not in this file, try the next one
973
+ fileIndex++;
974
  }
975
+ } catch (error) {
976
+ // File doesn't exist - episode not found
977
+ throw new Error(`Episode ${episodeId} not found in metadata (searched up to file-${fileIndex.toString().padStart(3, "0")}.parquet)`);
978
  }
 
 
 
 
 
979
  }
980
+
981
+ // Convert the row to a usable format
982
+ return parseEpisodeRowSimple(episodeRow);
983
  }
984
 
985
  // Simple parser for episode row - focuses on key fields for episodes