LINQPad Dump using Util.Run - linqpad

I have a LINQ script that runs in a loop for a long time, over many records, and dumps the records it processed at the end of each iteration. So I see a dump of records as the script is running.
foreach (string record in lotsOfRecords) {
// do stuff ...
record.Dump()
}
When I run this from another script using Util.Compile, Util.run, I don't see the output from the results window as the application is running. But I can tell the script is running by seeing the database changes it is causing.
using (var query = Util.Compile (myQueryPath))
{
query.Run (QueryResultFormat.HtmlFragment).AsString().Dump(); // no results
}
I've tried Text, HTML and HTML fragment and I don't see any output while the script is running. I'm running LINQPad 5.43 (Pro)

Related

Displaying NLog in date descending order

I am using NLog with a VB.Net project:
Private m_logger As NLog.Logger = LogManager.GetCurrentClassLogger()
Issues are populated in the log like this:
Catch ex As Exception
Console.WriteLine("Error: {0}", ex.Message.ToString())
m_logger.Error(ex, "ExtractCalendarInformation")
End Try
Now, this VB too is a console app that is used my another application which is written with Visual C++. If the parent app has detected that there are problems it displays the log file:
void CMeetingScheduleAssistantApp::ShowLogFile(CString strLogFile)
{
if (PathFileExists(strLogFile))
{
CLogDlg dlgLog;
CTextFileRead fileLog(strLogFile);
CString strText = _T("");
fileLog.ReadLine(strText);
while (!fileLog.Eof())
{
strText.Trim();
if (strText.IsEmpty())
continue;
dlgLog.AddLogEntry(strText, true);
fileLog.ReadLine(strText);
}
dlgLog.SetErrorMode(true);
dlgLog.DoModal();
}
}
The problem is that the log file is in date ascebdung order and sometimes users send me screen shots and don't scroll.
Is there anyway to display this error log from Nlog in fdate desending order? And I don't think it is OK to just read the file in reverse.
I include both VB and C++ tags since I might have to make changes in either tool.
I saw this question Write records in descending order in NLog. The thing is my C# tool uses SimpleLogger and that creates log folders in a folder and the log is named by date, and all items in that log are date descending. But I can't use SimpleLogger with VB.Net I don't think.
One option could be to load the file into a vector<CString>, and then just use rbegin() for reverse iteration and inserting file-contents into the dlgLog.
Another option is to investigate how update scrollbar in CTextFileRead after having loaded the file, so scroll-button is moved down to the bottom.
Update
I actually was using CEdit control to display the log contents. So in the end I used the following code to automatically scroll to the bottom:
m_editLogData.LineScroll(m_editLogData.GetLineCount());

Looking for a faster way to batch export pdf:s in InDesign

I'm using this script (below) to batch export pdf:s from several indesign files for a task i do every week. The filenames are always the same, i'm using 8-10 different indd files to create 12-15 different pdf:s.
The script is set up like this:
//Sets variables for print and web presets
var myPDFExportPreset = app.pdfExportPresets.item("my-present-for-print-pdf");
var myPDFExportPreset2 = app.pdfExportPresets.item("my-preset-for-web-pdf");
//sample of one pdf exported first with print, then web pdf preset as two different files
var firstFileIntoPdfs = function(){
var openDocument= app.open(File("MYFILEPATH/firstfile.indd"));
openDocument.exportFile(
ExportFormat.pdfType,
File("MYFILEPATH/print-pdfs/firstfile-print.pdf"),
false,
myPDFExportPreset
);
openDocument.exportFile(
ExportFormat.pdfType,
File("MYFILEPATH/web-pdfs/firstfile-web.pdf"),
false,
myPDFExportPreset2
);
};
I'm defining all exports like the one above as named functions, some using only one of the presets, some two like the one above. I'm calling all these functions at the end of the file
firstFileIntoPdfs();
secondFileIntoPdfs();
thirdFileIntoPdfs();
fourthFileIntoPdfs();
and so on... ยจ
The script is however quite slow, 10 files into 1 or 2 pdfs each, like the function above, can take 10 minutes. I don't think this is a CPU issue, what i noticed is that it seems like the script is waiting for the files in "firstFileIntoPdfs()" to be created, a process that takes some minutes, before proceeding to execute the next function. Then waiting again...
Selecting File -> Export manually you can set new files to be exported while the previous ones are still processing the pdf files, which to me has seemed faster than how this script is working. Manual clicking is however error prone and tedious, of course.
Is there a better way to write this batch export script than how i've done above, that would make all functions executed while pdfs from previous functions still are processed in the system? I'd like to keep them as separate functions in order to be able to comment out some when only needing certain specific pdf:s. (unless the process of exporting all becomes nearly as fast as exporting only 1 pdf).
I hope my question makes sense!
There is an asynch method available, replace exportFile with asynchrousExportFile:
var openDocument= app.open(File("MYFILEPATH/firstfile.indd"));
openDocument.asynchronousExportFile(
ExportFormat.pdfType,
File("MYFILEPATH/print-pdfs/firstfile-print.pdf"),
false,
myPDFExportPreset
);
which use a background task

create auto sql script that runs in every hour - in c# or any other easy way

