The Holy Donut has gained national renown for its wholesome yet intensely delicious treats, which give even the most ascetic of eaters reason to salivate. (Dark chocolate sea salt, anyone?)

With vegan and gluten-free varieties, and a promise to use only the purest of Maine-made ingredients, The Holy Donut offers an indulgence that even purist foodies can savor. The bakery has become a destination for locals and tourists alike who crave sweets but loathe guilt.

And while the ginger-glazed, ricotta-filled Holy Cannoli-flavored pastries may be a big part of what keeps the ovens humming, the secret recipe to The Holy Donut’s success in large part boils down to how well owner Leigh Kellis answers the perennial question: Is it time to make the doughnuts?

When Kellis opened in March 2012 on Park Avenue in Portland, she and her staff would adjust doughnut production on the fly, after evaluating traffic throughout the day.

“We were just winging it,” she said.

But there was so much margin for error. A line out the door at 10 a.m. didn’t necessarily mean that business was going to be bustling all day.

So Kellis took careful notes on the constellation of factors that seemed to impact doughnut demand.

To some extent, she saw some of the same seasonal and weekly fluctuations as any Portland-based food business. A Monday in autumn is much quieter than a Monday in summer. And sales of doughnuts on Saturday are double what they are on a Monday, regardless of the season.

In some cases, demand varied, inexplicably from week to week. Sales dropped by 20 percent the week after Labor Day, for instance. The next week, they jumped by 10 percent.

But other, unique buying patterns also started to emerge.

Location

Demand at the bakery’s Park Avenue location turned out to be completely different from its location at 7 Exchange St. in the Old Port, which opened last October. On Exchange Street, where the mainstay is downtown office workers and tourists, on the busiest days they’ll sell one doughnut and a cup of coffee 600 times a day. At the Park Avenue location, they’ll sell a dozen doughnuts and a cup of coffee 300 times. And vegan doughnuts are more popular at the Park Avenue spot than they are in the Old Port.

Weather

Any time the mercury climbs or drops dramatically, sales plunge. And if any forecaster within earshot utters the words “90 degrees,” Kellis knows to cut back production by 25 percent. “People just don’t eat when it’s 90 degrees,” she said.

Cruise ships

Cruise ships, naturally, boost sales. She keeps a schedule of arriving ships that she gets from Portland’s Downtown District. “If tourists are out strolling in full force, it has a huge impact on us,” she said.

But even that’s not a sure thing. They may ramp up production in anticipation of a ship that brings 3,000 tourists to the city. But if passengers take a bus to Freeport to shop, or the weather isn’t conducive to walking around, she’s left with a lot of extra doughnuts at closing time.

Other

And surprisingly, sales are slow if the Sea Dogs have a home game at Hadlock Field, which is right across the street from the Park Avenue store. People, Kellis figures, are just focused on getting to the ballpark and eating hot dogs. “We often don’t get much business even when hundreds of them are walking by the shop on the way to a game,” she said.

Other factors seem to be tied to the nuances of human behavior. Demand triples, for instance, on the eve before any major holiday, such as Thanksgiving, Christmas and New Year’s. People wait for hours in lines that snake out the door and around the corner, in a frenzy that Kellis attributes to holiday pressure to produce and provide, whether they’re feeding a house of family members or bringing some local flavor to share on trips out of town.

“People eat like it’s the end of the world and this is the very last meal that they’ll have,” she said. “It’s frantic on those days. We just brace ourselves.”

So about a month ago, she made a bold move. Instead of staying open until 3 p.m. and 4 p.m., she started closing whenever the doughnuts sold out. She posted a warning on her website and in each store: “Best selections before noon … each location closes upon selling out!”

When her alarm sounds at 4 each morning, she checks the weather forecast, her calendar and tells the bakery how many doughnuts they’ll make that day.

So far, the plan is working. She estimates waste has been cut by 50 percent since she set up her algorithm. Often there are just a few dozen doughnuts unsold at the end of the day, which get donated to the Preble Street Resource Center. Sales are steady.

“There are days when we sell out early, which impacts sales negatively,” she said. “But waste has also been decreased. So hopefully the two will balance out eventually.”

Her system has also eliminated a lot of extra stress in the kitchen. In the past, making the call to ramp up production – based on how swift sales were throughout that day – placed an uncomfortable level of responsibility on her workers’ shoulders. After all, if they got it wrong, she could end up with hundreds of unsold doughnuts at closing time.

“Making that call affects the bottom line, so it shouldn’t be their responsibility,” said Kellis, who has about 30 employees. “It should be on me.”

Now that she makes the call every morning, “it really eliminates the insanity,” she said. “It’s the best thing we ever did.”

To be sure, the plan isn’t perfect. She probably loses some sales in the short term. And she does have to face some disappointed customers, some of whom say they have driven all the way from Boston for The Holy Donut. And sometimes she gets it wrong.

“I may miss out on $100 a day,” she said. “But I can’t pay people to make products that aren’t being sold.”