Skip to content

Refactor graphImageResponse to use Temporary Files#1667

Open
a1-su wants to merge 3 commits intoCourseography:masterfrom
a1-su:refactor-graph-image-response
Open

Refactor graphImageResponse to use Temporary Files#1667
a1-su wants to merge 3 commits intoCourseography:masterfrom
a1-su:refactor-graph-image-response

Conversation

@a1-su
Copy link
Contributor

@a1-su a1-su commented Feb 27, 2026

Proposed Changes

This PR refactors the graphImageResponse function in Controllers/Graph.hs to use withSystemTempFile temporary files (from the temporary library in Haskell). Previously the files were being manually generated and deleted, so if an error ever occurred during process of saving the graph, the files would be left undeleted.

Note that this modified some other functions, including getActiveGraphImage in Export/GetImages.hs, which was coupled with the exportTimetablePDF function in Controllers/Timetable.hs. I've modified getActiveGraphImage to keep its original functionality and graphImageResponse to use helper function writeActiveGraphImage, but should exportTimetablePDF ever get refactored to also use temporary files and writeActiveGraphImage, getActiveGraphImage can be safely removed.

Type of Change

(Write an X or a brief description next to the type or types that best describe your changes.)

Type Applies?
🚨 Breaking change (fix or feature that would cause existing functionality to change)
New feature (non-breaking change that adds functionality)
🐛 Bug fix (non-breaking change that fixes an issue)
🎨 User interface change (change to user interface; provide screenshots)
♻️ Refactoring (internal change to codebase, without changing functionality) X
🚦 Test update (change that only adds or modifies tests)
📦 Dependency update (change that updates a dependency)
🔧 Internal (change that only affects developers or continuous integration)

Checklist

(Complete each of the following items for your pull request. Indicate that you have completed an item by changing the [ ] into a [x] in the raw text, or by clicking on the checkbox in the rendered description on GitHub.)

Before opening your pull request:

  • I have performed a self-review of my changes.
    • Check that all changed files included in this pull request are intentional changes.
    • Check that all changes are relevant to the purpose of this pull request, as described above.
  • I have added tests for my changes, if applicable.
    • This is required for all bug fixes and new features.
  • I have updated the project documentation, if applicable.
    • This is required for new features.
  • If this is my first contribution, I have added myself to the list of contributors.
  • I have updated the project Changelog (this is required for all changes).

After opening your pull request:

  • I have verified that the CircleCI checks have passed.
  • I have requested a review from a project maintainer.

Questions and Comments

(Include any questions or comments you have regarding your changes.)

@coveralls
Copy link

coveralls commented Feb 27, 2026

Pull Request Test Coverage Report for Build b17a4f5a-53dd-4f11-807c-7a64c9ccb0cb

Details

  • 0 of 22 (0.0%) changed or added relevant lines in 4 files are covered.
  • 20 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.2%) to 56.605%

Changes Missing Coverage Covered Lines Changed/Added Lines %
app/Util/Helpers.hs 0 2 0.0%
app/Svg/Generator.hs 0 4 0.0%
app/Controllers/Graph.hs 0 7 0.0%
app/Export/GetImages.hs 0 9 0.0%
Files with Coverage Reduction New Missed Lines %
app/Export/GetImages.hs 20 0.0%
Totals Coverage Status
Change from base Build e318a04a-7db4-4eb7-98e7-21e1ce93c3b7: 0.2%
Covered Lines: 2280
Relevant Lines: 3951

💛 - Coveralls

graphInfo <- look "JsonLocalStorageObj"
(svgFilename, imageFilename) <- liftIO $ getActiveGraphImage graphInfo
liftIO $ returnImageData svgFilename imageFilename
liftIO $ withSystemTempFile "graph.svg" $ \svgPath svgHandle -> do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the filenames should still be random (want to avoid possible name collisions if multiple responses are being executed at the same time)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could be mistaken, but the withSystemTempFile accounts for this automatically. The "graph.svg" and "graph.png" are file name templates, which have a random number placed after the file name but before the file extension when created, so name collisions are avoided and a random number doesn't need to be added manually.
image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes that's great, thanks for sharing this element of the documentation. 👍

(svgFilename, imageFilename) <- liftIO $ getActiveGraphImage graphInfo
liftIO $ returnImageData svgFilename imageFilename
liftIO $ withSystemTempFile "graph.svg" $ \svgPath svgHandle -> do
hClose svgHandle
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to use this handle, passing it down all the way to buildSVGHelper in order to write directly to the file. This is different from the png temp file, since for the latter the file is being created externally to Courseography (by running magick in a subprocess)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately because of how the helper functions were implemented, the way I've decided to make this change included an hClose inside the getGraphImage function in Export/GetImages.hs, which I'm not sure is ideal if it's being called within a helper function inside a withSystemTempFile (or withFile, which was added because the helper functions are coupled with exportTimetablePDFResponse in Controllers/Timetable.hs).

I wasn't sure if I should have since it was outside the scope of the ticket, but if you'd have preferred, I can move createImageFile, which was previously called in getGraphImage, to be imported in Controllers/Graph.hs instead. This way, hClose can be called inside the withSystemTempFile instead of in a helper function inside withSystemTempFile. To keep the original functionality, this would mean the createImageFile would also need to be called in and duplicated in getActiveGraphImage (inside the withFile) instead of just in one place (because of the coupling with the timetable PDF export). I also chose not to do this since getTimetableImage, a very similar function, uses createImageFile as well and I was worried it would be unintuitive to have them behave differently.

If the timetable export also gets refactored, I can modify the code so it's more cohesive and better follows standard practices, and feel free to let me know if this should be changed.

@a1-su a1-su requested a review from david-yz-liu March 4, 2026 06:36
Copy link
Contributor

@david-yz-liu david-yz-liu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @a1-su, nice work. Overall this is a bit messy because of the issues you've identified with the existing timetable export. Please go ahead and refactor the timetable exporting functions to use the same approach as well. You're definitely on the right track with this and refactoring both will let you clean the up code, so I'm happy for you to proceed with this.

graphInfo <- look "JsonLocalStorageObj"
(svgFilename, imageFilename) <- liftIO $ getActiveGraphImage graphInfo
liftIO $ returnImageData svgFilename imageFilename
liftIO $ withSystemTempFile "graph.svg" $ \svgPath svgHandle -> do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes that's great, thanks for sharing this element of the documentation. 👍

@a1-su a1-su requested a review from david-yz-liu March 6, 2026 07:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants