Skip to content

Refactor graphImageResponse to use Temporary Files#1667

Open
a1-su wants to merge 1 commit intoCourseography:masterfrom
a1-su:refactor-graph-image-response
Open

Refactor graphImageResponse to use Temporary Files#1667
a1-su wants to merge 1 commit 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

Pull Request Test Coverage Report for Build da278d0c-dde3-470e-b0ed-e13fa305e2f9

Details

  • 0 of 23 (0.0%) changed or added relevant lines in 3 files are covered.
  • 30 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.1%) to 56.307%

Changes Missing Coverage Covered Lines Changed/Added Lines %
app/Controllers/Graph.hs 0 6 0.0%
app/Util/Helpers.hs 0 6 0.0%
app/Export/GetImages.hs 0 11 0.0%
Files with Coverage Reduction New Missed Lines %
app/Export/GetImages.hs 30 0.0%
Totals Coverage Status
Change from base Build e318a04a-7db4-4eb7-98e7-21e1ce93c3b7: -0.1%
Covered Lines: 2280
Relevant Lines: 3977

💛 - 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)

(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)

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