Skip to main content

Ralsina.Me — Roberto Alsina's website

Posts about grafito

Literate version of grafito code (WIP)

In the past cou­ple of weeks I have start­ed (and pret­ty much fin­ished) a tool called Grafi­to and the end re­sult is un­der two thou­sand lines of code, in­clud­ing HTML and CSS.

For small­er code­bas­es, I think it makes sense to make them lit­er­ate. Just a cou­ple hours writ­ing around the code can make it per­fect­ly clear to un­der­stand for any­one start­ing with the code­base and (to be hon­est) al­so for the me of the fu­ture who will re­mem­ber noth­ing about it.

So I am pub­lish­ing the com­ment­ed code­base of grafi­to pro­cessed through Cryc­co a lit­er­ate pro­gram­ming tool I wrote. Yes, the web­site for Cryc­co is Cryc­co's source code. That's tra­di­tion­al :-)

The code is not yet ful­ly com­ment­ed and I have found a cou­ple bugs in Cryc­co al­ready:

  • Links in the side­bar are wrong in some cas­es
  • There is no way to pub­lish a lit­er­ate HTML file!

In any case, I ex­pect no­body cares, but I think it's nice and it's not a ton of ef­fort so that makes it worth do­ing, so I did it.

Creating a demo site for a service

Re­cent­ly I wrote an app called Grafi­to to view sys­temd/jour­nald logs (those are the logs in most Lin­ux sys­tem­s) and be able to fil­ter them, see de­tails of a spe­cif­ic en­try, etc.

One prob­lem with this kind of tool is that I can't just open it to the world be­cause then ev­ery­one would be able to see the logs in a re­al ma­chine. While that is usu­al­ly not a prob­lem be­cause the in­for­ma­tion is not ter­ri­bly use­ful (sure, you will know what's run­ning, big whoop­s), it may dis­play a dan­ger­ous piece of da­ta which I may not want to ex­pose.

So, the right way to do this is to cre­ate a de­mo site. It could be show­ing the re­al da­ta from a throw­away sys­tem (like a vir­tu­al ma­chine) or like I did show fake da­ta.

To show fake da­ta you can use a fak­er. Fak­ers are fun! I am us­ing askn/­fak­er which is a Crys­tal one. Fak­ers let you ask for, you guessed it... fake da­ta.

For ex­am­ple, you can ask for an ad­dress or a cred­it card num­ber and it would give you some­thing ran­dom that match­es the ob­vi­ous pat­terns of what you ask.

One I love is to ask for say_something_smart which gives you smart things!

Faker::Hacker.say_something_smart #=> 
"Try to compress the SQL interface, maybe it will program the 
back-end hard drive!"

So, I wrote a function that works like journalctl but is totally fake. The source code is just a quick hack.

Then, I used a con­di­tion­al com­pile flag to route the in­fo re­quests in­to that fake func­tion:

{% if flag?(:fake_journal) %}
  require "./fake_journal_data" # For fake data generation
{% end %}

{% if flag?(:fake_journal) %}
    Log.info { "Journalctl.known_service_units: Using FAKE service units." }
    fake_units = FakeJournalData::SAMPLE_UNIT_NAMES.compact.uniq.sort
    Log.debug { "Returning #{fake_units.size} fake service units." }
    return fake_units
{% else %}
    # Return actual good stuff
{% end %}

And that's it! If I compile with -Dfake_journal it builds a binary that is using only fake data. Then I had it run in my home server and voilá: a demo!

See it in ac­tion! grafi­to-de­mo.ralsi­na.me


Contents © 2000-2025 Roberto Alsina