I have simple sql script:
Select * from student where score > 60
What i am trying to do is run this above script every 1 hour and getting notified on my computer in any way possibe that above condition was met. So basically i dont want to go in there and hit F5 every hour on the above statement and see if i get any result. I am hoping someone out here has something exactly for this, if you do please share the code.
You can use Sql Agent to create a job, Sql server 2008 also has mail functionality
Open SQL Management Studio and connect to your SQL Server
Expand the SQL Server Agent node (if you don't see it, use SQL configuration manager or check services and ensure that SQL Server Agent (SQLINSTANCENAME) is started)
Right click on Jobs and choose 'New Job'
You can run a SQL statement in a job. I'll let you figure out the rest of that part (it's pretty intuitive)
You may want to send your mail using xp_sendmail
Check out the SQL documentation for xp_sendmail
http://msdn.microsoft.com/en-us/library/ms189505(v=sql.105).aspx
You might need to turn the feature on (afaik it's off by default) and you need some server/machine to deliver the mail (so you might need IIS and SMTP installed if on a local machine)
Edit:
Assuming you can't access the server and want to do this on the client side, you can create a .NET framework app or windows service to do the work for you using a schedule or a timer approach:
Schedule approach:
Create a simple command line application which does the query and mails the results, and use the windows scheduler to invoke it every hour (or whatever your interval may be)
Timer approach:
Create a simple application or windows service that will run a timer thread which does the work every x number of minutes
I'd probably just go for the former. The code would be quite simple - new console app:
static void Main(string args[])
{
// No arguments needed so just do the work
using(SqlConnection conn = new SqlConnection("ConnectionString"))
{
using(SqlCommand cmd = new SqlCommand("sql query text", conn))
{
var dr = cmd.ExecuteReader();
List<myClass> results = new List<myClass>();
// Read the rows
while(dr.Read())
{
var someValue = dr.GetString(dr.GetOrdinal("ColumnName"));
// etc
// stuff these values into myClass and add to the list
results.Add(new myClass(someValue));
}
}
}
if(results.Count > 0) // Send mail
{
//Send the message.
SmtpClient client = new SmtpClient(server);
// Add credentials if the SMTP server requires them.
client.Credentials = CredentialCache.DefaultNetworkCredentials;
MailMessage message = new MailMessage(
"recipient#test.com",
"sender#test.com",
"Subject",
"Body");
// Obviously you'd have to read the rows from your list, maybe override ToString() on
// myClass and call that using a StringBuilder to build the email body and append the rows
// This may throw exceptions - maybe some error handling (in any of this code) is advisable
client.Send(message);
}
}
Disclaimer: probably none of this will compile :D
Edit 2: I'd go this way as it's much easier to debug than a windows service as you can just run it from the command line. You can also pass command line arguments so you don't need an application configuration file

executing pig script file with multiple stores in embedded pig program

I want to execute a pig script file in embedded pig program which is shown below
----testPig.pig-----
A = load '/user/biadmin/student' using PigStorage() as (name:chararray);
B = foreach A generate name;
store B into '/user/biadmin/myoutput001';
for this I have written code as shown below
> PigServer pigServer = new PigServer(ExecType.MAPREDUCE);
> pigServer.registerScript("testPig.pig");
but it is not working.I have checked this in grunt-shell mode. there it is working fine.
So I made changes like this
---testPig.pig -----
A = load '/user/biadmin/student' using PigStorage() as (name:chararray);
B = foreach A generate name;
--store B into '/user/biadmin/myoutput001';
Embedded pig code for this is
> PigServer pigServer = new PigServer(ExecType.MAPREDUCE,prt);
> pigServer.registerScript(path);
> pigServer.store("B","/user/biadmin/myoutput20");
Now the modified code is working fine.
So now my doubt is
why I was not able to execute pig script which is having store command?
How can I execute pig script file which is having store command?
Your PigServer code is not working because; when you call .registerScript(), by default, PigServer sets the interactive mode flag on GruntParser to false. From the PigServer source code:
public void registerScript(InputStream in, Map<String,String> params,List<String> paramsFiles) throws IOException {
try {
String substituted = doParamSubstitution(in, params, paramsFiles);
GruntParser grunt = new GruntParser(new StringReader(substituted));
/********************************************/
grunt.setInteractive(false);
/********************************************/
grunt.setParams(this);
grunt.parseStopOnError(true);
} catch (org.apache.pig.tools.pigscript.parser.ParseException e) {
log.error(e.getLocalizedMessage());
throw new IOException(e.getCause());
}
}
Quoting from the GruntParser source code:
In interactive mode, executes the plan right away whenever a STORE command is encountered.
This means that when interactive mode is not active, STOREcommands will be ignored (that is they won't run automatically) until further PigServer.openIterator or PigServer.store calls (that is you explicitly make a call requiring the STORE line).
As for your second question, you might want to have a look at PigRunner class.

how to confirm that jdbctemplate excuted query succesfully

I am using hsqldb for database. i am using jdbctemplate for sqlqueries. i just want to know how i can confirm that jdbctemplate executed query successfully, as i can't see the result in database, because my database is hsqldb.
Thank in advance
JdbcTemplate.update(..) returns the number of updated rows as an integer. Check if that is greater than zero or not:
if(jdbcTemplate.update("insert into mytable..") > 0) {
// all ok
} else {
// not inserted anything
}
Instead of using HSQLDB as a pure memory DB, you can write out the contents on disc by initializing HSQLDB with the following URL:
jdbc:hsqldb:file:/opt/db/testdb
I presume you are using a "memory" URL like this (all contents as you notice are gone after the JVM shuts down):
jdbc:hsqldb:mem:mycooldb
When you shut down the database after the test, you can either view the resulting script-file using a texteditor, or start the HSQLDB-manager contained in the main HSQLDB jar.
java -jar hsqldb-version.jar